diff --git a/app/src/main/java/com/github/libretube/Globals.kt b/app/src/main/java/com/github/libretube/Globals.kt index 7d4ff4ddd..7a0a9df80 100644 --- a/app/src/main/java/com/github/libretube/Globals.kt +++ b/app/src/main/java/com/github/libretube/Globals.kt @@ -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 } diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index dc79c69b5..67b11515c 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -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) diff --git a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt index ed5b83e58..4425f6d5f 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt @@ -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=", "")) diff --git a/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt b/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt index 6a28be080..01f9466be 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt @@ -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 { diff --git a/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt index cbac097ad..c66b64095 100644 --- a/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt @@ -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) { diff --git a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt index d6ec9bea0..c69721723 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt @@ -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=", "")) diff --git a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt index 7818583e9..e02fdba20 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt @@ -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) diff --git a/app/src/main/java/com/github/libretube/adapters/RepliesAdapter.kt b/app/src/main/java/com/github/libretube/adapters/RepliesAdapter.kt index d53429a03..7227a4e25 100644 --- a/app/src/main/java/com/github/libretube/adapters/RepliesAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/RepliesAdapter.kt @@ -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) { diff --git a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt index 29a14e39d..76d1afc3b 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -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 diff --git a/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt index 9ba5513e7..791270913 100644 --- a/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionAdapter.kt @@ -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=", "")) diff --git a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt index 6def52cb1..ced628c3f 100644 --- a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt @@ -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("data_saver_mode") + dataSaverMode?.setOnPreferenceChangeListener { _, _ -> + val restartDialog = RequireRestartDialog() + restartDialog.show(childFragmentManager, "RequireRestartDialog") + true + } + val resetSettings = findPreference("reset_settings") resetSettings?.setOnPreferenceClickListener { showResetDialog() diff --git a/app/src/main/java/com/github/libretube/util/ConnectionHelper.kt b/app/src/main/java/com/github/libretube/util/ConnectionHelper.kt index 1ba230baa..f79bda880 100644 --- a/app/src/main/java/com/github/libretube/util/ConnectionHelper.kt +++ b/app/src/main/java/com/github/libretube/util/ConnectionHelper.kt @@ -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) + } + } } diff --git a/app/src/main/res/drawable/ic_data_saver.xml b/app/src/main/res/drawable/ic_data_saver.xml new file mode 100644 index 000000000..3b3a3bfd1 --- /dev/null +++ b/app/src/main/res/drawable/ic_data_saver.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/trending_row.xml b/app/src/main/res/layout/trending_row.xml index c62ad6cb9..1a05a5db5 100644 --- a/app/src/main/res/layout/trending_row.xml +++ b/app/src/main/res/layout/trending_row.xml @@ -23,8 +23,7 @@ + android:layout_height="match_parent" /> Pure theme Pure white/black theme No external player found. Please make sure you have one installed. + Data saver mode + Don\'t load thumbnails and other images to save data. \ No newline at end of file diff --git a/app/src/main/res/xml/advanced_settings.xml b/app/src/main/res/xml/advanced_settings.xml index 41298efff..ba8d17a89 100644 --- a/app/src/main/res/xml/advanced_settings.xml +++ b/app/src/main/res/xml/advanced_settings.xml @@ -70,6 +70,12 @@ + +