Skip to content

Commit

Permalink
feat: move to dynamic file list for better pack refresh (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
octera authored Aug 12, 2024
1 parent c26c53e commit 71d0579
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import android.app.DownloadManager
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.provider.OpenableColumns
import android.util.Log
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.net.toFile
import dagger.hilt.android.qualifiers.ApplicationContext
import info.octera.droidstorybox.data.file.FileSource
import info.octera.droidstorybox.data.remote.BasicHttpSource
import info.octera.droidstorybox.domain.model.PlayerState
import info.octera.droidstorybox.domain.model.ProgressState
import info.octera.droidstorybox.domain.model.pack.PackMetadata
import info.octera.droidstorybox.domain.repository.PacksRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
Expand All @@ -31,11 +35,16 @@ class PacksRepositoryImpl @Inject constructor(
private val basicHttpSource: BasicHttpSource,
private val fileSource: FileSource
) : PacksRepository {
private val packFileList = MutableStateFlow(emptyList<File>())
private val location = "packs"
private val handler = Handler(Looper.getMainLooper())

override fun getPacksFile(): List<File> {
return fileSource
.listFiles(location)
init {
checkPackFiles()
}

override fun getPacksFile(): Flow<List<File>> {
return packFileList
}

override suspend fun addPack(uri: Uri): Flow<ProgressState> {
Expand All @@ -52,7 +61,8 @@ class PacksRepositoryImpl @Inject constructor(
}

override suspend fun deletePack(packMetadata: PackMetadata) {
packMetadata.uri.toFile().delete()
val res = packMetadata.uri.toFile().delete()
Log.e("DELETE", "deleted? $res")
}

override fun downloadPackFile(downloadUrl: String, fileName: String): Flow<ProgressState> {
Expand Down Expand Up @@ -150,5 +160,10 @@ class PacksRepositoryImpl @Inject constructor(
throw IllegalStateException("Unknown filename")
}

private fun checkPackFiles() : Boolean = handler.postDelayed({
packFileList.value = fileSource.listFiles(location)
checkPackFiles()
}, 1000L)


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import android.net.Uri
import info.octera.droidstorybox.domain.model.ProgressState
import info.octera.droidstorybox.domain.model.pack.PackMetadata
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import java.io.File

interface PacksRepository {
fun getPacksFile(): List<File>
fun getPacksFile(): Flow<List<File>>
suspend fun addPack(uri: Uri): Flow<ProgressState>
suspend fun deletePack(packMetadata: PackMetadata)
fun downloadPackFile(downloadUrl: String, fileName: String): Flow<ProgressState>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ package info.octera.droidstorybox.domain.usecases.packs
import info.octera.droidstorybox.domain.model.pack.PackMetadata
import info.octera.droidstorybox.domain.repository.PackRepository
import info.octera.droidstorybox.domain.repository.PacksRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class GetPacks @Inject constructor(
private val packsRepository: PacksRepository,
private val packRepository: PackRepository
) {
operator fun invoke(): List<PackMetadata> {
operator fun invoke(): Flow<List<PackMetadata>> {
return packsRepository.getPacksFile()
.map {
packRepository.readPackMetaData(it)
.map { files ->
files.mapNotNull {
try {
packRepository.readPackMetaData(it)
} catch (e: Exception) {
null
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import info.octera.droidstorybox.domain.model.pack.PackMetadata
import info.octera.droidstorybox.domain.usecases.pack.PackUseCases
import info.octera.droidstorybox.domain.usecases.packs.PacksUseCases
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -22,7 +24,9 @@ class HomeViewModel @Inject constructor(
}

private fun getPacks() {
state.value = state.value.copy(packs = packsUseCases.getPacks())
packsUseCases.getPacks().onEach {
state.value = state.value.copy(packs = it)
}.launchIn(viewModelScope)
}

fun setPackFocused(packMetadata: PackMetadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package info.octera.droidstorybox.presentation.local_packs

import android.content.Context
import android.net.Uri
import android.util.Log
import android.widget.Toast
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
Expand All @@ -11,6 +12,8 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import info.octera.droidstorybox.domain.model.ProgressState
import info.octera.droidstorybox.domain.model.pack.PackMetadata
import info.octera.droidstorybox.domain.usecases.packs.PacksUseCases
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -26,7 +29,9 @@ class LocalPacksViewModel @Inject constructor(
}

private fun getPacks() {
state.value = state.value.copy(packs = packsUseCases.getPacks())
packsUseCases.getPacks().onEach {
state.value = state.value.copy(packs = it)
}.launchIn(viewModelScope)
}

fun addPack(uri: Uri) {
Expand All @@ -50,7 +55,6 @@ class LocalPacksViewModel @Inject constructor(
fun deletePack(packMetadata: PackMetadata) {
viewModelScope.launch {
packsUseCases.deletePack(packMetadata)
getPacks()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class ReadPackViewModel @Inject constructor(
state.value = state.value.copy(pack = pack, currendStages = listOfNotNull(firstStage))
val flow = packUseCases.getPlayerState()
flow.onEach {
Log.e("FLOW", ""+it)
state.value = state.value.copy(playerInfo = PlayerInfo(
playing = it.playbackState == PlaybackState.PLAYING,
duration = it.duration,
Expand Down

0 comments on commit 71d0579

Please sign in to comment.