android - reload list if game folder changed, fix game updates scanning

This commit is contained in:
Emmanuel Hansen 2023-12-03 14:03:25 +00:00
parent d7b139a4a8
commit 5028673968
5 changed files with 35 additions and 41 deletions

View File

@ -72,6 +72,7 @@ class Helpers {
} }
return null return null
} }
fun copyToData( fun copyToData(
file: DocumentFile, path: String, storageHelper: SimpleStorageHelper, file: DocumentFile, path: String, storageHelper: SimpleStorageHelper,
isCopying: MutableState<Boolean>, isCopying: MutableState<Boolean>,
@ -79,17 +80,18 @@ class Helpers {
currentProgressName: MutableState<String>, currentProgressName: MutableState<String>,
finish: () -> Unit finish: () -> Unit
) { ) {
var fPath = path + "/${file.name}";
var callback: FileCallback? = object : FileCallback() { var callback: FileCallback? = object : FileCallback() {
override fun onFailed(errorCode: FileCallback.ErrorCode) { override fun onFailed(errorCode: FileCallback.ErrorCode) {
super.onFailed(errorCode) super.onFailed(errorCode)
File(path).delete() File(fPath).delete()
finish() finish()
} }
override fun onStart(file: Any, workerThread: Thread): Long { override fun onStart(file: Any, workerThread: Thread): Long {
copyProgress.value = 0f copyProgress.value = 0f
(file as DocumentFile)?.apply { (file as DocumentFile).apply {
currentProgressName.value = "Copying ${file.name}" currentProgressName.value = "Copying ${file.name}"
} }
return super.onStart(file, workerThread) return super.onStart(file, workerThread)
@ -98,7 +100,7 @@ class Helpers {
override fun onReport(report: Report) { override fun onReport(report: Report) {
super.onReport(report) super.onReport(report)
if(!isCopying.value) { if (!isCopying.value) {
Thread.currentThread().interrupt() Thread.currentThread().interrupt()
} }
@ -113,17 +115,16 @@ class Helpers {
} }
val ioScope = CoroutineScope(Dispatchers.IO) val ioScope = CoroutineScope(Dispatchers.IO)
isCopying.value = true isCopying.value = true
File(fPath).delete()
file.apply { file.apply {
if (!File(path + "/${file.name}").exists()) { val f = this
val f = this ioScope.launch {
ioScope.launch { f.copyFileTo(
f.copyFileTo( storageHelper.storage.context,
storageHelper.storage.context, File(path),
File(path), callback = callback!!
callback = callback!! )
)
}
} }
} }
} }

View File

@ -9,15 +9,17 @@ import com.anggrayudi.storage.file.DocumentFileType
import com.anggrayudi.storage.file.extension import com.anggrayudi.storage.file.extension
import com.anggrayudi.storage.file.search import com.anggrayudi.storage.file.search
import org.ryujinx.android.MainActivity import org.ryujinx.android.MainActivity
import java.util.Locale
import kotlin.concurrent.thread import kotlin.concurrent.thread
class HomeViewModel( class HomeViewModel(
val activity: MainActivity? = null, val activity: MainActivity? = null,
val mainViewModel: MainViewModel? = null val mainViewModel: MainViewModel? = null
) { ) {
private var shouldReload: Boolean = false
private var savedFolder: String = "" private var savedFolder: String = ""
private var isLoading: Boolean = false private var isLoading: Boolean = false
private var loadedCache: List<GameModel> = listOf() private var loadedCache: MutableList<GameModel> = mutableListOf()
private var gameFolderPath: DocumentFile? = null private var gameFolderPath: DocumentFile? = null
private var sharedPref: SharedPreferences? = null private var sharedPref: SharedPreferences? = null
val gameList: SnapshotStateList<GameModel> = SnapshotStateList() val gameList: SnapshotStateList<GameModel> = SnapshotStateList()
@ -25,31 +27,14 @@ class HomeViewModel(
init { init {
if (activity != null) { if (activity != null) {
sharedPref = PreferenceManager.getDefaultSharedPreferences(activity) sharedPref = PreferenceManager.getDefaultSharedPreferences(activity)
savedFolder = sharedPref?.getString("gameFolder", "") ?: ""
if (savedFolder.isNotEmpty()) {
try {
gameFolderPath = DocumentFileCompat.fromFullPath(
activity,
savedFolder,
documentType = DocumentFileType.FOLDER,
requiresWriteAccess = true
)
reloadGameList()
} catch (e: Exception) {
}
}
} }
} }
fun ensureReloadIfNecessary() { fun ensureReloadIfNecessary() {
val oldFolder = savedFolder val oldFolder = savedFolder
val savedFolder = sharedPref?.getString("gameFolder", "") ?: "" savedFolder = sharedPref?.getString("gameFolder", "") ?: ""
if(savedFolder.isNotEmpty() && savedFolder != oldFolder) { if (savedFolder.isNotEmpty() && (shouldReload || savedFolder != oldFolder)) {
gameFolderPath = DocumentFileCompat.fromFullPath( gameFolderPath = DocumentFileCompat.fromFullPath(
mainViewModel?.activity!!, mainViewModel?.activity!!,
savedFolder, savedFolder,
@ -61,6 +46,17 @@ class HomeViewModel(
} }
} }
fun filter(query : String){
gameList.clear()
gameList.addAll(loadedCache.filter { it.titleName != null && it.titleName!!.isNotEmpty() && (query.trim()
.isEmpty() || it.titleName!!.lowercase(Locale.getDefault())
.contains(query)) })
}
fun requestReload(){
shouldReload = true
}
fun reloadGameList() { fun reloadGameList() {
var storage = activity?.storageHelper ?: return var storage = activity?.storageHelper ?: return
@ -73,6 +69,7 @@ class HomeViewModel(
isLoading = true isLoading = true
thread { thread {
try { try {
loadedCache.clear()
val files = mutableListOf<GameModel>() val files = mutableListOf<GameModel>()
for (file in folder.search(false, DocumentFileType.FILE)) { for (file in folder.search(false, DocumentFileType.FILE)) {
if (file.extension == "xci" || file.extension == "nsp") if (file.extension == "xci" || file.extension == "nsp")
@ -80,22 +77,16 @@ class HomeViewModel(
val item = GameModel(file, it) val item = GameModel(file, it)
if(item.titleId?.isNotEmpty() == true && item.titleName?.isNotEmpty() == true) { if(item.titleId?.isNotEmpty() == true && item.titleName?.isNotEmpty() == true) {
files.add(item) loadedCache.add(item)
gameList.add(item) gameList.add(item)
} }
} }
} }
loadedCache = files.toList()
isLoading = false isLoading = false
} finally { } finally {
isLoading = false isLoading = false
} }
} }
} }
fun clearLoadedCache(){
loadedCache = listOf()
}
} }

View File

@ -137,8 +137,8 @@ class TitleUpdateViewModel(val titleId: String) {
val gson = Gson() val gson = Gson()
data = gson.fromJson(File(jsonPath).readText(), TitleUpdateMetadata::class.java) data = gson.fromJson(File(jsonPath).readText(), TitleUpdateMetadata::class.java)
refreshPaths()
} }
refreshPaths()
storageHelper = MainActivity.StorageHelper!! storageHelper = MainActivity.StorageHelper!!
} }

View File

@ -81,6 +81,7 @@ class HomeViews {
viewModel: HomeViewModel = HomeViewModel(), viewModel: HomeViewModel = HomeViewModel(),
navController: NavHostController? = null navController: NavHostController? = null
) { ) {
viewModel.ensureReloadIfNecessary()
val showAppActions = remember { mutableStateOf(false) } val showAppActions = remember { mutableStateOf(false) }
val showLoading = remember { mutableStateOf(false) } val showLoading = remember { mutableStateOf(false) }
val openTitleUpdateDialog = remember { mutableStateOf(false) } val openTitleUpdateDialog = remember { mutableStateOf(false) }
@ -169,6 +170,7 @@ class HomeViews {
val list = remember { val list = remember {
viewModel.gameList viewModel.gameList
} }
viewModel.filter(query.value)
var settings = QuickSettings(viewModel.activity!!) var settings = QuickSettings(viewModel.activity!!)
if (settings.isGrid) { if (settings.isGrid) {

View File

@ -371,7 +371,7 @@ class SettingViews {
showImportCompletion.value = false showImportCompletion.value = false
importFile.value = null importFile.value = null
mainViewModel.userViewModel.refreshUsers() mainViewModel.userViewModel.refreshUsers()
mainViewModel.homeViewModel.clearLoadedCache() mainViewModel.homeViewModel.requestReload()
}) { }) {
Card( Card(
modifier = Modifier, modifier = Modifier,