feat: Add option to sort alarms

This commit is contained in:
jkemming 2020-12-26 14:09:14 +01:00 committed by jkemming
parent 6c91e072cc
commit 12ee706b4e
37 changed files with 198 additions and 7 deletions

View file

@ -0,0 +1,42 @@
package com.simplemobiletools.clock.dialogs
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.models.AlarmSort
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_change_alarm_sort.view.*
class ChangeAlarmSortDialog(val activity: BaseSimpleActivity, val callback: (AlarmSort) -> Unit) {
private var view: View
init {
view = activity.layoutInflater.inflate(R.layout.dialog_change_alarm_sort, null).apply {
val activeRadioButton = when (activity.config.alarmSort) {
AlarmSort.CREATED_AT -> sorting_dialog_radio_created_at
AlarmSort.TIME_OF_DAY -> sorting_dialog_radio_time_of_day
}
activeRadioButton?.isChecked = true
}
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok) { _, _ -> dialogConfirmed() }
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.sort_by)
}
}
private fun dialogConfirmed() {
val sort = when (view.sorting_dialog_radio_sorting.checkedRadioButtonId) {
R.id.sorting_dialog_radio_created_at -> AlarmSort.CREATED_AT
R.id.sorting_dialog_radio_time_of_day -> AlarmSort.TIME_OF_DAY
else -> AlarmSort.default()
}
activity.config.alarmSort = sort
callback(sort)
}
}

View file

