migrate custom instances

This commit is contained in:
Bnyro 2022-08-13 22:17:28 +02:00
parent 3eadc46780
commit 4244efc448
7 changed files with 57 additions and 31 deletions

View File

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

View File

@ -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<CustomInstance>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(vararg customInstances: CustomInstance)
@Delete
fun delete(customInstance: CustomInstance)
}

View File

@ -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) {

View File

@ -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<CustomInstance>()
Thread {
customInstances = DatabaseHolder.database.customInstanceDao().getAll()
}.apply {
start()
join()
}
// return the custom instance frontend url if available
customInstances.forEach { instance ->

View File

@ -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 = ""
)

View File

@ -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<CustomInstance>()
Thread {
customInstances = DatabaseHolder.database.customInstanceDao().getAll()
}.apply {
start()
join()
}
val instanceNames = arrayListOf<String>()
val instanceValues = arrayListOf<String>()

View File

@ -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<CustomInstance> {
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<String> {
return try {
val json = settings.getString("search_history", "")!!