From 6914e9500adeca55afedbd7ffa98ed4919ee264c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 15 Aug 2022 09:59:48 +0200 Subject: [PATCH 1/3] layout improvements --- .../java/com/github/libretube/api/ResettableLazy.kt | 2 +- .../github/libretube/dialogs/AddToPlaylistDialog.kt | 3 ++- .../github/libretube/fragments/LibraryFragment.kt | 7 ++++--- .../github/libretube/fragments/PlayerFragment.kt | 2 +- app/src/main/res/layout/channel_row.xml | 6 +----- .../main/res/layout/channel_subscription_row.xml | 8 +++----- app/src/main/res/layout/fragment_search_result.xml | 2 +- app/src/main/res/layout/fragment_watch_history.xml | 1 - app/src/main/res/layout/playlist_row.xml | 7 +------ app/src/main/res/layout/playlist_search_row.xml | 5 +---- app/src/main/res/layout/playlists_row.xml | 6 +----- app/src/main/res/layout/video_row.xml | 6 +----- app/src/main/res/layout/watch_history_row.xml | 6 +----- app/src/main/res/values/style.xml | 13 +++++++++++++ 14 files changed, 31 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/ResettableLazy.kt b/app/src/main/java/com/github/libretube/api/ResettableLazy.kt index 129e27de6..432dff009 100644 --- a/app/src/main/java/com/github/libretube/api/ResettableLazy.kt +++ b/app/src/main/java/com/github/libretube/api/ResettableLazy.kt @@ -1,7 +1,7 @@ package com.github.libretube.util -import java.util.* import kotlin.reflect.KProperty +import java.util.* class ResettableLazyManager { // we synchronize to make sure the timing of a reset() call and new inits do not collide diff --git a/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt b/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt index 6d840997e..29511a227 100644 --- a/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt @@ -72,7 +72,8 @@ class AddToPlaylistDialog : DialogFragment() { if (viewModel.lastSelectedPlaylistId != null) { var selectionIndex = 0 response.forEachIndexed { index, playlist -> - if (playlist.id == viewModel.lastSelectedPlaylistId) selectionIndex = index + if (playlist.id == viewModel.lastSelectedPlaylistId) selectionIndex = + index } binding.playlistsSpinner.setSelection(selectionIndex) } diff --git a/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt b/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt index 8df79aa2f..0db99547b 100644 --- a/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/LibraryFragment.kt @@ -68,8 +68,12 @@ class LibraryFragment : BaseFragment() { } if (token != "") { + binding.boogh.setImageResource(R.drawable.ic_list) + binding.textLike.text = getString(R.string.emptyList) + binding.loginOrRegister.visibility = View.GONE fetchPlaylists() + binding.playlistRefresh.isEnabled = true binding.playlistRefresh.setOnRefreshListener { fetchPlaylists() @@ -123,7 +127,6 @@ class LibraryFragment : BaseFragment() { playlistsAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { override fun onChanged() { - Log.e(TAG(), playlistsAdapter.itemCount.toString()) if (playlistsAdapter.itemCount == 0) { binding.loginOrRegister.visibility = View.VISIBLE } @@ -135,8 +138,6 @@ class LibraryFragment : BaseFragment() { } else { runOnUiThread { binding.loginOrRegister.visibility = View.VISIBLE - binding.boogh.setImageResource(R.drawable.ic_list) - binding.textLike.text = getString(R.string.emptyList) } } } diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 95fdbb90b..bd3dc3f4f 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -95,6 +95,7 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSource import com.google.android.exoplayer2.util.RepeatModeUtil import com.google.android.exoplayer2.video.VideoSize import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlin.math.abs import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -102,7 +103,6 @@ import org.chromium.net.CronetEngine import retrofit2.HttpException import java.io.IOException import java.util.concurrent.Executors -import kotlin.math.abs class PlayerFragment : BaseFragment() { diff --git a/app/src/main/res/layout/channel_row.xml b/app/src/main/res/layout/channel_row.xml index 702c892d0..72e496255 100644 --- a/app/src/main/res/layout/channel_row.xml +++ b/app/src/main/res/layout/channel_row.xml @@ -1,11 +1,7 @@ + style="@style/ItemRow"> + android:background="?android:attr/selectableItemBackground" + android:paddingHorizontal="8dp" + android:paddingVertical="4dp"> + android:layout_marginVertical="10dp" /> diff --git a/app/src/main/res/layout/fragment_watch_history.xml b/app/src/main/res/layout/fragment_watch_history.xml index da62ec3cc..8efd89276 100644 --- a/app/src/main/res/layout/fragment_watch_history.xml +++ b/app/src/main/res/layout/fragment_watch_history.xml @@ -30,7 +30,6 @@ android:id="@+id/watchHistoryRecView" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_margin="8dp" android:nestedScrollingEnabled="false" android:visibility="gone" /> diff --git a/app/src/main/res/layout/playlist_row.xml b/app/src/main/res/layout/playlist_row.xml index 9a69f8594..8b3374adf 100644 --- a/app/src/main/res/layout/playlist_row.xml +++ b/app/src/main/res/layout/playlist_row.xml @@ -2,12 +2,7 @@ + style="@style/ItemRow"> + style="@style/ItemRow"> + style="@style/ItemRow"> + style="@style/ItemRow"> + style="@style/ItemRow"> + + \ No newline at end of file From 80a24e3774b6d9a5d12a54e06fe0c247324a98dc Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 15 Aug 2022 10:16:53 +0200 Subject: [PATCH 2/3] limit search history size --- .../com/github/libretube/api/ResettableLazy.kt | 2 +- .../com/github/libretube/db/DatabaseHelper.kt | 15 +++++++++++++++ .../github/libretube/fragments/PlayerFragment.kt | 2 +- .../libretube/fragments/SearchResultFragment.kt | 13 +++++-------- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/ResettableLazy.kt b/app/src/main/java/com/github/libretube/api/ResettableLazy.kt index 432dff009..129e27de6 100644 --- a/app/src/main/java/com/github/libretube/api/ResettableLazy.kt +++ b/app/src/main/java/com/github/libretube/api/ResettableLazy.kt @@ -1,7 +1,7 @@ package com.github.libretube.util -import kotlin.reflect.KProperty import java.util.* +import kotlin.reflect.KProperty class ResettableLazyManager { // we synchronize to make sure the timing of a reset() call and new inits do not collide diff --git a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt index d56c6e94f..0c162dc3a 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt @@ -1,5 +1,6 @@ package com.github.libretube.db +import com.github.libretube.db.obj.SearchHistoryItem import com.github.libretube.db.obj.WatchHistoryItem import com.github.libretube.db.obj.WatchPosition import com.github.libretube.obj.Streams @@ -59,4 +60,18 @@ object DatabaseHelper { ) }.start() } + + fun addToSearchHistory(searchHistoryItem: SearchHistoryItem) { + Thread { + DatabaseHolder.db.searchHistoryDao().insertAll(searchHistoryItem) + val maxHistorySize = 20 + + // delete the first watch history entry if the limit is reached + val searchHistory = DatabaseHolder.db.searchHistoryDao().getAll() + if (searchHistory.size > maxHistorySize) { + DatabaseHolder.db.searchHistoryDao() + .delete(searchHistory.first()) + } + }.start() + } } diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index bd3dc3f4f..95fdbb90b 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -95,7 +95,6 @@ import com.google.android.exoplayer2.upstream.DefaultHttpDataSource import com.google.android.exoplayer2.util.RepeatModeUtil import com.google.android.exoplayer2.video.VideoSize import com.google.android.material.dialog.MaterialAlertDialogBuilder -import kotlin.math.abs import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -103,6 +102,7 @@ import org.chromium.net.CronetEngine import retrofit2.HttpException import java.io.IOException import java.util.concurrent.Executors +import kotlin.math.abs class PlayerFragment : BaseFragment() { diff --git a/app/src/main/java/com/github/libretube/fragments/SearchResultFragment.kt b/app/src/main/java/com/github/libretube/fragments/SearchResultFragment.kt index 8019812e9..c088720ea 100644 --- a/app/src/main/java/com/github/libretube/fragments/SearchResultFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/SearchResultFragment.kt @@ -11,11 +11,10 @@ import com.github.libretube.R import com.github.libretube.adapters.SearchAdapter import com.github.libretube.api.RetrofitInstance import com.github.libretube.databinding.FragmentSearchResultBinding -import com.github.libretube.db.DatabaseHolder +import com.github.libretube.db.DatabaseHelper import com.github.libretube.db.obj.SearchHistoryItem import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.TAG -import com.github.libretube.extensions.await import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceKeys import com.github.libretube.util.hideKeyboard @@ -135,13 +134,11 @@ class SearchResultFragment : BaseFragment() { val searchHistoryEnabled = PreferenceHelper.getBoolean(PreferenceKeys.SEARCH_HISTORY_TOGGLE, true) if (searchHistoryEnabled && query != "") { - Thread { - DatabaseHolder.db.searchHistoryDao().insertAll( - SearchHistoryItem( - query = query - ) + DatabaseHelper.addToSearchHistory( + SearchHistoryItem( + query = query ) - }.await() + ) } } } From 777edca2ac4aaf222af150c5706f68b0425c1d9f Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 15 Aug 2022 10:47:31 +0200 Subject: [PATCH 3/3] max image cache pref --- .../main/java/com/github/libretube/MyApp.kt | 7 +-- .../libretube/activities/MainActivity.kt | 4 +- .../activities/NoInternetActivity.kt | 4 +- .../libretube/adapters/ChannelAdapter.kt | 4 +- .../libretube/adapters/ChaptersAdapter.kt | 4 +- .../libretube/adapters/CommentsAdapter.kt | 4 +- .../libretube/adapters/PlaylistAdapter.kt | 4 +- .../libretube/adapters/PlaylistsAdapter.kt | 4 +- .../libretube/adapters/RepliesAdapter.kt | 4 +- .../libretube/adapters/SearchAdapter.kt | 10 ++-- .../adapters/SubscriptionChannelAdapter.kt | 4 +- .../libretube/adapters/TrendingAdapter.kt | 6 +-- .../libretube/adapters/WatchHistoryAdapter.kt | 6 +-- .../libretube/fragments/ChannelFragment.kt | 6 +-- .../libretube/fragments/PlayerFragment.kt | 4 +- .../libretube/preferences/AdvancedSettings.kt | 8 ++++ .../libretube/preferences/MainSettings.kt | 4 +- .../libretube/preferences/PreferenceHelper.kt | 4 ++ .../libretube/preferences/PreferenceKeys.kt | 1 + .../com/github/libretube/util/ImageHelper.kt | 46 +++++++++++++++++++ .../{ConnectionHelper.kt => NetworkHelper.kt} | 22 ++------- app/src/main/res/drawable/ic_cache.xml | 19 ++++++++ app/src/main/res/values/array.xml | 18 ++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/advanced_settings.xml | 9 ++++ 25 files changed, 148 insertions(+), 59 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/util/ImageHelper.kt rename app/src/main/java/com/github/libretube/util/{ConnectionHelper.kt => NetworkHelper.kt} (68%) create mode 100644 app/src/main/res/drawable/ic_cache.xml diff --git a/app/src/main/java/com/github/libretube/MyApp.kt b/app/src/main/java/com/github/libretube/MyApp.kt index 1fbacc843..5f74f948b 100644 --- a/app/src/main/java/com/github/libretube/MyApp.kt +++ b/app/src/main/java/com/github/libretube/MyApp.kt @@ -9,7 +9,6 @@ import android.os.StrictMode import android.os.StrictMode.VmPolicy import androidx.preference.PreferenceManager import androidx.work.ExistingPeriodicWorkPolicy -import coil.ImageLoader import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.ObjectMapper import com.github.libretube.api.CronetHelper @@ -19,8 +18,8 @@ import com.github.libretube.db.obj.WatchHistoryItem import com.github.libretube.db.obj.WatchPosition import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceKeys -import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.ExceptionHandler +import com.github.libretube.util.ImageHelper import com.github.libretube.util.NotificationHelper class MyApp : Application() { @@ -93,9 +92,7 @@ class MyApp : Application() { RetrofitInstance.url } CronetHelper.initCronet(this) - ConnectionHelper.imageLoader = ImageLoader.Builder(this) - .callFactory(CronetHelper.callFactory) - .build() + ImageHelper.initializeImageLoader(this) } /** 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 2c8826c7a..d54f59104 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -36,8 +36,8 @@ import com.github.libretube.models.SearchViewModel import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceKeys import com.github.libretube.services.ClosingService -import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.LocaleHelper +import com.github.libretube.util.NetworkHelper import com.github.libretube.util.ThemeHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.elevation.SurfaceColors @@ -73,7 +73,7 @@ class MainActivity : BaseActivity() { } // show noInternet Activity if no internet available on app startup - if (!ConnectionHelper.isNetworkAvailable(this)) { + if (!NetworkHelper.isNetworkAvailable(this)) { val noInternetIntent = Intent(this, NoInternetActivity::class.java) startActivity(noInternetIntent) finish() diff --git a/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt b/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt index fa5f2cae7..17cb13b97 100644 --- a/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/NoInternetActivity.kt @@ -6,7 +6,7 @@ import com.github.libretube.R import com.github.libretube.databinding.ActivityNointernetBinding import com.github.libretube.extensions.BaseActivity import com.github.libretube.extensions.showSnackBar -import com.github.libretube.util.ConnectionHelper +import com.github.libretube.util.NetworkHelper import com.github.libretube.util.ThemeHelper class NoInternetActivity : BaseActivity() { @@ -18,7 +18,7 @@ class NoInternetActivity : BaseActivity() { binding = ActivityNointernetBinding.inflate(layoutInflater) // retry button binding.retryButton.setOnClickListener { - if (ConnectionHelper.isNetworkAvailable(this)) { + if (NetworkHelper.isNetworkAvailable(this)) { ThemeHelper.restartMainActivity(this) } else { binding.root.showSnackBar(R.string.turnInternetOn) 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 3574d9117..9fdc4893c 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt @@ -8,7 +8,7 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.databinding.VideoRowBinding import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.obj.StreamItem -import com.github.libretube.util.ConnectionHelper +import com.github.libretube.util.ImageHelper import com.github.libretube.util.NavigationHelper import com.github.libretube.util.formatShort import com.github.libretube.util.setWatchProgressLength @@ -45,7 +45,7 @@ class ChannelAdapter( DateUtils.getRelativeTimeSpanString(trending.uploaded!!) thumbnailDuration.text = DateUtils.formatElapsedTime(trending.duration!!) - ConnectionHelper.loadImage(trending.thumbnail, thumbnail) + ImageHelper.loadImage(trending.thumbnail, thumbnail) root.setOnClickListener { NavigationHelper.navigateVideo(root.context, trending.url) } 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 8ddaba1cd..9a333403d 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt @@ -6,7 +6,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.github.libretube.util.ImageHelper import com.github.libretube.util.ThemeHelper import com.google.android.exoplayer2.ExoPlayer @@ -25,7 +25,7 @@ class ChaptersAdapter( override fun onBindViewHolder(holder: ChaptersViewHolder, position: Int) { val chapter = chapters[position] holder.binding.apply { - ConnectionHelper.loadImage(chapter.image, chapterImage) + ImageHelper.loadImage(chapter.image, chapterImage) chapterTitle.text = chapter.title if (selectedPosition == position) { 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 b9165be4c..44e03ba82 100644 --- a/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/CommentsAdapter.kt @@ -13,7 +13,7 @@ import com.github.libretube.databinding.CommentsRowBinding import com.github.libretube.extensions.TAG import com.github.libretube.obj.Comment import com.github.libretube.obj.CommentsPage -import com.github.libretube.util.ConnectionHelper +import com.github.libretube.util.ImageHelper import com.github.libretube.util.NavigationHelper import com.github.libretube.util.formatShort import kotlinx.coroutines.CoroutineScope @@ -51,7 +51,7 @@ class CommentsAdapter( " • " + comment.commentedTime.toString() commentText.text = comment.commentText.toString() - ConnectionHelper.loadImage(comment.thumbnail, commentorImage) + ImageHelper.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 9f9a5fa7d..2874843c2 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistAdapter.kt @@ -15,7 +15,7 @@ import com.github.libretube.extensions.setFormattedDuration 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.ImageHelper import com.github.libretube.util.NavigationHelper import com.github.libretube.util.setWatchProgressLength import com.github.libretube.util.toID @@ -54,7 +54,7 @@ class PlaylistAdapter( playlistTitle.text = streamItem.title playlistDescription.text = streamItem.uploaderName thumbnailDuration.setFormattedDuration(streamItem.duration!!) - ConnectionHelper.loadImage(streamItem.thumbnail, playlistThumbnail) + ImageHelper.loadImage(streamItem.thumbnail, playlistThumbnail) root.setOnClickListener { NavigationHelper.navigateVideo(root.context, streamItem.url, playlistId) } 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 d9355c303..55a146a73 100644 --- a/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/PlaylistsAdapter.kt @@ -14,7 +14,7 @@ import com.github.libretube.extensions.TAG 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.ImageHelper import com.github.libretube.util.NavigationHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.CoroutineScope @@ -52,7 +52,7 @@ class PlaylistsAdapter( playlistThumbnail.setImageResource(R.drawable.ic_empty_playlist) playlistThumbnail.setBackgroundColor(R.attr.colorSurface) } else { - ConnectionHelper.loadImage(playlist.thumbnail, playlistThumbnail) + ImageHelper.loadImage(playlist.thumbnail, playlistThumbnail) } playlistTitle.text = playlist.name deletePlaylist.setOnClickListener { 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 a155ccd85..e4a31b4af 100644 --- a/app/src/main/java/com/github/libretube/adapters/RepliesAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/RepliesAdapter.kt @@ -6,7 +6,7 @@ import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.github.libretube.databinding.RepliesRowBinding import com.github.libretube.obj.Comment -import com.github.libretube.util.ConnectionHelper +import com.github.libretube.util.ImageHelper import com.github.libretube.util.NavigationHelper import com.github.libretube.util.formatShort @@ -40,7 +40,7 @@ class RepliesAdapter( " • " + reply.commentedTime.toString() commentText.text = reply.commentText.toString() - ConnectionHelper.loadImage(reply.thumbnail, commentorImage) + ImageHelper.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 522dde4c8..97635d6d8 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -14,7 +14,7 @@ import com.github.libretube.dialogs.PlaylistOptionsDialog import com.github.libretube.dialogs.VideoOptionsDialog import com.github.libretube.extensions.setFormattedDuration import com.github.libretube.obj.SearchItem -import com.github.libretube.util.ConnectionHelper +import com.github.libretube.util.ImageHelper import com.github.libretube.util.NavigationHelper import com.github.libretube.util.formatShort import com.github.libretube.util.setWatchProgressLength @@ -79,9 +79,9 @@ class SearchAdapter( private fun bindWatch(item: SearchItem, binding: VideoRowBinding) { binding.apply { - ConnectionHelper.loadImage(item.thumbnail, thumbnail) + ImageHelper.loadImage(item.thumbnail, thumbnail) thumbnailDuration.setFormattedDuration(item.duration!!) - ConnectionHelper.loadImage(item.uploaderAvatar, channelImage) + ImageHelper.loadImage(item.uploaderAvatar, channelImage) videoTitle.text = item.title val viewsString = if (item.views?.toInt() != -1) item.views.formatShort() else "" val uploadDate = if (item.uploadedDate != null) item.uploadedDate else "" @@ -110,7 +110,7 @@ class SearchAdapter( private fun bindChannel(item: SearchItem, binding: ChannelRowBinding) { binding.apply { - ConnectionHelper.loadImage(item.thumbnail, searchChannelImage) + ImageHelper.loadImage(item.thumbnail, searchChannelImage) searchChannelName.text = item.name searchViews.text = root.context.getString( R.string.subscribers, @@ -157,7 +157,7 @@ class SearchAdapter( private fun bindPlaylist(item: SearchItem, binding: PlaylistSearchRowBinding) { binding.apply { - ConnectionHelper.loadImage(item.thumbnail, searchThumbnail) + ImageHelper.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/SubscriptionChannelAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt index ffcee61a6..1357b25dd 100644 --- a/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SubscriptionChannelAdapter.kt @@ -7,7 +7,7 @@ import com.github.libretube.R import com.github.libretube.api.SubscriptionHelper import com.github.libretube.databinding.ChannelSubscriptionRowBinding import com.github.libretube.obj.Subscription -import com.github.libretube.util.ConnectionHelper +import com.github.libretube.util.ImageHelper import com.github.libretube.util.NavigationHelper import com.github.libretube.util.toID @@ -31,7 +31,7 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList(PreferenceKeys.MAX_IMAGE_CACHE) + maxImageCache?.setOnPreferenceChangeListener { _, _ -> + ImageHelper.initializeImageLoader(requireContext()) + true + } + val resetSettings = findPreference(PreferenceKeys.RESET_SETTINGS) resetSettings?.setOnPreferenceClickListener { showResetDialog() diff --git a/app/src/main/java/com/github/libretube/preferences/MainSettings.kt b/app/src/main/java/com/github/libretube/preferences/MainSettings.kt index b465a0652..20c0d5bfd 100644 --- a/app/src/main/java/com/github/libretube/preferences/MainSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/MainSettings.kt @@ -9,7 +9,7 @@ import com.github.libretube.activities.SettingsActivity import com.github.libretube.dialogs.UpdateDialog import com.github.libretube.extensions.showSnackBar import com.github.libretube.update.UpdateChecker -import com.github.libretube.util.ConnectionHelper +import com.github.libretube.util.NetworkHelper import com.github.libretube.views.MaterialPreferenceFragment import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -86,7 +86,7 @@ class MainSettings : MaterialPreferenceFragment() { // checking for update: yes -> dialog, no -> snackBar update?.setOnPreferenceClickListener { CoroutineScope(Dispatchers.IO).launch { - if (!ConnectionHelper.isNetworkAvailable(requireContext())) { + if (!NetworkHelper.isNetworkAvailable(requireContext())) { (activity as SettingsActivity).binding.root.showSnackBar(R.string.unknown_error) return@launch } diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt index 284d05666..7781c3321 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt @@ -39,6 +39,10 @@ object PreferenceHelper { return settings.getBoolean(key, defValue) } + fun getInt(key: String?, defValue: Int): Int { + return settings.getInt(key, defValue) + } + fun clearPreferences() { editor.clear().apply() } diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt index fa3fdff01..dbe69a7f1 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceKeys.kt @@ -92,6 +92,7 @@ object PreferenceKeys { * Advanced */ const val DATA_SAVER_MODE = "data_saver_mode" + const val MAX_IMAGE_CACHE = "" const val RESET_SETTINGS = "reset_settings" const val CLEAR_SEARCH_HISTORY = "clear_search_history" const val CLEAR_WATCH_HISTORY = "clear_watch_history" diff --git a/app/src/main/java/com/github/libretube/util/ImageHelper.kt b/app/src/main/java/com/github/libretube/util/ImageHelper.kt new file mode 100644 index 000000000..f072dacd4 --- /dev/null +++ b/app/src/main/java/com/github/libretube/util/ImageHelper.kt @@ -0,0 +1,46 @@ +package com.github.libretube.util + +import android.content.Context +import android.widget.ImageView +import coil.ImageLoader +import coil.disk.DiskCache +import coil.load +import com.github.libretube.api.CronetHelper +import com.github.libretube.preferences.PreferenceHelper +import com.github.libretube.preferences.PreferenceKeys + +object ImageHelper { + lateinit var imageLoader: ImageLoader + + /** + * Initialize the image loader + */ + fun initializeImageLoader(context: Context) { + val maxImageCacheSize = PreferenceHelper.getInt( + PreferenceKeys.MAX_IMAGE_CACHE, + 128 + ) + + val diskCache = DiskCache.Builder() + .directory(context.filesDir.resolve("coil")) + .maxSizeBytes(maxImageCacheSize * 1024 * 1024L) + .build() + + imageLoader = ImageLoader.Builder(context) + .callFactory(CronetHelper.callFactory) + .diskCache(diskCache) + .build() + } + + /** + * 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 + val dataSaverModeEnabled = PreferenceHelper.getBoolean( + PreferenceKeys.DATA_SAVER_MODE, + false + ) + if (!dataSaverModeEnabled) target.load(url, imageLoader) + } +} diff --git a/app/src/main/java/com/github/libretube/util/ConnectionHelper.kt b/app/src/main/java/com/github/libretube/util/NetworkHelper.kt similarity index 68% rename from app/src/main/java/com/github/libretube/util/ConnectionHelper.kt rename to app/src/main/java/com/github/libretube/util/NetworkHelper.kt index f4d15d6f5..98d61e5a8 100644 --- a/app/src/main/java/com/github/libretube/util/ConnectionHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NetworkHelper.kt @@ -2,15 +2,11 @@ package com.github.libretube.util import android.content.Context import android.net.ConnectivityManager -import android.widget.ImageView -import coil.ImageLoader -import coil.load -import com.github.libretube.preferences.PreferenceHelper -import com.github.libretube.preferences.PreferenceKeys - -object ConnectionHelper { - lateinit var imageLoader: ImageLoader +object NetworkHelper { + /** + * Detect whether network is available + */ fun isNetworkAvailable(context: Context): Boolean { val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager @@ -40,14 +36,4 @@ 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 - val dataSaverModeEnabled = PreferenceHelper.getBoolean( - PreferenceKeys.DATA_SAVER_MODE, - false - ) - if (!dataSaverModeEnabled) target.load(url, imageLoader) - } } diff --git a/app/src/main/res/drawable/ic_cache.xml b/app/src/main/res/drawable/ic_cache.xml new file mode 100644 index 000000000..018572f36 --- /dev/null +++ b/app/src/main/res/drawable/ic_cache.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index 7488678f1..e4d0eff1c 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -858,4 +858,22 @@ fill + + 16MB + 32MB + 64MB + 128MB + 256MB + 512MB + + + + 16 + 32 + 64 + 128 + 256 + 512 + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a096ddd64..cb567d137 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -314,4 +314,5 @@ Backup Picture in Picture Resize mode + Max image cache size diff --git a/app/src/main/res/xml/advanced_settings.xml b/app/src/main/res/xml/advanced_settings.xml index 3a4d7413d..27a644e7e 100644 --- a/app/src/main/res/xml/advanced_settings.xml +++ b/app/src/main/res/xml/advanced_settings.xml @@ -40,6 +40,15 @@ app:key="data_saver_mode" app:title="@string/data_saver_mode" /> + +