Skip to content

Commit

Permalink
Create generic JSON migration helper
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelvcaetano committed Aug 13, 2023
1 parent d440aa9 commit 41d342c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 108 deletions.
15 changes: 10 additions & 5 deletions app/src/main/java/me/magnum/melonds/di/MigrationModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,7 +34,8 @@ object MigrationModule {
settingsRepository: SettingsRepository,
directoryAccessValidator: DirectoryAccessValidator,
uriHandler: UriHandler,
gson: Gson
gson: Gson,
genericJsonArrayMigrationHelper: GenericJsonArrayMigrationHelper,
): Migrator {

return Migrator(context, sharedPreferences).apply {
Expand All @@ -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))
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Rom22, RomDto25>(ROM_DATA_FILE) { rom ->
runCatching {
context.contentResolver.openInputStream(rom.uri)?.use {
RomProcessor.getRomMetadata(it.buffered())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LayoutConfiguration25, LayoutConfigurationDto>(LAYOUTS_DATA_FILE) {
try {
LayoutConfigurationDto(
it.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <reified T, U> migrateJsonArrayData(jsonFile: String, noinline dataMapper: (T) -> U?) {
migrateJsonArrayData(jsonFile, T::class.java, dataMapper)
}

fun <T, U> migrateJsonArrayData(jsonFile: String, fromClass: Class<T>, dataMapper: (T) -> U?) {
val originalData = getOriginalData(jsonFile, fromClass)
val newData = originalData.mapNotNull { data ->
dataMapper(data)
}

saveNewData(jsonFile, newData)
}

private fun <T> getOriginalData(jsonFile: String, fromClass: Class<T>): List<T> {
val dataFile = File(context.filesDir, jsonFile)
if (!dataFile.isFile) {
return emptyList()
}

val dataListType = TypeToken.getParameterized(List::class.java, fromClass).type
return runCatching {
gson.fromJson<List<T>>(FileReader(dataFile), dataListType)
}.getOrElse { emptyList() }
}

private fun <U> saveNewData(jsonFile: String, data: List<U>) {
val dataFile = File(context.filesDir, jsonFile)

OutputStreamWriter(dataFile.outputStream()).use {
val json = gson.toJson(data)
it.write(json)
}
}
}

This file was deleted.

This file was deleted.

0 comments on commit 41d342c

Please sign in to comment.