Skip to content

Commit

Permalink
Automatically import key files from search location
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbeth authored and PixelyIon committed Mar 25, 2022
1 parent 6cf2ef8 commit 882b939
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/emu/skyline/KeyReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/emu/skyline/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/emu/skyline/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 882b939

Please sign in to comment.