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

Code before:

        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 TrendingAndRecommendationsNotificationType.Recommendations -> {
                val isSignedIn = userManager.getSignInState().map { it.isSignedIn }.asFlow().firstOrNull()
                isSignedIn ?: false
            }
            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()
            }

Review comment (suggestion):
Can be expressed as

```kotlin
userManager.getSignInState().awaitFirstOrNull()?.isSignedIn == true
```

Code after:

        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 TrendingAndRecommendationsNotificationType.Recommendations -> {
                userManager.getSignInState().awaitFirstOrNull()?.isSignedIn == true
            }
            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()
            }