Skip to content

Commit

Permalink
Improve notifications permission flow on blogging reminders
Browse files Browse the repository at this point in the history
  • Loading branch information
irfano committed Apr 6, 2023
1 parent fb0ece3 commit af470ad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ class BloggingReminderBottomSheetFragment : BottomSheetDialogFragment() {
if (granted) {
viewModel.onPermissionGranted()
} else {
viewModel.onPermissionDenied()
val isAlwaysDenied = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
WPPermissionUtils.isPermissionAlwaysDenied(
requireActivity(),
Manifest.permission.POST_NOTIFICATIONS
)
viewModel.onPermissionDenied(isAlwaysDenied)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class BloggingRemindersViewModel @Inject constructor(

private val _bloggingRemindersModel = MutableLiveData<BloggingRemindersUiModel>()
private val _isFirstTimeFlow = MutableLiveData<Boolean>()
private val _hasNotificationsPermission = MutableLiveData<Boolean>()
private val _notificationsPermissionAlwaysDenied = MutableLiveData<Boolean>()
private var hasNotificationsPermissionState = false
private var notificationsPermissionAlwaysDeniedState = false

val uiState: LiveData<UiState> = merge(
selectedScreen,
Expand All @@ -85,7 +85,10 @@ class BloggingRemindersViewModel @Inject constructor(
this::showBloggingPromptDialog
)
Screen.NOTIFICATIONS_PERMISSION -> {
notificationsPermissionBuilder.buildUiItems(resourceProvider.getString(R.string.app_name))
notificationsPermissionBuilder.buildUiItems(
appName = resourceProvider.getString(R.string.app_name),
showAppSettingsGuide = notificationsPermissionAlwaysDeniedState
)
}
Screen.EPILOGUE -> epilogueBuilder.buildUiItems(bloggingRemindersModel)
}
Expand All @@ -99,7 +102,9 @@ class BloggingRemindersViewModel @Inject constructor(
isFirstTimeFlow == true,
this::onSelectionButtonClick
)
Screen.NOTIFICATIONS_PERMISSION -> notificationsPermissionBuilder.buildPrimaryButton(showAppSettings)
Screen.NOTIFICATIONS_PERMISSION -> {
notificationsPermissionBuilder.buildPrimaryButton(onPermissionButtonTapped)
}
Screen.EPILOGUE -> epilogueBuilder.buildPrimaryButton(finish)
}
UiState(uiItems, primaryButton)
Expand All @@ -119,8 +124,12 @@ class BloggingRemindersViewModel @Inject constructor(
_isBottomSheetShowing.value = Event(false)
}

private val showAppSettings: () -> Unit = {
_showDevicePermissionSettings.value = Event(true)
private val onPermissionButtonTapped: () -> Unit = {
if (notificationsPermissionAlwaysDeniedState) {
_showDevicePermissionSettings.value = Event(true)
} else {
_requestPermission.value = Event(true)
}
}

private fun onScreenChanged(screen: Screen) {
Expand Down Expand Up @@ -155,7 +164,7 @@ class BloggingRemindersViewModel @Inject constructor(
}
}

fun selectDay(day: DayOfWeek) {
private fun selectDay(day: DayOfWeek) {
val currentState = _bloggingRemindersModel.value!!
val enabledDays = currentState.enabledDays.toMutableSet()
if (enabledDays.contains(day)) {
Expand All @@ -167,11 +176,11 @@ class BloggingRemindersViewModel @Inject constructor(
}

fun setPermissionState(hasNotificationsPermission: Boolean, notificationsPermissionAlwaysDenied: Boolean) {
_hasNotificationsPermission.value = hasNotificationsPermission
_notificationsPermissionAlwaysDenied.value = notificationsPermissionAlwaysDenied
hasNotificationsPermissionState = hasNotificationsPermission
notificationsPermissionAlwaysDeniedState = notificationsPermissionAlwaysDenied
}

fun selectTime() {
private fun selectTime() {
_isTimePickerShowing.value = Event(true)
}

Expand Down Expand Up @@ -233,14 +242,12 @@ class BloggingRemindersViewModel @Inject constructor(
}

private fun checkPermission(): Boolean {
val hasPermission = _hasNotificationsPermission.value == true
val alwaysDenied = _notificationsPermissionAlwaysDenied.value == true
return when {
!hasPermission && alwaysDenied -> {
!hasNotificationsPermissionState && notificationsPermissionAlwaysDeniedState -> {
_selectedScreen.value = Screen.NOTIFICATIONS_PERMISSION
false
}
!hasPermission -> {
!hasNotificationsPermissionState -> {
_requestPermission.value = Event(true)
false
}
Expand Down Expand Up @@ -327,18 +334,21 @@ class BloggingRemindersViewModel @Inject constructor(
}

fun onPermissionGranted() {
if (_hasNotificationsPermission.value == false) {
if (!hasNotificationsPermissionState) {
// Permission state is changed.
_hasNotificationsPermission.value = true
_notificationsPermissionAlwaysDenied.value = false
hasNotificationsPermissionState = true
notificationsPermissionAlwaysDeniedState = false

if (_selectedScreen.value == Screen.NOTIFICATIONS_PERMISSION) {
onSelectionButtonClick(_bloggingRemindersModel.value)
}
}
}

fun onPermissionDenied() {
_hasNotificationsPermission.value = false
fun onPermissionDenied(isAlwaysDenied: Boolean) {
hasNotificationsPermissionState = false
notificationsPermissionAlwaysDeniedState = isAlwaysDenied

_selectedScreen.value = Screen.NOTIFICATIONS_PERMISSION
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ import org.wordpress.android.ui.utils.UiString.UiStringRes
import javax.inject.Inject

class NotificationsPermissionBuilder @Inject constructor() {
fun buildUiItems(appName: String): List<BloggingRemindersItem> {
fun buildUiItems(appName: String, showAppSettingsGuide: Boolean): List<BloggingRemindersItem> {
val title = UiStringRes(string.blogging_reminders_notifications_permission_title)

val body = UiString.UiStringResWithParams(
string.blogging_reminders_notifications_permission_description,
UiString.UiStringText(appName)
)

return listOf(
val uiItems = mutableListOf(
Illustration(drawable.img_illustration_bell_yellow_96dp),
Title(title),
Caption(UiStringRes(string.blogging_reminders_notifications_permission_caption)),
HighEmphasisText(EmphasizedText(body, false))
Caption(UiStringRes(string.blogging_reminders_notifications_permission_caption))
)
if (showAppSettingsGuide) {
uiItems.add(HighEmphasisText(EmphasizedText(body, false)))
}
return uiItems
}

fun buildPrimaryButton(onDone: () -> Unit): PrimaryButton {
Expand Down

0 comments on commit af470ad

Please sign in to comment.