Skip to content

Commit 6845f68

Browse files
authored
feat: 앱이 백그라운드 상태에서 포그라운드로 돌아온 경우 식당 정보를 업데이트하는 로직 구현 (#40)
* fix: 함수명 통일 * fix: 마커 클릭시 ViewPager의 item이 표시되는 애니메이션 제거 * fix: 식당 정보 업데이트 로직 구현(개선 필요) * fix: 코드 가독성을 위해 scope 함수 적용
1 parent 00c40ad commit 6845f68

File tree

3 files changed

+79
-28
lines changed

3 files changed

+79
-28
lines changed

app/src/main/java/com/woozoo/menumonya/MainActivity.kt

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
3636
private lateinit var binding: ActivityMainBinding
3737

3838
private lateinit var viewPager: ViewPager2
39+
private var restaurantAdapter: RestaurantAdapter? = null
3940
private lateinit var locationPermissionDialog: LocationPermissionDialog
4041

4142
override fun onCreate(savedInstanceState: Bundle?) {
@@ -100,19 +101,25 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
100101

101102
private fun handleEvent(event: Event) = when (event) {
102103
is Event.ShowToast -> Toast.makeText(this, event.text, Toast.LENGTH_SHORT).show()
104+
is Event.FetchRestaurantInfo -> {
105+
if (restaurantAdapter != null) {
106+
restaurantAdapter?.setData(event.data)
107+
viewPager.adapter?.notifyDataSetChanged()
108+
} else { }
109+
}
103110
is Event.OnMarkerClicked -> {
104111
if (viewPager.adapter != null) {
105-
viewPager.currentItem = event.markerIndex
112+
viewPager.setCurrentItem(event.markerIndex, false)
106113
} else {
107114
viewModel.showLocationViewPager(event.markerIndex)
108115
}
109116
}
110117
is Event.ShowRestaurantView -> {
111118
if (viewPager.adapter == null) {
112-
viewPager.adapter =
113-
RestaurantAdapter(event.data, this, remoteConfigRepository, analyticsUtils)
119+
restaurantAdapter = RestaurantAdapter(event.data, this, remoteConfigRepository, analyticsUtils)
120+
viewPager.adapter = restaurantAdapter
114121
if (event.markerIndex != -1) {
115-
viewPager.currentItem = event.markerIndex
122+
viewPager.setCurrentItem(event.markerIndex, false)
116123
} else { }
117124
} else { }
118125
}
@@ -177,6 +184,9 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
177184
binding.naverMap.onResume()
178185

179186
viewModel.checkLatestAppVersion()
187+
if (viewPager != null && restaurantAdapter != null) {
188+
viewModel.updateLocationInfo(viewPager.currentItem)
189+
}
180190
}
181191

182192
override fun onPause() {
@@ -211,28 +221,30 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
211221
viewPager.adapter = null
212222
viewModel.showLocationInfo("강남")
213223

214-
binding.locationGnBtn.background = applicationContext.getDrawable(R.drawable.color_button_background)
215-
binding.locationYsBtn.background = applicationContext.getDrawable(R.drawable.white_button_background)
216-
binding.locationGnBtn.setTextColor(applicationContext.getColor(R.color.white))
217-
binding.locationYsBtn.setTextColor(applicationContext.getColor(R.color.gray600))
218-
219-
binding.currentLocationBtn.background = resources.getDrawable(R.drawable.current_location_button)
220-
binding.currentLocationTv.setTextColor(resources.getColor(R.color.colorPrimary))
221-
binding.currentLocationIv.setColorFilter(resources.getColor(R.color.colorPrimary))
224+
binding.apply {
225+
locationGnBtn.background = applicationContext.getDrawable(R.drawable.color_button_background)
226+
locationYsBtn.background = applicationContext.getDrawable(R.drawable.white_button_background)
227+
locationGnBtn.setTextColor(applicationContext.getColor(R.color.white))
228+
locationYsBtn.setTextColor(applicationContext.getColor(R.color.gray600))
229+
currentLocationBtn.background = resources.getDrawable(R.drawable.current_location_button)
230+
currentLocationTv.setTextColor(resources.getColor(R.color.colorPrimary))
231+
currentLocationIv.setColorFilter(resources.getColor(R.color.colorPrimary))
232+
}
222233
}
223234
R.id.location_ys_btn -> {
224235
viewPager.invalidate()
225236
viewPager.adapter = null
226237
viewModel.showLocationInfo("역삼")
227238

228-
binding.locationYsBtn.background = applicationContext.getDrawable(R.drawable.color_button_background)
229-
binding.locationGnBtn.background = applicationContext.getDrawable(R.drawable.white_button_background)
230-
binding.locationYsBtn.setTextColor(applicationContext.getColor(R.color.white))
231-
binding.locationGnBtn.setTextColor(applicationContext.getColor(R.color.gray600))
232-
233-
binding.currentLocationBtn.background = resources.getDrawable(R.drawable.current_location_button)
234-
binding.currentLocationTv.setTextColor(resources.getColor(R.color.colorPrimary))
235-
binding.currentLocationIv.setColorFilter(resources.getColor(R.color.colorPrimary))
239+
binding.apply {
240+
locationYsBtn.background = applicationContext.getDrawable(R.drawable.color_button_background)
241+
locationGnBtn.background = applicationContext.getDrawable(R.drawable.white_button_background)
242+
locationYsBtn.setTextColor(applicationContext.getColor(R.color.white))
243+
locationGnBtn.setTextColor(applicationContext.getColor(R.color.gray600))
244+
currentLocationBtn.background = resources.getDrawable(R.drawable.current_location_button)
245+
currentLocationTv.setTextColor(resources.getColor(R.color.colorPrimary))
246+
currentLocationIv.setColorFilter(resources.getColor(R.color.colorPrimary))
247+
}
236248
}
237249
R.id.feedback_iv -> {
238250
val feedbackUrl = viewModel.getFeedbackUrl()

app/src/main/java/com/woozoo/menumonya/MainViewModel.kt

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class MainViewModel @Inject constructor(
5454
private var mRestaurantInfoArray: ArrayList<Restaurant> = ArrayList()
5555
private var markerList: ArrayList<Marker> = ArrayList()
5656
private var selectedLocation: String = ""
57+
private var isInitialized: Boolean = false
5758

5859
init {
5960
locationManager = application.getSystemService(LOCATION_SERVICE) as LocationManager
@@ -77,8 +78,10 @@ class MainViewModel @Inject constructor(
7778
minZoom = MAP_MIN_ZOOM
7879
}
7980

80-
moveCameraCoord(LATLNG_GN.latitude, LATLNG_GN.longitude)
81+
moveCameraToCoord(LATLNG_GN.latitude, LATLNG_GN.longitude)
8182
showLocationInfo("강남")
83+
84+
isInitialized = true
8285
}
8386
}
8487

@@ -116,7 +119,7 @@ class MainViewModel @Inject constructor(
116119
}
117120
}
118121

119-
private fun moveCameraCoord(latitude: Double, longitude: Double) {
122+
private fun moveCameraToCoord(latitude: Double, longitude: Double) {
120123
val coord = LatLng(latitude, longitude)
121124
val cameraUpdateParams = CameraUpdateParams().apply {
122125
scrollTo(coord)
@@ -141,8 +144,8 @@ class MainViewModel @Inject constructor(
141144
selectedLocation = location
142145

143146
when (selectedLocation) {
144-
"강남" -> moveCameraCoord(LATLNG_GN.latitude, LATLNG_GN.longitude)
145-
"역삼" -> moveCameraCoord(LATLNG_YS.latitude, LATLNG_YS.longitude)
147+
"강남" -> moveCameraToCoord(LATLNG_GN.latitude, LATLNG_GN.longitude)
148+
"역삼" -> moveCameraToCoord(LATLNG_YS.latitude, LATLNG_YS.longitude)
146149
}
147150

148151
mRestaurantInfoArray = fireStoreRepository.getRestaurantInLocation(location)
@@ -152,7 +155,26 @@ class MainViewModel @Inject constructor(
152155
}
153156
}
154157

155-
private fun setMarkers(restaurantInfo: ArrayList<Restaurant>) {
158+
fun updateLocationInfo(currentViewPagerIndex: Int) {
159+
if (isInitialized) {
160+
showLoading(true)
161+
viewModelScope.launch {
162+
mRestaurantInfoArray = fireStoreRepository.getRestaurantInLocation(selectedLocation)
163+
setMarkers(mRestaurantInfoArray, currentViewPagerIndex)
164+
165+
fetchRestaurantInfo(mRestaurantInfoArray)
166+
showLoading(false)
167+
}
168+
}
169+
}
170+
171+
/**
172+
* 지도에 식당 마커들을 표시함.
173+
* selectedIndex 값을 지정할 경우, 해당 인덱스의 마커를 클릭된 아이콘(@drawable/restaurant_marker_selected)으로 표시함.
174+
*
175+
* @param selectedIndex 선택된 아이콘으로 변경할 마커의 인덱스.
176+
*/
177+
private fun setMarkers(restaurantInfo: ArrayList<Restaurant>, selectedIndex: Int = -1) {
156178
if (restaurantInfo.size > 0) {
157179
// 마커 표시 초기화
158180
for (marker in markerList) {
@@ -181,6 +203,14 @@ class MainViewModel @Inject constructor(
181203
markerList.add(marker)
182204
}
183205

206+
// 클릭된 아이콘으로 변경
207+
if (selectedIndex != -1) {
208+
markerList[selectedIndex].apply {
209+
icon = OverlayImage.fromResource(R.drawable.restaurant_marker_selected)
210+
zIndex = Marker.DEFAULT_GLOBAL_Z_INDEX + 1
211+
}
212+
}
213+
184214
markerList.forEach { marker ->
185215
marker.map = naverMap
186216
}
@@ -206,7 +236,7 @@ class MainViewModel @Inject constructor(
206236
locationManager,
207237
LocationListener { location ->
208238
// 내 위치로 카메라 이동, 내 위치 표시
209-
moveCameraCoord(location.latitude, location.longitude)
239+
moveCameraToCoord(location.latitude, location.longitude)
210240
naverMap.apply {
211241
locationSource = FusedLocationSource(activity, LOCATION_PERMISSION_REQUEST_CODE)
212242
locationTrackingMode = LocationTrackingMode.Follow
@@ -262,9 +292,13 @@ class MainViewModel @Inject constructor(
262292
event(Event.ShowUpdateDialog(""))
263293
}
264294

295+
private fun fetchRestaurantInfo(data: ArrayList<Restaurant>) {
296+
event(Event.FetchRestaurantInfo(data))
297+
}
298+
265299
sealed class Event {
266300
/**
267-
* MainActivity에 전달할 이벤트를 이곳에 정
301+
* MainActivity에 전달할 이벤트를 이 곳에 정의함.
268302
*
269303
* (ex) data class ShowToast(val text: String) : Event()
270304
*/
@@ -276,5 +310,7 @@ class MainViewModel @Inject constructor(
276310
data class MoveToCurrentLocation(val data: String): Event()
277311
data class ShowLoading(val visibility: Boolean): Event()
278312
data class ShowUpdateDialog(val data: String): Event()
313+
314+
data class FetchRestaurantInfo(val data: ArrayList<Restaurant>): Event()
279315
}
280316
}

app/src/main/java/com/woozoo/menumonya/RestaurantAdapter.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import com.woozoo.menumonya.util.AnalyticsUtils.Companion.CONTENT_TYPE_REPORT_BU
1919
import com.woozoo.menumonya.util.DateUtils.Companion.getTodayDate
2020
import com.woozoo.menumonya.util.DateUtils.Companion.getTodayMenuDateText
2121

22-
class RestaurantAdapter(private val restaurantInfoArray: ArrayList<Restaurant>,
22+
class RestaurantAdapter(private var restaurantInfoArray: ArrayList<Restaurant>,
2323
private val context: Context,
2424
private val remoteConfigRepository: RemoteConfigRepository,
2525
private val analyticsUtils: AnalyticsUtils
@@ -100,4 +100,7 @@ class RestaurantAdapter(private val restaurantInfoArray: ArrayList<Restaurant>,
100100
return restaurantInfoArray.size
101101
}
102102

103+
fun setData(data: ArrayList<Restaurant>) {
104+
restaurantInfoArray = data
105+
}
103106
}

0 commit comments

Comments
 (0)