Eric Ampire [MOD]
10/30/2020, 12:56 PMLiveData and StateFlow.
class DownloadingModel {
private val _state = MutableStateFlow<DownloadStatus>(DownloadStatus.NOT_REQUESTED)
val state: StateFlow<DownloadStatus> get() = _state
suspend fun download() {
_state.value = DownloadStatus.INITIALIZED
initializeConnection()
processAvailableContent {
partialData: ByteArray,
downloadedBytes: Long,
totalBytes: Long
->
storePartialData(partialData)
_state = DownloadProgress(downloadedBytes.toDouble() / totalBytes)
}
_state.value = DownloadStatus.SUCCESS
}
}louiscad
10/30/2020, 12:58 PMLiveData is Android only and doesn't come with Flow operators.Adam Powell
10/30/2020, 1:27 PMEric Ampire [MOD]
10/30/2020, 2:41 PMAdam Powell
10/30/2020, 3:48 PMchannel.receiveAsFlow()Adam Powell
10/30/2020, 3:49 PMAdam Powell
10/30/2020, 3:49 PMLiveChannel or similar 🙂Eric Ampire [MOD]
10/30/2020, 4:24 PM