migrate search history

This commit is contained in:
Bnyro 2022-08-13 22:37:13 +02:00
parent 4244efc448
commit 7c95f5f252
14 changed files with 86 additions and 70 deletions

View File

@ -4,8 +4,9 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.database.DatabaseHolder
import com.github.libretube.databinding.SearchhistoryRowBinding
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.obj.SearchHistoryItem
class SearchHistoryAdapter(
private var historyList: List<String>,
@ -29,8 +30,12 @@ class SearchHistoryAdapter(
historyText.text = historyQuery
deleteHistory.setOnClickListener {
historyList = historyList - historyQuery
PreferenceHelper.removeFromSearchHistory(historyQuery)
historyList -= historyQuery
Thread {
DatabaseHolder.database.searchHistoryDao().delete(
SearchHistoryItem(query = historyQuery)
)
}.start()
notifyDataSetChanged()
}

View File

@ -3,6 +3,7 @@ package com.github.libretube.database
import androidx.room.Database
import androidx.room.RoomDatabase
import com.github.libretube.obj.CustomInstance
import com.github.libretube.obj.SearchHistoryItem
import com.github.libretube.obj.WatchHistoryItem
import com.github.libretube.obj.WatchPosition
@ -10,9 +11,10 @@ import com.github.libretube.obj.WatchPosition
entities = [
WatchHistoryItem::class,
WatchPosition::class,
SearchHistoryItem::class,
CustomInstance::class
],
version = 3
version = 5
)
abstract class AppDatabase : RoomDatabase() {
/**
@ -25,6 +27,11 @@ abstract class AppDatabase : RoomDatabase() {
*/
abstract fun watchPositionDao(): WatchPositionDao
/**
* Search History
*/
abstract fun searchHistoryDao(): SearchHistoryDao
/**
* Custom Instances
*/

View File

@ -0,0 +1,20 @@
package com.github.libretube.database
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.github.libretube.obj.SearchHistoryItem
@Dao
interface SearchHistoryDao {
@Query("SELECT * FROM searchHistoryItem")
fun getAll(): List<SearchHistoryItem>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(vararg searchHistoryItem: SearchHistoryItem)
@Delete
fun delete(searchHistoryItem: SearchHistoryItem)
}

View File

@ -8,6 +8,7 @@ import com.github.libretube.PIPED_FRONTEND_URL
import com.github.libretube.R
import com.github.libretube.YOUTUBE_FRONTEND_URL
import com.github.libretube.database.DatabaseHolder
import com.github.libretube.extensions.await
import com.github.libretube.obj.CustomInstance
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
@ -76,10 +77,7 @@ class ShareDialog(
var customInstances = listOf<CustomInstance>()
Thread {
customInstances = DatabaseHolder.database.customInstanceDao().getAll()
}.apply {
start()
join()
}
}.await()
// return the custom instance frontend url if available
customInstances.forEach { instance ->

View File

@ -0,0 +1,8 @@
package com.github.libretube.extensions
fun Thread.await() {
this.apply {
start()
join()
}
}

View File

@ -4,6 +4,7 @@ import android.view.View
import android.view.ViewTreeObserver
import android.widget.LinearLayout
import com.github.libretube.database.DatabaseHolder
import com.github.libretube.extensions.await
import com.github.libretube.obj.WatchPosition
/**
@ -14,11 +15,9 @@ fun View?.setWatchProgressLength(videoId: String, duration: Long) {
var positions = listOf<WatchPosition>()
var newWidth: Long? = null
val thread = Thread {
Thread {
positions = DatabaseHolder.database.watchPositionDao().getAll()
}
thread.start()
thread.join()
}.await()
view.getViewTreeObserver()
.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {

View File

@ -46,6 +46,7 @@ import com.github.libretube.dialogs.AddToPlaylistDialog
import com.github.libretube.dialogs.DownloadDialog
import com.github.libretube.dialogs.ShareDialog
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.await
import com.github.libretube.interfaces.DoubleTapInterface
import com.github.libretube.interfaces.PlayerOptionsInterface
import com.github.libretube.obj.ChapterSegment
@ -936,11 +937,9 @@ class PlayerFragment : BaseFragment() {
private fun seekToWatchPosition() {
// seek to saved watch position if available
var watchPositions = listOf<WatchPosition>()
val thread = Thread {
Thread {
watchPositions = DatabaseHolder.database.watchPositionDao().getAll()
}
thread.start()
thread.join()
}.await()
var position: Long? = null
watchPositions.forEach {
if (it.videoId == videoId &&

View File

@ -11,10 +11,11 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.activities.MainActivity
import com.github.libretube.adapters.SearchHistoryAdapter
import com.github.libretube.adapters.SearchSuggestionsAdapter
import com.github.libretube.database.DatabaseHolder
import com.github.libretube.databinding.FragmentSearchBinding
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.await
import com.github.libretube.models.SearchViewModel
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.RetrofitInstance
import retrofit2.HttpException
import java.io.IOException
@ -89,7 +90,11 @@ class SearchFragment() : BaseFragment() {
}
private fun showHistory() {
val historyList = PreferenceHelper.getSearchHistory()
var historyList = listOf<String>()
Thread {
val history = DatabaseHolder.database.searchHistoryDao().getAll()
historyList = history.map { it.query!! }
}.await()
if (historyList.isNotEmpty()) {
binding.suggestionsRecycler.adapter =
SearchHistoryAdapter(

View File

@ -9,8 +9,11 @@ import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.R
import com.github.libretube.adapters.SearchAdapter
import com.github.libretube.database.DatabaseHolder
import com.github.libretube.databinding.FragmentSearchResultBinding
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.await
import com.github.libretube.obj.SearchHistoryItem
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.RetrofitInstance
@ -132,7 +135,13 @@ class SearchResultFragment : BaseFragment() {
val searchHistoryEnabled =
PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_HISTORY_TOGGLE, true)
if (searchHistoryEnabled && query != "") {
PreferenceHelper.saveToSearchHistory(query)
Thread {
DatabaseHolder.database.searchHistoryDao().insertAll(
SearchHistoryItem(
query = query
)
)
}.await()
}
}
}

View File

@ -11,6 +11,7 @@ import com.github.libretube.adapters.WatchHistoryAdapter
import com.github.libretube.database.DatabaseHolder
import com.github.libretube.databinding.FragmentWatchHistoryBinding
import com.github.libretube.extensions.BaseFragment
import com.github.libretube.extensions.await
import com.github.libretube.obj.WatchHistoryItem
class WatchHistoryFragment : BaseFragment() {
@ -31,11 +32,9 @@ class WatchHistoryFragment : BaseFragment() {
var watchHistory = listOf<WatchHistoryItem>()
val thread = Thread {
Thread {
watchHistory = DatabaseHolder.database.watchHistoryDao().getAll()
}
thread.start()
thread.join()
}.await()
if (watchHistory.isEmpty()) return

View File

@ -0,0 +1,9 @@
package com.github.libretube.obj
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "searchHistoryItem")
data class SearchHistoryItem(
@PrimaryKey val query: String = ""
)

View File

@ -70,45 +70,6 @@ object PreferenceHelper {
authEditor.putString(PreferenceKeys.USERNAME, newValue).apply()
}
fun getSearchHistory(): List<String> {
return try {
val json = settings.getString("search_history", "")!!
val type = object : TypeReference<List<String>>() {}
return mapper.readValue(json, type)
} catch (e: Exception) {
emptyList()
}
}
fun saveToSearchHistory(query: String) {
val historyList = getSearchHistory().toMutableList()
if ((historyList.contains(query))) {
// remove from history list if already contained
historyList -= query
}
// append new query to history
historyList.add(0, query)
if (historyList.size > 10) {
historyList.removeAt(historyList.size - 1)
}
updateSearchHistory(historyList)
}
fun removeFromSearchHistory(query: String) {
val historyList = getSearchHistory().toMutableList()
historyList -= query
updateSearchHistory(historyList)
}
private fun updateSearchHistory(historyList: List<String>) {
val json = mapper.writeValueAsString(historyList)
editor.putString("search_history", json).apply()
}
fun setLatestVideoId(videoId: String) {
editor.putString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId)
}

View File

@ -2,6 +2,7 @@ package com.github.libretube.update
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.GITHUB_API_URL
import com.github.libretube.extensions.await
import java.net.URL
object UpdateChecker {
@ -15,10 +16,7 @@ object UpdateChecker {
versionInfo = getUpdateInfo()
} catch (e: Exception) {
}
}
thread.start()
// wait for the thread to finish
thread.join()
}.await()
// return the information about the latest version
return versionInfo

View File

@ -10,6 +10,7 @@ import android.support.v4.media.session.MediaSessionCompat
import com.github.libretube.BACKGROUND_CHANNEL_ID
import com.github.libretube.PLAYER_NOTIFICATION_ID
import com.github.libretube.activities.MainActivity
import com.github.libretube.extensions.await
import com.github.libretube.obj.Streams
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.Player
@ -87,7 +88,7 @@ class NowPlayingNotification(
/**
* running on a new thread to prevent a NetworkMainThreadException
*/
val thread = Thread {
Thread {
try {
/**
* try to GET the thumbnail from the URL
@ -97,9 +98,7 @@ class NowPlayingNotification(
} catch (ex: java.lang.Exception) {
ex.printStackTrace()
}
}
thread.start()
thread.join()
}.await()
/**
* returns the scaled bitmap if it got fetched successfully
*/