database cleanup

This commit is contained in:
Bnyro 2022-09-22 17:52:00 +02:00
parent 333e6b152f
commit 8828fc205f
14 changed files with 93 additions and 83 deletions

View File

@ -6,6 +6,8 @@ import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.LocalSubscription import com.github.libretube.db.obj.LocalSubscription
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.await import com.github.libretube.extensions.await
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -26,11 +28,11 @@ object SubscriptionHelper {
} }
} }
} else { } else {
Thread { query {
Database.localSubscriptionDao().insertAll( Database.localSubscriptionDao().insertAll(
LocalSubscription(channelId) LocalSubscription(channelId)
) )
}.start() }
} }
} }
@ -47,11 +49,11 @@ object SubscriptionHelper {
} }
} }
} else { } else {
Thread { query {
Database.localSubscriptionDao().delete( Database.localSubscriptionDao().delete(
LocalSubscription(channelId) LocalSubscription(channelId)
) )
}.start() }
} }
} }
@ -68,11 +70,9 @@ object SubscriptionHelper {
} }
return isSubscribed.subscribed return isSubscribed.subscribed
} else { } else {
var isSubscribed = false return awaitQuery {
Thread { Database.localSubscriptionDao().includes(channelId)
isSubscribed = Database.localSubscriptionDao().includes(channelId) }
}.await()
return isSubscribed
} }
} }
@ -93,20 +93,18 @@ object SubscriptionHelper {
newChannels.forEach { newChannels.forEach {
newLocalSubscriptions += LocalSubscription(channelId = it) newLocalSubscriptions += LocalSubscription(channelId = it)
} }
Thread { query {
Database.localSubscriptionDao().insertAll( Database.localSubscriptionDao().insertAll(
*newChannels.map { LocalSubscription(it) }.toTypedArray() *newChannels.map { LocalSubscription(it) }.toTypedArray()
) )
}.start() }
} }
} }
fun getLocalSubscriptions(): List<LocalSubscription> { fun getLocalSubscriptions(): List<LocalSubscription> {
var localSubscriptions = listOf<LocalSubscription>() return awaitQuery {
Thread { Database.localSubscriptionDao().getAll()
localSubscriptions = Database.localSubscriptionDao().getAll() }
}.await()
return localSubscriptions
} }
fun getFormattedLocalSubscriptions(): String { fun getFormattedLocalSubscriptions(): String {

View File

@ -6,11 +6,12 @@ import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.SearchHistoryItem import com.github.libretube.db.obj.SearchHistoryItem
import com.github.libretube.db.obj.WatchHistoryItem import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.db.obj.WatchPosition import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
object DatabaseHelper { object DatabaseHelper {
fun addToWatchHistory(videoId: String, streams: com.github.libretube.api.obj.Streams) { fun addToWatchHistory(videoId: String, streams: Streams) {
val watchHistoryItem = WatchHistoryItem( val watchHistoryItem = WatchHistoryItem(
videoId, videoId,
streams.title, streams.title,
@ -21,11 +22,11 @@ object DatabaseHelper {
streams.thumbnailUrl, streams.thumbnailUrl,
streams.duration streams.duration
) )
Thread { query {
Database.watchHistoryDao().insertAll(watchHistoryItem) Database.watchHistoryDao().insertAll(watchHistoryItem)
val maxHistorySize = val maxHistorySize =
PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "unlimited") PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "unlimited")
if (maxHistorySize == "unlimited") return@Thread if (maxHistorySize == "unlimited") return@query
// delete the first watch history entry if the limit is reached // delete the first watch history entry if the limit is reached
val watchHistory = Database.watchHistoryDao().getAll() val watchHistory = Database.watchHistoryDao().getAll()
@ -33,15 +34,15 @@ object DatabaseHelper {
Database.watchHistoryDao() Database.watchHistoryDao()
.delete(watchHistory.first()) .delete(watchHistory.first())
} }
}.start() }
} }
fun removeFromWatchHistory(index: Int) { fun removeFromWatchHistory(index: Int) {
Thread { query {
Database.watchHistoryDao().delete( Database.watchHistoryDao().delete(
Database.watchHistoryDao().getAll()[index] Database.watchHistoryDao().getAll()[index]
) )
}.start() }
} }
fun saveWatchPosition(videoId: String, position: Long) { fun saveWatchPosition(videoId: String, position: Long) {
@ -49,21 +50,21 @@ object DatabaseHelper {
videoId, videoId,
position position
) )
Thread { query {
Database.watchPositionDao().insertAll(watchPosition) Database.watchPositionDao().insertAll(watchPosition)
}.start() }
} }
fun removeWatchPosition(videoId: String) { fun removeWatchPosition(videoId: String) {
Thread { query {
Database.watchPositionDao().findById(videoId)?.let { Database.watchPositionDao().findById(videoId)?.let {
Database.watchPositionDao().delete(it) Database.watchPositionDao().delete(it)
} }
}.start() }
} }
fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) { fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) {
Thread { query {
Database.searchHistoryDao().insertAll(searchHistoryItem) Database.searchHistoryDao().insertAll(searchHistoryItem)
val maxHistorySize = 20 val maxHistorySize = 20
@ -73,6 +74,6 @@ object DatabaseHelper {
Database.searchHistoryDao() Database.searchHistoryDao()
.delete(searchHistory.first()) .delete(searchHistory.first())
} }
}.start() }
} }
} }

