Skip to content

Commit

Permalink
Merge pull request #66 from Hyesung82/feature/location
Browse files Browse the repository at this point in the history
[develop] 위치 확인하기 화면에서 위치 선택 기능 구현
  • Loading branch information
Hyesung82 authored Mar 16, 2024
2 parents 9c4fe7c + 059cd13 commit 1b924bc
Show file tree
Hide file tree
Showing 22 changed files with 527 additions and 109 deletions.
8 changes: 5 additions & 3 deletions presentation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

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

<application>
<meta-data
android:name="com.google.android.geo.API_KEY"
Expand All @@ -9,8 +14,5 @@
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ package com.cheocharm.presentation.common
const val SIGN_UP_TYPE = "signUpType"
const val GOOGLE_ID_TOKEN = "googleIdToken"
const val GROUP_JOIN_REQUEST_BOTTOM = "groupJoinRequestBottom"

const val SOUTH_KOREA_LAT = 35.95
const val SOUTH_KOREA_LNG = 128.25
const val SOUTH_KOREA_ZOOM_LEVEL = 6.5F
const val DEFAULT_ZOOM_LEVEL = 15F
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.cheocharm.presentation.common

import android.location.Location
import com.google.android.gms.maps.model.LatLng

fun Location.toLatLng() = LatLng(this.latitude, this.longitude)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.cheocharm.presentation.enum

