Skip to content

Commit

Permalink
refactor: add getResult, getWithLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
ksw4015 committed Nov 29, 2024
1 parent 29f43d9 commit 6c70f0e
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package kr.ksw.visitkorea.presentation.core

import android.annotation.SuppressLint
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.Priority
import com.google.android.gms.tasks.CancellationTokenSource
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import kr.ksw.visitkorea.presentation.common.latitudeToStringOrDefault
import kr.ksw.visitkorea.presentation.common.longitudeToStringOrDefault

open class BaseViewModel<EFFECT> : ViewModel() {
open class BaseViewModel<EFFECT> (
private val fusedLocationProviderClient: FusedLocationProviderClient? = null
) : ViewModel() {
private val _uiEffect = MutableSharedFlow<EFFECT>(replay = 0)
val uiEffect: SharedFlow<EFFECT>
get() = _uiEffect.asSharedFlow()
Expand All @@ -17,4 +25,16 @@ open class BaseViewModel<EFFECT> : ViewModel() {
_uiEffect.emit(effect)
}
}

@SuppressLint("MissingPermission")
protected fun getWithLocation(block: (lat: String, lng: String) -> Unit) {
fusedLocationProviderClient?.getCurrentLocation(
Priority.PRIORITY_HIGH_ACCURACY,
CancellationTokenSource().token
)?.addOnCompleteListener { task ->
val lat = task.result.latitudeToStringOrDefault()
val lng = task.result.longitudeToStringOrDefault()
block(lat, lng)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kr.ksw.visitkorea.presentation.core

fun <T> Result<T>.getResult(block: (result: T) -> Unit) {
getOrNull()?.run {
block(this)
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package kr.ksw.visitkorea.presentation.detail.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kr.ksw.visitkorea.domain.usecase.detail.GetDetailImageUseCase
import kr.ksw.visitkorea.domain.usecase.detail.GetHotelDetailUseCase
import kr.ksw.visitkorea.domain.usecase.detail.GetHotelRoomDetailUseCase
import kr.ksw.visitkorea.domain.usecase.util.toImageUrl
import kr.ksw.visitkorea.presentation.common.DetailParcel
import kr.ksw.visitkorea.presentation.core.getResult
import kr.ksw.visitkorea.presentation.core.viewModelLauncher
import javax.inject.Inject

Expand Down Expand Up @@ -56,10 +55,10 @@ class DetailHotelViewModel @Inject constructor(
private fun getHotelDetail(contentId: String) {
viewModelLauncher {
getHotelDetailUseCase(contentId)
.getOrNull()?.run {
.getResult { result ->
_hotelDetailState.update {
it.copy(
hotelDetail = this
hotelDetail = result
)
}
}
Expand All @@ -69,10 +68,10 @@ class DetailHotelViewModel @Inject constructor(
private fun getHotelRoomDetail(contentId: String) {
viewModelLauncher {
getHotelRoomDetailUseCase(contentId)
.getOrNull()?.run {
.getResult { result ->
_hotelDetailState.update {
it.copy(
hotelRoomDetail = this
hotelRoomDetail = result
)
}
}
Expand All @@ -85,10 +84,10 @@ class DetailHotelViewModel @Inject constructor(
viewModelLauncher {
getDetailImageUseCase(
contentId, "Y"
).getOrNull()?.run {
).getResult { result ->
_hotelDetailState.update {
it.copy(
images = this.map { image ->
images = result.map { image ->
image.copy(
originImgUrl = image.originImgUrl.toImageUrl(),
smallImageUrl = image.smallImageUrl.toImageUrl()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package kr.ksw.visitkorea.presentation.detail.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kr.ksw.visitkorea.domain.common.TYPE_RESTAURANT
import kr.ksw.visitkorea.domain.usecase.detail.GetDetailCommonUseCase
import kr.ksw.visitkorea.domain.usecase.detail.GetDetailImageUseCase
import kr.ksw.visitkorea.domain.usecase.detail.GetDetailIntroUseCase
import kr.ksw.visitkorea.domain.usecase.util.toImageUrl
import kr.ksw.visitkorea.presentation.common.DetailParcel
import kr.ksw.visitkorea.presentation.core.getResult
import kr.ksw.visitkorea.presentation.core.viewModelLauncher
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -53,29 +53,30 @@ class DetailViewModel @Inject constructor(
}

private fun getDetailCommon(contentId: String) {
viewModelScope.launch {
getDetailCommonUseCase(contentId).getOrNull()?.run {
_detailState.update {
it.copy(
detailCommon = this
)
viewModelLauncher {
getDetailCommonUseCase(contentId)
.getResult { result ->
_detailState.update {
it.copy(
detailCommon = result
)
}
}
}
}
}

private fun getDetailIntro(
contentId: String,
contentTypeId: String
) {
viewModelScope.launch {
viewModelLauncher {
getDetailIntroUseCase(
contentId,
contentTypeId
).getOrNull()?.run {
).getResult { result ->
_detailState.update {
it.copy(
detailIntro = this
detailIntro = result
)
}
}
Expand All @@ -85,13 +86,13 @@ class DetailViewModel @Inject constructor(
private fun getDetailImage(
contentId: String
) {
viewModelScope.launch {
viewModelLauncher {
getDetailImageUseCase(
contentId, "Y"
).getOrNull()?.run {
).getResult { result ->
_detailState.update {
it.copy(
images = this.map { image ->
images = result.map { image ->
image.copy(
originImgUrl = image.originImgUrl.toImageUrl(),
smallImageUrl = image.smallImageUrl.toImageUrl()
Expand All @@ -106,7 +107,7 @@ class DetailViewModel @Inject constructor(
private fun getDetailMenuImage(
contentId: String
) {
viewModelScope.launch {
viewModelLauncher {
val result = getDetailImageUseCase(
contentId, "N"
).getOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kr.ksw.visitkorea.domain.usecase.festival.GetFestivalListUseCase
import kr.ksw.visitkorea.domain.usecase.mapper.toFestival
import kr.ksw.visitkorea.presentation.core.BaseViewModel
import kr.ksw.visitkorea.presentation.core.getResult
import kr.ksw.visitkorea.presentation.core.viewModelLauncher
import javax.inject.Inject

@HiltViewModel
Expand All @@ -36,16 +37,15 @@ class FestivalViewModel @Inject constructor(
}

private fun getFestivalList(forceFetch: Boolean = false) {
viewModelScope.launch {
val hotelListFlow = getFestivalListUseCase(
viewModelLauncher {
getFestivalListUseCase(
forceFetch,
"20241007",
"20241007",
null,
null
).getOrNull()
if(hotelListFlow != null) {
val festivalModelFlow = hotelListFlow.map { pagingData ->
).getResult { result ->
val festivalModelFlow = result.map { pagingData ->
pagingData.map {
it.toFestival()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
package kr.ksw.visitkorea.presentation.home.viewmodel

import android.annotation.SuppressLint
import androidx.lifecycle.viewModelScope
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.Priority
import com.google.android.gms.tasks.CancellationTokenSource
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kr.ksw.visitkorea.domain.usecase.home.GetCultureCenterForHomeUseCase
import kr.ksw.visitkorea.domain.usecase.home.GetLeisureSportsForHomeUseCase
import kr.ksw.visitkorea.domain.usecase.home.GetRestaurantForHomeUseCase
import kr.ksw.visitkorea.domain.usecase.home.GetTouristSpotForHomeUseCase
import kr.ksw.visitkorea.presentation.core.BaseViewModel
import kr.ksw.visitkorea.presentation.core.getResult
import kr.ksw.visitkorea.presentation.core.viewModelLauncher
import javax.inject.Inject

@SuppressLint("MissingPermission")
@HiltViewModel
class HomeViewModel @Inject constructor(
private val getTouristSpotForHomeUseCase: GetTouristSpotForHomeUseCase,
private val getCultureCenterForHomeUseCase: GetCultureCenterForHomeUseCase,
private val getLeisureSportsForHomeUseCase: GetLeisureSportsForHomeUseCase,
private val getRestaurantForHomeUseCase: GetRestaurantForHomeUseCase,
private val fusedLocationProviderClient: FusedLocationProviderClient
): BaseViewModel<HomeUiEffect>() {
fusedLocationProviderClient: FusedLocationProviderClient
): BaseViewModel<HomeUiEffect>(fusedLocationProviderClient) {
private val _homeState = MutableStateFlow(HomeState())
val homeState: StateFlow<HomeState>
get() = _homeState.asStateFlow()

init {
fusedLocationProviderClient.getCurrentLocation(
Priority.PRIORITY_HIGH_ACCURACY,
CancellationTokenSource().token
).addOnCompleteListener { task ->
val lat = task.result.latitude.run { if(this == 0.0) "37.5678958128" else this.toString() }
val lng = task.result.longitude.run { if(this == 0.0) "126.9817290217" else this.toString() }
getWithLocation { lat, lng ->
getTouristSpot(lat, lng)
getCultureCenter(lat, lng)
getLeisureSports(lat, lng)
Expand All @@ -64,16 +55,15 @@ class HomeViewModel @Inject constructor(
lat: String,
lng: String,
) {
viewModelScope.launch {
val items = getTouristSpotForHomeUseCase(
viewModelLauncher {
getTouristSpotForHomeUseCase(
mapX = lng,
mapY = lat
).getOrNull()
if(items != null) {
).getResult { result ->
_homeState.update {
it.copy(
mainImage = items[0].firstImage,
touristSpotList = items
mainImage = result[0].firstImage,
touristSpotList = result
)
}
}
Expand All @@ -84,15 +74,14 @@ class HomeViewModel @Inject constructor(
lat: String,
lng: String,
) {
viewModelScope.launch {
val items = getCultureCenterForHomeUseCase(
viewModelLauncher {
getCultureCenterForHomeUseCase(
mapX = lng,
mapY = lat
).getOrNull()
if(items != null) {
).getResult { result ->
_homeState.update {
it.copy(
cultureCenterList = items
cultureCenterList = result
)
}
}
Expand All @@ -103,15 +92,14 @@ class HomeViewModel @Inject constructor(
lat: String,
lng: String,
) {
viewModelScope.launch {
val items = getRestaurantForHomeUseCase(
viewModelLauncher {
getRestaurantForHomeUseCase(
mapX = lng,
mapY = lat
).getOrNull()
if(items != null) {
).getResult { result ->
_homeState.update {
it.copy(
restaurantList = items
restaurantList = result
)
}
}
Expand All @@ -122,15 +110,14 @@ class HomeViewModel @Inject constructor(
lat: String,
lng: String,
) {
viewModelScope.launch {
val items = getLeisureSportsForHomeUseCase(
viewModelLauncher {
getLeisureSportsForHomeUseCase(
mapX = lng,
mapY = lat
).getOrNull()
if(items != null) {
).getResult { result ->
_homeState.update {
it.copy(
leisureSportsList = items
leisureSportsList = result
)
}
}
Expand Down
Loading

0 comments on commit 6c70f0e

Please sign in to comment.