|
| 1 | +package com.flipcash.app.notifications |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.annotation.StringRes |
| 5 | +import androidx.core.app.NotificationChannelCompat |
| 6 | +import androidx.core.app.NotificationChannelGroupCompat |
| 7 | +import androidx.core.app.NotificationManagerCompat |
| 8 | +import com.flipcash.services.models.NotificationCategory |
| 9 | +import com.flipcash.shared.notifications.R |
| 10 | + |
| 11 | +object NotificationChannels { |
| 12 | + const val CHANNEL_DEFAULT = "fcm_fallback_notification_channel" |
| 13 | + const val CHANNEL_DEPOSIT_WITHDRAWAL = "channel_deposit_withdrawal" |
| 14 | + const val CHANNEL_BUY_SELL = "channel_buy_sell" |
| 15 | + const val CHANNEL_GAIN = "channel_gain" |
| 16 | + const val CHANNEL_CHAT = "channel_chat" |
| 17 | + const val CHANNEL_CONTACT_JOIN = "channel_contact_join" |
| 18 | + |
| 19 | + private const val GROUP_TRANSACTIONS = "group_transactions" |
| 20 | + private const val GROUP_SOCIAL = "group_social" |
| 21 | + |
| 22 | + private data class ChannelDef( |
| 23 | + val id: String, |
| 24 | + @StringRes val nameRes: Int, |
| 25 | + @StringRes val descriptionRes: Int, |
| 26 | + val importance: Int, |
| 27 | + val groupId: String? = null, |
| 28 | + val showBadge: Boolean = true, |
| 29 | + ) |
| 30 | + |
| 31 | + fun ensureChannelGroups(context: Context, manager: NotificationManagerCompat) { |
| 32 | + val groups = listOf( |
| 33 | + NotificationChannelGroupCompat.Builder(GROUP_TRANSACTIONS) |
| 34 | + .setName(context.getString(R.string.notification_group_transactions)) |
| 35 | + .build(), |
| 36 | + NotificationChannelGroupCompat.Builder(GROUP_SOCIAL) |
| 37 | + .setName(context.getString(R.string.notification_group_social)) |
| 38 | + .build(), |
| 39 | + ) |
| 40 | + groups.forEach { manager.createNotificationChannelGroup(it) } |
| 41 | + } |
| 42 | + |
| 43 | + fun channelFor(context: Context, category: NotificationCategory): NotificationChannelCompat { |
| 44 | + val def = when (category) { |
| 45 | + NotificationCategory.DEFAULT -> ChannelDef( |
| 46 | + id = CHANNEL_DEFAULT, |
| 47 | + nameRes = R.string.notification_channel_default, |
| 48 | + descriptionRes = R.string.notification_channel_default_description, |
| 49 | + importance = NotificationManagerCompat.IMPORTANCE_DEFAULT, |
| 50 | + showBadge = false, |
| 51 | + ) |
| 52 | + NotificationCategory.DEPOSIT_WITHDRAWAL -> ChannelDef( |
| 53 | + id = CHANNEL_DEPOSIT_WITHDRAWAL, |
| 54 | + nameRes = R.string.notification_channel_deposit_withdrawal, |
| 55 | + descriptionRes = R.string.notification_channel_deposit_withdrawal_description, |
| 56 | + importance = NotificationManagerCompat.IMPORTANCE_HIGH, |
| 57 | + groupId = GROUP_TRANSACTIONS, |
| 58 | + ) |
| 59 | + NotificationCategory.BUY_SELL -> ChannelDef( |
| 60 | + id = CHANNEL_BUY_SELL, |
| 61 | + nameRes = R.string.notification_channel_buy_sell, |
| 62 | + descriptionRes = R.string.notification_channel_buy_sell_description, |
| 63 | + importance = NotificationManagerCompat.IMPORTANCE_HIGH, |
| 64 | + groupId = GROUP_TRANSACTIONS, |
| 65 | + ) |
| 66 | + NotificationCategory.GAIN -> ChannelDef( |
| 67 | + id = CHANNEL_GAIN, |
| 68 | + nameRes = R.string.notification_channel_gain, |
| 69 | + descriptionRes = R.string.notification_channel_gain_description, |
| 70 | + importance = NotificationManagerCompat.IMPORTANCE_HIGH, |
| 71 | + groupId = GROUP_TRANSACTIONS, |
| 72 | + ) |
| 73 | + NotificationCategory.CHAT -> ChannelDef( |
| 74 | + id = CHANNEL_CHAT, |
| 75 | + nameRes = R.string.notification_channel_chat, |
| 76 | + descriptionRes = R.string.notification_channel_chat_description, |
| 77 | + importance = NotificationManagerCompat.IMPORTANCE_HIGH, |
| 78 | + groupId = GROUP_SOCIAL, |
| 79 | + ) |
| 80 | + NotificationCategory.CONTACT_JOIN -> ChannelDef( |
| 81 | + id = CHANNEL_CONTACT_JOIN, |
| 82 | + nameRes = R.string.notification_channel_contact_join, |
| 83 | + descriptionRes = R.string.notification_channel_contact_join_description, |
| 84 | + importance = NotificationManagerCompat.IMPORTANCE_HIGH, |
| 85 | + groupId = GROUP_SOCIAL, |
| 86 | + showBadge = false, |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + return NotificationChannelCompat.Builder(def.id, def.importance) |
| 91 | + .setName(context.getString(def.nameRes)) |
| 92 | + .setDescription(context.getString(def.descriptionRes)) |
| 93 | + .setShowBadge(def.showBadge) |
| 94 | + .apply { def.groupId?.let { setGroup(it) } } |
| 95 | + .build() |
| 96 | + } |
| 97 | +} |
0 commit comments