Repository: Automattic/pocket-casts-android
File: modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/notification/NotificationWorker.kt (Kotlin)

Code before:
    override suspend fun doWork(): Result {
        val subcategory = inputData.getString(SUBCATEGORY) ?: return Result.failure()

        val type = NotificationType.fromSubCategory(subcategory) ?: return Result.failure()

        if (!type.isSettingsToggleOn(settings)) {
            return Result.failure()
        }

        if (notificationManager.hasUserInteractedWithFeature(type) && shouldSchedule(type)) {
            return Result.failure()
        }

        val notification = getNotificationBuilder(type).build()

        if (ActivityCompat.checkSelfPermission(applicationContext, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED && FeatureFlag.isEnabled(Feature.NOTIFICATIONS_REVAMP)) {
            NotificationManagerCompat.from(applicationContext).notify(type.notificationId, notification)
            notificationManager.updateNotificationSent(type)
        }

        return Result.success()
    }

    private fun shouldSchedule(type: NotificationType): Boolean {
        return when (type) {
            is NewFeaturesAndTipsNotificationType.SmartFolders -> podcastManager.getSubscribedPodcastUuidsRxSingle().blockingGet().isNotEmpty()
            else -> true
        }
    }

    private fun getNotificationBuilder(type: NotificationType): NotificationCompat.Builder {
        val downloadedEpisodes = inputData.getInt(DOWNLOADED_EPISODES, 0)

        val builder = when (type) {
            is TrendingAndRecommendationsNotificationType -> {
                notificationHelper.trendingAndRecommendationsChannelBuilder()
            }

            is NewFeaturesAndTipsNotificationType -> {
                notificationHelper.featuresAndTipsChannelBuilder()
            }

            else -> notificationHelper.dailyRemindersChannelBuilder()
        }

        return builder
            .setSmallIcon(IR.drawable.notification)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle(applicationContext.resources.getString(type.titleRes))
            .setContentText(type.formattedMessage(applicationContext, downloadedEpisodes))
            .setColor(ContextCompat.getColor(applicationContext, R.color.notification_color))

Review comment (suggestion):
This could be changed to a `suspend` function with using `await()` instead of `blockingGet()`. It doesn't **really** matter here since worker is executed on the background thread anyway but avoiding blocking is a good habit to have.

Code after:
    private val suggestedFoldersManager: SuggestedFoldersManager,
) : CoroutineWorker(context, params) {
    override suspend fun doWork(): Result {
        val subcategory = inputData.getString(SUBCATEGORY) ?: return Result.failure()

        val type = NotificationType.fromSubCategory(subcategory) ?: return Result.failure()

        if (!type.isSettingsToggleOn(settings)) {
            return Result.failure()
        }

        if (notificationManager.hasUserInteractedWithFeature(type) || !shouldSchedule(type)) {
            return Result.failure()
        }

        val notification = getNotificationBuilder(type).build()

        if (ActivityCompat.checkSelfPermission(applicationContext, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED && FeatureFlag.isEnabled(Feature.NOTIFICATIONS_REVAMP)) {
            NotificationManagerCompat.from(applicationContext).notify(type.notificationId, notification)
            notificationManager.updateNotificationSent(type)
        }

        return Result.success()
    }

    private suspend fun shouldSchedule(type: NotificationType): Boolean {
        return when (type) {
            is NewFeaturesAndTipsNotificationType.SmartFolders -> {
                suggestedFoldersManager.refreshSuggestedFolders()
                val folders = suggestedFoldersManager.observeSuggestedFolders().firstOrNull()
                !folders.isNullOrEmpty()
            }
            is OffersNotificationType.UpgradeNow -> {
                val subscription = settings.cachedSubscription.value
                subscription == null || subscription.expiryDate.isBefore(Instant.now())
            }
            else -> true
        }
    }

    private fun getNotificationBuilder(type: NotificationType): NotificationCompat.Builder {
        val downloadedEpisodes = inputData.getInt(DOWNLOADED_EPISODES, 0)

        val builder = when (type) {
            is TrendingAndRecommendationsNotificationType -> {
                notificationHelper.trendingAndRecommendationsChannelBuilder()
            }

            is NewFeaturesAndTipsNotificationType -> {
                notificationHelper.featuresAndTipsChannelBuilder()
            }