Simplify callbacks for handleFullScreenNotificationsPermission

This commit is contained in:
Ensar Sarajčić 2023-08-07 08:22:35 +02:00
parent ca2b14263c
commit e816a02fde
3 changed files with 18 additions and 21 deletions

View file

@ -19,7 +19,6 @@ import com.simplemobiletools.clock.helpers.TOMORROW_BIT
import com.simplemobiletools.clock.helpers.getCurrentDayMinutes
import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
import com.simplemobiletools.commons.dialogs.SelectAlarmSoundDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.models.AlarmSound
@ -159,7 +158,7 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba
alarm.isEnabled = true
var alarmId = alarm.id
activity.handleFullScreenNotificationsPermission { granted, _ ->
activity.handleFullScreenNotificationsPermission { granted ->
if (granted) {
if (alarm.id == 0) {
alarmId = activity.dbHelper.insertAlarm(alarm)
@ -175,11 +174,6 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba
activity.config.alarmLastConfig = alarm
callback(alarmId)
alertDialog.dismiss()
} else {
PermissionRequiredDialog(
activity,
com.simplemobiletools.commons.R.string.allow_notifications_reminders,
{ activity.openNotificationSettings() })
}
}
}

View file

@ -5,14 +5,15 @@ import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
import com.simplemobiletools.commons.extensions.canUseFullScreenIntent
import com.simplemobiletools.commons.extensions.openFullScreenIntentSettings
import com.simplemobiletools.commons.extensions.openNotificationSettings
fun BaseSimpleActivity.handleFullScreenNotificationsPermission(
notificationsCallback: (granted: Boolean, fullScreenGranted: Boolean) -> Unit,
notificationsCallback: (granted: Boolean) -> Unit,
) {
handleNotificationPermission { granted ->
if (granted) {
if (canUseFullScreenIntent()) {
notificationsCallback(true, true)
notificationsCallback(true)
} else {
PermissionRequiredDialog(
activity = this,
@ -21,12 +22,21 @@ fun BaseSimpleActivity.handleFullScreenNotificationsPermission(
openFullScreenIntentSettings(BuildConfig.APPLICATION_ID)
},
negativeActionCallback = {
notificationsCallback(true, false)
notificationsCallback(false)
}
)
}
} else {
notificationsCallback(false, false)
PermissionRequiredDialog(
activity = this,
textId = com.simplemobiletools.commons.R.string.allow_notifications_reminders,
positiveActionCallback = {
openNotificationSettings()
},
negativeActionCallback = {
notificationsCallback(false)
}
)
}
}
}

View file

@ -15,9 +15,7 @@ import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.clock.helpers.*
import com.simplemobiletools.clock.interfaces.ToggleAlarmInterface
import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
import com.simplemobiletools.commons.extensions.getProperTextColor
import com.simplemobiletools.commons.extensions.openNotificationSettings
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_CREATED
@ -124,8 +122,8 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
}
override fun alarmToggled(id: Int, isEnabled: Boolean) {
(activity as SimpleActivity).handleFullScreenNotificationsPermission { granted, fullScreenGranted ->
if (granted && fullScreenGranted) {
(activity as SimpleActivity).handleFullScreenNotificationsPermission { granted ->
if (granted) {
if (requireContext().dbHelper.updateAlarmEnabledState(id, isEnabled)) {
val alarm = alarms.firstOrNull { it.id == id } ?: return@handleFullScreenNotificationsPermission
alarm.isEnabled = isEnabled
@ -134,13 +132,8 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
requireActivity().toast(com.simplemobiletools.commons.R.string.unknown_error_occurred)
}
requireContext().updateWidgets()
} else if (!fullScreenGranted) {
setupAlarms()
} else {
PermissionRequiredDialog(
activity as SimpleActivity,
com.simplemobiletools.commons.R.string.allow_notifications_reminders,
{ (activity as SimpleActivity).openNotificationSettings() })
setupAlarms()
}
}
}