Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#37 [feat] 위치 시스템 #50

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[feat/#37]: 위치 권한 요청 및 위치 변경 시 데이터 재로드
  • Loading branch information
NaZe0320 committed Feb 8, 2024
commit 1f0c08209d02770e98af09e888c50679cd39e91f
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera.any" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.umc.coumo.domain.model

data class LocationLatLng(
val longitude: Double,
val latitude: Double
){
}
Original file line number Diff line number Diff line change
@@ -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<List<StoreInfoItemModel>>()
val popularStoreList: LiveData<List<StoreInfoItemModel>> get() = _popularStoreList

private val _currentLongitude = MutableLiveData<Double>(127.00091673551657)
val currentLongitude: LiveData<Double> get() = _currentLongitude
private val _currentLocation = MutableLiveData<LocationLatLng>(LocationLatLng(127.00091673551657, 37.55800312017019))
val currentLocation: LiveData<LocationLatLng> get() = _currentLocation

private val _currentLatitude = MutableLiveData<Double>(37.55800312017019)
val currentLatitude: LiveData<Double> 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!!)
}
}

Original file line number Diff line number Diff line change
@@ -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<ActivityMainBinding>(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<ActivityMainBinding>(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 () {
Original file line number Diff line number Diff line change
@@ -26,6 +26,13 @@ class HomeMainFragment: BindingFragment<FragmentHomeMainBinding>(R.layout.fragme
setBanner()
setRecyclerView()
setButton()
observeLocationChange()
}

private fun observeLocationChange() {
viewModel.currentLocation.observe(viewLifecycleOwner) {
viewModel.getPopularStoreList()
}
}

private fun setButton() {