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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
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
){
}
24 changes: 17 additions & 7 deletions app/src/main/java/com/umc/coumo/domain/viewmodel/HomeViewModel.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -38,21 +40,30 @@ 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
private val _currentAddress = MutableLiveData<String>()
val currentAddress: LiveData<String> get() = _currentAddress

fun setCurrentLocation(longitude: Double, latitude: Double ) {
_currentLocation.value = LocationLatLng(longitude, latitude)
}

fun setCurrentAddress(address: List<Address>?) {
_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!!)
}
}

Expand All @@ -76,7 +87,6 @@ class HomeViewModel @Inject constructor(
}
}
}
Log.d("OKHTTP_TEST","$storeId \n${_storeData.value}")
}

}
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
Expand All @@ -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)
Expand All @@ -27,6 +35,30 @@ class MainActivity : BindingActivity<ActivityMainBinding>(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 () {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<FragmentHomeMainBinding>(R.layout.fragment_home_main) {

Expand All @@ -23,11 +27,29 @@ class HomeMainFragment: BindingFragment<FragmentHomeMainBinding>(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)
Expand Down Expand Up @@ -65,13 +87,15 @@ class HomeMainFragment: BindingFragment<FragmentHomeMainBinding>(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)))
Expand Down Expand Up @@ -120,4 +144,8 @@ class HomeMainFragment: BindingFragment<FragmentHomeMainBinding>(R.layout.fragme
}
})
}

private fun getLocation(location: Location) {
viewModel.setCurrentLocation(location.longitude,location.latitude)
}
}
4 changes: 3 additions & 1 deletion app/src/main/res/layout/fragment_home_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Expand Down