code cleanup

This commit is contained in:
Bnyro 2022-09-18 19:24:31 +02:00
parent 8be5a293bc
commit 8f5de822d1
15 changed files with 59 additions and 55 deletions

View File

@ -34,7 +34,7 @@ class LibreTubeApp : Application() {
/** /**
* Initialize the [DatabaseHolder] * Initialize the [DatabaseHolder]
*/ */
DatabaseHolder.initializeDatabase(this) DatabaseHolder().initializeDatabase(this)
/** /**
* Bypassing fileUriExposedException, see https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed * Bypassing fileUriExposedException, see https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed

View File

@ -5,7 +5,7 @@ import android.view.ViewGroup
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.SearchhistoryRowBinding import com.github.libretube.databinding.SearchhistoryRowBinding
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.SearchHistoryItem import com.github.libretube.db.obj.SearchHistoryItem
class SearchHistoryAdapter( class SearchHistoryAdapter(
@ -32,7 +32,7 @@ class SearchHistoryAdapter(
deleteHistory.setOnClickListener { deleteHistory.setOnClickListener {
historyList -= historyQuery historyList -= historyQuery
Thread { Thread {
DatabaseHolder.db.searchHistoryDao().delete( Database.searchHistoryDao().delete(
SearchHistoryItem(query = historyQuery) SearchHistoryItem(query = historyQuery)
) )
}.start() }.start()

View File

@ -1,7 +1,7 @@
package com.github.libretube.api package com.github.libretube.api
import android.util.Log import android.util.Log
import com.github.libretube.db.DatabaseHolder 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
@ -27,7 +27,7 @@ object SubscriptionHelper {
} }
} else { } else {
Thread { Thread {
DatabaseHolder.db.localSubscriptionDao().insertAll( Database.localSubscriptionDao().insertAll(
LocalSubscription(channelId) LocalSubscription(channelId)
) )
}.start() }.start()
@ -48,7 +48,7 @@ object SubscriptionHelper {
} }
} else { } else {
Thread { Thread {
DatabaseHolder.db.localSubscriptionDao().delete( Database.localSubscriptionDao().delete(
LocalSubscription(channelId) LocalSubscription(channelId)
) )
}.start() }.start()
@ -70,7 +70,7 @@ object SubscriptionHelper {
} else { } else {
var isSubscribed = false var isSubscribed = false
Thread { Thread {
isSubscribed = DatabaseHolder.db.localSubscriptionDao().includes(channelId) isSubscribed = Database.localSubscriptionDao().includes(channelId)
}.await() }.await()
return isSubscribed return isSubscribed
} }
@ -94,7 +94,7 @@ object SubscriptionHelper {
newLocalSubscriptions += LocalSubscription(channelId = it) newLocalSubscriptions += LocalSubscription(channelId = it)
} }
Thread { Thread {
DatabaseHolder.db.localSubscriptionDao().insertAll( Database.localSubscriptionDao().insertAll(
*newChannels.map { LocalSubscription(it) }.toTypedArray() *newChannels.map { LocalSubscription(it) }.toTypedArray()
) )
}.start() }.start()
@ -104,7 +104,7 @@ object SubscriptionHelper {
fun getLocalSubscriptions(): List<LocalSubscription> { fun getLocalSubscriptions(): List<LocalSubscription> {
var localSubscriptions = listOf<LocalSubscription>() var localSubscriptions = listOf<LocalSubscription>()
Thread { Thread {
localSubscriptions = DatabaseHolder.db.localSubscriptionDao().getAll() localSubscriptions = Database.localSubscriptionDao().getAll()
}.await() }.await()
return localSubscriptions return localSubscriptions
} }

View File

@ -1,6 +1,7 @@
package com.github.libretube.db package com.github.libretube.db
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.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
@ -21,15 +22,15 @@ object DatabaseHelper {
streams.duration streams.duration
) )
Thread { Thread {
DatabaseHolder.db.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@Thread
// delete the first watch history entry if the limit is reached // delete the first watch history entry if the limit is reached
val watchHistory = DatabaseHolder.db.watchHistoryDao().getAll() val watchHistory = Database.watchHistoryDao().getAll()
if (watchHistory.size > maxHistorySize.toInt()) { if (watchHistory.size > maxHistorySize.toInt()) {
DatabaseHolder.db.watchHistoryDao() Database.watchHistoryDao()
.delete(watchHistory.first()) .delete(watchHistory.first())
} }
}.start() }.start()
@ -37,8 +38,8 @@ object DatabaseHelper {
fun removeFromWatchHistory(index: Int) { fun removeFromWatchHistory(index: Int) {
Thread { Thread {
DatabaseHolder.db.watchHistoryDao().delete( Database.watchHistoryDao().delete(
DatabaseHolder.db.watchHistoryDao().getAll()[index] Database.watchHistoryDao().getAll()[index]
) )
}.start() }.start()
} }
@ -49,27 +50,27 @@ object DatabaseHelper {
position position
) )
Thread { Thread {
DatabaseHolder.db.watchPositionDao().insertAll(watchPosition) Database.watchPositionDao().insertAll(watchPosition)
}.start() }.start()
} }
fun removeWatchPosition(videoId: String) { fun removeWatchPosition(videoId: String) {
Thread { Thread {
DatabaseHolder.db.watchPositionDao().delete( Database.watchPositionDao().delete(
DatabaseHolder.db.watchPositionDao().findById(videoId) Database.watchPositionDao().findById(videoId)
) )
}.start() }.start()
} }
fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) { fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) {
Thread { Thread {
DatabaseHolder.db.searchHistoryDao().insertAll(searchHistoryItem) Database.searchHistoryDao().insertAll(searchHistoryItem)
val maxHistorySize = 20 val maxHistorySize = 20
// delete the first watch history entry if the limit is reached // delete the first watch history entry if the limit is reached
val searchHistory = DatabaseHolder.db.searchHistoryDao().getAll() val searchHistory = Database.searchHistoryDao().getAll()
if (searchHistory.size > maxHistorySize) { if (searchHistory.size > maxHistorySize) {
DatabaseHolder.db.searchHistoryDao() Database.searchHistoryDao()
.delete(searchHistory.first()) .delete(searchHistory.first())
} }
}.start() }.start()

View File

@ -4,11 +4,9 @@ import android.content.Context
import androidx.room.Room import androidx.room.Room
import com.github.libretube.constants.DATABASE_NAME import com.github.libretube.constants.DATABASE_NAME
object DatabaseHolder { class DatabaseHolder {
lateinit var db: AppDatabase
fun initializeDatabase(context: Context) { fun initializeDatabase(context: Context) {
db = Room.databaseBuilder( Database = Room.databaseBuilder(
context, context,
AppDatabase::class.java, AppDatabase::class.java,
DATABASE_NAME DATABASE_NAME
@ -16,4 +14,8 @@ object DatabaseHolder {
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.build() .build()
} }
companion object {
lateinit var Database: AppDatabase
}
} }

View File

@ -7,7 +7,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.adapters.BackupOptionsAdapter import com.github.libretube.adapters.BackupOptionsAdapter
import com.github.libretube.databinding.DialogBackupBinding import com.github.libretube.databinding.DialogBackupBinding
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.await import com.github.libretube.extensions.await
import com.github.libretube.obj.BackupFile import com.github.libretube.obj.BackupFile
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -45,23 +45,23 @@ class BackupDialog(
Thread { Thread {
if (selected[0]) { if (selected[0]) {
backupFile.watchHistory = backupFile.watchHistory =
DatabaseHolder.db.watchHistoryDao().getAll() Database.watchHistoryDao().getAll()
} }
if (selected[1]) { if (selected[1]) {
backupFile.watchPositions = backupFile.watchPositions =
DatabaseHolder.db.watchPositionDao().getAll() Database.watchPositionDao().getAll()
} }
if (selected[2]) { if (selected[2]) {
backupFile.searchHistory = backupFile.searchHistory =
DatabaseHolder.db.searchHistoryDao().getAll() Database.searchHistoryDao().getAll()
} }
if (selected[3]) { if (selected[3]) {
backupFile.localSubscriptions = backupFile.localSubscriptions =
DatabaseHolder.db.localSubscriptionDao().getAll() Database.localSubscriptionDao().getAll()
} }
if (selected[4]) { if (selected[4]) {
backupFile.customInstances = backupFile.customInstances =
DatabaseHolder.db.customInstanceDao().getAll() Database.customInstanceDao().getAll()
} }
}.await() }.await()

View File

@ -6,7 +6,7 @@ import android.widget.Toast
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.DialogCustomInstanceBinding import com.github.libretube.databinding.DialogCustomInstanceBinding
import com.github.libretube.db.DatabaseHolder 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.util.ThemeHelper import com.github.libretube.util.ThemeHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -40,7 +40,7 @@ class CustomInstanceDialog : DialogFragment() {
URL(customInstance.frontendUrl).toURI() URL(customInstance.frontendUrl).toURI()
Thread { Thread {
DatabaseHolder.db.customInstanceDao().insertAll(customInstance) Database.customInstanceDao().insertAll(customInstance)
}.start() }.start()
activity?.recreate() activity?.recreate()

View File

@ -9,7 +9,7 @@ import com.github.libretube.constants.PIPED_FRONTEND_URL
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.constants.YOUTUBE_FRONTEND_URL import com.github.libretube.constants.YOUTUBE_FRONTEND_URL
import com.github.libretube.databinding.DialogShareBinding import com.github.libretube.databinding.DialogShareBinding
import com.github.libretube.db.DatabaseHolder 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.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
@ -82,7 +82,7 @@ class ShareDialog(
// get the api urls of the other custom instances // get the api urls of the other custom instances
var customInstances = listOf<CustomInstance>() var customInstances = listOf<CustomInstance>()
Thread { Thread {
customInstances = DatabaseHolder.db.customInstanceDao().getAll() customInstances = Database.customInstanceDao().getAll()
}.await() }.await()
// return the custom instance frontend url if available // return the custom instance frontend url if available

View File

@ -3,7 +3,7 @@ package com.github.libretube.extensions
import android.view.View import android.view.View
import android.view.ViewTreeObserver import android.view.ViewTreeObserver
import android.widget.LinearLayout import android.widget.LinearLayout
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder.Companion.Database
/** /**
* shows the already watched time under the video * shows the already watched time under the video
@ -14,7 +14,7 @@ fun View?.setWatchProgressLength(videoId: String, duration: Long) {
Thread { Thread {
try { try {
progress = DatabaseHolder.db.watchPositionDao().findById(videoId).position progress = Database.watchPositionDao().findById(videoId).position
} catch (e: Exception) { } catch (e: Exception) {
progress = null progress = null
} }

View File

@ -50,7 +50,7 @@ import com.github.libretube.databinding.DoubleTapOverlayBinding
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding 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 import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.dialogs.AddToPlaylistDialog import com.github.libretube.dialogs.AddToPlaylistDialog
import com.github.libretube.dialogs.DownloadDialog import com.github.libretube.dialogs.DownloadDialog
import com.github.libretube.dialogs.ShareDialog import com.github.libretube.dialogs.ShareDialog
@ -861,10 +861,11 @@ class PlayerFragment : BaseFragment() {
var position: Long? = null var position: Long? = null
Thread { Thread {
try { try {
position = DatabaseHolder.db.watchPositionDao().findById(videoId!!).position position = Database.watchPositionDao().findById(videoId!!).position
// position is almost the end of the video => don't seek, start from beginning // position is almost the end of the video => don't seek, start from beginning
if (position!! > streams.duration!! * 1000 * 0.9) position = null if (position!! > streams.duration!! * 1000 * 0.9) position = null
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace()
} }
}.await() }.await()
if (position != null) exoPlayer.seekTo(position!!) if (position != null) exoPlayer.seekTo(position!!)

View File

@ -15,7 +15,7 @@ import com.github.libretube.adapters.SearchHistoryAdapter
import com.github.libretube.adapters.SearchSuggestionsAdapter import com.github.libretube.adapters.SearchSuggestionsAdapter
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.databinding.FragmentSearchBinding import com.github.libretube.databinding.FragmentSearchBinding
import com.github.libretube.db.DatabaseHolder 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
@ -97,7 +97,7 @@ class SearchFragment : BaseFragment() {
private fun showHistory() { private fun showHistory() {
var historyList = listOf<String>() var historyList = listOf<String>()
Thread { Thread {
val history = DatabaseHolder.db.searchHistoryDao().getAll() val history = Database.searchHistoryDao().getAll()
historyList = history.map { it.query } historyList = history.map { it.query }
}.await() }.await()
if (historyList.isNotEmpty()) { if (historyList.isNotEmpty()) {

View File

@ -9,7 +9,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.adapters.WatchHistoryAdapter import com.github.libretube.adapters.WatchHistoryAdapter
import com.github.libretube.databinding.FragmentWatchHistoryBinding import com.github.libretube.databinding.FragmentWatchHistoryBinding
import com.github.libretube.db.DatabaseHolder 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
@ -32,7 +32,7 @@ class WatchHistoryFragment : BaseFragment() {
var watchHistory = listOf<WatchHistoryItem>() var watchHistory = listOf<WatchHistoryItem>()
Thread { Thread {
watchHistory = DatabaseHolder.db.watchHistoryDao().getAll() watchHistory = Database.watchHistoryDao().getAll()
}.await() }.await()
if (watchHistory.isEmpty()) return if (watchHistory.isEmpty()) return

View File

@ -5,7 +5,7 @@ import androidx.preference.Preference
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity import com.github.libretube.activities.SettingsActivity
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.views.MaterialPreferenceFragment import com.github.libretube.views.MaterialPreferenceFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -21,7 +21,7 @@ class HistorySettings : MaterialPreferenceFragment() {
val clearHistory = findPreference<Preference>(PreferenceKeys.CLEAR_SEARCH_HISTORY) val clearHistory = findPreference<Preference>(PreferenceKeys.CLEAR_SEARCH_HISTORY)
clearHistory?.setOnPreferenceClickListener { clearHistory?.setOnPreferenceClickListener {
showClearDialog(R.string.clear_history) { showClearDialog(R.string.clear_history) {
DatabaseHolder.db.searchHistoryDao().deleteAll() Database.searchHistoryDao().deleteAll()
} }
true true
} }
@ -30,7 +30,7 @@ class HistorySettings : MaterialPreferenceFragment() {
val clearWatchHistory = findPreference<Preference>(PreferenceKeys.CLEAR_WATCH_HISTORY) val clearWatchHistory = findPreference<Preference>(PreferenceKeys.CLEAR_WATCH_HISTORY)
clearWatchHistory?.setOnPreferenceClickListener { clearWatchHistory?.setOnPreferenceClickListener {
showClearDialog(R.string.clear_history) { showClearDialog(R.string.clear_history) {
DatabaseHolder.db.watchHistoryDao().deleteAll() Database.watchHistoryDao().deleteAll()
} }
true true
} }
@ -39,7 +39,7 @@ class HistorySettings : MaterialPreferenceFragment() {
val clearWatchPositions = findPreference<Preference>(PreferenceKeys.CLEAR_WATCH_POSITIONS) val clearWatchPositions = findPreference<Preference>(PreferenceKeys.CLEAR_WATCH_POSITIONS)
clearWatchPositions?.setOnPreferenceClickListener { clearWatchPositions?.setOnPreferenceClickListener {
showClearDialog(R.string.reset_watch_positions) { showClearDialog(R.string.reset_watch_positions) {
DatabaseHolder.db.watchPositionDao().deleteAll() Database.watchPositionDao().deleteAll()
} }
true true
} }

View File

@ -15,7 +15,7 @@ import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity import com.github.libretube.activities.SettingsActivity
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder 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.dialogs.CustomInstanceDialog import com.github.libretube.dialogs.CustomInstanceDialog
import com.github.libretube.dialogs.DeleteAccountDialog import com.github.libretube.dialogs.DeleteAccountDialog
@ -111,7 +111,7 @@ class InstanceSettings : MaterialPreferenceFragment() {
val clearCustomInstances = findPreference<Preference>(PreferenceKeys.CLEAR_CUSTOM_INSTANCES) val clearCustomInstances = findPreference<Preference>(PreferenceKeys.CLEAR_CUSTOM_INSTANCES)
clearCustomInstances?.setOnPreferenceClickListener { clearCustomInstances?.setOnPreferenceClickListener {
Thread { Thread {
DatabaseHolder.db.customInstanceDao().deleteAll() Database.customInstanceDao().deleteAll()
}.await() }.await()
activity?.recreate() activity?.recreate()
true true
@ -158,7 +158,7 @@ class InstanceSettings : MaterialPreferenceFragment() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
var customInstances = listOf<CustomInstance>() var customInstances = listOf<CustomInstance>()
Thread { Thread {
customInstances = DatabaseHolder.db.customInstanceDao().getAll() customInstances = Database.customInstanceDao().getAll()
}.await() }.await()
val instanceNames = arrayListOf<String>() val instanceNames = arrayListOf<String>()

View File

@ -5,7 +5,7 @@ import android.net.Uri
import androidx.core.content.edit import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.query import com.github.libretube.extensions.query
import com.github.libretube.obj.BackupFile import com.github.libretube.obj.BackupFile
import java.io.FileInputStream import java.io.FileInputStream
@ -101,19 +101,19 @@ class BackupHelper(private val context: Context) {
val backupFile = mapper.readValue(json, BackupFile::class.java) val backupFile = mapper.readValue(json, BackupFile::class.java)
query { query {
DatabaseHolder.db.watchHistoryDao().insertAll( Database.watchHistoryDao().insertAll(
*backupFile.watchHistory?.toTypedArray().orEmpty() *backupFile.watchHistory?.toTypedArray().orEmpty()
) )
DatabaseHolder.db.searchHistoryDao().insertAll( Database.searchHistoryDao().insertAll(
*backupFile.searchHistory?.toTypedArray().orEmpty() *backupFile.searchHistory?.toTypedArray().orEmpty()
) )
DatabaseHolder.db.watchPositionDao().insertAll( Database.watchPositionDao().insertAll(
*backupFile.watchPositions?.toTypedArray().orEmpty() *backupFile.watchPositions?.toTypedArray().orEmpty()
) )
DatabaseHolder.db.localSubscriptionDao().insertAll( Database.localSubscriptionDao().insertAll(
*backupFile.localSubscriptions?.toTypedArray().orEmpty() *backupFile.localSubscriptions?.toTypedArray().orEmpty()
) )
DatabaseHolder.db.customInstanceDao().insertAll( Database.customInstanceDao().insertAll(
*backupFile.customInstances?.toTypedArray().orEmpty() *backupFile.customInstances?.toTypedArray().orEmpty()
) )
} }