@ -1,14 +1,13 @@
package com.simplemobiletools.clock.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.*
import androidx.fragment.app.Fragment
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.MainActivity
import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.adapters.AlarmsAdapter
import com.simplemobiletools.clock.dialogs.ChangeAlarmSortDialog
import com.simplemobiletools.clock.dialogs.EditAlarmDialog
import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.clock.helpers.DEFAULT_ALARM_MINUTES
@ -34,6 +33,7 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
storeStateVariables()
setHasOptionsMenu(true)
view = inflater.inflate(R.layout.fragment_alarm, container, false) as ViewGroup
return view
}
@ -53,6 +53,24 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
storeStateVariables()
}
override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.menu_alarm, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.sort -> showSortingDialog()
else -> return super.onOptionsItemSelected(item)
}
return true
}
private fun showSortingDialog() {
ChangeAlarmSortDialog(activity as SimpleActivity) {
setupAlarms()
}
}
private fun storeStateVariables() {
storedTextColor = requireContext().config.textColor
}
@ -72,7 +90,7 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
}
private fun setupAlarms() {
alarms = context?.dbHelper?.getAlarms() ?: return
alarms = context?.dbHelper?.getAlarms(context?.config?.alarmSort) ?: return
if (context?.getNextAlarm()?.isEmpty() == true) {
alarms.forEach {
if (it.days == TODAY_BIT && it.isEnabled && it.timeInMinutes <= getCurrentDayMinutes()) {

View file

@ -1,6 +1,7 @@
package com.simplemobiletools.clock.helpers
import android.content.Context
import com.simplemobiletools.clock.models.AlarmSort
import com.simplemobiletools.clock.extensions.gson.gson
import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.clock.models.StateWrapper
@ -57,6 +58,11 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getString(TIMER_LABEL, null)
set(label) = prefs.edit().putString(TIMER_LABEL, label).apply()
var alarmSort: AlarmSort
get() = AlarmSort.valueOf(prefs.getInt(ALARM_SORT, AlarmSort.default().value))
?: AlarmSort.default()
set(alarmSort) = prefs.edit().putInt(ALARM_SORT, alarmSort.value).apply()
var alarmMaxReminderSecs: Int
get() = prefs.getInt(ALARM_MAX_REMINDER_SECS, DEFAULT_MAX_ALARM_REMINDER_SECS)
set(alarmMaxReminderSecs) = prefs.edit().putInt(ALARM_MAX_REMINDER_SECS, alarmMaxReminderSecs).apply()

View file

@ -17,6 +17,7 @@ const val TIMER_SOUND_TITLE = "timer_sound_title"
const val TIMER_CHANNEL_ID = "timer_channel_id"
const val TIMER_LABEL = "timer_label"
const val TIMER_MAX_REMINDER_SECS = "timer_max_reminder_secs"
const val ALARM_SORT = "alarm_sort"
const val ALARM_MAX_REMINDER_SECS = "alarm_max_reminder_secs"
const val ALARM_LAST_CONFIG = "alarm_last_config"
const val USE_TEXT_SHADOW = "use_text_shadow"

View file

@ -6,6 +6,7 @@ import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.text.TextUtils
import com.simplemobiletools.clock.models.AlarmSort
import com.simplemobiletools.clock.extensions.cancelAlarmClock
import com.simplemobiletools.clock.extensions.createNewAlarm
import com.simplemobiletools.clock.models.Alarm
@ -106,12 +107,17 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun getEnabledAlarms() = getAlarms().filter { it.isEnabled }
fun getAlarms(): ArrayList<Alarm> {
fun getAlarms(sort: AlarmSort? = null): ArrayList<Alarm> {
val alarms = ArrayList<Alarm>()
val cols = arrayOf(COL_ID, COL_TIME_IN_MINUTES, COL_DAYS, COL_IS_ENABLED, COL_VIBRATE, COL_SOUND_TITLE, COL_SOUND_URI, COL_LABEL)
val orderBy = when (sort) {
AlarmSort.CREATED_AT -> "$COL_ID ASC"
AlarmSort.TIME_OF_DAY -> "$COL_TIME_IN_MINUTES ASC"
else -> null
}
var cursor: Cursor? = null
try {
cursor = mDb.query(ALARMS_TABLE_NAME, cols, null, null, null, null, null)
cursor = mDb.query(ALARMS_TABLE_NAME, cols, null, null, null, null, orderBy)
if (cursor?.moveToFirst() == true) {
do {
try {

View file

@ -0,0 +1,18 @@
package com.simplemobiletools.clock.models
enum class AlarmSort(open val value: Int) {
CREATED_AT(0),
TIME_OF_DAY(1);
companion object {
fun default(): AlarmSort {
return CREATED_AT
}
fun valueOf(value: Int): AlarmSort? {
return AlarmSort.values()
.filter { it.value == value }
.getOrNull(0)
}
}
}

View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sorting_dialog_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/sorting_dialog_radio_sorting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/medium_margin"
android:paddingStart="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
android:paddingEnd="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/sorting_dialog_radio_created_at"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin"
android:text="@string/created_at" />
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/sorting_dialog_radio_time_of_day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/medium_margin"
android:paddingBottom="@dimen/medium_margin"
android:text="@string/time_of_day" />
</RadioGroup>
</ScrollView>

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/sort"
android:icon="@drawable/ic_sort_vector"
android:title="@string/sort_by"
app:showAsAction="ifRoom"/>
</menu>

View file

@ -2,6 +2,8 @@
<string name="app_name">Sadə Saat</string>
<string name="app_launcher_name">Saat</string>
<string name="time_zone">Vaxt zonası</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Titrət</string>
<string name="label">Etiket</string>
<string name="no_days_selected">Gün seçilməyib</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Jednoduché hodiny</string>
<string name="app_launcher_name">Hodiny</string>
<string name="time_zone">Časové pásmo</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibrovat</string>
<string name="label">Štítek</string>
<string name="no_days_selected">Nebyly označeny žádné dny</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Cloc</string>
<string name="time_zone">Cylchfa amser</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Dirgrynu</string>
<string name="label">Label</string>
<string name="no_days_selected">Dim diwrnodau wedi\'u dewis</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Ur</string>
<string name="time_zone">Tidszone</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibrer</string>
<string name="label">Etiket</string>
<string name="no_days_selected">Der er ikke valgt nogen dage</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Schlichte Uhr</string>
<string name="app_launcher_name">Uhr</string>
<string name="time_zone">Zeitzone</string>
<string name="created_at">Erstellt am</string>
<string name="time_of_day">Tageszeit</string>
<string name="vibrate">Vibration</string>
<string name="label">Label</string>
<string name="no_days_selected">Keine Tage ausgewählt</string>

View file

@ -3,6 +3,8 @@
<string name="app_name">Απλό Ρολόι</string>
<string name="app_launcher_name">Ρολόι</string>
<string name="time_zone">Ζώνη ώρας</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Δόνηση</string>
<string name="label">Ετικέτα</string>
<string name="no_days_selected">Δεν έχουν επιλεγεί ημέρες</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Reloj</string>
<string name="time_zone">Zona horaria</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibrar</string>
<string name="label">Etiqueta</string>
<string name="no_days_selected">Ningún día seleccionado</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Ordulari sinplea</string>
<string name="app_launcher_name">Ordularia</string>
<string name="time_zone">Ordu eremua</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Bibratu</string>
<string name="label">Etiketa</string>
<string name="no_days_selected">Ez duzu egunik hautatu</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Kello</string>
<string name="time_zone">Aikavyöhyke</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Värinä</string>
<string name="label">Nimi</string>
<string name="no_days_selected">Ei päiviä valittuna</string>
@ -56,7 +58,7 @@
<b>Reddit:</b>
https://www.reddit.com/r/SimpleMobileTools
</string>
 
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Horloge</string>
<string name="time_zone">Fuseau Horaire</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibrer</string>
<string name="label">Titre</string>
<string name="no_days_selected">Aucun jour sélectionné</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Jednostavan sat</string>
<string name="app_launcher_name">Sat</string>
<string name="time_zone">Vremenska zona</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibriraj</string>
<string name="label">Oznaka</string>
<string name="no_days_selected">Nije odabran nijedan dan</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Jam</string>
<string name="time_zone">Zona waktu</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Getar</string>
<string name="label">Label</string>
<string name="no_days_selected">Tidak ada hari yang dipilih</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Jam</string>
<string name="time_zone">Zona waktu</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Getar</string>
<string name="label">Label</string>
<string name="no_days_selected">Tidak ada hari yang dipilih</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Semplice Orologio</string>
<string name="app_launcher_name">Orologio</string>
<string name="time_zone">Fuso orario</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibrazione</string>
<string name="label">Etichetta</string>
<string name="no_days_selected">Nessun giorno selezionato</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">時計</string>
<string name="time_zone">タイムゾーン</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">バイブレーション</string>
<string name="label">ラベル</string>
<string name="no_days_selected">曜日が選択されていません</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Paprastas Laikrodis</string>
<string name="app_launcher_name">Laikrodis</string>
<string name="time_zone">Laiko juosta</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibruoti</string>
<string name="label">Etiketė</string>
<string name="no_days_selected">Nepasirinkta nė vienos dienos</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">ക്ലോക്ക്</string>
<string name="time_zone">സമയ മേഖല</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">വൈബ്രേറ്റുചെയ്യുക</string>
<string name="label">അടിക്കുറിപ്പ് </string>
<string name="no_days_selected">ദിവസങ്ങളൊന്നും തിരഞ്ഞെടുത്തിട്ടില്ല</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Klokke</string>
<string name="time_zone">Tidssone</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibrer</string>
<string name="label">Påskrift</string>
<string name="no_days_selected">Ingen dager valgte</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Eenvoudige Klok</string>
<string name="app_launcher_name">Klok</string>
<string name="time_zone">Tijdzone</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Trillen</string>
<string name="label">Label</string>
<string name="no_days_selected">Geen dagen geselecteerd</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Prosty Zegar</string>
<string name="app_launcher_name">Zegar</string>
<string name="time_zone">Strefa czasowa</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Wibracje</string>
<string name="label">Etykieta</string>
<string name="no_days_selected">Nie wybrano dni</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Relógio</string>
<string name="time_zone">Fuso horário</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibrar</string>
<string name="label">Legenda</string>
<string name="no_days_selected">Nenhum dia selecionado</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Часы</string>
<string name="time_zone">Часовой пояс</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Вибрация</string>
<string name="label">Метка</string>
<string name="no_days_selected">Дни не выбраны</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Jednoduché hodinky</string>
<string name="app_launcher_name">Hodinky</string>
<string name="time_zone">Časové pásmo</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibrovanie</string>
<string name="label">Štítok</string>
<string name="no_days_selected">Neboli označené žiadne dni</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Klocka</string>
<string name="time_zone">Tidszon</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibrera</string>
<string name="label">Etikett</string>
<string name="no_days_selected">Inga dagar har valts</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Basit Saat</string>
<string name="app_launcher_name">Saat</string>
<string name="time_zone">Saat dilimi</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Titret</string>
<string name="label">Etiket</string>
<string name="no_days_selected">Hiçbir gün seçilmedi</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Простий Годинник</string>
<string name="app_launcher_name">Годинник</string>
<string name="time_zone">Часовий пояс</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Вібрувати</string>
<string name="label">Мітка</string>
<string name="no_days_selected">Дні не обрано</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">简易时钟</string>
<string name="app_launcher_name">时钟</string>
<string name="time_zone">时区</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">震动</string>
<string name="label">标签</string>
<string name="no_days_selected">未选择哪一天</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">簡易時鐘</string>
<string name="app_launcher_name">簡易時鐘</string>
<string name="time_zone">時區</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">震動</string>
<string name="label">標籤</string>
<string name="no_days_selected">未選擇哪一天</string>

View file

@ -2,6 +2,8 @@
<string name="app_name">Simple Clock</string>
<string name="app_launcher_name">Clock</string>
<string name="time_zone">Time zone</string>
<string name="created_at">Created at</string>
<string name="time_of_day">Time of day</string>
<string name="vibrate">Vibrate</string>
<string name="label">Label</string>
<string name="no_days_selected">No days selected</string>