Repository: skyline-emu/skyline
File: app/src/main/java/emu/skyline/MainViewModel.kt (Kotlin)

Code before:
                }
            }

            state = if (searchLocation.toString().isEmpty()) {
                @Suppress("ReplaceWithEnumMap")
                MainState.Loaded(HashMap())
            } else {
                try {
                    KeyReader.importFromLocation(context, searchLocation)
                    val romElements = romProvider.loadRoms(searchLocation, systemLanguage)
                    romElements.toFile(romsFile)
                    MainState.Loaded(romElements)
                } catch (e : Exception) {
                    Log.w(TAG, "Ran into exception while saving: ${e.message}")
                    MainState.Error(e)
                }
            }
        }
    }

    /**
     * This checks if the roms have changed since the last time they were loaded and if so it reloads them
     */
    fun checkRomHash(context : Context, searchLocation : Uri, systemLanguage : Int) {
        CoroutineScope(Dispatchers.IO).launch {
            val oldHashFile = File(getApplication<SkylineApplication>().filesDir.canonicalPath + "/roms.txt")
            val oldHash = if (oldHashFile.exists()) oldHashFile.readText().toIntOrNull() else null
            val romElements = romProvider.loadRoms(searchLocation, systemLanguage).values.flatten().toList()
            val newHash = romElements.map { it.name }.toSet().hashCode()
            if (newHash != oldHash) {
                loadRoms(context, false, searchLocation, systemLanguage)
                oldHashFile.writeText(newHash.toString())
            }
        }
    }
}


Review comment (suggestion):
You should check that state is `MainState.Loaded` here to avoid running the check if it's still loading or there was an error. Do a check for `state != MainState.Loaded` and return early in that case.

Code after:
                }
            }

            state = if (searchLocation.toString().isEmpty()) {
                @Suppress("ReplaceWithEnumMap")
                MainState.Loaded(HashMap())
            } else {
                try {
                    KeyReader.importFromLocation(context, searchLocation)
                    val romElements = romProvider.loadRoms(searchLocation, systemLanguage)
                    romElements.toFile(romsFile)
                    MainState.Loaded(romElements)
                } catch (e : Exception) {
                    Log.w(TAG, "Ran into exception while saving: ${e.message}")
                    MainState.Error(e)
                }
            }
        }
    }

    /**
     * This checks if the roms have changed since the last time they were loaded and if so it reloads them
     */
    fun checkRomHash(searchLocation : Uri, systemLanguage : Int) {
        if(state !is MainState.Loaded) return
        CoroutineScope(Dispatchers.IO).launch {
            val currentHash = (state as MainState.Loaded).items.hashCode()
            val romElements = romProvider.loadRoms(searchLocation, systemLanguage)
            val newHash = romElements.hashCode()
            if (newHash != currentHash) {
                state = MainState.Loaded(romElements)
            }
        }
    }
}