Repository: Automattic/pocket-casts-android
File: modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/notifications/NotificationsSettingsViewModel.kt (Kotlin)

Code before:
                    analyticsTracker.track(
                        AnalyticsEvent.SETTINGS_NOTIFICATIONS_TRENDING_AND_RECOMMENDATIONS_TOGGLED,
                        mapOf("enabled" to preference.isEnabled),
                    )
                    if (preference.isEnabled) {
                        notificationScheduler.setupTrendingAndRecommendationsNotifications()
                    } else {
                        notificationScheduler.cancelScheduledTrendingAndRecommendationsNotifications()
                    }
                }

                is NotificationPreferenceType.RecommendationSettings -> {
                    analyticsTracker.track(AnalyticsEvent.SETTINGS_TRENDING_AND_RECOMMENDATIONS_ADVANCED_SETTINGS_TAPPED)
                }

                is NotificationPreferenceType.NotifyOnThesePodcasts -> Unit
            }
            loadPreferences()
        }
    }

    internal fun onShown() {
        analyticsTracker.track(AnalyticsEvent.SETTINGS_NOTIFICATIONS_SHOWN)
    }

    internal fun onResumed() {
        _state.update {
            it.copy(areSystemNotificationsEnabled = notificationHelper.hasNotificationsPermission())
        }
    }

    internal fun onSelectedPodcastsChanged(newSelection: List<String>) {
        viewModelScope.launch(Dispatchers.IO) {
            podcastManager.findSubscribedBlocking().forEach {
                podcastManager.updateShowNotifications(it.uuid, newSelection.contains(it.uuid))
            }
            loadPreferences()
        }
    }

    internal suspend fun getSelectedPodcastIds(): List<String> = withContext(Dispatchers.IO) {
        val uuids = podcastManager.findSubscribedBlocking().filter { it.isShowNotifications }.map { it.uuid }
        uuids
    }

    internal data class State(
        val areSystemNotificationsEnabled: Boolean,
        val categories: List<NotificationPreferenceCategory>,
    )
}


Review comment (bug):
Can we avoid such function naming? `ViewModel` doesn't have a lifecycle so `onResumed()` doesn't relate to it. I think being explicit with something like `checkNotificationPermission()` is clearer.

Code after:
                    analyticsTracker.track(AnalyticsEvent.SETTINGS_DAILY_REMINDERS_ADVANCED_SETTINGS_TAPPED)
                }

                is NotificationPreferenceType.EnableRecommendations -> {
                    preferenceRepository.setPreference(preference)
                    analyticsTracker.track(
                        AnalyticsEvent.SETTINGS_NOTIFICATIONS_TRENDING_AND_RECOMMENDATIONS_TOGGLED,
                        mapOf("enabled" to preference.isEnabled),
                    )
                    if (preference.isEnabled) {
                        notificationScheduler.setupTrendingAndRecommendationsNotifications()
                    } else {
                        notificationScheduler.cancelScheduledTrendingAndRecommendationsNotifications()
                    }
                }

                is NotificationPreferenceType.RecommendationSettings -> {
                    analyticsTracker.track(AnalyticsEvent.SETTINGS_TRENDING_AND_RECOMMENDATIONS_ADVANCED_SETTINGS_TAPPED)
                }

                is NotificationPreferenceType.NotifyOnThesePodcasts -> Unit
            }
            loadPreferences()
        }
    }

    internal fun onShown() {
        analyticsTracker.track(AnalyticsEvent.SETTINGS_NOTIFICATIONS_SHOWN)
    }

    internal fun checkNotificationPermission() {
        _state.update {
            it.copy(areSystemNotificationsEnabled = notificationHelper.hasNotificationsPermission())
        }
    }

    internal fun onSelectedPodcastsChanged(newSelection: List<String>) {
        viewModelScope.launch(Dispatchers.IO) {
            podcastManager.findSubscribedBlocking().forEach {
                podcastManager.updateShowNotifications(it.uuid, newSelection.contains(it.uuid))
            }
            loadPreferences()
        }
    }

    internal suspend fun getSelectedPodcastIds(): List<String> = withContext(Dispatchers.IO) {
        val uuids = podcastManager.findSubscribedBlocking().filter { it.isShowNotifications }.map { it.uuid }
        uuids
    }

    internal data class State(