enum class LatLngSelectionType(val locationString: String) {
DEFAULT("대한민국"),
CURRENT("현재 위치"),
SPECIFIED("")
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package com.cheocharm.presentation.model

import android.net.Uri
import com.cheocharm.presentation.common.toCoordString
import com.google.android.gms.maps.model.LatLng

data class Picture(val uri: Uri, val latLng: LatLng?) {
var address: String? = null

fun getLocationString(): String {
return address ?: latLng.toCoordString() ?: "현재 위치"
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
package com.cheocharm.presentation.ui

import android.Manifest
import android.content.Context
import android.graphics.Rect
import android.os.Build
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.core.view.isVisible
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.cheocharm.presentation.R
import com.cheocharm.presentation.base.BaseActivity
import com.cheocharm.presentation.databinding.ActivityMainBinding
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import com.google.android.gms.maps.SupportMapFragment
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {
private var mapFragment: SupportMapFragment? = null
private var fusedLocationClient: FusedLocationProviderClient? = null

@RequiresApi(Build.VERSION_CODES.N)
private val locationPermissionRequest = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
when {
permissions[Manifest.permission.ACCESS_FINE_LOCATION] ?: false -> {
fusedLocationClient =
LocationServices.getFusedLocationProviderClient(this)
}
permissions[Manifest.permission.ACCESS_COARSE_LOCATION] ?: false -> {
fusedLocationClient =
LocationServices.getFusedLocationProviderClient(this)
}
else -> {
// TODO: 위치 권한을 얻지 못했을 때
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -37,6 +65,21 @@ class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {
binding.bottomNavMain.visibility = View.VISIBLE
}
}

mapFragment =
supportFragmentManager.findFragmentById(R.id.fragment_main_map) as? SupportMapFragment
}

@RequiresApi(Build.VERSION_CODES.N)
override fun onStart() {
super.onStart()

locationPermissionRequest.launch(
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
)
}

override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
Expand All @@ -60,4 +103,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {
}

fun getBinding(): ActivityMainBinding = binding

fun setMapVisible(boolean: Boolean) {
binding.fragmentMainMap.isVisible = boolean
}

fun getMap(): SupportMapFragment? = mapFragment

fun getLocationClient(): FusedLocationProviderClient? = fusedLocationClient
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package com.cheocharm.presentation.ui.home

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.core.content.ContextCompat
import androidx.fragment.app.viewModels
import com.cheocharm.presentation.R
import com.cheocharm.presentation.base.BaseFragment
import com.cheocharm.presentation.common.DEFAULT_ZOOM_LEVEL
import com.cheocharm.presentation.common.SOUTH_KOREA_LAT
import com.cheocharm.presentation.common.SOUTH_KOREA_LNG
import com.cheocharm.presentation.common.SOUTH_KOREA_ZOOM_LEVEL
import com.cheocharm.presentation.common.toLatLng
import com.cheocharm.presentation.databinding.FragmentHomeBinding
import com.cheocharm.presentation.ui.MainActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.LatLng
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -19,9 +29,47 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home) {

binding.viewmodel = homeViewModel

val mainActivityBinding = (activity as MainActivity).getBinding()
mainActivityBinding.fragmentMainMap.isVisible = true
val mainActivity = requireActivity() as MainActivity
mainActivity.setMapVisible(true)

homeViewModel.countUp()
val mapFragment = (activity as MainActivity).getMap()
mapFragment?.getMapAsync { map ->
map.setOnCameraMoveListener {
homeViewModel.updateZoomLevel(map.cameraPosition.zoom)
}

map.setOnMapLoadedCallback {
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
val locationClient = mainActivity.getLocationClient()
locationClient?.lastLocation?.addOnSuccessListener { location ->
if (location != null) {
map.moveCamera(
CameraUpdateFactory.newLatLngZoom(
location.toLatLng(),
DEFAULT_ZOOM_LEVEL
)
)
} else {
map.moveCameraToDefaultLocation()
}
}
} else {
map.moveCameraToDefaultLocation()
}
}
}
}

private fun GoogleMap.moveCameraToDefaultLocation() {
moveCamera(
CameraUpdateFactory.newLatLngZoom(
LatLng(SOUTH_KOREA_LAT, SOUTH_KOREA_LNG),
SOUTH_KOREA_ZOOM_LEVEL
)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cheocharm.presentation.ui.home

import android.location.Location
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand All @@ -8,10 +9,17 @@ import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor() : ViewModel() {
private val _count = MutableLiveData<Int>()
val count: LiveData<Int> = _count
private val _location = MutableLiveData<Location>()
val location: LiveData<Location> = _location

fun countUp() {
_count.value = count.value?.plus(1) ?: 1
private val _zoomLevel = MutableLiveData<Float>()
val zoomLevel: LiveData<Float> = _zoomLevel

fun updateLocation(location: Location) {
_location.value = location
}

fun updateZoomLevel(level: Float) {
_zoomLevel.value = level
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.cheocharm.presentation.ui.mypage
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import com.cheocharm.presentation.BuildConfig
import com.cheocharm.presentation.R
Expand Down Expand Up @@ -38,8 +37,8 @@ class MyPageFragment : BaseFragment<FragmentMyPageBinding>(R.layout.fragment_my_

binding.viewmodel = myPageViewModel

val mainActivityBinding = (activity as MainActivity).getBinding()
mainActivityBinding.fragmentMainMap.isVisible = false
val mainActivity = requireActivity() as MainActivity
mainActivity.setMapVisible(false)

initButton()
myPageViewModel.countUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.cheocharm.presentation.ui.search
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.core.view.isVisible
import androidx.hilt.navigation.fragment.hiltNavGraphViewModels
import androidx.navigation.fragment.findNavController
import com.cheocharm.presentation.R
Expand All @@ -24,16 +23,16 @@ class SearchFragment : BaseFragment<FragmentSearchBinding>(R.layout.fragment_sea

binding.viewmodel = searchViewModel

mainActivityBinding = (activity as MainActivity).getBinding()
val mainActivity = requireActivity() as MainActivity
mainActivity.setMapVisible(false)
mainActivityBinding = mainActivity.getBinding()

initEditTexts()
initObservers()
}

override fun onResume() {
super.onResume()

mainActivityBinding.fragmentMainMap.isVisible = false
mainActivityBinding.bottomNavMain.visibility = View.VISIBLE
}

Expand Down
Loading

0 comments on commit 1b924bc

Please sign in to comment.