initial database commit

This commit is contained in:
Bnyro 2022-08-13 20:04:07 +02:00
parent 11fc6fd1f2
commit 1268dce7bd
11 changed files with 110 additions and 20 deletions

View File

@ -106,6 +106,8 @@ dependencies {
implementation libs.lifecycle.viewmodel implementation libs.lifecycle.viewmodel
implementation libs.lifecycle.runtime implementation libs.lifecycle.runtime
implementation libs.lifecycle.livedata implementation libs.lifecycle.livedata
implementation libs.room
} }
static def getUnixTime() { static def getUnixTime() {

View File

@ -49,3 +49,8 @@ const val DOWNLOAD_SUCCESS_NOTIFICATION_ID = 5
const val DOWNLOAD_CHANNEL_ID = "download_service" const val DOWNLOAD_CHANNEL_ID = "download_service"
const val BACKGROUND_CHANNEL_ID = "background_mode" const val BACKGROUND_CHANNEL_ID = "background_mode"
const val PUSH_CHANNEL_ID = "notification_worker" const val PUSH_CHANNEL_ID = "notification_worker"
/**
* Database
*/
const val DATABASE_NAME = "LibreTubeDatabase"

View File

@ -8,6 +8,7 @@ import android.os.Build
import android.os.StrictMode import android.os.StrictMode
import android.os.StrictMode.VmPolicy import android.os.StrictMode.VmPolicy
import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingPeriodicWorkPolicy
import com.github.libretube.database.DatabaseHolder
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.ExceptionHandler import com.github.libretube.util.ExceptionHandler
@ -19,15 +20,20 @@ class MyApp : Application() {
super.onCreate() super.onCreate()
/** /**
* initialize the needed [NotificationChannel]s for DownloadService and BackgroundMode * Initialize the needed [NotificationChannel]s for DownloadService and BackgroundMode
*/ */
initializeNotificationChannels() initializeNotificationChannels()
/** /**
* set the applicationContext as context for the [PreferenceHelper] * Set the applicationContext as context for the [PreferenceHelper]
*/ */
PreferenceHelper.setContext(applicationContext) PreferenceHelper.setContext(applicationContext)
/**
* Initialize the [DatabaseHolder]
*/
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
*/ */
@ -35,12 +41,12 @@ class MyApp : Application() {
StrictMode.setVmPolicy(builder.build()) StrictMode.setVmPolicy(builder.build())
/** /**
* set the api and the auth api url * Set the api and the auth api url
*/ */
setRetrofitApiUrls() setRetrofitApiUrls()
/** /**
* initialize the notification listener in the background * Initialize the notification listener in the background
*/ */
NotificationHelper.enqueueWork(this, ExistingPeriodicWorkPolicy.KEEP) NotificationHelper.enqueueWork(this, ExistingPeriodicWorkPolicy.KEEP)
@ -52,13 +58,13 @@ class MyApp : Application() {
Thread.setDefaultUncaughtExceptionHandler(exceptionHandler) Thread.setDefaultUncaughtExceptionHandler(exceptionHandler)
/** /**
* legacy preference file migration * Legacy preference file migration
*/ */
prefFileMigration() prefFileMigration()
} }
/** /**
* set the api urls needed for the [RetrofitInstance] * Set the api urls needed for the [RetrofitInstance]
*/ */
private fun setRetrofitApiUrls() { private fun setRetrofitApiUrls() {
RetrofitInstance.url = RetrofitInstance.url =

View File

@ -0,0 +1,10 @@
package com.github.libretube.database
import androidx.room.Database
import androidx.room.RoomDatabase
import com.github.libretube.obj.WatchHistoryItem
@Database(entities = [WatchHistoryItem::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun watchHistoryDao(): WatchHistoryDao
}

View File

@ -0,0 +1,21 @@
package com.github.libretube.database
import com.github.libretube.obj.Streams
import com.github.libretube.obj.WatchHistoryItem
import com.github.libretube.util.toID
object DatabaseHelper {
fun addToWatchHistory(videoId: String, streams: Streams) {
val watchHistoryItem = WatchHistoryItem(
videoId,
streams.title,
streams.uploadDate,
streams.uploader,
streams.uploaderUrl.toID(),
streams.uploaderAvatar,
streams.thumbnailUrl,
streams.duration
)
DatabaseHolder.database.watchHistoryDao().insertAll(watchHistoryItem)
}
}

View File

@ -0,0 +1,17 @@
package com.github.libretube.database
import android.content.Context
import androidx.room.Room
import com.github.libretube.DATABASE_NAME
object DatabaseHolder {
lateinit var database: AppDatabase
fun initializeDatabase(context: Context) {
database = Room.databaseBuilder(
context,
AppDatabase::class.java,
DATABASE_NAME
).build()
}
}

View File

@ -0,0 +1,22 @@
package com.github.libretube.database
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import com.github.libretube.obj.WatchHistoryItem
@Dao
interface WatchHistoryDao {
@Query("SELECT * FROM watchHistoryItem")
fun getAll(): List<WatchHistoryItem>
@Query("SELECT * FROM watchHistoryItem WHERE videoId LIKE :videoId LIMIT 1")
fun findById(videoId: String): WatchHistoryItem
@Insert
fun insertAll(vararg watchHistoryItems: WatchHistoryItem)
@Delete
fun delete(watchHistoryItem: WatchHistoryItem)
}

View File

@ -37,6 +37,7 @@ import com.github.libretube.activities.MainActivity
import com.github.libretube.adapters.ChaptersAdapter import com.github.libretube.adapters.ChaptersAdapter
import com.github.libretube.adapters.CommentsAdapter import com.github.libretube.adapters.CommentsAdapter
import com.github.libretube.adapters.TrendingAdapter import com.github.libretube.adapters.TrendingAdapter
import com.github.libretube.database.DatabaseHelper
import com.github.libretube.databinding.DoubleTapOverlayBinding 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
@ -88,7 +89,6 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSource
import com.google.android.exoplayer2.util.RepeatModeUtil import com.google.android.exoplayer2.util.RepeatModeUtil
import com.google.android.exoplayer2.video.VideoSize import com.google.android.exoplayer2.video.VideoSize
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.android.synthetic.main.bottom_sheet.repeatMode
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -878,7 +878,7 @@ class PlayerFragment : BaseFragment() {
if (!relatedStreamsEnabled) toggleComments() if (!relatedStreamsEnabled) toggleComments()
// prepare for autoplay // prepare for autoplay
if (autoplayEnabled) setNextStream() if (autoplayEnabled) setNextStream()
if (watchHistoryEnabled) PreferenceHelper.addToWatchHistory(videoId!!, streams) if (watchHistoryEnabled) DatabaseHelper.addToWatchHistory(videoId!!, streams)
} }
} }
} }

View File

@ -8,9 +8,9 @@ import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager 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.database.DatabaseHolder
import com.github.libretube.databinding.FragmentWatchHistoryBinding import com.github.libretube.databinding.FragmentWatchHistoryBinding
import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.BaseFragment
import com.github.libretube.preferences.PreferenceHelper
class WatchHistoryFragment : BaseFragment() { class WatchHistoryFragment : BaseFragment() {
private val TAG = "WatchHistoryFragment" private val TAG = "WatchHistoryFragment"
@ -28,7 +28,7 @@ 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)
val watchHistory = PreferenceHelper.getWatchHistory() val watchHistory = DatabaseHolder.database.watchHistoryDao().getAll()
if (watchHistory.isEmpty()) return if (watchHistory.isEmpty()) return
@ -39,7 +39,7 @@ class WatchHistoryFragment : BaseFragment() {
} }
val watchHistoryAdapter = WatchHistoryAdapter( val watchHistoryAdapter = WatchHistoryAdapter(
watchHistory, watchHistory.toMutableList(),
childFragmentManager childFragmentManager
) )

View File

@ -1,12 +1,17 @@
package com.github.libretube.obj package com.github.libretube.obj
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "watchHistoryItem")
data class WatchHistoryItem( data class WatchHistoryItem(
val videoId: String? = null, @PrimaryKey val videoId: String? = null,
val title: String? = null, @ColumnInfo val title: String? = null,
val uploadDate: String? = null, @ColumnInfo val uploadDate: String? = null,
val uploader: String? = null, @ColumnInfo val uploader: String? = null,
val uploaderUrl: String? = null, @ColumnInfo val uploaderUrl: String? = null,
val uploaderAvatar: String? = null, @ColumnInfo val uploaderAvatar: String? = null,
val thumbnailUrl: String? = null, @ColumnInfo val thumbnailUrl: String? = null,
val duration: Long? = null @ColumnInfo val duration: Long? = null
) )

View File

@ -20,6 +20,7 @@ cronetEmbedded = "101.4951.41"
cronetOkHttp = "0.1.0" cronetOkHttp = "0.1.0"
coil = "2.1.0" coil = "2.1.0"
leakcanary = "2.8.1" leakcanary = "2.8.1"
room = "2.4.3"
[libraries] [libraries]
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
@ -48,4 +49,5 @@ coil = { group = "io.coil-kt", name = "coil", version.ref="coil" }
square-leakcanary = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary" } square-leakcanary = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary" }
lifecycle-viewmodel = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycle" } lifecycle-viewmodel = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycle" }
lifecycle-runtime = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycle" } lifecycle-runtime = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycle" }
lifecycle-livedata = { group = "androidx.lifecycle", name = "lifecycle-livedata-ktx", version.ref = "lifecycle" } lifecycle-livedata = { group = "androidx.lifecycle", name = "lifecycle-livedata-ktx", version.ref = "lifecycle" }
room = { group = "androidx.room", name="room-runtime", version.ref = "room" }