Mutable List Updates

This commit is contained in:
shabinder 2021-02-23 17:22:35 +05:30
parent f7e71da827
commit bc6f91c7ef
3 changed files with 19 additions and 16 deletions

View File

@ -63,7 +63,7 @@ interface SpotiFlyerList {
"Loading","", emptyList(),
Source.Spotify),
val link:String = "",
val trackList:SnapshotStateList<TrackDetails> = mutableStateListOf()
val trackList:List<TrackDetails> = emptyList()
)
}

View File

@ -37,7 +37,7 @@ fun SpotiFlyerListContent(
val coroutineScope = rememberCoroutineScope()
Box(modifier = modifier.fillMaxSize()) {
//TODO Null Handling
//TODO Better Null Handling
val result = model.queryResult!!
LazyColumn(

View File

@ -18,6 +18,7 @@ import com.shabinder.common.ui.showPopUpMessage
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collectLatest
import org.koin.ext.scope
internal class SpotiFlyerListStoreProvider(
private val dir: Dir,
@ -37,8 +38,8 @@ internal class SpotiFlyerListStoreProvider(
) {}
private sealed class Result {
data class ResultFetched(val result: PlatformQueryResult,val trackList: SnapshotStateList<TrackDetails>) : Result()
data class UpdateTrackList(val list:SnapshotStateList<TrackDetails>): Result()
data class ResultFetched(val result: PlatformQueryResult,val trackList: List<TrackDetails>) : Result()
data class UpdateTrackList(val list:List<TrackDetails>): Result()
data class UpdateTrackItem(val item:TrackDetails): Result()
}
@ -46,7 +47,6 @@ internal class SpotiFlyerListStoreProvider(
override suspend fun executeAction(action: Unit, getState: () -> State) {
executeIntent(Intent.SearchLink(link),getState)
executeIntent(Intent.RefreshTracksStatuses,getState)
downloadProgressFlow.collectLatest { map ->
logger.d(map.size.toString(),"ListStore: flow Updated")
@ -59,7 +59,8 @@ internal class SpotiFlyerListStoreProvider(
when (intent) {
is Intent.SearchLink -> fetchQuery.query(link)?.let{ result ->
result.trackList = result.trackList.toMutableList()
dispatch((Result.ResultFetched(result,result.trackList.toMutableList().updateTracksStatuses(downloadProgressFlow.replayCache.getOrElse(0){ hashMapOf()}))))
dispatch((Result.ResultFetched(result,result.trackList.updateTracksStatuses(downloadProgressFlow.replayCache.getOrElse(0){ hashMapOf()}))))
executeIntent(Intent.RefreshTracksStatuses,getState)
}
is Intent.StartDownloadAll -> {
@ -74,7 +75,7 @@ internal class SpotiFlyerListStoreProvider(
}
it
}
dispatch(Result.UpdateTrackList(list.toMutableList().updateTracksStatuses(downloadProgressFlow.replayCache.getOrElse(0){ hashMapOf()})))
dispatch(Result.UpdateTrackList(list.updateTracksStatuses(downloadProgressFlow.replayCache.getOrElse(0){ hashMapOf()})))
}
is Intent.StartDownload -> {
downloadTracks(listOf(intent.track),fetchQuery.youtubeMusic::getYTIDBestMatch,dir::saveFileWithMetadata)
@ -95,23 +96,25 @@ internal class SpotiFlyerListStoreProvider(
private fun State.updateTrackItem(item: TrackDetails):State{
val position = this.trackList.map { it.title }.indexOf(item.title)
if(position != -1){
return copy(trackList = trackList.apply { set(position,item) })
return copy(trackList = trackList.toMutableList().apply { set(position,item) })
}
return this
}
}
private fun MutableList<TrackDetails>.updateTracksStatuses(map:HashMap<String,DownloadStatus>):SnapshotStateList<TrackDetails>{
private fun List<TrackDetails>.updateTracksStatuses(map:HashMap<String,DownloadStatus>):List<TrackDetails>{
val titleList = this.map { it.title }
val newStateList = mutableStateListOf<TrackDetails>()
val updatedList = mutableListOf<TrackDetails>().also { it.addAll(this) }
for(newTrack in map){
titleList.indexOf(newTrack.key).let { position ->
this.getOrNull(position)?.apply { downloaded = newTrack.value }?.also { updatedTrack ->
this[position] = updatedTrack
logger.d(updatedTrack.toString(),"List Store Track Update")
if(position != -1){
updatedList.getOrNull(position)?.apply { downloaded = newTrack.value }?.also { updatedTrack ->
updatedList[position] = updatedTrack
logger.d("$position) ${updatedTrack.downloaded} - ${updatedTrack.title}","List Store Track Update")
}
}
}
newStateList.addAll(this)
return newStateList
}
return updatedList
}
}