From 1f0c08209d02770e98af09e888c50679cd39e91f Mon Sep 17 00:00:00 2001 From: NaZe0320 Date: Fri, 9 Feb 2024 04:37:03 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[feat/#37]:=20=EC=9C=84=EC=B9=98=20?= =?UTF-8?q?=EA=B6=8C=ED=95=9C=20=EC=9A=94=EC=B2=AD=20=EB=B0=8F=20=EC=9C=84?= =?UTF-8?q?=EC=B9=98=20=EB=B3=80=EA=B2=BD=20=EC=8B=9C=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=20=EC=9E=AC=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 2 ++ .../umc/coumo/domain/model/LocationLatLng.kt | 7 ++++ .../coumo/domain/viewmodel/HomeViewModel.kt | 14 ++++---- .../presentation/activity/MainActivity.kt | 32 +++++++++++++++++++ .../fragment/home/HomeMainFragment.kt | 7 ++++ 5 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/umc/coumo/domain/model/LocationLatLng.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5494422..5b0c206 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + diff --git a/app/src/main/java/com/umc/coumo/domain/model/LocationLatLng.kt b/app/src/main/java/com/umc/coumo/domain/model/LocationLatLng.kt new file mode 100644 index 0000000..cfa6f1c --- /dev/null +++ b/app/src/main/java/com/umc/coumo/domain/model/LocationLatLng.kt @@ -0,0 +1,7 @@ +package com.umc.coumo.domain.model + +data class LocationLatLng( + val longitude: Double, + val latitude: Double +){ +} \ No newline at end of file diff --git a/app/src/main/java/com/umc/coumo/domain/viewmodel/HomeViewModel.kt b/app/src/main/java/com/umc/coumo/domain/viewmodel/HomeViewModel.kt index fb6b0c2..4196fd9 100644 --- a/app/src/main/java/com/umc/coumo/domain/viewmodel/HomeViewModel.kt +++ b/app/src/main/java/com/umc/coumo/domain/viewmodel/HomeViewModel.kt @@ -5,6 +5,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.umc.coumo.domain.model.LocationLatLng import com.umc.coumo.domain.model.StoreCouponCountModel import com.umc.coumo.domain.model.StoreInfoItemModel import com.umc.coumo.domain.model.StoreInfoModel @@ -38,21 +39,22 @@ class HomeViewModel @Inject constructor( private val _popularStoreList = MutableLiveData>() val popularStoreList: LiveData> get() = _popularStoreList - private val _currentLongitude = MutableLiveData(127.00091673551657) - val currentLongitude: LiveData get() = _currentLongitude + private val _currentLocation = MutableLiveData(LocationLatLng(127.00091673551657, 37.55800312017019)) + val currentLocation: LiveData get() = _currentLocation - private val _currentLatitude = MutableLiveData(37.55800312017019) - val currentLatitude: LiveData get() = _currentLatitude + fun setCurrentLocation(longitude: Double, latitude: Double ) { + _currentLocation.value = LocationLatLng(longitude, latitude) + } fun getPopularStoreList() { viewModelScope.launch { - _popularStoreList.value = repository.getPopularStoreList(longitude = _currentLongitude.value!!, latitude = _currentLatitude.value!!) //빈 값 없으니 이렇게 처리 + _popularStoreList.value = repository.getPopularStoreList(longitude = _currentLocation.value?.longitude!!, latitude = _currentLocation.value?.latitude!!) //빈 값 없으니 이렇게 처리 } } private fun getNearStoreList(category: CategoryType?) { viewModelScope.launch { - _nearStoreList.value = repository.getNearStoreList(category = category, longitude = _currentLongitude.value!!, latitude = _currentLatitude.value!!) + _nearStoreList.value = repository.getNearStoreList(category = category,longitude = _currentLocation.value?.longitude!!, latitude = _currentLocation.value?.latitude!!) } } diff --git a/app/src/main/java/com/umc/coumo/presentation/activity/MainActivity.kt b/app/src/main/java/com/umc/coumo/presentation/activity/MainActivity.kt index 9895b1b..a350d2e 100644 --- a/app/src/main/java/com/umc/coumo/presentation/activity/MainActivity.kt +++ b/app/src/main/java/com/umc/coumo/presentation/activity/MainActivity.kt @@ -1,10 +1,17 @@ package com.umc.coumo.presentation.activity +import android.Manifest +import android.content.Context +import android.content.pm.PackageManager +import android.location.Location +import android.location.LocationManager import android.os.Bundle +import android.util.Log import androidx.activity.viewModels import com.umc.coumo.R import com.umc.coumo.databinding.ActivityMainBinding import com.umc.coumo.domain.type.TabType +import com.umc.coumo.domain.viewmodel.HomeViewModel import com.umc.coumo.domain.viewmodel.MainViewModel import com.umc.coumo.presentation.adapter.MainFragmentAdapter import com.umc.coumo.utils.binding.BindingActivity @@ -14,6 +21,7 @@ import dagger.hilt.android.AndroidEntryPoint class MainActivity : BindingActivity(R.layout.activity_main) { private val viewModel: MainViewModel by viewModels() + private val homeViewModel: HomeViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -27,6 +35,30 @@ class MainActivity : BindingActivity(R.layout.activity_main setNaviButton() setObserver() + setLocationPermission() + } + + private fun setLocationPermission() { + if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED || + checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + with(getSystemService(Context.LOCATION_SERVICE) as LocationManager) { + requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000L, 5F) { + Log.d("LocationService", "GPS_PROVIDER") + getLocation(it) + } + requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000L, 5F) { + Log.d("LocationService", "NETWORK_PROVIDER") + getLocation(it) + } + } + } else { + //권한 요청 필요한데 이거 메인에서 처리하자 + } + + } + + private fun getLocation(location: Location) { + homeViewModel.setCurrentLocation(location.longitude,location.latitude) } private fun setObserver () { diff --git a/app/src/main/java/com/umc/coumo/presentation/fragment/home/HomeMainFragment.kt b/app/src/main/java/com/umc/coumo/presentation/fragment/home/HomeMainFragment.kt index 23b701b..16223bc 100644 --- a/app/src/main/java/com/umc/coumo/presentation/fragment/home/HomeMainFragment.kt +++ b/app/src/main/java/com/umc/coumo/presentation/fragment/home/HomeMainFragment.kt @@ -26,6 +26,13 @@ class HomeMainFragment: BindingFragment(R.layout.fragme setBanner() setRecyclerView() setButton() + observeLocationChange() + } + + private fun observeLocationChange() { + viewModel.currentLocation.observe(viewLifecycleOwner) { + viewModel.getPopularStoreList() + } } private fun setButton() { From 4705ae351b134889f1c2851d259f1ad2a2cf0e55 Mon Sep 17 00:00:00 2001 From: NaZe0320 Date: Fri, 9 Feb 2024 05:20:31 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[feat/#37]:=20=EC=9C=84=EC=B9=98=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20=EC=8B=9C=20=EC=A3=BC=EC=86=8C=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coumo/domain/viewmodel/HomeViewModel.kt | 10 ++++++- .../presentation/activity/MainActivity.kt | 2 +- .../fragment/home/HomeMainFragment.kt | 29 ++++++++++++++++--- .../main/res/layout/fragment_home_main.xml | 4 ++- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/umc/coumo/domain/viewmodel/HomeViewModel.kt b/app/src/main/java/com/umc/coumo/domain/viewmodel/HomeViewModel.kt index 4196fd9..0fbf6bd 100644 --- a/app/src/main/java/com/umc/coumo/domain/viewmodel/HomeViewModel.kt +++ b/app/src/main/java/com/umc/coumo/domain/viewmodel/HomeViewModel.kt @@ -1,5 +1,6 @@ package com.umc.coumo.domain.viewmodel +import android.location.Address import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData @@ -42,10 +43,18 @@ class HomeViewModel @Inject constructor( private val _currentLocation = MutableLiveData(LocationLatLng(127.00091673551657, 37.55800312017019)) val currentLocation: LiveData get() = _currentLocation + private val _currentAddress = MutableLiveData() + val currentAddress: LiveData get() = _currentAddress + fun setCurrentLocation(longitude: Double, latitude: Double ) { _currentLocation.value = LocationLatLng(longitude, latitude) } + fun setCurrentAddress(address: List
?) { + _currentAddress.value = address?.get(0)?.let { it.adminArea + " "+ it.subLocality + " "+ it.thoroughfare } + Log.d("TEST Address","${address?.get(0)}") + } + fun getPopularStoreList() { viewModelScope.launch { _popularStoreList.value = repository.getPopularStoreList(longitude = _currentLocation.value?.longitude!!, latitude = _currentLocation.value?.latitude!!) //빈 값 없으니 이렇게 처리 @@ -78,7 +87,6 @@ class HomeViewModel @Inject constructor( } } } - Log.d("OKHTTP_TEST","$storeId \n${_storeData.value}") } } \ No newline at end of file diff --git a/app/src/main/java/com/umc/coumo/presentation/activity/MainActivity.kt b/app/src/main/java/com/umc/coumo/presentation/activity/MainActivity.kt index a350d2e..51c9876 100644 --- a/app/src/main/java/com/umc/coumo/presentation/activity/MainActivity.kt +++ b/app/src/main/java/com/umc/coumo/presentation/activity/MainActivity.kt @@ -38,7 +38,7 @@ class MainActivity : BindingActivity(R.layout.activity_main setLocationPermission() } - private fun setLocationPermission() { + fun setLocationPermission() { if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { with(getSystemService(Context.LOCATION_SERVICE) as LocationManager) { diff --git a/app/src/main/java/com/umc/coumo/presentation/fragment/home/HomeMainFragment.kt b/app/src/main/java/com/umc/coumo/presentation/fragment/home/HomeMainFragment.kt index 16223bc..64aba05 100644 --- a/app/src/main/java/com/umc/coumo/presentation/fragment/home/HomeMainFragment.kt +++ b/app/src/main/java/com/umc/coumo/presentation/fragment/home/HomeMainFragment.kt @@ -1,6 +1,9 @@ package com.umc.coumo.presentation.fragment.home +import android.location.Geocoder +import android.location.Location import android.os.Bundle +import android.util.Log import android.view.View import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.findNavController @@ -15,6 +18,7 @@ import com.umc.coumo.presentation.adapter.BannerPagerAdapter import com.umc.coumo.presentation.adapter.StoreInfoAdapter import com.umc.coumo.utils.ItemSpacingDecoration import com.umc.coumo.utils.binding.BindingFragment +import java.io.IOException class HomeMainFragment: BindingFragment(R.layout.fragment_home_main) { @@ -23,15 +27,26 @@ class HomeMainFragment: BindingFragment(R.layout.fragme override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + binding.viewModel = viewModel + binding.lifecycleOwner = viewLifecycleOwner + setBanner() + setLocation() setRecyclerView() setButton() - observeLocationChange() } - private fun observeLocationChange() { + private fun setLocation() { + val geocoder: Geocoder = Geocoder(requireContext()) + viewModel.currentLocation.observe(viewLifecycleOwner) { viewModel.getPopularStoreList() + try { + viewModel.setCurrentAddress(geocoder.getFromLocation(viewModel.currentLocation.value?.latitude!!,viewModel.currentLocation.value?.longitude!!,1)) + } catch (e: IOException) { + Log.e("Location","Location change address IO Exception") + } + } } @@ -72,13 +87,15 @@ class HomeMainFragment: BindingFragment(R.layout.fragme R.id.action_homeMainFragment_to_homeListFragment ) } + + binding.btnRefresh.setOnClickListener { + + } } private fun setRecyclerView() { val storeInfoAdapter = StoreInfoAdapter() - viewModel.getPopularStoreList() - binding.rvPopular.apply { adapter = storeInfoAdapter addItemDecoration(ItemSpacingDecoration(requireContext(),resources.getDimensionPixelSize(R.dimen.item_between_horizontal))) @@ -127,4 +144,8 @@ class HomeMainFragment: BindingFragment(R.layout.fragme } }) } + + private fun getLocation(location: Location) { + viewModel.setCurrentLocation(location.longitude,location.latitude) + } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home_main.xml b/app/src/main/res/layout/fragment_home_main.xml index 01fb299..bb70ec2 100644 --- a/app/src/main/res/layout/fragment_home_main.xml +++ b/app/src/main/res/layout/fragment_home_main.xml @@ -48,12 +48,14 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:background="@drawable/shape_rect_round_6" - android:text="위치를 조정해보세요" + android:text="@{viewModel.currentAddress}" android:textColor="@color/font_text" android:fontFamily="@font/pretendard_500" android:layout_marginStart="2dp" + android:maxLines="1" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toEndOf="@id/iv_locate" + app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"/>