|
| 1 | +package com.fsck.k9.activity.compose |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import android.view.LayoutInflater |
| 5 | +import android.view.View |
| 6 | +import android.view.ViewGroup |
| 7 | +import androidx.compose.animation.animateContentSize |
| 8 | +import androidx.compose.ui.Modifier |
| 9 | +import androidx.compose.ui.platform.ComposeView |
| 10 | +import androidx.compose.ui.platform.ViewCompositionStrategy |
| 11 | +import androidx.core.os.bundleOf |
| 12 | +import androidx.fragment.app.Fragment |
| 13 | +import com.google.android.material.snackbar.Snackbar |
| 14 | +import kotlinx.collections.immutable.persistentSetOf |
| 15 | +import net.thunderbird.core.logging.Logger |
| 16 | +import net.thunderbird.core.ui.theme.api.FeatureThemeProvider |
| 17 | +import net.thunderbird.feature.account.AccountId |
| 18 | +import net.thunderbird.feature.account.AccountIdFactory |
| 19 | +import net.thunderbird.feature.notification.api.ui.InAppNotificationHost |
| 20 | +import net.thunderbird.feature.notification.api.ui.action.NotificationAction |
| 21 | +import net.thunderbird.feature.notification.api.ui.host.DisplayInAppNotificationFlag |
| 22 | +import net.thunderbird.feature.notification.api.ui.host.visual.SnackbarVisual |
| 23 | +import net.thunderbird.feature.notification.api.ui.style.SnackbarDuration |
| 24 | +import org.koin.android.ext.android.inject |
| 25 | + |
| 26 | +private const val TAG = "MessageComposeInAppNotificationFragment" |
| 27 | + |
| 28 | +class MessageComposeInAppNotificationFragment : Fragment() { |
| 29 | + private val themeProvider: FeatureThemeProvider by inject() |
| 30 | + private val logger: Logger by inject() |
| 31 | + private var parentView: View? = null |
| 32 | + private var accountId: AccountId? = null |
| 33 | + |
| 34 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 35 | + super.onCreate(savedInstanceState) |
| 36 | + arguments?.let { arg -> |
| 37 | + accountId = requireNotNull(arg.getString(ARG_ACCOUNT_ID)?.let { AccountIdFactory.of(it) }) { |
| 38 | + "Argument $ARG_ACCOUNT_ID is required" |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View = |
| 44 | + ComposeView(requireContext()).apply { |
| 45 | + setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) |
| 46 | + parentView = container |
| 47 | + setContent { |
| 48 | + themeProvider.WithTheme { |
| 49 | + InAppNotificationHost( |
| 50 | + onActionClick = ::onNotificationActionClick, |
| 51 | + enabled = persistentSetOf( |
| 52 | + DisplayInAppNotificationFlag.BannerGlobalNotifications, |
| 53 | + DisplayInAppNotificationFlag.SnackbarNotifications, |
| 54 | + ), |
| 55 | + onSnackbarNotificationEvent = ::onSnackbarInAppNotificationEvent, |
| 56 | + eventFilter = { event -> |
| 57 | + val accountUuid = event.notification.accountUuid |
| 58 | + accountUuid != null && accountUuid == accountId?.asRaw() |
| 59 | + }, |
| 60 | + modifier = Modifier.animateContentSize(), |
| 61 | + ) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + override fun onDestroyView() { |
| 67 | + super.onDestroyView() |
| 68 | + parentView = null |
| 69 | + } |
| 70 | + |
| 71 | + private suspend fun onSnackbarInAppNotificationEvent(visual: SnackbarVisual) { |
| 72 | + parentView?.let { view -> |
| 73 | + val (message, action, duration) = visual |
| 74 | + Snackbar.make( |
| 75 | + view, |
| 76 | + message, |
| 77 | + when (duration) { |
| 78 | + SnackbarDuration.Short -> Snackbar.LENGTH_SHORT |
| 79 | + SnackbarDuration.Long -> Snackbar.LENGTH_LONG |
| 80 | + SnackbarDuration.Indefinite -> Snackbar.LENGTH_INDEFINITE |
| 81 | + }, |
| 82 | + ).apply { |
| 83 | + if (action != null) { |
| 84 | + setAction(action.resolveTitle()) { |
| 85 | + // TODO. |
| 86 | + } |
| 87 | + } |
| 88 | + }.show() |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private fun onNotificationActionClick(action: NotificationAction) { |
| 93 | + logger.verbose(TAG) { "onNotificationActionClick() called with: action = $action" } |
| 94 | + } |
| 95 | + |
| 96 | + companion object { |
| 97 | + private const val ARG_ACCOUNT_ID = "MessageComposeInAppNotificationFragment_account_id" |
| 98 | + const val FRAGMENT_TAG = "MessageComposeInAppNotificationFragment" |
| 99 | + |
| 100 | + fun newInstance(accountId: AccountId): MessageComposeInAppNotificationFragment = |
| 101 | + MessageComposeInAppNotificationFragment().apply { |
| 102 | + arguments = bundleOf(ARG_ACCOUNT_ID to accountId.asRaw()) |
| 103 | + } |
| 104 | + |
| 105 | + @JvmStatic |
| 106 | + fun newInstance(accountUuid: String): MessageComposeInAppNotificationFragment = |
| 107 | + newInstance(AccountIdFactory.of(accountUuid)) |
| 108 | + } |
| 109 | +} |
0 commit comments