forked from technophilist/Musify
-
Notifications
You must be signed in to change notification settings - Fork 3
/
StateProduction.kt
174 lines (159 loc) · 6.29 KB
/
StateProduction.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.example.musify.ui.screens.podcastshowdetailscreen
import com.example.musify.data.repositories.podcastsrepository.PodcastQuery
import com.example.musify.data.repositories.podcastsrepository.PodcastsRepository
import com.example.musify.data.tiling.Page
import com.example.musify.data.tiling.PagedItem
import com.example.musify.data.tiling.toNetworkBackedTiledList
import com.example.musify.data.tiling.withPlaceholders
import com.example.musify.data.utils.FetchedResource
import com.example.musify.domain.PodcastEpisode
import com.example.musify.domain.PodcastShow
import com.example.musify.musicplayer.MusicPlaybackMonitor
import com.example.musify.musicplayer.currentlyPlayingEpisodePlaybackStateStream
import com.tunjid.mutator.Mutation
import com.tunjid.mutator.coroutines.SuspendingStateHolder
import com.tunjid.mutator.coroutines.actionStateFlowMutator
import com.tunjid.mutator.coroutines.mapToMutation
import com.tunjid.mutator.coroutines.toMutationStream
import com.tunjid.tiler.TiledList
import com.tunjid.tiler.distinctBy
import com.tunjid.tiler.emptyTiledList
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
sealed class PodcastShowDetailAction {
object Retry : PodcastShowDetailAction()
data class LoadAround(val podcastQuery: PodcastQuery?) : PodcastShowDetailAction()
}
data class PodcastShowDetailUiState(
val currentQuery: PodcastQuery,
val podcastShow: PodcastShow? = null,
val currentlyPlayingEpisode: PodcastEpisode? = null,
val isCurrentlyPlayingEpisodePaused: Boolean? = null,
val loadingState: LoadingState = LoadingState.LOADING,
val items: TiledList<PodcastQuery, ShowItem> = emptyTiledList()
) {
enum class LoadingState { IDLE, LOADING, PLAYBACK_LOADING, ERROR }
}
sealed interface ShowItem : PagedItem {
data class Loaded(
override val pagedIndex: Int,
val episode: PodcastEpisode
) : ShowItem
data class Placeholder(
override val pagedIndex: Int,
) : ShowItem
}
private val ShowItem.internalKey
get() = when (this) {
is ShowItem.Loaded -> episode.id
is ShowItem.Placeholder -> pagedIndex
}
fun CoroutineScope.podcastShowDetailStateProducer(
showId: String,
countryCode: String,
podcastsRepository: PodcastsRepository,
musicPlaybackMonitor: MusicPlaybackMonitor,
) = actionStateFlowMutator<PodcastShowDetailAction, PodcastShowDetailUiState>(
initialState = PodcastShowDetailUiState(
currentQuery = PodcastQuery(
showId = showId,
countryCode = countryCode,
page = Page(offset = 0)
)
),
inputs = listOf(
musicPlaybackMonitor.playbackStateMutations(),
podcastsRepository.fetchShowMutations(
showId = showId,
countryCode = countryCode
)
),
actionTransform = { actions ->
actions.toMutationStream {
when (val action = type()) {
is PodcastShowDetailAction.LoadAround -> action.flow.episodeLoadMutations(
podcastsRepository = podcastsRepository
)
is PodcastShowDetailAction.Retry -> action.flow.retryMutations(
podcastsRepository = podcastsRepository,
showId = showId,
countryCode = countryCode
)
}
}
}
)
private fun MusicPlaybackMonitor.playbackStateMutations(): Flow<Mutation<PodcastShowDetailUiState>> =
currentlyPlayingEpisodePlaybackStateStream
.mapToMutation {
when (it) {
is MusicPlaybackMonitor.PlaybackState.Ended -> copy(
isCurrentlyPlayingEpisodePaused = null,
currentlyPlayingEpisode = null
)
is MusicPlaybackMonitor.PlaybackState.Loading -> copy(
loadingState = PodcastShowDetailUiState.LoadingState.PLAYBACK_LOADING
)
is MusicPlaybackMonitor.PlaybackState.Paused -> copy(
currentlyPlayingEpisode = it.pausedEpisode,
isCurrentlyPlayingEpisodePaused = true
)
is MusicPlaybackMonitor.PlaybackState.Playing -> copy(
loadingState = PodcastShowDetailUiState.LoadingState.IDLE,
isCurrentlyPlayingEpisodePaused = when (isCurrentlyPlayingEpisodePaused) {
null, true -> false
else -> isCurrentlyPlayingEpisodePaused
},
currentlyPlayingEpisode = it.playingEpisode
)
}
}
private fun Flow<PodcastShowDetailAction.Retry>.retryMutations(
podcastsRepository: PodcastsRepository,
showId: String,
countryCode: String
): Flow<Mutation<PodcastShowDetailUiState>> =
flatMapLatest {
podcastsRepository.fetchShowMutations(
showId = showId,
countryCode = countryCode
)
}
context(SuspendingStateHolder<PodcastShowDetailUiState>)
private suspend fun Flow<PodcastShowDetailAction.LoadAround>.episodeLoadMutations(
podcastsRepository: PodcastsRepository
): Flow<Mutation<PodcastShowDetailUiState>> {
val initialState = state()
return map { it.podcastQuery ?: state().currentQuery }
.toNetworkBackedTiledList(
startQuery = initialState.currentQuery,
fetcher = podcastsRepository::podcastsFor.withPlaceholders(
cachedItems = initialState.items,
placeholderMapper = ShowItem::Placeholder,
loadedMapper = ShowItem::Loaded
)
)
.mapToMutation { newItems ->
copy(items = newItems.distinctBy(ShowItem::internalKey))
}
}
private fun PodcastsRepository.fetchShowMutations(
showId: String,
countryCode: String
) = flow<Mutation<PodcastShowDetailUiState>> {
val result = fetchPodcastShow(
showId = showId,
countryCode = countryCode,
)
if (result is FetchedResource.Success) emit {
copy(
loadingState = PodcastShowDetailUiState.LoadingState.IDLE,
podcastShow = result.data
)
} else emit {
copy(loadingState = PodcastShowDetailUiState.LoadingState.ERROR)
}
}