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(), "Loading","", emptyList(),
Source.Spotify), Source.Spotify),
val link:String = "", val link:String = "",
val trackList:SnapshotStateList<TrackDetails> = mutableStateListOf() val trackList:List<TrackDetails> = emptyList()
) )
} }

View File

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

View File

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