improve watch history

This commit is contained in:
Bnyro 2022-07-29 08:47:37 +02:00
parent 4070b9893c
commit 7fc855aaef
10 changed files with 189 additions and 88 deletions

View File

@ -5,9 +5,10 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.VideoRowBinding
import com.github.libretube.databinding.WatchHistoryRowBinding
import com.github.libretube.dialogs.VideoOptionsDialog
import com.github.libretube.obj.WatchHistoryItem
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.setWatchProgressLength
@ -19,15 +20,9 @@ class WatchHistoryAdapter(
RecyclerView.Adapter<WatchHistoryViewHolder>() {
private val TAG = "WatchHistoryAdapter"
fun clear() {
val size = watchHistory.size
watchHistory.clear()
notifyItemRangeRemoved(0, size)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WatchHistoryViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = VideoRowBinding.inflate(layoutInflater, parent, false)
val binding = WatchHistoryRowBinding.inflate(layoutInflater, parent, false)
return WatchHistoryViewHolder(binding)
}
@ -45,6 +40,12 @@ class WatchHistoryAdapter(
NavigationHelper.navigateChannel(root.context, video.uploaderUrl)
}
deleteBTN.setOnClickListener {
PreferenceHelper.removeFromWatchHistory(video.videoId!!)
watchHistory.removeAt(position)
notifyItemRemoved(position)
}
root.setOnClickListener {
NavigationHelper.navigateVideo(root.context, video.videoId)
}
@ -63,5 +64,5 @@ class WatchHistoryAdapter(
}
}
class WatchHistoryViewHolder(val binding: VideoRowBinding) :
class WatchHistoryViewHolder(val binding: WatchHistoryRowBinding) :
RecyclerView.ViewHolder(binding.root)

View File

@ -27,12 +27,12 @@ class WatchHistoryFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
val watchHistory = PreferenceHelper.getWatchHistory()
val watchHistoryAdapter = WatchHistoryAdapter(watchHistory, childFragmentManager)
binding.watchHistoryRecView.adapter = watchHistoryAdapter
binding.clearHistory.setOnClickListener {
PreferenceHelper.removePreference("watch_history")
watchHistoryAdapter.clear()
if (watchHistory.isNotEmpty()) {
val watchHistoryAdapter = WatchHistoryAdapter(watchHistory, childFragmentManager)
binding.watchHistoryRecView.adapter = watchHistoryAdapter
binding.historyEmpty.visibility = View.GONE
binding.watchHistoryRecView.visibility = View.VISIBLE
}
// reverse order

View File

@ -25,38 +25,10 @@ object PreferenceHelper {
editor = settings.edit()
}
fun setString(key: String?, value: String?) {
editor.putString(key, value)
editor.apply()
}
fun setInt(key: String?, value: Int) {
editor.putInt(key, value)
editor.apply()
}
fun setLong(key: String?, value: Long) {
editor.putLong(key, value)
editor.apply()
}
fun setBoolean(key: String?, value: Boolean) {
editor.putBoolean(key, value)
editor.apply()
}
fun getString(key: String?, defValue: String?): String {
return settings.getString(key, defValue)!!
}
fun getInt(key: String?, defValue: Int): Int {
return settings.getInt(key, defValue)
}
fun getLong(key: String?, defValue: Long): Long {
return settings.getLong(key, defValue)
}
fun getBoolean(key: String?, defValue: Boolean): Boolean {
return settings.getBoolean(key, defValue)
}
@ -153,7 +125,7 @@ object PreferenceHelper {
}
fun addToWatchHistory(videoId: String, streams: Streams) {
val mapper = ObjectMapper()
removeFromWatchHistory(videoId)
val watchHistoryItem = WatchHistoryItem(
videoId,
@ -166,21 +138,30 @@ object PreferenceHelper {
streams.duration
)
val mapper = ObjectMapper()
val watchHistory = getWatchHistory()
// delete entries that have the same videoId
var indexToRemove: Int? = null
watchHistory.forEachIndexed { index, item ->
if (item.videoId == videoId) indexToRemove = index
}
if (indexToRemove != null) watchHistory.removeAt(indexToRemove!!)
watchHistory += watchHistoryItem
val json = mapper.writeValueAsString(watchHistory)
editor.putString("watch_history", json).apply()
}
fun removeFromWatchHistory(videoId: String) {
val mapper = ObjectMapper()
val watchHistory = getWatchHistory()
var indexToRemove: Int? = null
watchHistory.forEachIndexed { index, item ->
if (item.videoId == videoId) indexToRemove = index
}
if (indexToRemove != null) {
watchHistory.removeAt(indexToRemove!!)
val json = mapper.writeValueAsString(watchHistory)
editor.putString("watch_history", json).commit()
}
}
fun getWatchHistory(): ArrayList<WatchHistoryItem> {
val mapper = ObjectMapper()

View File

@ -97,8 +97,7 @@
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="15dp"
android:autoLink="web"
android:text="" />
android:autoLink="web" />
<RelativeLayout
android:layout_width="match_parent"

View File

@ -264,7 +264,6 @@
android:layout_toEndOf="@+id/player_channelImage"
android:ellipsize="end"
android:maxLines="1"
android:text=""
android:textSize="15sp" />

View File

@ -34,7 +34,6 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:textSize="24sp"
android:textStyle="bold" />

View File

@ -1,43 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/history_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="16dp"
android:src="@drawable/ic_history" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:paddingHorizontal="8dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/watch_history"
android:textSize="16sp" />
<ImageView
android:id="@+id/clearHistory"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:src="@drawable/ic_reset" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/watchHistoryRecView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:nestedScrollingEnabled="false" />
android:layout_marginHorizontal="10dp"
android:gravity="center"
android:text="@string/history_empty"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/watchHistoryRecView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:nestedScrollingEnabled="false"
android:visibility="gone" />
</FrameLayout>

View File

@ -42,7 +42,6 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text=""
app:layout_constraintEnd_toStartOf="@+id/delete_playlist"
app:layout_constraintStart_toEndOf="@+id/card_playlist_thumbnail"
app:layout_constraintTop_toTopOf="parent" />
@ -52,8 +51,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text=""
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@id/delete_playlist"
app:layout_constraintStart_toEndOf="@+id/card_playlist_thumbnail"
app:layout_constraintTop_toBottomOf="@+id/playlist_title" />

View File

@ -0,0 +1,129 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/video_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="?android:attr/selectableItemBackground">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent=".45" />
<com.google.android.material.card.MaterialCardView
android:id="@+id/thumbnail_card"
android:layout_width="0dp"
android:layout_height="0dp"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:strokeWidth="0dp">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:srcCompat="@tools:sample/backgrounds/scenic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
app:cardBackgroundColor="@color/duration_background_color"
app:cardCornerRadius="8dp"
app:cardElevation="0dp">
<TextView
android:id="@+id/thumbnail_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingHorizontal="6dp"
android:paddingVertical="2dp"
android:textColor="@color/duration_text_color"
android:textSize="11sp"
tools:text="05:36" />
</androidx.cardview.widget.CardView>
<View
android:id="@+id/watch_progress"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_gravity="bottom"
android:background="@android:color/holo_red_dark" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<TextView
android:id="@+id/video_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ellipsize="end"
android:maxLines="2"
app:layout_constraintEnd_toStartOf="@id/deleteBTN"
app:layout_constraintStart_toEndOf="@id/thumbnail_card"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/video_info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toStartOf="@id/deleteBTN"
app:layout_constraintStart_toEndOf="@id/thumbnail_card"
app:layout_constraintTop_toBottomOf="@id/video_title" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/channel_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@id/video_info" />
<TextView
android:id="@+id/channel_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/channel_image"
app:layout_constraintTop_toBottomOf="@id/video_info" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/deleteBTN"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:padding="8dp"
android:src="@drawable/ic_delete"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="@style/roundedImageViewRounded" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -266,4 +266,5 @@
<string name="worst_quality">Worst quality</string>
<string name="default_subtitle_language">Default subtitle language</string>
<string name="irreversible">Are you sure? This can\'t be undone!</string>
<string name="history_empty">History is empty.</string>
</resources>