diff --git a/app/src/main/java/com/github/libretube/LibreTubeApp.kt b/app/src/main/java/com/github/libretube/LibreTubeApp.kt index 281de0cec..77212a998 100644 --- a/app/src/main/java/com/github/libretube/LibreTubeApp.kt +++ b/app/src/main/java/com/github/libretube/LibreTubeApp.kt @@ -34,7 +34,7 @@ class LibreTubeApp : Application() { /** * 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 diff --git a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt index 4db969d87..eb046336e 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchHistoryAdapter.kt @@ -5,7 +5,7 @@ import android.view.ViewGroup import androidx.appcompat.widget.SearchView import androidx.recyclerview.widget.RecyclerView 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 class SearchHistoryAdapter( @@ -32,7 +32,7 @@ class SearchHistoryAdapter( deleteHistory.setOnClickListener { historyList -= historyQuery Thread { - DatabaseHolder.db.searchHistoryDao().delete( + Database.searchHistoryDao().delete( SearchHistoryItem(query = historyQuery) ) }.start() diff --git a/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt b/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt index 8360ab3f9..510b1362a 100644 --- a/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt +++ b/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt @@ -1,7 +1,7 @@ package com.github.libretube.api 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.extensions.TAG import com.github.libretube.extensions.await @@ -27,7 +27,7 @@ object SubscriptionHelper { } } else { Thread { - DatabaseHolder.db.localSubscriptionDao().insertAll( + Database.localSubscriptionDao().insertAll( LocalSubscription(channelId) ) }.start() @@ -48,7 +48,7 @@ object SubscriptionHelper { } } else { Thread { - DatabaseHolder.db.localSubscriptionDao().delete( + Database.localSubscriptionDao().delete( LocalSubscription(channelId) ) }.start() @@ -70,7 +70,7 @@ object SubscriptionHelper { } else { var isSubscribed = false Thread { - isSubscribed = DatabaseHolder.db.localSubscriptionDao().includes(channelId) + isSubscribed = Database.localSubscriptionDao().includes(channelId) }.await() return isSubscribed } @@ -94,7 +94,7 @@ object SubscriptionHelper { newLocalSubscriptions += LocalSubscription(channelId = it) } Thread { - DatabaseHolder.db.localSubscriptionDao().insertAll( + Database.localSubscriptionDao().insertAll( *newChannels.map { LocalSubscription(it) }.toTypedArray() ) }.start() @@ -104,7 +104,7 @@ object SubscriptionHelper { fun getLocalSubscriptions(): List { var localSubscriptions = listOf() Thread { - localSubscriptions = DatabaseHolder.db.localSubscriptionDao().getAll() + localSubscriptions = Database.localSubscriptionDao().getAll() }.await() return localSubscriptions } diff --git a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt index f5015b7a5..919177c4b 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt @@ -1,6 +1,7 @@ package com.github.libretube.db 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.WatchHistoryItem import com.github.libretube.db.obj.WatchPosition @@ -21,15 +22,15 @@ object DatabaseHelper { streams.duration ) Thread { - DatabaseHolder.db.watchHistoryDao().insertAll(watchHistoryItem) + Database.watchHistoryDao().insertAll(watchHistoryItem) val maxHistorySize = PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "unlimited") if (maxHistorySize == "unlimited") return@Thread // 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()) { - DatabaseHolder.db.watchHistoryDao() + Database.watchHistoryDao() .delete(watchHistory.first()) } }.start() @@ -37,8 +38,8 @@ object DatabaseHelper { fun removeFromWatchHistory(index: Int) { Thread { - DatabaseHolder.db.watchHistoryDao().delete( - DatabaseHolder.db.watchHistoryDao().getAll()[index] + Database.watchHistoryDao().delete( + Database.watchHistoryDao().getAll()[index] ) }.start() } @@ -49,27 +50,27 @@ object DatabaseHelper { position ) Thread { - DatabaseHolder.db.watchPositionDao().insertAll(watchPosition) + Database.watchPositionDao().insertAll(watchPosition) }.start() } fun removeWatchPosition(videoId: String) { Thread { - DatabaseHolder.db.watchPositionDao().delete( - DatabaseHolder.db.watchPositionDao().findById(videoId) + Database.watchPositionDao().delete( + Database.watchPositionDao().findById(videoId) ) }.start() } fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) { Thread { - DatabaseHolder.db.searchHistoryDao().insertAll(searchHistoryItem) + Database.searchHistoryDao().insertAll(searchHistoryItem) val maxHistorySize = 20 // 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) { - DatabaseHolder.db.searchHistoryDao() + Database.searchHistoryDao() .delete(searchHistory.first()) } }.start() diff --git a/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt b/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt index 16e47e1f3..8164bdd09 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt @@ -4,11 +4,9 @@ import android.content.Context import androidx.room.Room import com.github.libretube.constants.DATABASE_NAME -object DatabaseHolder { - lateinit var db: AppDatabase - +class DatabaseHolder { fun initializeDatabase(context: Context) { - db = Room.databaseBuilder( + Database = Room.databaseBuilder( context, AppDatabase::class.java, DATABASE_NAME @@ -16,4 +14,8 @@ object DatabaseHolder { .fallbackToDestructiveMigration() .build() } + + companion object { + lateinit var Database: AppDatabase + } } diff --git a/app/src/main/java/com/github/libretube/dialogs/BackupDialog.kt b/app/src/main/java/com/github/libretube/dialogs/BackupDialog.kt index 0a6ffad96..62148198f 100644 --- a/app/src/main/java/com/github/libretube/dialogs/BackupDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/BackupDialog.kt @@ -7,7 +7,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import com.github.libretube.R import com.github.libretube.adapters.BackupOptionsAdapter 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.obj.BackupFile import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -45,23 +45,23 @@ class BackupDialog( Thread { if (selected[0]) { backupFile.watchHistory = - DatabaseHolder.db.watchHistoryDao().getAll() + Database.watchHistoryDao().getAll() } if (selected[1]) { backupFile.watchPositions = - DatabaseHolder.db.watchPositionDao().getAll() + Database.watchPositionDao().getAll() } if (selected[2]) { backupFile.searchHistory = - DatabaseHolder.db.searchHistoryDao().getAll() + Database.searchHistoryDao().getAll() } if (selected[3]) { backupFile.localSubscriptions = - DatabaseHolder.db.localSubscriptionDao().getAll() + Database.localSubscriptionDao().getAll() } if (selected[4]) { backupFile.customInstances = - DatabaseHolder.db.customInstanceDao().getAll() + Database.customInstanceDao().getAll() } }.await() 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 05d509f9e..a1e246dc6 100644 --- a/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/CustomInstanceDialog.kt @@ -6,7 +6,7 @@ import android.widget.Toast import androidx.fragment.app.DialogFragment import com.github.libretube.R 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.util.ThemeHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -40,7 +40,7 @@ class CustomInstanceDialog : DialogFragment() { URL(customInstance.frontendUrl).toURI() Thread { - DatabaseHolder.db.customInstanceDao().insertAll(customInstance) + Database.customInstanceDao().insertAll(customInstance) }.start() activity?.recreate() 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 fd3c78d2b..4118e0c6f 100644 --- a/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/ShareDialog.kt @@ -9,7 +9,7 @@ import com.github.libretube.constants.PIPED_FRONTEND_URL import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.YOUTUBE_FRONTEND_URL 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.extensions.await import com.github.libretube.util.PreferenceHelper @@ -82,7 +82,7 @@ class ShareDialog( // get the api urls of the other custom instances var customInstances = listOf() Thread { - customInstances = DatabaseHolder.db.customInstanceDao().getAll() + customInstances = Database.customInstanceDao().getAll() }.await() // return the custom instance frontend url if available diff --git a/app/src/main/java/com/github/libretube/extensions/SetWatchProgressLength.kt b/app/src/main/java/com/github/libretube/extensions/SetWatchProgressLength.kt index 99add5759..0bd7d4f54 100644 --- a/app/src/main/java/com/github/libretube/extensions/SetWatchProgressLength.kt +++ b/app/src/main/java/com/github/libretube/extensions/SetWatchProgressLength.kt @@ -3,7 +3,7 @@ package com.github.libretube.extensions import android.view.View import android.view.ViewTreeObserver 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 @@ -14,7 +14,7 @@ fun View?.setWatchProgressLength(videoId: String, duration: Long) { Thread { try { - progress = DatabaseHolder.db.watchPositionDao().findById(videoId).position + progress = Database.watchPositionDao().findById(videoId).position } catch (e: Exception) { progress = null } diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 9674680fb..1c534c8c6 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -50,7 +50,7 @@ import com.github.libretube.databinding.DoubleTapOverlayBinding import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding import com.github.libretube.databinding.FragmentPlayerBinding 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.DownloadDialog import com.github.libretube.dialogs.ShareDialog @@ -861,10 +861,11 @@ class PlayerFragment : BaseFragment() { var position: Long? = null Thread { 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 if (position!! > streams.duration!! * 1000 * 0.9) position = null } catch (e: Exception) { + e.printStackTrace() } }.await() if (position != null) exoPlayer.seekTo(position!!) diff --git a/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt b/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt index 14137b616..14d4356a7 100644 --- a/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/SearchFragment.kt @@ -15,7 +15,7 @@ import com.github.libretube.adapters.SearchHistoryAdapter import com.github.libretube.adapters.SearchSuggestionsAdapter import com.github.libretube.api.RetrofitInstance 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.TAG import com.github.libretube.extensions.await @@ -97,7 +97,7 @@ class SearchFragment : BaseFragment() { private fun showHistory() { var historyList = listOf() Thread { - val history = DatabaseHolder.db.searchHistoryDao().getAll() + val history = Database.searchHistoryDao().getAll() historyList = history.map { it.query } }.await() if (historyList.isNotEmpty()) { diff --git a/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt b/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt index 4ef54c47c..967a565a9 100644 --- a/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt @@ -9,7 +9,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.github.libretube.adapters.WatchHistoryAdapter 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.extensions.BaseFragment import com.github.libretube.extensions.await @@ -32,7 +32,7 @@ class WatchHistoryFragment : BaseFragment() { var watchHistory = listOf() Thread { - watchHistory = DatabaseHolder.db.watchHistoryDao().getAll() + watchHistory = Database.watchHistoryDao().getAll() }.await() if (watchHistory.isEmpty()) return diff --git a/app/src/main/java/com/github/libretube/preferences/HistorySettings.kt b/app/src/main/java/com/github/libretube/preferences/HistorySettings.kt index ad3d0895b..f89a827b4 100644 --- a/app/src/main/java/com/github/libretube/preferences/HistorySettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/HistorySettings.kt @@ -5,7 +5,7 @@ import androidx.preference.Preference import com.github.libretube.R import com.github.libretube.activities.SettingsActivity 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.google.android.material.dialog.MaterialAlertDialogBuilder @@ -21,7 +21,7 @@ class HistorySettings : MaterialPreferenceFragment() { val clearHistory = findPreference(PreferenceKeys.CLEAR_SEARCH_HISTORY) clearHistory?.setOnPreferenceClickListener { showClearDialog(R.string.clear_history) { - DatabaseHolder.db.searchHistoryDao().deleteAll() + Database.searchHistoryDao().deleteAll() } true } @@ -30,7 +30,7 @@ class HistorySettings : MaterialPreferenceFragment() { val clearWatchHistory = findPreference(PreferenceKeys.CLEAR_WATCH_HISTORY) clearWatchHistory?.setOnPreferenceClickListener { showClearDialog(R.string.clear_history) { - DatabaseHolder.db.watchHistoryDao().deleteAll() + Database.watchHistoryDao().deleteAll() } true } @@ -39,7 +39,7 @@ class HistorySettings : MaterialPreferenceFragment() { val clearWatchPositions = findPreference(PreferenceKeys.CLEAR_WATCH_POSITIONS) clearWatchPositions?.setOnPreferenceClickListener { showClearDialog(R.string.reset_watch_positions) { - DatabaseHolder.db.watchPositionDao().deleteAll() + Database.watchPositionDao().deleteAll() } true } 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 8412532b5..b90a24854 100644 --- a/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/InstanceSettings.kt @@ -15,7 +15,7 @@ import com.github.libretube.R import com.github.libretube.activities.SettingsActivity import com.github.libretube.api.RetrofitInstance 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.dialogs.CustomInstanceDialog import com.github.libretube.dialogs.DeleteAccountDialog @@ -111,7 +111,7 @@ class InstanceSettings : MaterialPreferenceFragment() { val clearCustomInstances = findPreference(PreferenceKeys.CLEAR_CUSTOM_INSTANCES) clearCustomInstances?.setOnPreferenceClickListener { Thread { - DatabaseHolder.db.customInstanceDao().deleteAll() + Database.customInstanceDao().deleteAll() }.await() activity?.recreate() true @@ -158,7 +158,7 @@ class InstanceSettings : MaterialPreferenceFragment() { lifecycleScope.launchWhenCreated { var customInstances = listOf() Thread { - customInstances = DatabaseHolder.db.customInstanceDao().getAll() + customInstances = Database.customInstanceDao().getAll() }.await() val instanceNames = arrayListOf() diff --git a/app/src/main/java/com/github/libretube/util/BackupHelper.kt b/app/src/main/java/com/github/libretube/util/BackupHelper.kt index 9b002297d..899072cf2 100644 --- a/app/src/main/java/com/github/libretube/util/BackupHelper.kt +++ b/app/src/main/java/com/github/libretube/util/BackupHelper.kt @@ -5,7 +5,7 @@ import android.net.Uri import androidx.core.content.edit import androidx.preference.PreferenceManager 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.obj.BackupFile import java.io.FileInputStream @@ -101,19 +101,19 @@ class BackupHelper(private val context: Context) { val backupFile = mapper.readValue(json, BackupFile::class.java) query { - DatabaseHolder.db.watchHistoryDao().insertAll( + Database.watchHistoryDao().insertAll( *backupFile.watchHistory?.toTypedArray().orEmpty() ) - DatabaseHolder.db.searchHistoryDao().insertAll( + Database.searchHistoryDao().insertAll( *backupFile.searchHistory?.toTypedArray().orEmpty() ) - DatabaseHolder.db.watchPositionDao().insertAll( + Database.watchPositionDao().insertAll( *backupFile.watchPositions?.toTypedArray().orEmpty() ) - DatabaseHolder.db.localSubscriptionDao().insertAll( + Database.localSubscriptionDao().insertAll( *backupFile.localSubscriptions?.toTypedArray().orEmpty() ) - DatabaseHolder.db.customInstanceDao().insertAll( + Database.customInstanceDao().insertAll( *backupFile.customInstances?.toTypedArray().orEmpty() ) }