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..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,10 +1,12 @@ package com.umc.coumo.domain.viewmodel +import android.location.Address import android.util.Log 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 +40,30 @@ 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 + 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 = _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!!) } } @@ -76,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 9895b1b..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 @@ -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() + } + + 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..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,11 +27,29 @@ 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() } + 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") + } + + } + } + private fun setButton() { binding.ivCafe.setOnClickListener { viewModel.selectCategory(CategoryType.CAFE) @@ -65,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))) @@ -120,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"/>