Skip to content

Commit

Permalink
fix : 일관적인 이벤트 처리를 위해 기존 이벤트 핸들링 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
HamBP committed Jul 24, 2024
1 parent a1bb0cc commit 71b76f3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nexters.boolti.presentation.screen.home

sealed interface HomeEvent {
data class DeepLinkEvent(
val deepLink: String,
) : HomeEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
Expand All @@ -32,7 +33,7 @@ import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navDeepLink
import com.nexters.boolti.presentation.R
import com.nexters.boolti.presentation.screen.HomeViewModel
import com.nexters.boolti.presentation.extension.requireActivity
import com.nexters.boolti.presentation.screen.my.MyScreen
import com.nexters.boolti.presentation.screen.show.ShowScreen
import com.nexters.boolti.presentation.screen.ticket.TicketLoginScreen
Expand All @@ -58,10 +59,24 @@ fun HomeScreen(
val currentDestination = navBackStackEntry?.destination?.route ?: Destination.Show.route

val loggedIn by viewModel.loggedIn.collectAsStateWithLifecycle()
val context = LocalContext.current

LaunchedEffect(Unit) {
viewModel.event.collect { deepLink ->
navController.navigate(Uri.parse(deepLink))
viewModel.events.collect { event ->
when (event) {
is HomeEvent.DeepLinkEvent -> navController.navigate(Uri.parse(event.deepLink))
}
}
}

LaunchedEffect(Unit) {
val intent = context.requireActivity().intent
intent.action?.let { deepLink ->
val regex = "^https://app.boolti.in/gift/(\\w)+$".toRegex()
if (regex.matches(deepLink)) {
val giftUuid = deepLink.split("/").last()
viewModel.receiveGift(giftUuid)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.nexters.boolti.presentation.screen
package com.nexters.boolti.presentation.screen.home

import androidx.lifecycle.viewModelScope
import com.nexters.boolti.domain.repository.AuthRepository
import com.nexters.boolti.domain.repository.GiftRepository
import com.nexters.boolti.presentation.base.BaseViewModel
import com.nexters.boolti.presentation.screen.DeepLinkEvent
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
Expand All @@ -18,24 +23,22 @@ import javax.inject.Inject
@HiltViewModel
class HomeViewModel @Inject constructor(
private val authRepository: AuthRepository,
deepLinkEvent: DeepLinkEvent,
private val giftRepository: GiftRepository,
private val deepLinkEvent: DeepLinkEvent,
) : BaseViewModel() {
val loggedIn = authRepository.loggedIn.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(5000),
null,
)

val event: SharedFlow<String> =
deepLinkEvent.events.filter { it.startsWith("https://app.boolti.in/home") }
.shareIn(
scope = viewModelScope,
started = SharingStarted.Lazily,
)
private val _events = MutableSharedFlow<HomeEvent>()
val events: SharedFlow<HomeEvent> = _events.asSharedFlow()

init {
initUserInfo()
sendFcmToken()
collectDeepLinkEvent()
}

private fun initUserInfo() {
Expand All @@ -50,4 +53,21 @@ class HomeViewModel @Inject constructor(
}
}
}

private fun collectDeepLinkEvent() {
deepLinkEvent.events
.filter { it.startsWith("https://app.boolti.in/home") }
.onEach { sendEvent(HomeEvent.DeepLinkEvent(it)) }
.launchIn(viewModelScope)
}

fun receiveGift(giftUuid: String) {
// TODO: 선물 받는 로직
}

private fun sendEvent(event: HomeEvent) {
viewModelScope.launch {
_events.emit(event)
}
}
}

0 comments on commit 71b76f3

Please sign in to comment.