add data saver mode

This commit is contained in:
Bnyro 2022-07-15 21:37:44 +02:00
parent be49850e1c
commit 728f345f1d
21 changed files with 99 additions and 36 deletions

View File

@ -1,7 +1,16 @@
package com.github.libretube
/**
* Global variables can be stored here
*/
object Globals {
// for the player fragment
var isFullScreen = false
var isMiniPlayerVisible = false
// for the settings
var isCurrentViewMainSettings = true
// for the data saver mode
var dataSaverModeEnabled = false
}

View File

@ -75,6 +75,13 @@ class MainActivity : AppCompatActivity() {
RetrofitInstance.url
}
// save whether the data saver mode is enabled
Globals.dataSaverModeEnabled = PreferenceHelper.getBoolean(
this,
"data_saver_mode",
false
)
// show noInternet Activity if no internet available on app startup
if (!ConnectionHelper.isNetworkAvailable(this)) {
val noInternetIntent = Intent(this, NoInternetActivity::class.java)

View File

@ -12,6 +12,7 @@ import com.github.libretube.databinding.VideoChannelRowBinding
import com.github.libretube.dialogs.VideoOptionsDialog
import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.obj.StreamItem
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.formatShort
import com.squareup.picasso.Picasso
@ -45,7 +46,7 @@ class ChannelAdapter(
DateUtils.getRelativeTimeSpanString(trending.uploaded!!)
channelDuration.text =
DateUtils.formatElapsedTime(trending.duration!!)
Picasso.get().load(trending.thumbnail).into(channelThumbnail)
ConnectionHelper.loadImage(trending.thumbnail, channelThumbnail)
root.setOnClickListener {
var bundle = Bundle()
bundle.putString("videoId", trending.url!!.replace("/watch?v=", ""))

View File

@ -5,6 +5,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.ChapterColumnBinding
import com.github.libretube.obj.ChapterSegment
import com.github.libretube.util.ConnectionHelper
import com.google.android.exoplayer2.ExoPlayer
import com.squareup.picasso.Picasso
@ -23,7 +24,7 @@ class ChaptersAdapter(
override fun onBindViewHolder(holder: ChaptersViewHolder, position: Int) {
val chapter = chapters[position]
holder.binding.apply {
Picasso.get().load(chapter.image).fit().centerCrop().into(chapterImage)
ConnectionHelper.loadImage(chapter.image, chapterImage)
chapterTitle.text = chapter.title
root.setOnClickListener {

View File

@ -14,6 +14,7 @@ import com.github.libretube.activities.MainActivity
import com.github.libretube.databinding.CommentsRowBinding
import com.github.libretube.obj.Comment
import com.github.libretube.obj.CommentsPage
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.formatShort
import com.squareup.picasso.Picasso
@ -53,7 +54,7 @@ class CommentsAdapter(
"" + comment.commentedTime.toString()
commentText.text =
comment.commentText.toString()
Picasso.get().load(comment.thumbnail).fit().centerCrop().into(commentorImage)
ConnectionHelper.loadImage(comment.thumbnail, commentorImage)
likesTextView.text =
comment.likeCount?.toLong().formatShort()
if (comment.verified == true) {

View File

@ -17,6 +17,7 @@ import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.obj.PlaylistId
import com.github.libretube.obj.StreamItem
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.RetrofitInstance
import com.squareup.picasso.Picasso
import kotlinx.coroutines.CoroutineScope
@ -55,7 +56,7 @@ class PlaylistAdapter(
playlistTitle.text = streamItem.title
playlistDescription.text = streamItem.uploaderName
playlistDuration.text = DateUtils.formatElapsedTime(streamItem.duration!!)
Picasso.get().load(streamItem.thumbnail).into(playlistThumbnail)
ConnectionHelper.loadImage(streamItem.thumbnail, playlistThumbnail)
root.setOnClickListener {
var bundle = Bundle()
bundle.putString("videoId", streamItem.url!!.replace("/watch?v=", ""))

View File

@ -12,6 +12,7 @@ import com.github.libretube.databinding.PlaylistsRowBinding
import com.github.libretube.obj.PlaylistId
import com.github.libretube.obj.Playlists
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.RetrofitInstance
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.squareup.picasso.Picasso
@ -45,7 +46,7 @@ class PlaylistsAdapter(
override fun onBindViewHolder(holder: PlaylistsViewHolder, position: Int) {
val playlist = playlists[position]
holder.binding.apply {
Picasso.get().load(playlist.thumbnail).into(playlistThumbnail)
ConnectionHelper.loadImage(playlist.thumbnail, playlistThumbnail)
// set imageview drawable as empty playlist if imageview empty
if (playlistThumbnail.drawable == null) {
playlistThumbnail.setImageResource(R.drawable.ic_empty_playlist)

View File

@ -10,6 +10,7 @@ import com.github.libretube.R
import com.github.libretube.activities.MainActivity
import com.github.libretube.databinding.RepliesRowBinding
import com.github.libretube.obj.Comment
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.formatShort
import com.squareup.picasso.Picasso
@ -44,7 +45,7 @@ class RepliesAdapter(
"" + reply.commentedTime.toString()
commentText.text =
reply.commentText.toString()
Picasso.get().load(reply.thumbnail).fit().centerCrop().into(commentorImage)
ConnectionHelper.loadImage(reply.thumbnail, commentorImage)
likesTextView.text =
reply.likeCount?.toLong().formatShort()
if (reply.verified == true) {

View File

@ -20,6 +20,7 @@ import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.obj.SearchItem
import com.github.libretube.obj.Subscribe
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.formatShort
import com.squareup.picasso.Picasso
@ -84,14 +85,14 @@ class SearchAdapter(
private fun bindWatch(item: SearchItem, binding: VideoSearchRowBinding) {
binding.apply {
Picasso.get().load(item.thumbnail).fit().centerCrop().into(searchThumbnail)
ConnectionHelper.loadImage(item.thumbnail, searchThumbnail)
if (item.duration != -1L) {
searchThumbnailDuration.text = DateUtils.formatElapsedTime(item.duration!!)
} else {
searchThumbnailDuration.text = root.context.getString(R.string.live)
searchThumbnailDuration.setBackgroundColor(R.attr.colorPrimaryDark)
}
Picasso.get().load(item.uploaderAvatar).fit().centerCrop().into(searchChannelImage)
ConnectionHelper.loadImage(item.uploaderAvatar, searchChannelImage)
searchDescription.text = item.title
val viewsString = if (item.views?.toInt() != -1) item.views.formatShort() else ""
val uploadDate = if (item.uploadedDate != null) item.uploadedDate else ""
@ -131,7 +132,7 @@ class SearchAdapter(
private fun bindChannel(item: SearchItem, binding: ChannelSearchRowBinding) {
binding.apply {
Picasso.get().load(item.thumbnail).fit().centerCrop().into(searchChannelImage)
ConnectionHelper.loadImage(item.thumbnail, searchChannelImage)
searchChannelName.text = item.name
searchViews.text = root.context.getString(
R.string.subscribers,
@ -219,7 +220,7 @@ class SearchAdapter(
private fun bindPlaylist(item: SearchItem, binding: PlaylistSearchRowBinding) {
binding.apply {
Picasso.get().load(item.thumbnail).fit().centerCrop().into(searchThumbnail)
ConnectionHelper.loadImage(item.thumbnail, searchThumbnail)
if (item.videos?.toInt() != -1) searchPlaylistNumber.text = item.videos.toString()
searchDescription.text = item.name
searchName.text = item.uploaderName

View File

@ -15,6 +15,7 @@ import com.github.libretube.databinding.TrendingRowBinding
import com.github.libretube.dialogs.VideoOptionsDialog
import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.obj.StreamItem
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.formatShort
import com.squareup.picasso.Picasso
@ -72,8 +73,8 @@ class SubscriptionAdapter(
} catch (e: Exception) {
}
}
Picasso.get().load(trending.thumbnail).into(thumbnail)
Picasso.get().load(trending.uploaderAvatar).into(channelImage)
ConnectionHelper.loadImage(trending.thumbnail, thumbnail)
ConnectionHelper.loadImage(trending.uploaderAvatar, channelImage)
root.setOnClickListener {
val bundle = Bundle()
bundle.putString("videoId", trending.url!!.replace("/watch?v=", ""))

View File

@ -12,6 +12,7 @@ import com.github.libretube.databinding.ChannelSubscriptionRowBinding
import com.github.libretube.obj.Subscribe
import com.github.libretube.obj.Subscription
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.RetrofitInstance
import com.squareup.picasso.Picasso
import kotlinx.coroutines.CoroutineScope
@ -40,7 +41,7 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
val subscription = subscriptions[position]
holder.binding.apply {
subscriptionChannelName.text = subscription.name
Picasso.get().load(subscription.avatar).into(subscriptionChannelImage)
ConnectionHelper.loadImage(subscription.avatar, subscriptionChannelImage)
root.setOnClickListener {
val activity = root.context as MainActivity
val bundle = bundleOf("channel_id" to subscription.url)

View File

@ -15,6 +15,7 @@ import com.github.libretube.databinding.TrendingRowBinding
import com.github.libretube.dialogs.VideoOptionsDialog
import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.obj.StreamItem
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.formatShort
import com.squareup.picasso.Picasso
@ -63,12 +64,8 @@ class TrendingAdapter(
} catch (e: Exception) {
}
}
if (trending.thumbnail!!.isNotEmpty()) {
Picasso.get().load(trending.thumbnail).into(thumbnail)
}
if (trending.uploaderAvatar!!.isNotEmpty()) {
Picasso.get().load(trending.uploaderAvatar).into(channelImage)
}
ConnectionHelper.loadImage(trending.thumbnail, thumbnail)
ConnectionHelper.loadImage(trending.uploaderAvatar, channelImage)
root.setOnClickListener {
var bundle = Bundle()

View File

@ -15,6 +15,7 @@ import com.github.libretube.databinding.WatchHistoryRowBinding
import com.github.libretube.dialogs.VideoOptionsDialog
import com.github.libretube.fragments.PlayerFragment
import com.github.libretube.obj.WatchHistoryItem
import com.github.libretube.util.ConnectionHelper
import com.squareup.picasso.Picasso
class WatchHistoryAdapter(
@ -43,8 +44,8 @@ class WatchHistoryAdapter(
channelName.text = video.uploader
uploadDate.text = video.uploadDate
thumbnailDuration.text = DateUtils.formatElapsedTime(video.duration?.toLong()!!)
Picasso.get().load(video.thumbnailUrl).into(thumbnail)
Picasso.get().load(video.uploaderAvatar).into(channelImage)
ConnectionHelper.loadImage(video.thumbnailUrl, thumbnail)
ConnectionHelper.loadImage(video.uploaderAvatar, channelImage)
channelImage.setOnClickListener {
val activity = root.context as MainActivity

View File

@ -14,6 +14,7 @@ import com.github.libretube.adapters.ChannelAdapter
import com.github.libretube.databinding.FragmentChannelBinding
import com.github.libretube.obj.Subscribe
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.formatShort
import com.squareup.picasso.Picasso
@ -194,8 +195,8 @@ class ChannelFragment : Fragment() {
binding.channelDescription.text = response.description?.trim()
}
Picasso.get().load(response.bannerUrl).into(binding.channelBanner)
Picasso.get().load(response.avatarUrl).into(binding.channelImage)
ConnectionHelper.loadImage(response.bannerUrl, binding.channelBanner)
ConnectionHelper.loadImage(response.avatarUrl, binding.channelImage)
channelAdapter = ChannelAdapter(
response.relatedStreams!!.toMutableList(),
childFragmentManager

View File

@ -57,6 +57,7 @@ import com.github.libretube.obj.Subscribe
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.services.IS_DOWNLOAD_RUNNING
import com.github.libretube.util.BackgroundMode
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.CronetHelper
import com.github.libretube.util.DescriptionAdapter
import com.github.libretube.util.RetrofitInstance
@ -87,7 +88,6 @@ import com.google.android.exoplayer2.util.RepeatModeUtil
import com.google.android.exoplayer2.video.VideoSize
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.squareup.picasso.Picasso
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -739,17 +739,19 @@ class PlayerFragment : Fragment() {
}
private fun initializePlayerView(view: View, response: Streams) {
binding.playerViewsInfo.text =
context?.getString(R.string.views, response.views.formatShort()) +
"" + response.uploadDate
binding.textLike.text = response.likes.formatShort()
binding.textDislike.text = response.dislikes.formatShort()
Picasso.get().load(response.uploaderAvatar).into(binding.playerChannelImage)
binding.playerChannelName.text = response.uploader
binding.apply {
playerViewsInfo.text =
context?.getString(R.string.views, response.views.formatShort()) +
"" + response.uploadDate
textLike.text = response.likes.formatShort()
textDislike.text = response.dislikes.formatShort()
ConnectionHelper.loadImage(response.uploaderAvatar, binding.playerChannelImage)
playerChannelName.text = response.uploader
binding.titleTextView.text = response.title
binding.playerTitle.text = response.title
binding.playerDescription.text = response.description
titleTextView.text = response.title
playerTitle.text = response.title
playerDescription.text = response.description
}
playerBinding.exoTitle.text = response.title

View File

@ -3,6 +3,7 @@ package com.github.libretube.preferences
import android.os.Bundle
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.dialogs.RequireRestartDialog
@ -32,6 +33,13 @@ class AdvancedSettings : PreferenceFragmentCompat() {
true
}
val dataSaverMode = findPreference<SwitchPreferenceCompat>("data_saver_mode")
dataSaverMode?.setOnPreferenceChangeListener { _, _ ->
val restartDialog = RequireRestartDialog()
restartDialog.show(childFragmentManager, "RequireRestartDialog")
true
}
val resetSettings = findPreference<Preference>("reset_settings")
resetSettings?.setOnPreferenceClickListener {
showResetDialog()

View File

@ -1,7 +1,11 @@
package com.github.libretube.util
import android.content.Context
import android.graphics.Color
import android.net.ConnectivityManager
import android.widget.ImageView
import com.github.libretube.Globals
import com.squareup.picasso.Picasso
object ConnectionHelper {
fun isNetworkAvailable(context: Context): Boolean {
@ -33,4 +37,12 @@ object ConnectionHelper {
return connectivityManager.activeNetworkInfo?.isConnected ?: false
}
// load an image from a url into an imageView
fun loadImage(url: String?, target: ImageView) {
// only load the image if the data saver mode is disabled
if (!Globals.dataSaverModeEnabled) {
Picasso.get().load(url).fit().centerCrop().into(target)
}
}
}

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?android:attr/colorControlNormal"
android:viewportWidth="48"
android:viewportHeight="48">
<path
android:fillColor="#FF000000"
android:pathData="M22.55,31.45L22.55,25.4L16.5,25.4v-3h6.05v-6.05h3v6.05h6v3h-6v6.05ZM42.15,32.55 L37.75,30.1q0.6,-1.55 0.9,-3.075 0.3,-1.525 0.3,-3.025 0,-5.8 -3.825,-10.05Q31.3,9.7 25.6,9.2L25.6,4.1q7.85,0.55 13.15,6.225Q44.05,16 44.05,23.8q0,2.25 -0.475,4.425T42.15,32.55ZM24.05,44q-4.1,0 -7.75,-1.575 -3.65,-1.575 -6.375,-4.275t-4.3,-6.35Q4.05,28.15 4.05,24.05q0,-7.9 5.325,-13.6Q14.7,4.75 22.6,4.15v5.1q-5.75,0.55 -9.6,4.775Q9.15,18.25 9.15,24.05q0,6.2 4.35,10.525T24.05,38.9q3.6,0 6.8,-1.625 3.2,-1.625 5.45,-4.575l4.4,2.55q-2.9,4.15 -7.275,6.45T24.05,44Z" />
</vector>

View File

@ -23,8 +23,7 @@
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher" />
android:layout_height="match_parent" />
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"

View File

@ -242,4 +242,6 @@
<string name="pure_theme">Pure theme</string>
<string name="pure_theme_summary">Pure white/black theme</string>
<string name="no_player_found">No external player found. Please make sure you have one installed.</string>
<string name="data_saver_mode">Data saver mode</string>
<string name="data_saver_mode_summary">Don\'t load thumbnails and other images to save data.</string>
</resources>

View File

@ -70,6 +70,12 @@
<PreferenceCategory>
<SwitchPreferenceCompat
android:icon="@drawable/ic_data_saver"
android:summary="@string/data_saver_mode_summary"
app:key="data_saver_mode"
app:title="@string/data_saver_mode" />
<Preference
android:icon="@drawable/ic_reset"
app:key="reset_settings"