From bf5cd55b25f883622e5fc26054c888e0c07bba7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sikora?= Date: Mon, 24 Feb 2025 13:31:45 +0100 Subject: [PATCH] Refactor callbacks and notifications interaction --- .../podcasts/view/podcast/PodcastAdapter.kt | 22 +++++++++---------- .../podcasts/view/podcast/PodcastFragment.kt | 16 +++++++------- .../viewmodel/PodcastSettingsViewModel.kt | 2 +- .../podcasts/viewmodel/PodcastViewModel.kt | 11 ++++------ .../settings/NotificationsSettingsFragment.kt | 2 +- .../pocketcasts/models/db/dao/PodcastDao.kt | 2 +- .../repositories/podcast/PodcastManager.kt | 2 +- .../podcast/PodcastManagerImpl.kt | 4 ++-- 8 files changed, 29 insertions(+), 32 deletions(-) diff --git a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastAdapter.kt b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastAdapter.kt index adb9910e641..f5283dd454f 100644 --- a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastAdapter.kt +++ b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastAdapter.kt @@ -145,7 +145,7 @@ class PodcastAdapter( private val onBookmarkRowLongPress: (Bookmark) -> Unit, private val onFoldersClicked: () -> Unit, private val onPodcastDescriptionClicked: () -> Unit, - private val onNotificationsClicked: () -> Unit, + private val onNotificationsClicked: (Podcast, Boolean) -> Unit, private val onSettingsClicked: () -> Unit, private val playButtonListener: PlayButton.OnClickListener, private val onRowClicked: (PodcastEpisode) -> Unit, @@ -161,8 +161,8 @@ class PodcastAdapter( private val onBookmarkPlayClicked: (Bookmark) -> Unit, private val onHeadsetSettingsClicked: () -> Unit, private val onChangeHeaderExpanded: (String, Boolean) -> Unit, - private val onClickRating: (String, RatingTappedSource) -> Unit, - private val onArtworkAvailable: (String) -> Unit, + private val onClickRating: (Podcast, RatingTappedSource) -> Unit, + private val onArtworkAvailable: (Podcast) -> Unit, private val sourceView: SourceView, ) : LargeListAdapter(1500, differ) { @@ -371,7 +371,7 @@ class PodcastAdapter( if (ratingState is RatingState.Loaded) { PodcastRatingRow( state = ratingState, - onClick = { source -> onClickRating(podcast.uuid, source) }, + onClick = { source -> onClickRating(podcast, source) }, ) } }, @@ -806,7 +806,7 @@ class PodcastAdapter( adapter.onFoldersClicked() } binding.top.notifications.setOnClickListener { - adapter.onNotificationsClicked() + adapter.onNotificationsClicked(podcast, !podcast.isShowNotifications) } binding.top.settings.setOnClickListener { adapter.onSettingsClicked() @@ -925,17 +925,17 @@ class PodcastAdapter( context: Context, private val theme: Theme, private val useBlurredArtwork: Boolean, - private val onClickRating: (String, RatingTappedSource) -> Unit, + private val onClickRating: (Podcast, RatingTappedSource) -> Unit, private val onClickFollow: () -> Unit, private val onClickUnfollow: () -> Unit, private val onClickFolder: () -> Unit, - private val onClickNotification: () -> Unit, + private val onClickNotification: (Podcast, Boolean) -> Unit, private val onClickSettings: () -> Unit, private val onClickWebsiteLink: () -> Unit, private val onToggleHeader: () -> Unit, private val onToggleDescription: () -> Unit, private val onLongClickArtwork: () -> Unit, - private val onArtworkAvailable: (String) -> Unit, + private val onArtworkAvailable: (Podcast) -> Unit, ) : RecyclerView.ViewHolder(ComposeView(context)) { private val composeView get() = itemView as ComposeView @@ -988,17 +988,17 @@ class PodcastAdapter( bottom = 16.dp, ), useBlurredArtwork = useBlurredArtwork, - onClickRating = { source -> onClickRating(podcast.uuid, source) }, + onClickRating = { source -> onClickRating(podcast, source) }, onClickFollow = onClickFollow, onClickUnfollow = onClickUnfollow, onClickFolder = onClickFolder, - onClickNotification = onClickNotification, + onClickNotification = { onClickNotification(podcast, !podcast.isShowNotifications) }, onClickSettings = onClickSettings, onClickWebsiteLink = onClickWebsiteLink, onToggleHeader = onToggleHeader, onToggleDescription = onToggleDescription, onLongClickArtwork = onLongClickArtwork, - onArtworkAvailable = { onArtworkAvailable(podcast.uuid) }, + onArtworkAvailable = { onArtworkAvailable(podcast) }, ) } } diff --git a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastFragment.kt b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastFragment.kt index afdf66dc7d6..3335eee9107 100644 --- a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastFragment.kt +++ b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastFragment.kt @@ -10,6 +10,7 @@ import android.view.ViewGroup import android.widget.LinearLayout import android.widget.ProgressBar import android.widget.TextView +import android.widget.Toast import androidx.annotation.ColorInt import androidx.annotation.FloatRange import androidx.annotation.MenuRes @@ -607,10 +608,9 @@ class PodcastFragment : BaseFragment() { viewModel.multiSelectBookmarksHelper.isMultiSelecting = false } - private val onNotificationsClicked: () -> Unit = { - context?.let { - viewModel.toggleNotifications(it) - } + private val onNotificationsClicked: (Podcast, Boolean) -> Unit = { podcast, show -> + viewModel.showNotifications(podcast.uuid, show) + Toast.makeText(context, if (show) LR.string.podcast_notifications_on else LR.string.podcast_notifications_off, Toast.LENGTH_SHORT).show() } private val onSettingsClicked: () -> Unit = { @@ -774,16 +774,16 @@ class PodcastFragment : BaseFragment() { onChangeHeaderExpanded = { uuid, isExpanded -> viewModel.updateIsHeaderExpanded(uuid, isExpanded) }, - onClickRating = { podcastUuid, source -> + onClickRating = { podcast, source -> ratingsViewModel.onRatingStarsTapped( - podcastUuid = podcastUuid, + podcastUuid = podcast.uuid, fragmentManager = parentFragmentManager, source = source, ) }, - onArtworkAvailable = { uuid -> + onArtworkAvailable = { podcast -> viewLifecycleOwner.lifecycleScope.launch { - artworkDominantColor = colorAnalyzer.getArtworkDominantColor(uuid) + artworkDominantColor = colorAnalyzer.getArtworkDominantColor(podcast.uuid) updateStausBarForBackground() } }, diff --git a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/PodcastSettingsViewModel.kt b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/PodcastSettingsViewModel.kt index a7cefd1a069..33edec9c29e 100644 --- a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/PodcastSettingsViewModel.kt +++ b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/PodcastSettingsViewModel.kt @@ -95,7 +95,7 @@ class PodcastSettingsViewModel @Inject constructor( fun showNotifications(show: Boolean) { val podcast = this.podcast.value ?: return launch { - podcastManager.updateShowNotificationsBlocking(podcast, show) + podcastManager.updateShowNotifications(podcast.uuid, show) } } diff --git a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/PodcastViewModel.kt b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/PodcastViewModel.kt index 90692b2384d..304b8e3c46f 100644 --- a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/PodcastViewModel.kt +++ b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/viewmodel/PodcastViewModel.kt @@ -334,13 +334,10 @@ class PodcastViewModel } } - fun toggleNotifications(context: Context) { - val podcast = podcast.value ?: return - val showNotifications = !podcast.isShowNotifications - analyticsTracker.track(AnalyticsEvent.PODCAST_SCREEN_NOTIFICATIONS_TAPPED, AnalyticsProp.notificationEnabled(showNotifications)) - Toast.makeText(context, if (showNotifications) LR.string.podcast_notifications_on else LR.string.podcast_notifications_off, Toast.LENGTH_SHORT).show() - launch { - podcastManager.updateShowNotificationsBlocking(podcast, showNotifications) + fun showNotifications(podcastUuid: String, show: Boolean) { + analyticsTracker.track(AnalyticsEvent.PODCAST_SCREEN_NOTIFICATIONS_TAPPED, AnalyticsProp.notificationEnabled(show)) + viewModelScope.launch { + podcastManager.updateShowNotifications(podcastUuid, show) } } diff --git a/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/NotificationsSettingsFragment.kt b/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/NotificationsSettingsFragment.kt index 06e03c4b91f..449826ea5ed 100644 --- a/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/NotificationsSettingsFragment.kt +++ b/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/NotificationsSettingsFragment.kt @@ -184,7 +184,7 @@ class NotificationsSettingsFragment : override fun podcastSelectFragmentSelectionChanged(newSelection: List) { launch(Dispatchers.Default) { podcastManager.findSubscribedBlocking().forEach { - podcastManager.updateShowNotificationsBlocking(it, newSelection.contains(it.uuid)) + podcastManager.updateShowNotifications(it.uuid, newSelection.contains(it.uuid)) } } } diff --git a/modules/services/model/src/main/java/au/com/shiftyjelly/pocketcasts/models/db/dao/PodcastDao.kt b/modules/services/model/src/main/java/au/com/shiftyjelly/pocketcasts/models/db/dao/PodcastDao.kt index 56c413b4774..7eb6a2d20bd 100644 --- a/modules/services/model/src/main/java/au/com/shiftyjelly/pocketcasts/models/db/dao/PodcastDao.kt +++ b/modules/services/model/src/main/java/au/com/shiftyjelly/pocketcasts/models/db/dao/PodcastDao.kt @@ -305,7 +305,7 @@ abstract class PodcastDao { abstract fun updateEpisodesSortTypeBlocking(episodesSortType: EpisodesSortType, uuid: String, modified: Date = Date()) @Query("UPDATE podcasts SET show_notifications = :show, show_notifications_modified = :modified, sync_status = 0 WHERE uuid = :uuid") - abstract fun updateShowNotificationsBlocking(show: Boolean, uuid: String, modified: Date = Date()) + abstract suspend fun updateShowNotifications(uuid: String, show: Boolean, modified: Date = Date()) @Query("UPDATE podcasts SET subscribed = :subscribed WHERE uuid = :uuid") abstract fun updateSubscribedBlocking(subscribed: Boolean, uuid: String) diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManager.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManager.kt index da9717bd124..858f20a0fe0 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManager.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManager.kt @@ -82,7 +82,7 @@ interface PodcastManager { fun updatePlaybackSpeedBlocking(podcast: Podcast, speed: Double) fun updateEffectsBlocking(podcast: Podcast, effects: PlaybackEffects) fun updateEpisodesSortTypeBlocking(podcast: Podcast, episodesSortType: EpisodesSortType) - fun updateShowNotificationsBlocking(podcast: Podcast, show: Boolean) + suspend fun updateShowNotifications(podcastUuid: String, show: Boolean) suspend fun updatePodcastPositions(podcasts: List) suspend fun updateRefreshAvailable(podcastUuid: String, refreshAvailable: Boolean) suspend fun updateStartFromInSec(podcast: Podcast, autoStartFrom: Int) diff --git a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManagerImpl.kt b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManagerImpl.kt index 95efb0984df..74c754a495a 100644 --- a/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManagerImpl.kt +++ b/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/PodcastManagerImpl.kt @@ -521,11 +521,11 @@ class PodcastManagerImpl @Inject constructor( podcastDao.updateEpisodesSortTypeBlocking(episodesSortType, podcast.uuid) } - override fun updateShowNotificationsBlocking(podcast: Podcast, show: Boolean) { + override suspend fun updateShowNotifications(podcastUuid: String, show: Boolean) { if (show) { settings.notifyRefreshPodcast.set(true, updateModifiedAt = true) } - podcastDao.updateShowNotificationsBlocking(show, podcast.uuid) + podcastDao.updateShowNotifications(podcastUuid, show) } override suspend fun updateRefreshAvailable(podcastUuid: String, refreshAvailable: Boolean) {