@@ -5,11 +5,6 @@ import androidx.lifecycle.viewModelScope
5
5
import com.hoc081098.paginationmviflow.FlowTransformer
6
6
import com.hoc081098.paginationmviflow.pipe
7
7
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
13
8
import dagger.hilt.android.lifecycle.HiltViewModel
14
9
import kotlinx.coroutines.ExperimentalCoroutinesApi
15
10
import kotlinx.coroutines.FlowPreview
@@ -44,14 +39,14 @@ class MainVM @Inject constructor(
44
39
) : ViewModel() {
45
40
private val initialVS = VS .initial()
46
41
47
- private val _stateSF = MutableStateFlow (initialVS)
42
+ private val _stateFlow = MutableStateFlow (initialVS)
48
43
private val _singleEventChannel = Channel <SE >(Channel .UNLIMITED )
49
- private val _intentSF = MutableSharedFlow <VI >(extraBufferCapacity = 64 )
44
+ private val _intentFlow = MutableSharedFlow <VI >(extraBufferCapacity = 64 )
50
45
51
- val stateFlow: StateFlow <VS > get() = _stateSF .asStateFlow()
46
+ val stateFlow: StateFlow <VS > get() = _stateFlow .asStateFlow()
52
47
val singleEventFlow: Flow <SE > get() = _singleEventChannel .receiveAsFlow()
53
48
54
- suspend fun processIntent (intent : VI ) = _intentSF .emit(intent)
49
+ suspend fun processIntent (intent : VI ) = _intentFlow .emit(intent)
55
50
56
51
private val toPartialStateChanges: FlowTransformer <VI , PartialStateChange > =
57
52
FlowTransformer { intents ->
@@ -79,39 +74,18 @@ class MainVM @Inject constructor(
79
74
80
75
private val sendSingleEvent: FlowTransformer <PartialStateChange , PartialStateChange > =
81
76
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
+ }
106
80
}
107
81
108
82
init {
109
- _intentSF
83
+ _intentFlow
110
84
.pipe(intentFilterer)
111
85
.pipe(toPartialStateChanges)
112
86
.pipe(sendSingleEvent)
113
87
.scan(initialVS) { vs, change -> change.reduce(vs) }
114
- .onEach { _stateSF .value = it }
88
+ .onEach { _stateFlow .value = it }
115
89
.launchIn(viewModelScope)
116
90
}
117
91
0 commit comments