From aae9f1ea2f9e14dfd50b828fb541c871425d0df9 Mon Sep 17 00:00:00 2001 From: Pavel Poley Date: Wed, 15 Jun 2022 15:59:02 +0300 Subject: [PATCH 1/2] day and alarm time sorting order --- .../clock/dialogs/ChangeAlarmSortDialog.kt | 3 +++ .../clock/extensions/Context.kt | 24 ++++++++++++++++--- .../clock/fragments/AlarmFragment.kt | 12 +++++----- .../clock/helpers/Constants.kt | 1 + .../res/layout/dialog_change_alarm_sort.xml | 8 +++++++ 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/ChangeAlarmSortDialog.kt b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/ChangeAlarmSortDialog.kt index 1214ea0d..6e75d096 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/ChangeAlarmSortDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/ChangeAlarmSortDialog.kt @@ -6,6 +6,7 @@ import com.simplemobiletools.clock.R import com.simplemobiletools.clock.extensions.config import com.simplemobiletools.clock.helpers.SORT_BY_ALARM_TIME import com.simplemobiletools.clock.helpers.SORT_BY_CREATION_ORDER +import com.simplemobiletools.clock.helpers.SORT_BY_DATE_AND_TIME import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.setupDialogStuff import kotlinx.android.synthetic.main.dialog_change_alarm_sort.view.* @@ -14,6 +15,7 @@ class ChangeAlarmSortDialog(val activity: BaseSimpleActivity, val callback: () - private var view: View = activity.layoutInflater.inflate(R.layout.dialog_change_alarm_sort, null).apply { val activeRadioButton = when (activity.config.alarmSort) { SORT_BY_ALARM_TIME -> sorting_dialog_radio_alarm_time + SORT_BY_DATE_AND_TIME -> sorting_dialog_radio_day_and_time else -> sorting_dialog_radio_creation_order } activeRadioButton?.isChecked = true @@ -31,6 +33,7 @@ class ChangeAlarmSortDialog(val activity: BaseSimpleActivity, val callback: () - private fun dialogConfirmed() { val sort = when (view.sorting_dialog_radio_sorting.checkedRadioButtonId) { R.id.sorting_dialog_radio_alarm_time -> SORT_BY_ALARM_TIME + R.id.sorting_dialog_radio_day_and_time -> SORT_BY_DATE_AND_TIME else -> SORT_BY_CREATION_ORDER } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt index 2f521c72..c27ee414 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt @@ -31,9 +31,7 @@ import com.simplemobiletools.clock.receivers.HideAlarmReceiver import com.simplemobiletools.clock.receivers.HideTimerReceiver import com.simplemobiletools.clock.services.SnoozeService import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.DAY_MINUTES -import com.simplemobiletools.commons.helpers.EVERY_DAY_BIT -import com.simplemobiletools.commons.helpers.SILENT +import com.simplemobiletools.commons.helpers.* import java.util.* import kotlin.math.pow @@ -468,3 +466,23 @@ fun Context.getAlarmSelectedDaysString(bitMask: Int): String { else -> getSelectedDaysString(bitMask) } } + +fun Context.firstDayOrder(bitMask: Int): Int { + if (bitMask == TODAY_BIT) return -2 + if (bitMask == TOMORROW_BIT) return -1 + + val dayBits = arrayListOf(MONDAY_BIT, TUESDAY_BIT, WEDNESDAY_BIT, THURSDAY_BIT, FRIDAY_BIT, SATURDAY_BIT, SUNDAY_BIT) + + val sundayFirst = baseConfig.isSundayFirst + if (sundayFirst) { + dayBits.moveLastItemToFront() + } + + dayBits.forEach { bit -> + if (bitMask and bit != 0) { + return if (bit == SUNDAY_BIT && sundayFirst) 0 else bit + } + } + + return bitMask +} diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt index 905d689d..89aba004 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt @@ -18,6 +18,7 @@ import com.simplemobiletools.clock.models.Alarm import com.simplemobiletools.commons.extensions.getProperTextColor import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.updateTextColors +import com.simplemobiletools.commons.helpers.SORT_BY_DATE_CREATED import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.models.AlarmSound import kotlinx.android.synthetic.main.fragment_alarm.view.* @@ -77,12 +78,11 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface { private fun setupAlarms() { alarms = context?.dbHelper?.getAlarms() ?: return - alarms.sortBy { - if (requireContext().config.alarmSort == SORT_BY_ALARM_TIME) { - it.timeInMinutes - } else { - it.id - } + + when (requireContext().config.alarmSort) { + SORT_BY_ALARM_TIME -> alarms.sortBy { it.timeInMinutes } + SORT_BY_DATE_CREATED -> alarms.sortBy { it.id } + SORT_BY_DATE_AND_TIME -> alarms.sortWith(compareBy { requireContext().firstDayOrder(it.days) }.thenBy { it.timeInMinutes }) } if (context?.getNextAlarm()?.isEmpty() == true) { diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt index 3ad9c261..7e7f16e9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt @@ -54,6 +54,7 @@ const val SORT_BY_TOTAL_TIME = 4 // alarm sorting const val SORT_BY_CREATION_ORDER = 0 const val SORT_BY_ALARM_TIME = 1 +const val SORT_BY_DATE_AND_TIME = 2 const val TODAY_BIT = -1 const val TOMORROW_BIT = -2 diff --git a/app/src/main/res/layout/dialog_change_alarm_sort.xml b/app/src/main/res/layout/dialog_change_alarm_sort.xml index 22ae749c..feaa80f7 100644 --- a/app/src/main/res/layout/dialog_change_alarm_sort.xml +++ b/app/src/main/res/layout/dialog_change_alarm_sort.xml @@ -29,5 +29,13 @@ android:paddingBottom="@dimen/medium_margin" android:text="@string/sort_by_alarm_time" /> + + From 2cfd00a6714e92868eee121088af254b1d15af70 Mon Sep 17 00:00:00 2001 From: Tibor Kaputa Date: Thu, 16 Jun 2022 10:17:09 +0200 Subject: [PATCH 2/2] adding some spacing --- .../com/simplemobiletools/clock/fragments/AlarmFragment.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt index 89aba004..3cba640b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt @@ -82,7 +82,11 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface { when (requireContext().config.alarmSort) { SORT_BY_ALARM_TIME -> alarms.sortBy { it.timeInMinutes } SORT_BY_DATE_CREATED -> alarms.sortBy { it.id } - SORT_BY_DATE_AND_TIME -> alarms.sortWith(compareBy { requireContext().firstDayOrder(it.days) }.thenBy { it.timeInMinutes }) + SORT_BY_DATE_AND_TIME -> alarms.sortWith(compareBy { + requireContext().firstDayOrder(it.days) + }.thenBy { + it.timeInMinutes + }) } if (context?.getNextAlarm()?.isEmpty() == true) {