-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPinSearchFragment.kt
251 lines (212 loc) · 9.26 KB
/
PinSearchFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.devkor.kodaero
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.devkor.kodaero.databinding.FragmentPinSearchBinding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.naver.maps.geometry.LatLng
import com.naver.maps.map.CameraAnimation
import com.naver.maps.map.CameraUpdate
import com.naver.maps.map.LocationTrackingMode
import com.naver.maps.map.MapFragment
import com.naver.maps.map.NaverMap
import com.naver.maps.map.OnMapReadyCallback
import com.naver.maps.map.overlay.Marker
import com.naver.maps.map.overlay.OverlayImage
import com.naver.maps.map.util.FusedLocationSource
class PinSearchFragment : Fragment(), OnMapReadyCallback {
private var _binding: FragmentPinSearchBinding? = null
private val binding get() = _binding!!
private lateinit var naverMap: NaverMap
private lateinit var locationSource: FusedLocationSource
private val LOCATION_PERMISSION_REQUEST_CODE = 5000
private var cameraPosition: LatLng? = null
private lateinit var viewModel: FetchDataViewModel
private lateinit var facilityType: String
private val markers = mutableListOf<Marker>()
private lateinit var bottomSheetBehavior: BottomSheetBehavior<*>
private lateinit var pinSearchAdapter: PinSearchAdapter
// To hold facilities for each building
private val facilitiesMap = mutableMapOf<Int, List<FacilityItem>>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentPinSearchBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.backToHomeButton.setOnClickListener {
requireActivity().supportFragmentManager.popBackStack()
}
facilityType = arguments?.getString(ARG_FACILITY_NAME) ?: "Facility"
binding.facilityName.text = getFacilityNameInKorean(facilityType)
initMapView()
viewModel = ViewModelProvider(this).get(FetchDataViewModel::class.java)
viewModel.buildingList.observe(viewLifecycleOwner, { buildingList ->
if (buildingList.isNotEmpty()) {
updateMarkers(buildingList)
}
})
viewModel.searchFacilities(facilityType)
bottomSheetBehavior = BottomSheetBehavior.from(binding.pinsearchIncludedLayout.standardBottomSheet)
bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
pinSearchAdapter = PinSearchAdapter()
binding.pinsearchIncludedLayout.pinSearchListRecyclerview.layoutManager = LinearLayoutManager(requireContext())
binding.pinsearchIncludedLayout.pinSearchListRecyclerview.adapter = pinSearchAdapter
bottomSheetBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetBehavior.peekHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250f, resources.displayMetrics).toInt()
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
// Handle on slide events if needed
}
})
}
private fun getFacilityNameInKorean(facilityType: String): String {
return when (facilityType) {
"CAFE" -> "카페"
"CAFETERIA" -> "식당"
"CONVENIENCE_STORE" -> "편의점"
"READING_ROOM" -> "열람실"
"STUDY_ROOM" -> "스터디룸"
"REST_AREA" -> "휴게 공간"
"WATER_PURIFIER" -> "정수기"
"PRINTER" -> "프린터"
"VENDING_MACHINE" -> "자판기"
"SMOKING_AREA" -> "흡연구역"
"SLEEPING_ROOM" -> "수면실"
else -> facilityType
}
}
private fun initMapView() {
val mapFragment = childFragmentManager.findFragmentById(R.id.map_view) as MapFragment?
?: MapFragment.newInstance().also {
childFragmentManager.beginTransaction().add(R.id.map_view, it).commit()
}
mapFragment.getMapAsync(this)
locationSource = FusedLocationSource(this, LOCATION_PERMISSION_REQUEST_CODE)
}
private fun updateMarkers(buildingList: List<BuildingItem>) {
markers.clear()
buildingList.forEach { building ->
viewModel.getFacilities(building.buildingId, facilityType)
}
// Observe the facilityList to update markers once data is fetched
viewModel.facilityList.observe(viewLifecycleOwner, { facilityMap ->
facilityMap.forEach { (buildingId, facilities) ->
facilitiesMap[buildingId] = facilities
val count = facilities.size
val building = buildingList.find { it.buildingId == buildingId }
if (building != null) {
addMarker(building, count)
}
}
if (::naverMap.isInitialized) {
showMarkersOnMap()
}
})
}
private fun addMarker(building: BuildingItem, count: Int) {
val drawableName = "pin_${facilityType.lowercase()}"
val drawableResId = resources.getIdentifier(drawableName, "drawable", requireContext().packageName)
val marker = Marker().apply {
position = LatLng(building.latitude ?: 0.0, building.longitude ?: 0.0)
icon = OverlayImage.fromBitmap(createDrawableWithText(drawableResId, count.toString()))
tag = building.buildingId
}
marker.setOnClickListener {
onMarkerClick(marker)
true
}
markers.add(marker)
}
private fun onMarkerClick(marker: Marker) {
val buildingId = marker.tag as Int
val facilities = facilitiesMap[buildingId] ?: emptyList()
pinSearchAdapter.submitList(facilities)
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}
private fun showMarkersOnMap() {
markers.forEach { it.map = naverMap }
}
private fun getDrawableAsBitmap(drawableResId: Int): Bitmap {
val drawable = ContextCompat.getDrawable(requireContext(), drawableResId)!!
return Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888).also { bitmap ->
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
}
}
private fun createDrawableWithText(drawableResId: Int, text: String): Bitmap {
val bitmap = getDrawableAsBitmap(drawableResId)
val canvas = Canvas(bitmap)
val paintCircle = Paint().apply {
color = ContextCompat.getColor(requireContext(), R.color.red)
isAntiAlias = true
}
val circleRadius = 25f
val circleX = bitmap.width - circleRadius - 10f
val circleY = circleRadius + 10f
canvas.drawCircle(circleX, circleY, circleRadius, paintCircle)
val paintText = Paint().apply {
color = ContextCompat.getColor(requireContext(), android.R.color.white)
textSize = 30f
typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
isAntiAlias = true
}
val textBounds = android.graphics.Rect()
paintText.getTextBounds(text, 0, text.length, textBounds)
val textX = circleX - (textBounds.width() / 2)
val textY = circleY + (textBounds.height() / 2)
canvas.drawText(text, textX, textY, paintText)
return bitmap
}
override fun onMapReady(naverMap: NaverMap) {
this.naverMap = naverMap
naverMap.locationSource = locationSource
naverMap.uiSettings.isLocationButtonEnabled = true
naverMap.locationTrackingMode = LocationTrackingMode.Follow
val cameraUpdate = CameraUpdate.zoomTo(16.0)
naverMap.moveCamera(cameraUpdate)
cameraPosition?.let {
moveCameraToPosition(it)
}
showMarkersOnMap()
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
fun moveCameraToPosition(position: LatLng) {
if (::naverMap.isInitialized) {
naverMap.moveCamera(CameraUpdate.scrollTo(position).animate(CameraAnimation.Easing, 1000L))
} else {
cameraPosition = position
}
}
companion object {
private const val ARG_FACILITY_NAME = "facility_name"
fun newInstance(facilityName: String): PinSearchFragment {
val fragment = PinSearchFragment()
val args = Bundle().apply {
putString(ARG_FACILITY_NAME, facilityName)
}
fragment.arguments = args
return fragment
}
}
}