Skip to content

Commit f07674c

Browse files
committed
refactor vm
1 parent 4094535 commit f07674c

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

app/src/main/java/com/hoc081098/paginationmviflow/ui/main/MainContract.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,28 @@ interface MainContract {
378378
}
379379
}
380380
}
381+
382+
fun toEvent(): SingleEvent? = when (this) {
383+
is PhotoFirstPage.Data -> if (photos.isEmpty()) SingleEvent.HasReachedMax else null
384+
is PhotoFirstPage.Error -> SingleEvent.GetPhotosFailure(error)
385+
PhotoFirstPage.Loading -> null
386+
//
387+
is PhotoNextPage.Data -> if (photos.isEmpty()) SingleEvent.HasReachedMax else null
388+
is PhotoNextPage.Error -> SingleEvent.GetPhotosFailure(error)
389+
PhotoNextPage.Loading -> null
390+
//
391+
is PostFirstPage.Data -> if (posts.isEmpty()) SingleEvent.HasReachedMaxHorizontal else null
392+
is PostFirstPage.Error -> SingleEvent.GetPostsFailure(error)
393+
PostFirstPage.Loading -> null
394+
//
395+
is PostNextPage.Data -> if (posts.isEmpty()) SingleEvent.HasReachedMaxHorizontal else null
396+
is PostNextPage.Error -> SingleEvent.GetPostsFailure(error)
397+
PostNextPage.Loading -> null
398+
//
399+
is Refresh.Success -> SingleEvent.RefreshSuccess
400+
is Refresh.Error -> SingleEvent.RefreshFailure(error)
401+
Refresh.Refreshing -> null
402+
}
381403
}
382404

383405
sealed class SingleEvent {

app/src/main/java/com/hoc081098/paginationmviflow/ui/main/MainVM.kt

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import androidx.lifecycle.viewModelScope
55
import com.hoc081098.paginationmviflow.FlowTransformer
66
import com.hoc081098.paginationmviflow.pipe
77
import com.hoc081098.paginationmviflow.ui.main.MainContract.PartialStateChange
8-
import com.hoc081098.paginationmviflow.ui.main.MainContract.PartialStateChange.PhotoFirstPage
9-
import com.hoc081098.paginationmviflow.ui.main.MainContract.PartialStateChange.PhotoNextPage
10-
import com.hoc081098.paginationmviflow.ui.main.MainContract.PartialStateChange.PostFirstPage
11-
import com.hoc081098.paginationmviflow.ui.main.MainContract.PartialStateChange.PostNextPage
12-
import com.hoc081098.paginationmviflow.ui.main.MainContract.PartialStateChange.Refresh
138
import dagger.hilt.android.lifecycle.HiltViewModel
149
import kotlinx.coroutines.ExperimentalCoroutinesApi
1510
import kotlinx.coroutines.FlowPreview
@@ -44,14 +39,14 @@ class MainVM @Inject constructor(
4439
) : ViewModel() {
4540
private val initialVS = VS.initial()
4641

47-
private val _stateSF = MutableStateFlow(initialVS)
42+
private val _stateFlow = MutableStateFlow(initialVS)
4843
private val _singleEventChannel = Channel<SE>(Channel.UNLIMITED)
49-
private val _intentSF = MutableSharedFlow<VI>(extraBufferCapacity = 64)
44+
private val _intentFlow = MutableSharedFlow<VI>(extraBufferCapacity = 64)
5045

51-
val stateFlow: StateFlow<VS> get() = _stateSF.asStateFlow()
46+
val stateFlow: StateFlow<VS> get() = _stateFlow.asStateFlow()
5247
val singleEventFlow: Flow<SE> get() = _singleEventChannel.receiveAsFlow()
5348

54-
suspend fun processIntent(intent: VI) = _intentSF.emit(intent)
49+
suspend fun processIntent(intent: VI) = _intentFlow.emit(intent)
5550

5651
private val toPartialStateChanges: FlowTransformer<VI, PartialStateChange> =
5752
FlowTransformer { intents ->
@@ -79,39 +74,18 @@ class MainVM @Inject constructor(
7974

8075
private val sendSingleEvent: FlowTransformer<PartialStateChange, PartialStateChange> =
8176
FlowTransformer { changes ->
82-
changes
83-
.onEach { change ->
84-
when (change) {
85-
is PhotoFirstPage.Data -> if (change.photos.isEmpty()) _singleEventChannel.send(SE.HasReachedMax)
86-
is PhotoFirstPage.Error -> _singleEventChannel.send(SE.GetPhotosFailure(change.error))
87-
PhotoFirstPage.Loading -> Unit
88-
//
89-
is PhotoNextPage.Data -> if (change.photos.isEmpty()) _singleEventChannel.send(SE.HasReachedMax)
90-
is PhotoNextPage.Error -> _singleEventChannel.send(SE.GetPhotosFailure(change.error))
91-
PhotoNextPage.Loading -> Unit
92-
//
93-
is PostFirstPage.Data -> if (change.posts.isEmpty()) _singleEventChannel.send(SE.HasReachedMaxHorizontal)
94-
is PostFirstPage.Error -> _singleEventChannel.send(SE.GetPostsFailure(change.error))
95-
PostFirstPage.Loading -> Unit
96-
//
97-
is PostNextPage.Data -> if (change.posts.isEmpty()) _singleEventChannel.send(SE.HasReachedMaxHorizontal)
98-
is PostNextPage.Error -> _singleEventChannel.send(SE.GetPostsFailure(change.error))
99-
PostNextPage.Loading -> Unit
100-
//
101-
is Refresh.Success -> _singleEventChannel.send(SE.RefreshSuccess)
102-
is Refresh.Error -> _singleEventChannel.send(SE.RefreshFailure(change.error))
103-
Refresh.Refreshing -> Unit
104-
}
105-
}
77+
changes.onEach {
78+
_singleEventChannel.send(it.toEvent() ?: return@onEach)
79+
}
10680
}
10781

10882
init {
109-
_intentSF
83+
_intentFlow
11084
.pipe(intentFilterer)
11185
.pipe(toPartialStateChanges)
11286
.pipe(sendSingleEvent)
11387
.scan(initialVS) { vs, change -> change.reduce(vs) }
114-
.onEach { _stateSF.value = it }
88+
.onEach { _stateFlow.value = it }
11589
.launchIn(viewModelScope)
11690
}
11791

0 commit comments

Comments
 (0)