@@ -5,11 +5,6 @@ import androidx.lifecycle.viewModelScope
55import com.hoc081098.paginationmviflow.FlowTransformer
66import com.hoc081098.paginationmviflow.pipe
77import 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
138import dagger.hilt.android.lifecycle.HiltViewModel
149import kotlinx.coroutines.ExperimentalCoroutinesApi
1510import 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