Loading feature/funding/googleplay/build.gradle.kts +1 −0 Original line number Diff line number Diff line Loading @@ -22,4 +22,5 @@ dependencies { testImplementation(projects.core.ui.compose.testing) testImplementation(libs.androidx.lifecycle.runtime.testing) testImplementation(libs.androidx.fragment.testing) } feature/funding/googleplay/src/main/kotlin/app/k9mail/feature/funding/googleplay/ui/reminder/FundingReminder.kt +23 −9 Original line number Diff line number Diff line Loading @@ -3,10 +3,11 @@ package app.k9mail.feature.funding.googleplay.ui.reminder import android.content.Context import android.content.pm.PackageManager.NameNotFoundException import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.FragmentManager import app.k9mail.feature.funding.api.FundingSettings import kotlinx.datetime.Clock import app.k9mail.feature.funding.googleplay.ui.reminder.FundingReminderContract.ActivityLifecycleObserver import app.k9mail.feature.funding.googleplay.ui.reminder.FundingReminderContract.FragmentLifecycleObserver import kotlinx.datetime.Clock class FundingReminder( private val settings: FundingSettings, Loading @@ -20,6 +21,22 @@ class FundingReminder( activity: AppCompatActivity, onOpenFunding: () -> Unit, ) { // TODO: Let the caller make the decision on which FragmentManager to use. val dialogFragmentManager = activity.supportFragmentManager // TODO: Let the caller provide this. Or, better yet, let the caller notify FundingReminder when it's a good // time to display the funding reminder dialog. val observedFragmentManager = activity.supportFragmentManager dialogFragmentManager.setFragmentResultListener( FundingReminderContract.Dialog.FRAGMENT_REQUEST_KEY, activity ) { _, result -> if (result.getBoolean(FundingReminderContract.Dialog.FRAGMENT_RESULT_SHOW_FUNDING, false)) { onOpenFunding() } } // If the reminder reference timestamp is not set, we set it to the first install time. if (settings.getReminderReferenceTimestamp() == 0L) { resetReminderReferenceTimestamp(activity) Loading @@ -28,7 +45,7 @@ class FundingReminder( // We register the activity counter observer to keep track of the time the user spends in the app. // We also ensure that the observers are unregistered when the activity is destroyed. activityCounterObserver.register(activity.lifecycle) { fragmentObserver.unregister(activity.supportFragmentManager) fragmentObserver.unregister(observedFragmentManager) activityCounterObserver.unregister(activity.lifecycle) } Loading @@ -38,8 +55,8 @@ class FundingReminder( } if (shouldShowReminder()) { fragmentObserver.register(activity.supportFragmentManager) { showFundingReminderDialog(activity, onOpenFunding) fragmentObserver.register(observedFragmentManager) { showFundingReminderDialog(dialogFragmentManager) } } } Loading @@ -66,14 +83,11 @@ class FundingReminder( } } private fun showFundingReminderDialog( activity: AppCompatActivity, openFunding: () -> Unit, ) { private fun showFundingReminderDialog(fragmentManager: FragmentManager) { // We're about to show the funding reminder dialog. So mark it as being shown. This way, if there's an error, // we err on the side of the dialog not being shown rather than it being shown more than once. settings.setReminderShownTimestamp(clock.now().toEpochMilliseconds()) dialog.show(activity, openFunding) dialog.show(fragmentManager) } } feature/funding/googleplay/src/main/kotlin/app/k9mail/feature/funding/googleplay/ui/reminder/FundingReminderContract.kt +6 −1 Original line number Diff line number Diff line Loading @@ -16,7 +16,12 @@ interface FundingReminderContract { } fun interface Dialog { fun show(activity: AppCompatActivity, onOpenFunding: () -> Unit) fun show(fragmentManager: FragmentManager) companion object { const val FRAGMENT_REQUEST_KEY = "funding_reminder_dialog" const val FRAGMENT_RESULT_SHOW_FUNDING = "show_funding" } } interface FragmentLifecycleObserver { Loading feature/funding/googleplay/src/main/kotlin/app/k9mail/feature/funding/googleplay/ui/reminder/FundingReminderDialog.kt +4 −13 Original line number Diff line number Diff line package app.k9mail.feature.funding.googleplay.ui.reminder import androidx.appcompat.app.AppCompatActivity import app.k9mail.feature.funding.googleplay.R import com.google.android.material.dialog.MaterialAlertDialogBuilder import androidx.fragment.app.FragmentManager class FundingReminderDialog : FundingReminderContract.Dialog { override fun show(activity: AppCompatActivity, onOpenFunding: () -> Unit) { MaterialAlertDialogBuilder(activity) .setIcon(R.drawable.funding_googleplay_contribution_reminder_icon) .setTitle(R.string.funding_googleplay_contribution_reminder_title) .setMessage(R.string.funding_googleplay_contribution_reminder_message) .setPositiveButton(R.string.funding_googleplay_contribution_reminder_positive_button) { _, _ -> onOpenFunding() } .setNegativeButton(R.string.funding_googleplay_contribution_reminder_negative_button, null) .show() override fun show(fragmentManager: FragmentManager) { val dialogFragment = FundingReminderDialogFragment() dialogFragment.show(fragmentManager, null) } } feature/funding/googleplay/src/main/kotlin/app/k9mail/feature/funding/googleplay/ui/reminder/FundingReminderDialogFragment.kt 0 → 100644 +30 −0 Original line number Diff line number Diff line package app.k9mail.feature.funding.googleplay.ui.reminder import android.app.Dialog import android.os.Bundle import androidx.core.os.bundleOf import androidx.fragment.app.DialogFragment import androidx.fragment.app.setFragmentResult import app.k9mail.feature.funding.googleplay.R import com.google.android.material.dialog.MaterialAlertDialogBuilder internal class FundingReminderDialogFragment : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { return MaterialAlertDialogBuilder(requireContext()) .setIcon(R.drawable.funding_googleplay_contribution_reminder_icon) .setTitle(R.string.funding_googleplay_contribution_reminder_title) .setMessage(R.string.funding_googleplay_contribution_reminder_message) .setPositiveButton(R.string.funding_googleplay_contribution_reminder_positive_button) { _, _ -> handlePositiveButton() } .setNegativeButton(R.string.funding_googleplay_contribution_reminder_negative_button, null) .create() } private fun handlePositiveButton() { setFragmentResult( FundingReminderContract.Dialog.FRAGMENT_REQUEST_KEY, bundleOf(FundingReminderContract.Dialog.FRAGMENT_RESULT_SHOW_FUNDING to true), ) } } Loading
feature/funding/googleplay/build.gradle.kts +1 −0 Original line number Diff line number Diff line Loading @@ -22,4 +22,5 @@ dependencies { testImplementation(projects.core.ui.compose.testing) testImplementation(libs.androidx.lifecycle.runtime.testing) testImplementation(libs.androidx.fragment.testing) }
feature/funding/googleplay/src/main/kotlin/app/k9mail/feature/funding/googleplay/ui/reminder/FundingReminder.kt +23 −9 Original line number Diff line number Diff line Loading @@ -3,10 +3,11 @@ package app.k9mail.feature.funding.googleplay.ui.reminder import android.content.Context import android.content.pm.PackageManager.NameNotFoundException import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.FragmentManager import app.k9mail.feature.funding.api.FundingSettings import kotlinx.datetime.Clock import app.k9mail.feature.funding.googleplay.ui.reminder.FundingReminderContract.ActivityLifecycleObserver import app.k9mail.feature.funding.googleplay.ui.reminder.FundingReminderContract.FragmentLifecycleObserver import kotlinx.datetime.Clock class FundingReminder( private val settings: FundingSettings, Loading @@ -20,6 +21,22 @@ class FundingReminder( activity: AppCompatActivity, onOpenFunding: () -> Unit, ) { // TODO: Let the caller make the decision on which FragmentManager to use. val dialogFragmentManager = activity.supportFragmentManager // TODO: Let the caller provide this. Or, better yet, let the caller notify FundingReminder when it's a good // time to display the funding reminder dialog. val observedFragmentManager = activity.supportFragmentManager dialogFragmentManager.setFragmentResultListener( FundingReminderContract.Dialog.FRAGMENT_REQUEST_KEY, activity ) { _, result -> if (result.getBoolean(FundingReminderContract.Dialog.FRAGMENT_RESULT_SHOW_FUNDING, false)) { onOpenFunding() } } // If the reminder reference timestamp is not set, we set it to the first install time. if (settings.getReminderReferenceTimestamp() == 0L) { resetReminderReferenceTimestamp(activity) Loading @@ -28,7 +45,7 @@ class FundingReminder( // We register the activity counter observer to keep track of the time the user spends in the app. // We also ensure that the observers are unregistered when the activity is destroyed. activityCounterObserver.register(activity.lifecycle) { fragmentObserver.unregister(activity.supportFragmentManager) fragmentObserver.unregister(observedFragmentManager) activityCounterObserver.unregister(activity.lifecycle) } Loading @@ -38,8 +55,8 @@ class FundingReminder( } if (shouldShowReminder()) { fragmentObserver.register(activity.supportFragmentManager) { showFundingReminderDialog(activity, onOpenFunding) fragmentObserver.register(observedFragmentManager) { showFundingReminderDialog(dialogFragmentManager) } } } Loading @@ -66,14 +83,11 @@ class FundingReminder( } } private fun showFundingReminderDialog( activity: AppCompatActivity, openFunding: () -> Unit, ) { private fun showFundingReminderDialog(fragmentManager: FragmentManager) { // We're about to show the funding reminder dialog. So mark it as being shown. This way, if there's an error, // we err on the side of the dialog not being shown rather than it being shown more than once. settings.setReminderShownTimestamp(clock.now().toEpochMilliseconds()) dialog.show(activity, openFunding) dialog.show(fragmentManager) } }
feature/funding/googleplay/src/main/kotlin/app/k9mail/feature/funding/googleplay/ui/reminder/FundingReminderContract.kt +6 −1 Original line number Diff line number Diff line Loading @@ -16,7 +16,12 @@ interface FundingReminderContract { } fun interface Dialog { fun show(activity: AppCompatActivity, onOpenFunding: () -> Unit) fun show(fragmentManager: FragmentManager) companion object { const val FRAGMENT_REQUEST_KEY = "funding_reminder_dialog" const val FRAGMENT_RESULT_SHOW_FUNDING = "show_funding" } } interface FragmentLifecycleObserver { Loading
feature/funding/googleplay/src/main/kotlin/app/k9mail/feature/funding/googleplay/ui/reminder/FundingReminderDialog.kt +4 −13 Original line number Diff line number Diff line package app.k9mail.feature.funding.googleplay.ui.reminder import androidx.appcompat.app.AppCompatActivity import app.k9mail.feature.funding.googleplay.R import com.google.android.material.dialog.MaterialAlertDialogBuilder import androidx.fragment.app.FragmentManager class FundingReminderDialog : FundingReminderContract.Dialog { override fun show(activity: AppCompatActivity, onOpenFunding: () -> Unit) { MaterialAlertDialogBuilder(activity) .setIcon(R.drawable.funding_googleplay_contribution_reminder_icon) .setTitle(R.string.funding_googleplay_contribution_reminder_title) .setMessage(R.string.funding_googleplay_contribution_reminder_message) .setPositiveButton(R.string.funding_googleplay_contribution_reminder_positive_button) { _, _ -> onOpenFunding() } .setNegativeButton(R.string.funding_googleplay_contribution_reminder_negative_button, null) .show() override fun show(fragmentManager: FragmentManager) { val dialogFragment = FundingReminderDialogFragment() dialogFragment.show(fragmentManager, null) } }
feature/funding/googleplay/src/main/kotlin/app/k9mail/feature/funding/googleplay/ui/reminder/FundingReminderDialogFragment.kt 0 → 100644 +30 −0 Original line number Diff line number Diff line package app.k9mail.feature.funding.googleplay.ui.reminder import android.app.Dialog import android.os.Bundle import androidx.core.os.bundleOf import androidx.fragment.app.DialogFragment import androidx.fragment.app.setFragmentResult import app.k9mail.feature.funding.googleplay.R import com.google.android.material.dialog.MaterialAlertDialogBuilder internal class FundingReminderDialogFragment : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { return MaterialAlertDialogBuilder(requireContext()) .setIcon(R.drawable.funding_googleplay_contribution_reminder_icon) .setTitle(R.string.funding_googleplay_contribution_reminder_title) .setMessage(R.string.funding_googleplay_contribution_reminder_message) .setPositiveButton(R.string.funding_googleplay_contribution_reminder_positive_button) { _, _ -> handlePositiveButton() } .setNegativeButton(R.string.funding_googleplay_contribution_reminder_negative_button, null) .create() } private fun handlePositiveButton() { setFragmentResult( FundingReminderContract.Dialog.FRAGMENT_REQUEST_KEY, bundleOf(FundingReminderContract.Dialog.FRAGMENT_RESULT_SHOW_FUNDING to true), ) } }