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):
To calculate the hash of the current roms, you can just do `state.items.hashCode()`. This only works if `state` is of type `MainState.Loaded`, hence the above check. That also avoid having to save the hash to file. Also, rename `oldHash` to `currentHash` as it makes the most sense.

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)
            }
        }
    }
}