-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create generic JSON migration helper
- Loading branch information
1 parent
d440aa9
commit 41d342c
Showing
6 changed files
with
73 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 8 additions & 4 deletions
12
app/src/main/java/me/magnum/melonds/migrations/Migration24to25.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
app/src/main/java/me/magnum/melonds/migrations/helper/GenericJsonArrayMigrationHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
app/src/main/java/me/magnum/melonds/migrations/helper/LayoutMigrationHelper.kt
This file was deleted.
Oops, something went wrong.
48 changes: 0 additions & 48 deletions
48
app/src/main/java/me/magnum/melonds/migrations/helper/RomMigrationHelper.kt
This file was deleted.
Oops, something went wrong.