Skip to content

Commit

Permalink
Merge pull request #39 from ldh019/feature/#14
Browse files Browse the repository at this point in the history
Feature: #14, #16, #38 지도 마커, 상세화면 적용
  • Loading branch information
ldh019 authored Nov 18, 2022
2 parents 2ca3765 + fa76b59 commit 48fdf93
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ abstract class BaseFragment<VB : ViewDataBinding>(
}
}

fun isBindingNotNull(): Boolean = _binding != null

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down
110 changes: 87 additions & 23 deletions presentation/src/main/java/com/gta/presentation/ui/map/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@ package com.gta.presentation.ui.map

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.navigation.fragment.findNavController
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.gta.presentation.R
import com.gta.presentation.databinding.FragmentMapBinding
import com.gta.presentation.ui.base.BaseFragment
import com.naver.maps.geometry.LatLng
import com.naver.maps.map.LocationTrackingMode
import com.naver.maps.map.NaverMap
import com.naver.maps.map.OnMapReadyCallback
import com.naver.maps.map.overlay.Marker
import com.naver.maps.map.util.FusedLocationSource
import com.naver.maps.map.util.MarkerIcons
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MapFragment : BaseFragment<FragmentMapBinding>(R.layout.fragment_map), OnMapReadyCallback {
private lateinit var naverMap: NaverMap
private lateinit var locationSource: FusedLocationSource
private lateinit var backPressedCallback: OnBackPressedCallback
private lateinit var bottomSheetBehavior: BottomSheetBehavior<FrameLayout>

private val activityResultLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { resultMap ->
val isAllGranted = permissions.all { e -> resultMap[e] == true }
Expand All @@ -38,69 +50,121 @@ class MapFragment : BaseFragment<FragmentMapBinding>(R.layout.fragment_map), OnM
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.mapView.onCreate(savedInstanceState)

binding.mapView.getMapAsync(this)

locationSource = FusedLocationSource(this, LOCATION_PERMISSION_REQUEST_CODE)
bottomSheetBehavior = BottomSheetBehavior.from(binding.bottomSheet)
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
}

@SuppressLint("ClickableViewAccessibility")
override fun onMapReady(naverMap: NaverMap) {
this.naverMap = naverMap
naverMap.locationSource = locationSource

activityResultLauncher.launch(permissions)
this.naverMap = naverMap
setupWithMap()
setupWithMarker()
setupWithBottomSheet()
}

@SuppressLint("ClickableViewAccessibility")
private fun setupWithMap() {
naverMap.locationSource = locationSource
naverMap.uiSettings.apply {
isCompassEnabled = true
isScaleBarEnabled = true
isLocationButtonEnabled = true
}

binding.mapView.setOnTouchListener { _, event ->
binding.cgFilter.visibility = when (event.action) {
MotionEvent.ACTION_MOVE -> View.GONE
else -> View.VISIBLE
binding.mapView.setOnTouchListener { v, event ->
if (event.y >= binding.bottomSheet.top && event.y <= binding.bottomSheet.bottom) {
true
} else {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
binding.mapView.onTouchEvent(event)
}
}
}

@SuppressLint("ResourceAsColor")
private fun setupWithMarker() {
val marker = Marker().apply { // 변수를 없애지 않은 이유는 나중에 Firebase에서 들고와야 하니까,, 일단 냅두겠습니다
this.icon = MarkerIcons.BLACK
this.iconTintColor = requireContext().getColor(R.color.primaryColor)
this.position = LatLng(37.36, 127.1052)
this.map = naverMap

this.setOnClickListener {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
true
}
}
}

@SuppressLint("ClickableViewAccessibility")
private fun setupWithBottomSheet() {
binding.bottomSheet.setOnTouchListener { _, _ ->
val navAction = MapFragmentDirections.actionMapFragmentToCarDetailFragment("test")
findNavController().navigate(navAction)
false
}
}

override fun onAttach(context: Context) {
super.onAttach(context)
backPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (bottomSheetBehavior.state != BottomSheetBehavior.STATE_HIDDEN) {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
} else {
requireActivity().finishAffinity()
}
}
binding.mapView.onTouchEvent(event)
}
requireActivity().onBackPressedDispatcher.addCallback(this, backPressedCallback)
}

override fun onStart() {
super.onStart()
binding.mapView.onStart()
}

override fun onPause() {
binding.mapView.onPause()
super.onPause()
}

override fun onResume() {
super.onResume()
binding.mapView.onResume()
}

override fun onPause() {
super.onPause()
binding.mapView.onPause()
override fun onStop() {
binding.mapView.onStop()
super.onStop()
}

override fun onSaveInstanceState(outState: Bundle) {
if (super.isBindingNotNull()) {
binding.mapView.onSaveInstanceState(outState)
}
super.onSaveInstanceState(outState)
binding.mapView.onSaveInstanceState(outState)
}

override fun onStop() {
super.onStop()
binding.mapView.onStop()
}

override fun onDestroyView() {
super.onDestroyView()
binding.mapView.onDestroy()
super.onDestroyView()
}

override fun onDetach() {
super.onDetach()
backPressedCallback.remove()
}

override fun onLowMemory() {
super.onLowMemory()
binding.mapView.onLowMemory()
super.onLowMemory()
}

companion object {
private const val LOCATION_PERMISSION_REQUEST_CODE = 1000
private const val LOCATION_PERMISSION_REQUEST_CODE = 100
}
}
11 changes: 2 additions & 9 deletions presentation/src/main/res/layout/fragment_car_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
tools:context=".ui.cardetail.CarDetailFragment">

<data>

<variable
name="vm"
type="com.gta.presentation.ui.cardetail.CarDetailViewModel" />
Expand All @@ -16,9 +15,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">



<ScrollView
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
Expand Down Expand Up @@ -159,11 +156,7 @@
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>




</androidx.core.widget.NestedScrollView>

<com.google.android.material.button.MaterialButton
android:id="@+id/btn_next"
Expand Down
Loading

0 comments on commit 48fdf93

Please sign in to comment.