From 882b9393356572611c0951c49f85e07bce520a6c Mon Sep 17 00:00:00 2001 From: lynxnb Date: Fri, 25 Mar 2022 00:07:31 +0100 Subject: [PATCH] Automatically import key files from search location --- app/src/main/java/emu/skyline/KeyReader.kt | 14 ++++++++++++++ app/src/main/java/emu/skyline/MainActivity.kt | 2 +- app/src/main/java/emu/skyline/MainViewModel.kt | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/emu/skyline/KeyReader.kt b/app/src/main/java/emu/skyline/KeyReader.kt index 0c9d9070..8c93375b 100644 --- a/app/src/main/java/emu/skyline/KeyReader.kt +++ b/app/src/main/java/emu/skyline/KeyReader.kt @@ -19,6 +19,20 @@ object KeyReader { companion object { fun parse(keyName : String) = values().first { it.keyName == keyName } + fun parse(documentFile : DocumentFile) = values().first { it.fileName == documentFile.name } + fun parseOrNull(documentFile : DocumentFile) = values().find { it.fileName == documentFile.name } + } + } + + fun importFromLocation(context : Context, searchLocation : Uri) = importFromDirectory(context, DocumentFile.fromTreeUri(context, searchLocation)!!) + + private fun importFromDirectory(context : Context, directory : DocumentFile) { + directory.listFiles().forEach { file -> + if (file.isDirectory) { + importFromDirectory(context, file) + } else { + KeyType.parseOrNull(file)?.let { import(context, file.uri, it) } + } } } diff --git a/app/src/main/java/emu/skyline/MainActivity.kt b/app/src/main/java/emu/skyline/MainActivity.kt index 410e312f..6652262f 100644 --- a/app/src/main/java/emu/skyline/MainActivity.kt +++ b/app/src/main/java/emu/skyline/MainActivity.kt @@ -292,7 +292,7 @@ class MainActivity : AppCompatActivity() { } private fun loadRoms(loadFromFile : Boolean) { - viewModel.loadRoms(loadFromFile, Uri.parse(settings.searchLocation), settings.systemLanguage) + viewModel.loadRoms(this, loadFromFile, Uri.parse(settings.searchLocation), settings.systemLanguage) settings.refreshRequired = false } diff --git a/app/src/main/java/emu/skyline/MainViewModel.kt b/app/src/main/java/emu/skyline/MainViewModel.kt index 691c0598..cef46ca1 100644 --- a/app/src/main/java/emu/skyline/MainViewModel.kt +++ b/app/src/main/java/emu/skyline/MainViewModel.kt @@ -41,7 +41,7 @@ class MainViewModel @Inject constructor(@ApplicationContext context : Context, p * * @param loadFromFile If this is false then trying to load cached adapter data is skipped entirely */ - fun loadRoms(loadFromFile : Boolean, searchLocation : Uri, systemLanguage : Int) { + fun loadRoms(context : Context, loadFromFile : Boolean, searchLocation : Uri, systemLanguage : Int) { if (state == MainState.Loading) return state = MainState.Loading @@ -62,6 +62,7 @@ class MainViewModel @Inject constructor(@ApplicationContext context : Context, p MainState.Loaded(HashMap()) } else { try { + KeyReader.importFromLocation(context, searchLocation) val romElements = romProvider.loadRoms(searchLocation, systemLanguage) romElements.toFile(romsFile) MainState.Loaded(romElements)