diff --git a/app/src/main/java/me/magnum/melonds/di/MigrationModule.kt b/app/src/main/java/me/magnum/melonds/di/MigrationModule.kt index e412b9d8..8a88bf1f 100644 --- a/app/src/main/java/me/magnum/melonds/di/MigrationModule.kt +++ b/app/src/main/java/me/magnum/melonds/di/MigrationModule.kt @@ -14,13 +14,17 @@ import me.magnum.melonds.domain.repositories.RomsRepository import me.magnum.melonds.domain.repositories.SettingsRepository import me.magnum.melonds.impl.RomIconProvider import me.magnum.melonds.migrations.* -import me.magnum.melonds.migrations.helper.LayoutMigrationHelper -import me.magnum.melonds.migrations.helper.RomMigrationHelper +import me.magnum.melonds.migrations.helper.GenericJsonArrayMigrationHelper @Module @InstallIn(SingletonComponent::class) object MigrationModule { + @Provides + fun provideJsonArrayMigrationHelper(@ApplicationContext context: Context, gson: Gson): GenericJsonArrayMigrationHelper { + return GenericJsonArrayMigrationHelper(context, gson) + } + @Provides fun provideMigration( @ApplicationContext context: Context, @@ -30,7 +34,8 @@ object MigrationModule { settingsRepository: SettingsRepository, directoryAccessValidator: DirectoryAccessValidator, uriHandler: UriHandler, - gson: Gson + gson: Gson, + genericJsonArrayMigrationHelper: GenericJsonArrayMigrationHelper, ): Migrator { return Migrator(context, sharedPreferences).apply { @@ -40,8 +45,8 @@ object MigrationModule { registerMigration(Migration16to17(romsRepository)) registerMigration(Migration20to21(settingsRepository, romsRepository, directoryAccessValidator)) registerMigration(Migration21to22(context, gson, uriHandler)) - registerMigration(Migration24to25(RomMigrationHelper(context, gson), context)) - registerMigration(Migration25to26(LayoutMigrationHelper(context, gson))) + registerMigration(Migration24to25(genericJsonArrayMigrationHelper, context)) + registerMigration(Migration25to26(genericJsonArrayMigrationHelper)) } } } \ No newline at end of file diff --git a/app/src/main/java/me/magnum/melonds/migrations/Migration24to25.kt b/app/src/main/java/me/magnum/melonds/migrations/Migration24to25.kt index 0c0a15c9..ebdf30c8 100644 --- a/app/src/main/java/me/magnum/melonds/migrations/Migration24to25.kt +++ b/app/src/main/java/me/magnum/melonds/migrations/Migration24to25.kt @@ -1,22 +1,26 @@ package me.magnum.melonds.migrations import android.content.Context -import me.magnum.melonds.migrations.helper.RomMigrationHelper +import me.magnum.melonds.migrations.helper.GenericJsonArrayMigrationHelper import me.magnum.melonds.migrations.legacy.Rom22 -import me.magnum.melonds.migrations.legacy.RomDto25 import me.magnum.melonds.migrations.legacy.RomConfigDto25 +import me.magnum.melonds.migrations.legacy.RomDto25 import me.magnum.melonds.utils.RomProcessor class Migration24to25( - private val romMigrationHelper: RomMigrationHelper, + private val romMigrationHelper: GenericJsonArrayMigrationHelper, private val context: Context, ) : Migration { + companion object { + private const val ROM_DATA_FILE = "rom_data.json" + } + override val from = 24 override val to = 25 override fun migrate() { - romMigrationHelper.migrateRoms(Rom22::class.java) { rom -> + romMigrationHelper.migrateJsonArrayData(ROM_DATA_FILE) { rom -> runCatching { context.contentResolver.openInputStream(rom.uri)?.use { RomProcessor.getRomMetadata(it.buffered()) diff --git a/app/src/main/java/me/magnum/melonds/migrations/Migration25to26.kt b/app/src/main/java/me/magnum/melonds/migrations/Migration25to26.kt index d35a9639..7c1b8605 100644 --- a/app/src/main/java/me/magnum/melonds/migrations/Migration25to26.kt +++ b/app/src/main/java/me/magnum/melonds/migrations/Migration25to26.kt @@ -4,19 +4,23 @@ import me.magnum.melonds.impl.dtos.layout.LayoutConfigurationDto import me.magnum.melonds.impl.dtos.layout.PositionedLayoutComponentDto import me.magnum.melonds.impl.dtos.layout.RectDto import me.magnum.melonds.impl.dtos.layout.UILayoutDto -import me.magnum.melonds.migrations.helper.LayoutMigrationHelper +import me.magnum.melonds.migrations.helper.GenericJsonArrayMigrationHelper import me.magnum.melonds.migrations.legacy.layout.LayoutConfiguration25 import me.magnum.melonds.migrations.legacy.layout.UILayout25 class Migration25to26( - private val layoutMigrationHelper: LayoutMigrationHelper, + private val layoutMigrationHelper: GenericJsonArrayMigrationHelper, ) : Migration { + companion object { + private const val LAYOUTS_DATA_FILE = "layouts.json" + } + override val from = 25 override val to = 26 override fun migrate() { - layoutMigrationHelper.migrateLayouts(LayoutConfiguration25::class.java) { + layoutMigrationHelper.migrateJsonArrayData(LAYOUTS_DATA_FILE) { try { LayoutConfigurationDto( it.id, diff --git a/app/src/main/java/me/magnum/melonds/migrations/helper/GenericJsonArrayMigrationHelper.kt b/app/src/main/java/me/magnum/melonds/migrations/helper/GenericJsonArrayMigrationHelper.kt new file mode 100644 index 00000000..11dd673d --- /dev/null +++ b/app/src/main/java/me/magnum/melonds/migrations/helper/GenericJsonArrayMigrationHelper.kt @@ -0,0 +1,48 @@ +package me.magnum.melonds.migrations.helper + +import android.content.Context +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken +import java.io.File +import java.io.FileReader +import java.io.OutputStreamWriter + +class GenericJsonArrayMigrationHelper( + private val context: Context, + private val gson: Gson, +) { + + inline fun migrateJsonArrayData(jsonFile: String, noinline dataMapper: (T) -> U?) { + migrateJsonArrayData(jsonFile, T::class.java, dataMapper) + } + + fun migrateJsonArrayData(jsonFile: String, fromClass: Class, dataMapper: (T) -> U?) { + val originalData = getOriginalData(jsonFile, fromClass) + val newData = originalData.mapNotNull { data -> + dataMapper(data) + } + + saveNewData(jsonFile, newData) + } + + private fun getOriginalData(jsonFile: String, fromClass: Class): List { + val dataFile = File(context.filesDir, jsonFile) + if (!dataFile.isFile) { + return emptyList() + } + + val dataListType = TypeToken.getParameterized(List::class.java, fromClass).type + return runCatching { + gson.fromJson>(FileReader(dataFile), dataListType) + }.getOrElse { emptyList() } + } + + private fun saveNewData(jsonFile: String, data: List) { + val dataFile = File(context.filesDir, jsonFile) + + OutputStreamWriter(dataFile.outputStream()).use { + val json = gson.toJson(data) + it.write(json) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/me/magnum/melonds/migrations/helper/LayoutMigrationHelper.kt b/app/src/main/java/me/magnum/melonds/migrations/helper/LayoutMigrationHelper.kt deleted file mode 100644 index ff532fc5..00000000 --- a/app/src/main/java/me/magnum/melonds/migrations/helper/LayoutMigrationHelper.kt +++ /dev/null @@ -1,48 +0,0 @@ -package me.magnum.melonds.migrations.helper - -import android.content.Context -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken -import java.io.File -import java.io.FileReader -import java.io.OutputStreamWriter - -class LayoutMigrationHelper( - private val context: Context, - private val gson: Gson, -) { - - companion object { - private const val LAYOUTS_DATA_FILE = "layouts.json" - } - - fun migrateLayouts(fromClass: Class, layoutMapper: (T) -> U?) { - val originalLayouts = getOriginalLayouts(fromClass) - val newLayouts = originalLayouts.mapNotNull { layout -> - layoutMapper(layout) - } - - saveNewLayouts(newLayouts) - } - - private fun getOriginalLayouts(fromClass: Class): List { - val cacheFile = File(context.filesDir, LAYOUTS_DATA_FILE) - if (!cacheFile.isFile) { - return emptyList() - } - - val layoutListType = TypeToken.getParameterized(List::class.java, fromClass).type - return runCatching { - gson.fromJson>(FileReader(cacheFile), layoutListType) - }.getOrElse { emptyList() } - } - - private fun saveNewLayouts(roms: List) { - val cacheFile = File(context.filesDir, LAYOUTS_DATA_FILE) - - OutputStreamWriter(cacheFile.outputStream()).use { - val romsJson = gson.toJson(roms) - it.write(romsJson) - } - } -} \ No newline at end of file diff --git a/app/src/main/java/me/magnum/melonds/migrations/helper/RomMigrationHelper.kt b/app/src/main/java/me/magnum/melonds/migrations/helper/RomMigrationHelper.kt deleted file mode 100644 index ad4a1767..00000000 --- a/app/src/main/java/me/magnum/melonds/migrations/helper/RomMigrationHelper.kt +++ /dev/null @@ -1,48 +0,0 @@ -package me.magnum.melonds.migrations.helper - -import android.content.Context -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken -import java.io.File -import java.io.FileReader -import java.io.OutputStreamWriter - -class RomMigrationHelper( - private val context: Context, - private val gson: Gson, -) { - - companion object { - private const val ROM_DATA_FILE = "rom_data.json" - } - - fun migrateRoms(fromClass: Class, romMapper: (T) -> U?) { - val originalRoms = getOriginalRoms(fromClass) - val newRoms = originalRoms.mapNotNull { rom -> - romMapper(rom) - } - - saveNewRoms(newRoms) - } - - private fun getOriginalRoms(fromClass: Class): List { - val cacheFile = File(context.filesDir, ROM_DATA_FILE) - if (!cacheFile.isFile) { - return emptyList() - } - - val romListType = TypeToken.getParameterized(List::class.java, fromClass).type - return runCatching { - gson.fromJson>(FileReader(cacheFile), romListType) - }.getOrElse { emptyList() } - } - - private fun saveNewRoms(roms: List) { - val cacheFile = File(context.filesDir, ROM_DATA_FILE) - - OutputStreamWriter(cacheFile.outputStream()).use { - val romsJson = gson.toJson(roms) - it.write(romsJson) - } - } -} \ No newline at end of file