View File

@ -0,0 +1,13 @@
package com.github.libretube.extensions
fun <T> awaitQuery(
query: () -> T
):T {
var x: T? = null
val thread = Thread {
x = query.invoke()
}
thread.start()
thread.join()
return x!!
}

View File

@ -1,5 +1,11 @@
package com.github.libretube.extensions package com.github.libretube.extensions
fun query(block: () -> Unit) { fun query(block: () -> Unit) {
Thread(block).start() Thread {
try {
block.invoke()
} catch (e: Exception) {
e.printStackTrace()
}
}.start()
} }

View File

@ -10,15 +10,14 @@ import com.github.libretube.db.DatabaseHolder.Companion.Database
*/ */
fun View?.setWatchProgressLength(videoId: String, duration: Long) { fun View?.setWatchProgressLength(videoId: String, duration: Long) {
val view = this!! val view = this!!
var progress: Long? = null
Thread { val progress = try {
try { awaitQuery {
progress = Database.watchPositionDao().findById(videoId)?.position Database.watchPositionDao().findById(videoId)?.position
} catch (e: Exception) { }
progress = null } catch (e: Exception) {
return
} }
}.await()
view.getViewTreeObserver() view.getViewTreeObserver()
.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { .addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
@ -29,7 +28,7 @@ fun View?.setWatchProgressLength(videoId: String, duration: Long) {
return return
} }
val fullWidth = (parent as LinearLayout).width val fullWidth = (parent as LinearLayout).width
val newWidth = (fullWidth * (progress!! / (duration))) / 1000 val newWidth = (fullWidth * (progress / duration)) / 1000
val lp = view.layoutParams val lp = view.layoutParams
lp.width = newWidth.toInt() lp.width = newWidth.toInt()
view.layoutParams = lp view.layoutParams = lp

View File

@ -7,6 +7,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.SearchhistoryRowBinding import com.github.libretube.databinding.SearchhistoryRowBinding
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.SearchHistoryItem import com.github.libretube.db.obj.SearchHistoryItem
import com.github.libretube.extensions.query
class SearchHistoryAdapter( class SearchHistoryAdapter(
private var historyList: List<String>, private var historyList: List<String>,
@ -32,11 +33,11 @@ class SearchHistoryAdapter(
deleteHistory.setOnClickListener { deleteHistory.setOnClickListener {
val itemIndex = historyList.indexOf(historyQuery) val itemIndex = historyList.indexOf(historyQuery)
historyList -= historyQuery historyList -= historyQuery
Thread { query {
Database.searchHistoryDao().delete( Database.searchHistoryDao().delete(
SearchHistoryItem(query = historyQuery) SearchHistoryItem(query = historyQuery)
) )
}.start() }
notifyItemRemoved(itemIndex) notifyItemRemoved(itemIndex)
} }

View File

@ -11,6 +11,7 @@ import com.github.libretube.extensions.await
import com.github.libretube.obj.BackupFile import com.github.libretube.obj.BackupFile
import com.github.libretube.ui.adapters.BackupOptionsAdapter import com.github.libretube.ui.adapters.BackupOptionsAdapter
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.runBlocking
class BackupDialog( class BackupDialog(
private val createBackupFile: (BackupFile) -> Unit private val createBackupFile: (BackupFile) -> Unit
@ -42,7 +43,7 @@ class BackupDialog(
.setView(binding.root) .setView(binding.root)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.backup) { _, _ -> .setPositiveButton(R.string.backup) { _, _ ->
Thread { runBlocking {
if (selected[0]) { if (selected[0]) {
backupFile.watchHistory = backupFile.watchHistory =
Database.watchHistoryDao().getAll() Database.watchHistoryDao().getAll()
@ -63,7 +64,7 @@ class BackupDialog(
backupFile.customInstances = backupFile.customInstances =
Database.customInstanceDao().getAll() Database.customInstanceDao().getAll()
} }
}.await() }
createBackupFile(backupFile) createBackupFile(backupFile)
} }

View File

@ -8,6 +8,7 @@ import com.github.libretube.R
import com.github.libretube.databinding.DialogCustomInstanceBinding import com.github.libretube.databinding.DialogCustomInstanceBinding
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.CustomInstance import com.github.libretube.db.obj.CustomInstance
import com.github.libretube.extensions.query
import com.github.libretube.util.ThemeHelper import com.github.libretube.util.ThemeHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.net.URL import java.net.URL
@ -39,9 +40,9 @@ class CustomInstanceDialog : DialogFragment() {
URL(customInstance.apiUrl).toURI() URL(customInstance.apiUrl).toURI()
URL(customInstance.frontendUrl).toURI() URL(customInstance.frontendUrl).toURI()
Thread { query {
Database.customInstanceDao().insertAll(customInstance) Database.customInstanceDao().insertAll(customInstance)
}.start() }
activity?.recreate() activity?.recreate()
dismiss() dismiss()

View File

@ -12,6 +12,7 @@ import com.github.libretube.databinding.DialogShareBinding
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.CustomInstance import com.github.libretube.db.obj.CustomInstance
import com.github.libretube.extensions.await import com.github.libretube.extensions.await
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -80,10 +81,9 @@ class ShareDialog(
) )
// get the api urls of the other custom instances // get the api urls of the other custom instances
var customInstances = listOf<CustomInstance>() val customInstances = awaitQuery {
Thread { Database.customInstanceDao().getAll()
customInstances = Database.customInstanceDao().getAll() }
}.await()
// return the custom instance frontend url if available // return the custom instance frontend url if available
customInstances.forEach { instance -> customInstances.forEach { instance ->

View File

@ -46,13 +46,7 @@ import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
import com.github.libretube.databinding.FragmentPlayerBinding import com.github.libretube.databinding.FragmentPlayerBinding
import com.github.libretube.db.DatabaseHelper import com.github.libretube.db.DatabaseHelper
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.*
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.await
import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.hideKeyboard
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.interfaces.PlayerOptionsInterface import com.github.libretube.interfaces.PlayerOptionsInterface
import com.github.libretube.models.PlayerViewModel import com.github.libretube.models.PlayerViewModel
import com.github.libretube.services.BackgroundMode import com.github.libretube.services.BackgroundMode
@ -861,17 +855,15 @@ class PlayerFragment : BaseFragment() {
return return
} }
// browse the watch positions // browse the watch positions
var position: Long? = null val position = try {
Thread { awaitQuery {
try { Database.watchPositionDao().findById(videoId!!)?.position
position = Database.watchPositionDao().findById(videoId!!)?.position
// position is almost the end of the video => don't seek, start from beginning
if (position!! > streams.duration!! * 1000 * 0.9) position = null
} catch (e: Exception) {
e.printStackTrace()
} }
}.await() } catch (e: Exception) {
if (position != null) exoPlayer.seekTo(position!!) return
}
// position is almost the end of the video => don't seek, start from beginning
if (position != null && position < streams.duration!! * 1000 * 0.9) exoPlayer.seekTo(position)
} }
// used for autoplay and skipping to next video // used for autoplay and skipping to next video

View File

@ -16,6 +16,7 @@ import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.await import com.github.libretube.extensions.await
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.models.SearchViewModel import com.github.libretube.models.SearchViewModel
import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.adapters.SearchHistoryAdapter import com.github.libretube.ui.adapters.SearchHistoryAdapter
@ -95,11 +96,9 @@ class SearchFragment : BaseFragment() {
} }
private fun showHistory() { private fun showHistory() {
var historyList = listOf<String>() val historyList = awaitQuery {
Thread { Database.searchHistoryDao().getAll().map { it.query }
val history = Database.searchHistoryDao().getAll() }
historyList = history.map { it.query }
}.await()
if (historyList.isNotEmpty()) { if (historyList.isNotEmpty()) {
binding.suggestionsRecycler.adapter = binding.suggestionsRecycler.adapter =
SearchHistoryAdapter( SearchHistoryAdapter(

View File

@ -12,6 +12,7 @@ import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.WatchHistoryItem import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.await import com.github.libretube.extensions.await
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.ui.adapters.WatchHistoryAdapter import com.github.libretube.ui.adapters.WatchHistoryAdapter
class WatchHistoryFragment : BaseFragment() { class WatchHistoryFragment : BaseFragment() {
@ -29,11 +30,9 @@ class WatchHistoryFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
var watchHistory = listOf<WatchHistoryItem>() val watchHistory = awaitQuery {
Database.watchHistoryDao().getAll()
Thread { }
watchHistory = Database.watchHistoryDao().getAll()
}.await()
if (watchHistory.isEmpty()) return if (watchHistory.isEmpty()) return

View File

@ -5,6 +5,7 @@ import androidx.preference.Preference
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.query
import com.github.libretube.ui.activities.SettingsActivity import com.github.libretube.ui.activities.SettingsActivity
import com.github.libretube.ui.views.MaterialPreferenceFragment import com.github.libretube.ui.views.MaterialPreferenceFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -52,9 +53,7 @@ class HistorySettings : MaterialPreferenceFragment() {
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.okay) { _, _ -> .setPositiveButton(R.string.okay) { _, _ ->
// clear the selected preference preferences // clear the selected preference preferences
Thread { query(action)
action()
}.start()
} }
.show() .show()
} }

View File

@ -17,6 +17,8 @@ import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.CustomInstance import com.github.libretube.db.obj.CustomInstance
import com.github.libretube.extensions.await import com.github.libretube.extensions.await
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query
import com.github.libretube.ui.activities.SettingsActivity import com.github.libretube.ui.activities.SettingsActivity
import com.github.libretube.ui.dialogs.CustomInstanceDialog import com.github.libretube.ui.dialogs.CustomInstanceDialog
import com.github.libretube.ui.dialogs.DeleteAccountDialog import com.github.libretube.ui.dialogs.DeleteAccountDialog
@ -110,9 +112,9 @@ class InstanceSettings : MaterialPreferenceFragment() {
val clearCustomInstances = findPreference<Preference>(PreferenceKeys.CLEAR_CUSTOM_INSTANCES) val clearCustomInstances = findPreference<Preference>(PreferenceKeys.CLEAR_CUSTOM_INSTANCES)
clearCustomInstances?.setOnPreferenceClickListener { clearCustomInstances?.setOnPreferenceClickListener {
Thread { awaitQuery {
Database.customInstanceDao().deleteAll() Database.customInstanceDao().deleteAll()
}.await() }
activity?.recreate() activity?.recreate()
true true
} }
@ -156,10 +158,9 @@ class InstanceSettings : MaterialPreferenceFragment() {
private fun initCustomInstances(instancePref: ListPreference) { private fun initCustomInstances(instancePref: ListPreference) {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
var customInstances = listOf<CustomInstance>() val customInstances = awaitQuery {
Thread { Database.customInstanceDao().getAll()
customInstances = Database.customInstanceDao().getAll() }
}.await()
val instanceNames = arrayListOf<String>() val instanceNames = arrayListOf<String>()
val instanceValues = arrayListOf<String>() val instanceValues = arrayListOf<String>()