Automatically import key files from search location

This commit is contained in:
lynxnb 2022-03-25 00:07:31 +01:00 committed by ◱ Mark
parent 6cf2ef8fb9
commit 882b939335
3 changed files with 17 additions and 2 deletions

View File

@ -19,6 +19,20 @@ object KeyReader {
companion object { companion object {
fun parse(keyName : String) = values().first { it.keyName == keyName } 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) }
}
} }
} }

View File

@ -292,7 +292,7 @@ class MainActivity : AppCompatActivity() {
} }
private fun loadRoms(loadFromFile : Boolean) { 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 settings.refreshRequired = false
} }

View File

@ -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 * @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 if (state == MainState.Loading) return
state = MainState.Loading state = MainState.Loading
@ -62,6 +62,7 @@ class MainViewModel @Inject constructor(@ApplicationContext context : Context, p
MainState.Loaded(HashMap()) MainState.Loaded(HashMap())
} else { } else {
try { try {
KeyReader.importFromLocation(context, searchLocation)
val romElements = romProvider.loadRoms(searchLocation, systemLanguage) val romElements = romProvider.loadRoms(searchLocation, systemLanguage)
romElements.toFile(romsFile) romElements.toFile(romsFile)
MainState.Loaded(romElements) MainState.Loaded(romElements)