Skip to content
Merged
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
24 changes: 15 additions & 9 deletions core/ui/src/main/java/com/neki/android/core/ui/MviIntentStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.stateIn
Expand All @@ -22,7 +23,7 @@ interface MviIntentStore<STATE, INTENT, EFFECT> {

class MviIntentStoreImpl<STATE, INTENT, EFFECT>(
initialState: STATE,
initialFetchData: () -> Unit,
initialFetchData: (() -> Unit)?,
private val coroutineScope: CoroutineScope,
private val onIntent: (
intent: INTENT,
Expand All @@ -32,13 +33,18 @@ class MviIntentStoreImpl<STATE, INTENT, EFFECT>(
) -> Unit,
) : MviIntentStore<STATE, INTENT, EFFECT> {
private val _uiState = MutableStateFlow(initialState)
override val uiState: StateFlow<STATE> = _uiState
.onStart { initialFetchData() }
.stateIn(
scope = coroutineScope,
started = SharingStarted.WhileSubscribed(5000L),
initialValue = initialState,
)
override val uiState: StateFlow<STATE> =
if (initialFetchData != null) {
_uiState
.onStart { initialFetchData() }
.stateIn(
scope = coroutineScope,
started = SharingStarted.WhileSubscribed(5000L),
initialValue = initialState,
)
} else {
_uiState.asStateFlow()
}
private val _sideEffects = Channel<EFFECT>(Channel.BUFFERED)
override val sideEffects: Flow<EFFECT> = _sideEffects.receiveAsFlow()
private fun setState(reduce: STATE.() -> STATE) {
Expand All @@ -62,7 +68,7 @@ class MviIntentStoreImpl<STATE, INTENT, EFFECT>(
fun <STATE, INTENT, EFFECT> ViewModel.mviIntentStore(
initialState: STATE,
onIntent: (INTENT, STATE, (STATE.() -> STATE) -> Unit, (EFFECT) -> Unit) -> Unit,
initialFetchData: () -> Unit = {},
initialFetchData: (() -> Unit)? = null,
): MviIntentStore<STATE, INTENT, EFFECT> = MviIntentStoreImpl(
initialState = initialState,
coroutineScope = viewModelScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ class AllAlbumViewModel @Inject constructor(
mviIntentStore(
initialState = AllAlbumState(),
onIntent = ::onIntent,
initialFetchData = { store.onIntent(AllAlbumIntent.EnterAllAlbumScreen) },
)

init {
store.onIntent(AllAlbumIntent.EnterAllAlbumScreen)
}

private fun onIntent(
intent: AllAlbumIntent,
state: AllAlbumState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ class ArchiveMainViewModel @Inject constructor(
mviIntentStore(
initialState = ArchiveMainState(),
onIntent = ::onIntent,
initialFetchData = { store.onIntent(ArchiveMainIntent.EnterArchiveMainScreen) },
)

private var fetchJob: Job? = null

init {
store.onIntent(ArchiveMainIntent.EnterArchiveMainScreen)
}

private fun onIntent(
intent: ArchiveMainIntent,
state: ArchiveMainState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ class TermViewModel @Inject constructor(
mviIntentStore(
initialState = TermState(),
onIntent = ::onIntent,
initialFetchData = { store.onIntent(TermIntent.EnterTermScreen) },
)

init {
store.onIntent(TermIntent.EnterTermScreen)
}

private fun onIntent(
intent: TermIntent,
state: TermState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ internal class MyPageViewModel @Inject constructor(
mviIntentStore(
initialState = MyPageState(),
onIntent = ::onIntent,
initialFetchData = { store.onIntent(MyPageIntent.EnterMypageScreen) },
)

init {
store.onIntent(MyPageIntent.EnterMypageScreen)
}

private fun onIntent(
intent: MyPageIntent,
state: MyPageState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ class UploadAlbumViewModel @AssistedInject constructor(
selectedUris = uriStrings.map { it.toUri() }.toImmutableList(),
),
onIntent = ::onIntent,
initialFetchData = { store.onIntent(UploadAlbumIntent.EnterUploadAlbumScreen) },
)

init {
store.onIntent(UploadAlbumIntent.EnterUploadAlbumScreen)
}

private fun onIntent(
intent: UploadAlbumIntent,
state: UploadAlbumState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class PoseDetailViewModel @AssistedInject constructor(
mviIntentStore(
initialState = PoseDetailState(),
onIntent = ::onIntent,
initialFetchData = { store.onIntent(PoseDetailIntent.EnterPoseDetailScreen) },
)

init {
store.onIntent(PoseDetailIntent.EnterPoseDetailScreen)
viewModelScope.launch {
bookmarkRequests
.debounce(500)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ internal class RandomPoseViewModel @AssistedInject constructor(
mviIntentStore(
initialState = RandomPoseState(),
onIntent = ::onIntent,
initialFetchData = { store.onIntent(RandomPoseIntent.EnterRandomPoseScreen) },
)

init {
store.onIntent(RandomPoseIntent.EnterRandomPoseScreen)
}

private fun onIntent(
intent: RandomPoseIntent,
state: RandomPoseState,
Expand Down
Loading