Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.View
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.hilt.navigation.fragment.hiltNavGraphViewModels
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.widget.ViewPager2
import com.google.firebase.analytics.FirebaseAnalytics
Expand All @@ -29,6 +30,9 @@ import timber.log.Timber
class HintFragment : BaseFragment<FragmentHintBinding>(FragmentHintBinding::inflate) {

private val viewModel: HintViewModel by viewModels()
private val gameSharedViewModel: com.nextroom.nextroom.presentation.ui.main.GameSharedViewModel by hiltNavGraphViewModels(
R.id.game_navigation
)
private val state: HintState
get() = viewModel.container.stateFlow.value

Expand All @@ -43,6 +47,21 @@ class HintFragment : BaseFragment<FragmentHintBinding>(FragmentHintBinding::infl
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
FirebaseAnalytics.getInstance(requireContext()).logEvent("screen_view", bundleOf("screen_name" to "hint"))

// Shared ViewModel에서 hint 수집
viewLifecycleOwner.repeatOnStarted {
gameSharedViewModel.currentHint.collect { hint ->
hint?.let { viewModel.setHint(it) }
}
}

// Shared ViewModel에서 subscribeStatus 수집
viewLifecycleOwner.repeatOnStarted {
gameSharedViewModel.subscribeStatus.collect { subscribeStatus ->
viewModel.setSubscribeStatus(subscribeStatus)
}
}

initViews()
viewModel.observe(viewLifecycleOwner, state = ::render, sideEffect = ::handleEvent)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.nextroom.nextroom.presentation.ui.hint

import androidx.lifecycle.SavedStateHandle
import com.mangbaam.commonutil.DateTimeUtil
import com.nextroom.nextroom.domain.repository.AdminRepository
import com.nextroom.nextroom.domain.repository.DataStoreRepository
Expand All @@ -18,20 +17,14 @@ import javax.inject.Inject

@HiltViewModel
class HintViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val timerRepository: TimerRepository,
private val statsRepository: StatisticsRepository,
private val adminRepository: AdminRepository,
private val dataStoreRepository: DataStoreRepository,
) : BaseViewModel<HintState, HintEvent>() {

override val container: Container<HintState, HintEvent> =
container(
HintState(
hint = savedStateHandle.get<Hint>("hint") ?: Hint(),
userSubscribeStatus = HintFragmentArgs.fromSavedStateHandle(savedStateHandle).subscribeStatus,
)
)
container(HintState())

private val state: HintState
get() = container.stateFlow.value
Expand Down Expand Up @@ -60,6 +53,20 @@ class HintViewModel @Inject constructor(
reduce { state.copy(networkDisconnectedCount = count) }
}

/**
* Called by HintFragment when hint is received from GameSharedViewModel
*/
fun setHint(hint: Hint) = intent {
reduce { state.copy(hint = hint) }
}

/**
* Called by HintFragment when subscribeStatus is received from GameSharedViewModel
*/
fun setSubscribeStatus(subscribeStatus: com.nextroom.nextroom.domain.model.SubscribeStatus) = intent {
reduce { state.copy(userSubscribeStatus = subscribeStatus) }
}

fun openAnswer() = intent {
reduce { state.copy(hint = state.hint.copy(answerOpened = true)) }
// 정답 오픈 시간 통계 집계
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.nextroom.nextroom.presentation.ui.main

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.nextroom.nextroom.domain.model.SubscribeStatus
import com.nextroom.nextroom.presentation.model.Hint
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class GameSharedViewModel @Inject constructor(
savedStateHandle: SavedStateHandle
) : ViewModel() {

// subscribeStatus는 상위 navigation에서 전달받음
private val _subscribeStatus = MutableStateFlow(
TimerFragmentArgs.fromSavedStateHandle(savedStateHandle).subscribeStatus
)
val subscribeStatus: StateFlow<SubscribeStatus> = _subscribeStatus.asStateFlow()

// hint는 TimerFragment에서 설정
private val _currentHint = MutableStateFlow<Hint?>(null)
val currentHint: StateFlow<Hint?> = _currentHint.asStateFlow()

/**
* Called by TimerFragment when user opens a hint
*/
fun setCurrentHint(hint: Hint) {
_currentHint.value = hint
}

/**
* Called when HintFragment updates hint state (e.g., answer opened)
*/
fun updateCurrentHint(hint: Hint) {
_currentHint.value = hint
}

/**
* Clear hint when returning to timer screen
*/
fun clearCurrentHint() {
_currentHint.value = null
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.nextroom.nextroom.presentation.ui.main

import com.nextroom.nextroom.domain.model.SubscribeStatus
import com.nextroom.nextroom.presentation.model.Hint

sealed interface TimerEvent {
data class OnOpenHint(
val hint: Hint,
val subscribeStatus: SubscribeStatus
val hint: Hint
) : TimerEvent

data object TimerFinish : TimerEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.setFragmentResultListener
import androidx.fragment.app.viewModels
import androidx.hilt.navigation.fragment.hiltNavGraphViewModels
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
Expand Down Expand Up @@ -50,6 +51,7 @@ class TimerFragment : BaseFragment<FragmentTimerBinding>(FragmentTimerBinding::i

private val viewModel: TimerViewModel by viewModels()
private val painterViewModel: PainterViewModel by activityViewModels()
private val gameSharedViewModel: GameSharedViewModel by hiltNavGraphViewModels(R.id.game_navigation)

private var gameStartConfirmDialog: NRDialog? = null

Expand Down Expand Up @@ -266,7 +268,8 @@ class TimerFragment : BaseFragment<FragmentTimerBinding>(FragmentTimerBinding::i
when (event) {
is TimerEvent.ClearHintCode -> binding.customCodeInput.setCode("")
is TimerEvent.OnOpenHint -> {
val action = TimerFragmentDirections.moveToHintFragment(event.hint, event.subscribeStatus)
gameSharedViewModel.setCurrentHint(event.hint)
val action = TimerFragmentDirections.moveToHintFragment()
findNavController().safeNavigate(action)
viewModel.clearHintCode()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ class TimerViewModel @Inject constructor(
answerOpened = state.answerOpenedHints.contains(hint.id),
hintImageUrlList = hint.hintImageUrlList.toList(),
answerImageUrlList = hint.answerImageUrlList.toList()
),
TimerFragmentArgs.fromSavedStateHandle(savedStateHandle).subscribeStatus
)
),
)
setGameState()
Expand Down
15 changes: 1 addition & 14 deletions presentation/src/main/res/navigation/game_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@

<action
android:id="@+id/move_to_hint_fragment"
app:destination="@id/hint_fragment">
<argument
android:name="hint"
app:argType="com.nextroom.nextroom.presentation.model.Hint" />
<argument
android:name="subscribeStatus"
app:argType="com.nextroom.nextroom.domain.model.SubscribeStatus" />
</action>
app:destination="@id/hint_fragment" />
<action
android:id="@+id/move_to_memo_fragment"
app:destination="@id/memo_fragment" />
Expand All @@ -43,12 +36,6 @@
<action
android:id="@+id/move_to_memo_fragment"
app:destination="@id/memo_fragment" />
<argument
android:name="subscribeStatus"
app:argType="com.nextroom.nextroom.domain.model.SubscribeStatus" />
<argument
android:name="hint"
app:argType="com.nextroom.nextroom.presentation.model.Hint" />
</fragment>

<fragment
Expand Down