Skip to content

Commit

Permalink
Merge pull request #123 from plashdof/feature/aos-global
Browse files Browse the repository at this point in the history
[Global/aos] 전역 Refactoring
  • Loading branch information
plashdof authored Nov 26, 2023
2 parents ce4b663 + 51d8513 commit 9fa01ac
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import retrofit2.http.POST

interface IntroApi {

@POST("/api/user")
@POST("api/user")
suspend fun signup(
@Body params: DetailSignupRequest
): Response<Unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import retrofit2.http.Path

interface ValidationApi {

@GET("/api/user/nickname/{nickname}/exists")
@GET("api/user/nickname/{nickname}/exists")
suspend fun nickValidation(
@Path("nickname") nickname: String
): Response<BaseResponse<ValidateResponse>>

@GET("/api/user/email/{email}/exists")
@GET("api/user/email/{email}/exists")
suspend fun emailValidation(
@Path("email") email: String
): Response<BaseResponse<ValidateResponse>>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.avengers.nibobnebob.presentation.customview


import androidx.fragment.app.FragmentManager
import com.avengers.nibobnebob.presentation.ui.toDateString
import com.google.android.material.datepicker.MaterialDatePicker

class CalendarDatePicker(
private val onSelectDateListener: (String) -> Unit
) {
private val datePicker = MaterialDatePicker.Builder.datePicker()
.setTitleText("생일을 고르세요")
.setSelection(MaterialDatePicker.todayInUtcMilliseconds())
.build()

init{
datePicker.addOnPositiveButtonClickListener {
onSelectDateListener(it.toDateString())
}
}

fun show(fragmentManager: FragmentManager){
datePicker.show(fragmentManager,"")
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.avengers.nibobnebob.presentation.customview

import android.content.Context
import android.view.LayoutInflater
import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.BottomSheetRestaurantBinding
import com.avengers.nibobnebob.presentation.ui.main.home.model.UiMarkerData
import com.google.android.material.bottomsheet.BottomSheetDialog


class RestaurantBottomSheet(
context: Context,
private val data: UiMarkerData,
private val onClickAddWishRestaurant: (Int, Boolean) -> Boolean,
private val onClickAddMyRestaurant: (Int) -> Unit,
private val onClickGoReview: (Int) -> Unit
): BottomSheetDialog(context) {

private var binding: BottomSheetRestaurantBinding
private var isWishState = data.isInWishList

init{
binding = BottomSheetRestaurantBinding.inflate(LayoutInflater.from(context))
setContentView(binding.root)
binding.item = data
setBottomSheetListener()
}

private fun setBottomSheetListener(){
binding.btnAddMyRestaurant.setOnClickListener {
onClickAddMyRestaurant(data.id)
dismiss()
}

binding.btnAddWishRestaurant.setOnClickListener {
val result = onClickAddWishRestaurant(data.id, data.isInWishList)

if (result) {
isWishState = !isWishState

if (isWishState) {
binding.btnAddWishRestaurant.setBackgroundResource(R.drawable.ic_star_full)
} else {
binding.btnAddWishRestaurant.setBackgroundResource(R.drawable.ic_star_border)
}
}
}

binding.btnGoReview.setOnClickListener {
onClickGoReview(data.id)
dismiss()
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.avengers.nibobnebob.presentation.ui

import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone

internal fun Long.toDateString(): String {
val dateFormat = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault())
dateFormat.timeZone = TimeZone.getTimeZone("UTC")
val date = Date(this)
return dateFormat.format(date)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.avengers.nibobnebob.presentation.ui

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.LocationManager
import android.provider.Settings
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.core.content.ContextCompat

internal fun Context.hideKeyboard(view: View) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}

internal fun Context.requestLocationPermission(
permissionList: Array<String>,
onLauncherStart: () -> Unit,
onTrackingChangeListener: (Boolean) -> Unit
) {
var permissionFlag = false
permissionList.forEach { permission ->
permissionFlag = ContextCompat.checkSelfPermission(
this,
permission
) == PackageManager.PERMISSION_GRANTED
}

if (permissionFlag) {
checkLocationIsOn(){
onTrackingChangeListener(it)
}
} else {
onLauncherStart()
Toast.makeText(this, "위치권한을 허용해주세요", Toast.LENGTH_SHORT).show()
}
}


internal fun Context.checkLocationIsOn(
onTrackingChangeListener: (Boolean) -> Unit
) {
val locationManager =
getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
onTrackingChangeListener(true)
} else {
startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
Toast.makeText(this, "휴대폰 GPS를 켜주세요", Toast.LENGTH_SHORT).show()
onTrackingChangeListener(false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.avengers.nibobnebob.presentation.ui.intro.signup

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
Expand All @@ -13,10 +11,9 @@ import androidx.navigation.fragment.navArgs
import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentDetailSignupBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.customview.CalendarDatePicker
import com.avengers.nibobnebob.presentation.ui.intro.IntroViewModel
import com.avengers.nibobnebob.presentation.util.showCalendarDatePicker
import com.google.android.material.textfield.MaterialAutoCompleteTextView
import com.google.android.material.textfield.TextInputLayout
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand Down Expand Up @@ -65,9 +62,9 @@ class DetailSignupFragment :

private fun setDateBtnListener() {
binding.tilBirth.setEndIconOnClickListener {
showCalendarDatePicker(parentFragmentManager) {
CalendarDatePicker{
viewModel.setBirth(it)
}
}.show(parentFragmentManager)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.avengers.nibobnebob.data.model.BaseState
import com.avengers.nibobnebob.data.model.request.DetailSignupRequest
import com.avengers.nibobnebob.data.repository.IntroRepository
import com.avengers.nibobnebob.data.repository.ValidationRepository
import com.avengers.nibobnebob.presentation.util.Validation
import com.avengers.nibobnebob.presentation.util.ValidationUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -95,7 +95,7 @@ class DetailSignupViewModel @Inject constructor(

private fun observeBirth() {
birth.onEach {
if (Validation.checkBirth(it) || it.isBlank()) {
if (ValidationUtil.checkBirth(it) || it.isBlank()) {
_uiState.update { state ->
state.copy(
birthState = InputState.Empty
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package com.avengers.nibobnebob.presentation.ui.main.home

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.widget.ImageButton
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
Expand All @@ -21,10 +14,12 @@ import com.avengers.nibobnebob.NavGraphDirections
import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentHomeBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.customview.RestaurantBottomSheet
import com.avengers.nibobnebob.presentation.ui.checkLocationIsOn
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.home.adapter.HomeFilterAdapter
import com.avengers.nibobnebob.presentation.ui.main.home.model.UiMarkerData
import com.avengers.nibobnebob.presentation.util.restaurantSheet
import com.avengers.nibobnebob.presentation.ui.requestLocationPermission
import com.naver.maps.geometry.LatLng
import com.naver.maps.map.LocationTrackingMode
import com.naver.maps.map.MapFragment
Expand Down Expand Up @@ -107,16 +102,17 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
}
}

private fun initEventObserver(){
private fun initEventObserver() {
repeatOnStarted {
viewModel.events.collect{
when(it){
viewModel.events.collect {
when (it) {
is HomeEvents.NavigateToSearchRestaurant -> findNavController().toSearchRestaurant()
is HomeEvents.SetNewMarkers -> {
viewModel.uiState.value.markerList.forEach { data ->
viewModel.uiState.value.markerList.forEach { data ->
setMarker(data)
}
}

is HomeEvents.RemoveMarkers -> removeAllMarker()
else -> {}
}
Expand All @@ -128,7 +124,14 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
repeatOnStarted {
viewModel.uiState.collect {
when (it.locationTrackingState) {
is TrackingState.TryOn -> requestLocationPermission()
is TrackingState.TryOn -> {
requireContext().requestLocationPermission(
locationPermissionList,
::startPermissionLauncher,
::onTrackingChangeListener
)
}

is TrackingState.On -> naverMap.locationTrackingMode =
LocationTrackingMode.Follow

Expand All @@ -139,41 +142,21 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
}
}

private fun requestLocationPermission() {
var permissionFlag = false
locationPermissionList.forEach { permission ->
permissionFlag = ContextCompat.checkSelfPermission(
requireContext(),
permission
) == PackageManager.PERMISSION_GRANTED
}

if (permissionFlag) {
checkLocationIsOn()
} else {
requestPermissionLauncher.launch(locationPermissionList)
Toast.makeText(requireContext(), "위치권한을 허용해주세요", Toast.LENGTH_SHORT).show()
}
private fun startPermissionLauncher() {
requestPermissionLauncher.launch(locationPermissionList)
}

private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { resultMap ->
val isAllGranted = locationPermissionList.all { resultMap[it] == true }
if (isAllGranted) checkLocationIsOn()
if (isAllGranted) requireContext().checkLocationIsOn(::onTrackingChangeListener)
else viewModel.trackingOff()
}

private fun checkLocationIsOn() {
val locationManager =
requireContext().getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
viewModel.trackingOn()
} else {
startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
Toast.makeText(requireContext(), "휴대폰 GPS를 켜주세요", Toast.LENGTH_SHORT).show()
viewModel.trackingOff()
}
private fun onTrackingChangeListener(state: Boolean) {
if (state) viewModel.trackingOn()
else viewModel.trackingOff()
}

// todo markerData model을 정의하여, 파라미터로 해당 데이터를 삽입
Expand All @@ -185,21 +168,22 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
marker.map = naverMap

marker.setOnClickListener {
restaurantSheet(
val bottomSheet = RestaurantBottomSheet(
context = requireContext(),
data = data,
onClickAddWishRestaurant = ::addWishTest,
onClickAddMyRestaurant = ::addRestaurantTest,
onClickGoReview = ::goReviewTest
).show()
)
bottomSheet.show()

true
}
markerList.add(marker)
}

// todo 모든 marker 데이터 markerList 에 저장해 놨다가, remove 다음 방식으로 진행
private fun removeAllMarker(){
private fun removeAllMarker() {
markerList.forEach {
it.map = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import androidx.navigation.Navigation
import com.avengers.nibobnebob.R
import com.avengers.nibobnebob.databinding.FragmentEditProfileBinding
import com.avengers.nibobnebob.presentation.base.BaseFragment
import com.avengers.nibobnebob.presentation.customview.CalendarDatePicker
import com.avengers.nibobnebob.presentation.ui.main.MainViewModel
import com.avengers.nibobnebob.presentation.ui.main.mypage.share.MyPageSharedUiEvent
import com.avengers.nibobnebob.presentation.ui.main.mypage.share.MyPageSharedViewModel
import com.avengers.nibobnebob.presentation.util.showCalendarDatePicker
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest

Expand Down Expand Up @@ -76,9 +76,9 @@ class EditProfileFragment :

private fun setDateBtnListener() {
binding.tilBirth.setEndIconOnClickListener {
showCalendarDatePicker(parentFragmentManager) {
CalendarDatePicker{
viewModel.setBirth(it)
}
}.show(parentFragmentManager)
}
}

Expand Down
Loading

0 comments on commit 9fa01ac

Please sign in to comment.