2018-02-28 12:50:52 +01:00
|
|
|
package com.simplemobiletools.clock.fragments
|
|
|
|
|
2018-03-01 16:25:11 +01:00
|
|
|
import android.os.Bundle
|
2021-04-19 15:34:52 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2020-02-14 19:21:49 +01:00
|
|
|
import androidx.fragment.app.Fragment
|
2018-05-16 16:00:07 +02:00
|
|
|
import com.simplemobiletools.clock.activities.MainActivity
|
2018-03-04 11:21:36 +01:00
|
|
|
import com.simplemobiletools.clock.activities.SimpleActivity
|
|
|
|
import com.simplemobiletools.clock.adapters.AlarmsAdapter
|
2023-08-04 16:19:11 +02:00
|
|
|
import com.simplemobiletools.clock.databinding.FragmentAlarmBinding
|
2020-12-26 14:09:14 +01:00
|
|
|
import com.simplemobiletools.clock.dialogs.ChangeAlarmSortDialog
|
2018-03-05 17:11:04 +01:00
|
|
|
import com.simplemobiletools.clock.dialogs.EditAlarmDialog
|
2018-03-13 16:32:56 +01:00
|
|
|
import com.simplemobiletools.clock.extensions.*
|
2021-04-19 15:34:52 +02:00
|
|
|
import com.simplemobiletools.clock.helpers.*
|
2018-03-05 23:04:53 +01:00
|
|
|
import com.simplemobiletools.clock.interfaces.ToggleAlarmInterface
|
2018-03-05 17:11:04 +01:00
|
|
|
import com.simplemobiletools.clock.models.Alarm
|
2023-05-07 18:55:37 +02:00
|
|
|
import com.simplemobiletools.commons.dialogs.PermissionRequiredDialog
|
2022-04-20 20:05:21 +02:00
|
|
|
import com.simplemobiletools.commons.extensions.getProperTextColor
|
2023-07-17 15:02:59 +02:00
|
|
|
import com.simplemobiletools.commons.extensions.openNotificationSettings
|
2018-03-05 23:04:53 +01:00
|
|
|
import com.simplemobiletools.commons.extensions.toast
|
2018-03-03 23:00:48 +01:00
|
|
|
import com.simplemobiletools.commons.extensions.updateTextColors
|
2022-06-15 14:59:02 +02:00
|
|
|
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_CREATED
|
2023-07-07 20:13:35 +02:00
|
|
|
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
2018-03-30 16:00:35 +02:00
|
|
|
import com.simplemobiletools.commons.models.AlarmSound
|
2018-02-28 12:50:52 +01:00
|
|
|
|
2018-03-05 23:04:53 +01:00
|
|
|
class AlarmFragment : Fragment(), ToggleAlarmInterface {
|
|
|
|
private var alarms = ArrayList<Alarm>()
|
2018-03-29 21:16:45 +02:00
|
|
|
private var currentEditAlarmDialog: EditAlarmDialog? = null
|
2018-03-13 19:26:16 +01:00
|
|
|
|
|
|
|
private var storedTextColor = 0
|
|
|
|
|
2023-08-04 16:19:11 +02:00
|
|
|
private lateinit var binding: FragmentAlarmBinding
|
2018-02-28 22:31:29 +01:00
|
|
|
|
2018-03-01 16:25:11 +01:00
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
2018-03-13 19:26:16 +01:00
|
|
|
storeStateVariables()
|
2023-08-04 16:19:11 +02:00
|
|
|
binding = FragmentAlarmBinding.inflate(inflater, container, false)
|
|
|
|
return binding.root
|
2018-02-28 22:31:29 +01:00
|
|
|
}
|
2018-03-03 23:00:48 +01:00
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
setupViews()
|
2018-03-13 19:26:16 +01:00
|
|
|
|
2022-04-20 20:05:21 +02:00
|
|
|
val configTextColor = requireContext().getProperTextColor()
|
2018-03-13 19:26:16 +01:00
|
|
|
if (storedTextColor != configTextColor) {
|
2023-08-04 16:19:11 +02:00
|
|
|
(binding.alarmsList.adapter as AlarmsAdapter).updateTextColor(configTextColor)
|
2018-03-13 19:26:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
|
|
|
storeStateVariables()
|
|
|
|
}
|
|
|
|
|
2021-04-19 15:34:52 +02:00
|
|
|
fun showSortingDialog() {
|
2020-12-26 14:09:14 +01:00
|
|
|
ChangeAlarmSortDialog(activity as SimpleActivity) {
|
|
|
|
setupAlarms()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 19:26:16 +01:00
|
|
|
private fun storeStateVariables() {
|
2022-04-20 20:05:21 +02:00
|
|
|
storedTextColor = requireContext().getProperTextColor()
|
2018-03-03 23:00:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun setupViews() {
|
2023-08-04 16:19:11 +02:00
|
|
|
binding.apply {
|
|
|
|
requireContext().updateTextColors(alarmFragment)
|
|
|
|
alarmFab.setOnClickListener {
|
|
|
|
val newAlarm = root.context.createNewAlarm(DEFAULT_ALARM_MINUTES, 0)
|
2018-03-28 19:10:04 +02:00
|
|
|
newAlarm.isEnabled = true
|
2020-11-05 11:05:21 +01:00
|
|
|
newAlarm.days = getTomorrowBit()
|
2018-03-05 17:11:04 +01:00
|
|
|
openEditAlarm(newAlarm)
|
2018-03-03 23:00:48 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-04 11:21:36 +01:00
|
|
|
|
|
|
|
setupAlarms()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setupAlarms() {
|
2021-04-19 15:34:52 +02:00
|
|
|
alarms = context?.dbHelper?.getAlarms() ?: return
|
2022-06-15 14:59:02 +02:00
|
|
|
|
|
|
|
when (requireContext().config.alarmSort) {
|
|
|
|
SORT_BY_ALARM_TIME -> alarms.sortBy { it.timeInMinutes }
|
|
|
|
SORT_BY_DATE_CREATED -> alarms.sortBy { it.id }
|
2022-06-16 10:17:09 +02:00
|
|
|
SORT_BY_DATE_AND_TIME -> alarms.sortWith(compareBy<Alarm> {
|
|
|
|
requireContext().firstDayOrder(it.days)
|
|
|
|
}.thenBy {
|
|
|
|
it.timeInMinutes
|
|
|
|
})
|
2021-04-19 15:34:52 +02:00
|
|
|
}
|
2023-07-07 20:13:35 +02:00
|
|
|
context?.getEnabledAlarms { enabledAlarms ->
|
|
|
|
if (enabledAlarms.isNullOrEmpty()) {
|
2023-07-06 17:44:29 +02:00
|
|
|
alarms.forEach {
|
|
|
|
if (it.days == TODAY_BIT && it.isEnabled && it.timeInMinutes <= getCurrentDayMinutes()) {
|
|
|
|
it.isEnabled = false
|
2023-07-07 20:13:35 +02:00
|
|
|
ensureBackgroundThread {
|
|
|
|
context?.dbHelper?.updateAlarmEnabledState(it.id, false)
|
|
|
|
}
|
2020-11-05 12:02:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 16:19:11 +02:00
|
|
|
val currAdapter = binding.alarmsList.adapter
|
2018-03-04 11:21:36 +01:00
|
|
|
if (currAdapter == null) {
|
2023-08-04 16:19:11 +02:00
|
|
|
AlarmsAdapter(activity as SimpleActivity, alarms, this, binding.alarmsList) {
|
2018-03-05 17:11:04 +01:00
|
|
|
openEditAlarm(it as Alarm)
|
2018-03-08 21:23:16 +01:00
|
|
|
}.apply {
|
2023-08-04 16:19:11 +02:00
|
|
|
binding.alarmsList.adapter = this
|
2018-03-04 11:21:36 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(currAdapter as AlarmsAdapter).updateItems(alarms)
|
|
|
|
}
|
2018-03-03 23:00:48 +01:00
|
|
|
}
|
|
|
|
|
2018-03-05 17:11:04 +01:00
|
|
|
private fun openEditAlarm(alarm: Alarm) {
|
2018-03-29 21:16:45 +02:00
|
|
|
currentEditAlarmDialog = EditAlarmDialog(activity as SimpleActivity, alarm) {
|
2018-05-16 14:57:31 +02:00
|
|
|
alarm.id = it
|
2018-03-29 21:16:45 +02:00
|
|
|
currentEditAlarmDialog = null
|
2018-03-05 22:43:55 +01:00
|
|
|
setupAlarms()
|
2018-03-06 21:20:10 +01:00
|
|
|
checkAlarmState(alarm)
|
2018-03-05 17:11:04 +01:00
|
|
|
}
|
2018-03-03 23:00:48 +01:00
|
|
|
}
|
2018-03-05 23:04:53 +01:00
|
|
|
|
|
|
|
override fun alarmToggled(id: Int, isEnabled: Boolean) {
|
2023-08-04 14:07:11 +02:00
|
|
|
(activity as SimpleActivity).handleFullScreenNotificationsPermission { granted ->
|
2023-05-07 18:55:37 +02:00
|
|
|
if (granted) {
|
2022-08-31 21:26:49 +02:00
|
|
|
if (requireContext().dbHelper.updateAlarmEnabledState(id, isEnabled)) {
|
2023-08-04 14:07:11 +02:00
|
|
|
val alarm = alarms.firstOrNull { it.id == id } ?: return@handleFullScreenNotificationsPermission
|
2022-08-31 21:26:49 +02:00
|
|
|
alarm.isEnabled = isEnabled
|
|
|
|
checkAlarmState(alarm)
|
|
|
|
} else {
|
2023-08-04 16:19:11 +02:00
|
|
|
requireActivity().toast(com.simplemobiletools.commons.R.string.unknown_error_occurred)
|
2022-08-31 21:26:49 +02:00
|
|
|
}
|
|
|
|
requireContext().updateWidgets()
|
|
|
|
} else {
|
2023-07-17 15:02:59 +02:00
|
|
|
PermissionRequiredDialog(
|
|
|
|
activity as SimpleActivity,
|
2023-08-04 16:19:11 +02:00
|
|
|
com.simplemobiletools.commons.R.string.allow_notifications_reminders,
|
2023-07-17 15:02:59 +02:00
|
|
|
{ (activity as SimpleActivity).openNotificationSettings() })
|
2022-08-31 21:26:49 +02:00
|
|
|
}
|
2018-03-05 23:04:53 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-05 23:49:14 +01:00
|
|
|
|
2018-03-06 21:20:10 +01:00
|
|
|
private fun checkAlarmState(alarm: Alarm) {
|
|
|
|
if (alarm.isEnabled) {
|
2018-05-16 15:35:00 +02:00
|
|
|
context?.scheduleNextAlarm(alarm, true)
|
2018-03-06 21:20:10 +01:00
|
|
|
} else {
|
2018-05-16 15:35:00 +02:00
|
|
|
context?.cancelAlarmClock(alarm)
|
2018-03-06 14:06:08 +01:00
|
|
|
}
|
2018-05-16 16:00:07 +02:00
|
|
|
(activity as? MainActivity)?.updateClockTabAlarm()
|
2018-03-06 14:06:08 +01:00
|
|
|
}
|
2018-03-29 21:16:45 +02:00
|
|
|
|
|
|
|
fun updateAlarmSound(alarmSound: AlarmSound) {
|
|
|
|
currentEditAlarmDialog?.updateSelectedAlarmSound(alarmSound)
|
|
|
|
}
|
2018-02-28 12:50:52 +01:00
|
|
|
}
|