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.extensions.TAG
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 kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -26,11 +28,11 @@ object SubscriptionHelper {
}
}
} else {
Thread {
query {
Database.localSubscriptionDao().insertAll(
LocalSubscription(channelId)
)
}.start()
}
}
}
@ -47,11 +49,11 @@ object SubscriptionHelper {
}
}
} else {
Thread {
query {
Database.localSubscriptionDao().delete(
LocalSubscription(channelId)
)
}.start()
}
}
}
@ -68,11 +70,9 @@ object SubscriptionHelper {
}
return isSubscribed.subscribed
} else {
var isSubscribed = false
Thread {
isSubscribed = Database.localSubscriptionDao().includes(channelId)
}.await()
return isSubscribed
return awaitQuery {
Database.localSubscriptionDao().includes(channelId)
}
}
}
@ -93,20 +93,18 @@ object SubscriptionHelper {
newChannels.forEach {
newLocalSubscriptions += LocalSubscription(channelId = it)
}
Thread {
query {
Database.localSubscriptionDao().insertAll(
*newChannels.map { LocalSubscription(it) }.toTypedArray()
)
}.start()
}
}
}
fun getLocalSubscriptions(): List<LocalSubscription> {
var localSubscriptions = listOf<LocalSubscription>()
Thread {
localSubscriptions = Database.localSubscriptionDao().getAll()
}.await()
return localSubscriptions
return awaitQuery {
Database.localSubscriptionDao().getAll()
}
}
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.WatchHistoryItem
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.util.PreferenceHelper
object DatabaseHelper {
fun addToWatchHistory(videoId: String, streams: com.github.libretube.api.obj.Streams) {
fun addToWatchHistory(videoId: String, streams: Streams) {
val watchHistoryItem = WatchHistoryItem(
videoId,
streams.title,
@ -21,11 +22,11 @@ object DatabaseHelper {
streams.thumbnailUrl,
streams.duration
)
Thread {
query {
Database.watchHistoryDao().insertAll(watchHistoryItem)
val maxHistorySize =
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
val watchHistory = Database.watchHistoryDao().getAll()
@ -33,15 +34,15 @@ object DatabaseHelper {
Database.watchHistoryDao()
.delete(watchHistory.first())
}
}.start()
}
}
fun removeFromWatchHistory(index: Int) {
Thread {
query {
Database.watchHistoryDao().delete(
Database.watchHistoryDao().getAll()[index]
)
}.start()
}
}
fun saveWatchPosition(videoId: String, position: Long) {
@ -49,21 +50,21 @@ object DatabaseHelper {
videoId,
position
)
Thread {
query {
Database.watchPositionDao().insertAll(watchPosition)
}.start()
}
}
fun removeWatchPosition(videoId: String) {
Thread {
query {
Database.watchPositionDao().findById(videoId)?.let {
Database.watchPositionDao().delete(it)
}
}.start()
}
}
fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) {
Thread {
query {
Database.searchHistoryDao().insertAll(searchHistoryItem)
val maxHistorySize = 20
@ -73,6 +74,6 @@ object DatabaseHelper {
Database.searchHistoryDao()
.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
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) {
val view = this!!
var progress: Long? = null
Thread {
try {
progress = Database.watchPositionDao().findById(videoId)?.position
} catch (e: Exception) {
progress = null
val progress = try {
awaitQuery {
Database.watchPositionDao().findById(videoId)?.position
}
}.await()
} catch (e: Exception) {
return
}
view.getViewTreeObserver()
.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
@ -29,7 +28,7 @@ fun View?.setWatchProgressLength(videoId: String, duration: Long) {
return
}
val fullWidth = (parent as LinearLayout).width
val newWidth = (fullWidth * (progress!! / (duration))) / 1000
val newWidth = (fullWidth * (progress / duration)) / 1000
val lp = view.layoutParams
lp.width = newWidth.toInt()
view.layoutParams = lp

View File

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

View File

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

View File

@ -8,6 +8,7 @@ import com.github.libretube.R
import com.github.libretube.databinding.DialogCustomInstanceBinding
import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.CustomInstance
import com.github.libretube.extensions.query
import com.github.libretube.util.ThemeHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.net.URL
@ -39,9 +40,9 @@ class CustomInstanceDialog : DialogFragment() {
URL(customInstance.apiUrl).toURI()
URL(customInstance.frontendUrl).toURI()
Thread {
query {
Database.customInstanceDao().insertAll(customInstance)
}.start()
}
activity?.recreate()
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.obj.CustomInstance
import com.github.libretube.extensions.await
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -80,10 +81,9 @@ class ShareDialog(
)
// get the api urls of the other custom instances
var customInstances = listOf<CustomInstance>()
Thread {
customInstances = Database.customInstanceDao().getAll()
}.await()
val customInstances = awaitQuery {
Database.customInstanceDao().getAll()
}
// return the custom instance frontend url if available
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.db.DatabaseHelper
import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.BaseFragment
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.extensions.*
import com.github.libretube.interfaces.PlayerOptionsInterface
import com.github.libretube.models.PlayerViewModel
import com.github.libretube.services.BackgroundMode
@ -861,17 +855,15 @@ class PlayerFragment : BaseFragment() {
return
}
// browse the watch positions
var position: Long? = null
Thread {
try {
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()
val position = try {
awaitQuery {
Database.watchPositionDao().findById(videoId!!)?.position
}
}.await()
if (position != null) exoPlayer.seekTo(position!!)
} catch (e: Exception) {
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

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.TAG
import com.github.libretube.extensions.await
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.models.SearchViewModel
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.adapters.SearchHistoryAdapter
@ -95,11 +96,9 @@ class SearchFragment : BaseFragment() {
}
private fun showHistory() {
var historyList = listOf<String>()
Thread {
val history = Database.searchHistoryDao().getAll()
historyList = history.map { it.query }
}.await()
val historyList = awaitQuery {
Database.searchHistoryDao().getAll().map { it.query }
}
if (historyList.isNotEmpty()) {
binding.suggestionsRecycler.adapter =
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.extensions.BaseFragment
import com.github.libretube.extensions.await
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.ui.adapters.WatchHistoryAdapter
class WatchHistoryFragment : BaseFragment() {
@ -29,11 +30,9 @@ class WatchHistoryFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var watchHistory = listOf<WatchHistoryItem>()
Thread {
watchHistory = Database.watchHistoryDao().getAll()
}.await()
val watchHistory = awaitQuery {
Database.watchHistoryDao().getAll()
}
if (watchHistory.isEmpty()) return

View File

@ -5,6 +5,7 @@ import androidx.preference.Preference
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
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.views.MaterialPreferenceFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -52,9 +53,7 @@ class HistorySettings : MaterialPreferenceFragment() {
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.okay) { _, _ ->
// clear the selected preference preferences
Thread {
action()
}.start()
query(action)
}
.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.obj.CustomInstance
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.dialogs.CustomInstanceDialog
import com.github.libretube.ui.dialogs.DeleteAccountDialog
@ -110,9 +112,9 @@ class InstanceSettings : MaterialPreferenceFragment() {
val clearCustomInstances = findPreference<Preference>(PreferenceKeys.CLEAR_CUSTOM_INSTANCES)
clearCustomInstances?.setOnPreferenceClickListener {
Thread {
awaitQuery {
Database.customInstanceDao().deleteAll()
}.await()
}
activity?.recreate()
true
}
@ -156,10 +158,9 @@ class InstanceSettings : MaterialPreferenceFragment() {
private fun initCustomInstances(instancePref: ListPreference) {
lifecycleScope.launchWhenCreated {
var customInstances = listOf<CustomInstance>()
Thread {
customInstances = Database.customInstanceDao().getAll()
}.await()
val customInstances = awaitQuery {
Database.customInstanceDao().getAll()
}
val instanceNames = arrayListOf<String>()
val instanceValues = arrayListOf<String>()