From 4244efc448becf6aad948301f098ecd69d0b4d70 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 13 Aug 2022 22:17:28 +0200 Subject: [PATCH] migrate custom instances --- .../github/libretube/database/AppDatabase.kt | 11 ++++++++-- .../libretube/database/CustomInstanceDao.kt | 20 +++++++++++++++++ .../libretube/dialogs/CustomInstanceDialog.kt | 7 ++++-- .../github/libretube/dialogs/ShareDialog.kt | 10 ++++++++- .../github/libretube/obj/CustomInstance.kt | 8 ++++--- .../libretube/preferences/InstanceSettings.kt | 10 ++++++++- .../libretube/preferences/PreferenceHelper.kt | 22 ------------------- 7 files changed, 57 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/database/CustomInstanceDao.kt diff --git a/app/src/main/java/com/github/libretube/database/AppDatabase.kt b/app/src/main/java/com/github/libretube/database/AppDatabase.kt index 9ad08b99b..1a07f722b 100644 --- a/app/src/main/java/com/github/libretube/database/AppDatabase.kt +++ b/app/src/main/java/com/github/libretube/database/AppDatabase.kt @@ -2,15 +2,17 @@ package com.github.libretube.database import androidx.room.Database import androidx.room.RoomDatabase +import com.github.libretube.obj.CustomInstance import com.github.libretube.obj.WatchHistoryItem import com.github.libretube.obj.WatchPosition @Database( entities = [ WatchHistoryItem::class, - WatchPosition::class + WatchPosition::class, + CustomInstance::class ], - version = 2 + version = 3 ) abstract class AppDatabase : RoomDatabase() { /** @@ -22,4 +24,9 @@ abstract class AppDatabase : RoomDatabase() { * Watch Positions */ abstract fun watchPositionDao(): WatchPositionDao + + /** + * Custom Instances + */ + abstract fun customInstanceDao(): CustomInstanceDao } diff --git a/app/src/main/java/com/github/libretube/database/CustomInstanceDao.kt b/app/src/main/java/com/github/libretube/database/CustomInstanceDao.kt new file mode 100644 index 000000000..972a74ad1 --- /dev/null +++ b/app/src/main/java/com/github/libretube/database/CustomInstanceDao.kt @@ -0,0 +1,20 @@ +package com.github.libretube.database + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import com.github.libretube.obj.CustomInstance + +@Dao +interface CustomInstanceDao { + @Query("SELECT * FROM customInstance") + fun getAll(): List + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertAll(vararg customInstances: CustomInstance) + + @Delete + fun delete(customInstance: CustomInstance) +} diff --git a/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt b/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt index 96773bf35..8e686a2e4 100644 --- a/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt @@ -5,9 +5,9 @@ import android.os.Bundle import android.widget.Toast import androidx.fragment.app.DialogFragment import com.github.libretube.R +import com.github.libretube.database.DatabaseHolder import com.github.libretube.databinding.DialogCustomInstanceBinding import com.github.libretube.obj.CustomInstance -import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.util.ThemeHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder import java.net.URL @@ -41,7 +41,10 @@ class CustomInstanceDialog : DialogFragment() { URL(customInstance.apiUrl).toURI() URL(customInstance.frontendUrl).toURI() - PreferenceHelper.saveCustomInstance(customInstance) + Thread { + DatabaseHolder.database.customInstanceDao().insertAll(customInstance) + }.start() + activity?.recreate() dismiss() } catch (e: Exception) { diff --git a/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt index 7509229ce..6c7024357 100644 --- a/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt @@ -7,6 +7,8 @@ import androidx.fragment.app.DialogFragment import com.github.libretube.PIPED_FRONTEND_URL import com.github.libretube.R import com.github.libretube.YOUTUBE_FRONTEND_URL +import com.github.libretube.database.DatabaseHolder +import com.github.libretube.obj.CustomInstance import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceKeys import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -71,7 +73,13 @@ class ShareDialog( ) // get the api urls of the other custom instances - val customInstances = PreferenceHelper.getCustomInstances() + var customInstances = listOf() + Thread { + customInstances = DatabaseHolder.database.customInstanceDao().getAll() + }.apply { + start() + join() + } // return the custom instance frontend url if available customInstances.forEach { instance -> diff --git a/app/src/main/java/com/github/libretube/obj/CustomInstance.kt b/app/src/main/java/com/github/libretube/obj/CustomInstance.kt index c6d601171..c041b9b75 100644 --- a/app/src/main/java/com/github/libretube/obj/CustomInstance.kt +++ b/app/src/main/java/com/github/libretube/obj/CustomInstance.kt @@ -2,10 +2,12 @@ package com.github.libretube.obj import androidx.room.ColumnInfo import androidx.room.Entity +import androidx.room.PrimaryKey -@Entity +@Entity(tableName = "customInstance") class CustomInstance( + @PrimaryKey(autoGenerate = true) var id: Int = 0, @ColumnInfo var name: String = "", - var apiUrl: String = "", - var frontendUrl: String = "" + @ColumnInfo var apiUrl: String = "", + @ColumnInfo var frontendUrl: String = "" ) diff --git a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt index 170d1e12d..6be7e15ea 100644 --- a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt @@ -13,10 +13,12 @@ import androidx.preference.Preference import androidx.preference.SwitchPreferenceCompat import com.github.libretube.R import com.github.libretube.activities.SettingsActivity +import com.github.libretube.database.DatabaseHolder import com.github.libretube.dialogs.CustomInstanceDialog import com.github.libretube.dialogs.DeleteAccountDialog import com.github.libretube.dialogs.LoginDialog import com.github.libretube.dialogs.LogoutDialog +import com.github.libretube.obj.CustomInstance import com.github.libretube.util.ImportHelper import com.github.libretube.util.PermissionHelper import com.github.libretube.util.RetrofitInstance @@ -157,7 +159,13 @@ class InstanceSettings : MaterialPreferenceFragment() { private fun initCustomInstances(instancePref: ListPreference) { lifecycleScope.launchWhenCreated { - val customInstances = PreferenceHelper.getCustomInstances() + var customInstances = listOf() + Thread { + customInstances = DatabaseHolder.database.customInstanceDao().getAll() + }.apply { + start() + join() + } val instanceNames = arrayListOf() val instanceValues = arrayListOf() diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt index 0f8f68f2c..a6fc1fca0 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt @@ -5,7 +5,6 @@ import android.content.SharedPreferences import androidx.preference.PreferenceManager import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.ObjectMapper -import com.github.libretube.obj.CustomInstance object PreferenceHelper { private val TAG = "PreferenceHelper" @@ -71,27 +70,6 @@ object PreferenceHelper { authEditor.putString(PreferenceKeys.USERNAME, newValue).apply() } - fun saveCustomInstance(customInstance: CustomInstance) { - val customInstancesList = getCustomInstances() - customInstancesList += customInstance - - val json = mapper.writeValueAsString(customInstancesList) - editor.putString("customInstances", json).apply() - } - - fun getCustomInstances(): ArrayList { - val json: String = settings.getString("customInstances", "")!! - val type = mapper.typeFactory.constructCollectionType( - List::class.java, - CustomInstance::class.java - ) - return try { - mapper.readValue(json, type) - } catch (e: Exception) { - arrayListOf() - } - } - fun getSearchHistory(): List { return try { val json = settings.getString("search_history", "")!!