From 9b554864620bd7a8f0513d0f78fcc14130ff1523 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 5 Nov 2020 10:11:40 +0100 Subject: [PATCH] allow creating alarms without a day selected --- .../clock/dialogs/EditAlarmDialog.kt | 95 +++++++++++-------- app/src/main/res/layout/dialog_edit_alarm.xml | 39 +++++--- app/src/main/res/values/dimens.xml | 2 +- 3 files changed, 83 insertions(+), 53 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditAlarmDialog.kt b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditAlarmDialog.kt index 57be48f..b0b537f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditAlarmDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditAlarmDialog.kt @@ -15,6 +15,7 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.ALARM_SOUND_TYPE_ALARM import com.simplemobiletools.commons.models.AlarmSound import kotlinx.android.synthetic.main.dialog_edit_alarm.view.* +import java.util.* class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callback: (alarmId: Int) -> Unit) { private val view = activity.layoutInflater.inflate(R.layout.dialog_edit_alarm, null) @@ -33,17 +34,17 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba edit_alarm_sound.text = alarm.soundTitle edit_alarm_sound.setOnClickListener { SelectAlarmSoundDialog(activity, alarm.soundUri, AudioManager.STREAM_ALARM, PICK_AUDIO_FILE_INTENT_ID, ALARM_SOUND_TYPE_ALARM, true, - onAlarmPicked = { - if (it != null) { - updateSelectedAlarmSound(it) - } - }, onAlarmSoundDeleted = { - if (alarm.soundUri == it.uri) { - val defaultAlarm = context.getDefaultAlarmSound(ALARM_SOUND_TYPE_ALARM) - updateSelectedAlarmSound(defaultAlarm) - } - activity.checkAlarmsWithDeletedSoundUri(it.uri) - }) + onAlarmPicked = { + if (it != null) { + updateSelectedAlarmSound(it) + } + }, onAlarmSoundDeleted = { + if (alarm.soundUri == it.uri) { + val defaultAlarm = context.getDefaultAlarmSound(ALARM_SOUND_TYPE_ALARM) + updateSelectedAlarmSound(defaultAlarm) + } + activity.checkAlarmsWithDeletedSoundUri(it.uri) + }) } edit_alarm_vibrate.colorLeftDrawable(textColor) @@ -80,6 +81,7 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba } day.background = getProperDayDrawable(selectDay) day.setTextColor(if (selectDay) context.config.backgroundColor else textColor) + checkDaylessAlarm() } edit_alarm_days_holder.addView(day) @@ -87,36 +89,36 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba } AlertDialog.Builder(activity) - .setPositiveButton(R.string.ok, null) - .setNegativeButton(R.string.cancel, null) - .create().apply { - activity.setupDialogStuff(view, this) { - getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { - if (alarm.days == 0) { - activity.toast(R.string.no_days_selected) - return@setOnClickListener - } - - alarm.label = view.edit_alarm_label.value - - var alarmId = alarm.id - if (alarm.id == 0) { - alarmId = activity.dbHelper.insertAlarm(alarm) - if (alarmId == -1) { - activity.toast(R.string.unknown_error_occurred) - } - } else { - if (!activity.dbHelper.updateAlarm(alarm)) { - activity.toast(R.string.unknown_error_occurred) - } - } - - activity.config.alarmLastConfig = alarm - callback(alarmId) - dismiss() + .setPositiveButton(R.string.ok, null) + .setNegativeButton(R.string.cancel, null) + .create().apply { + activity.setupDialogStuff(view, this) { + getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { + if (alarm.days == 0) { + activity.toast(R.string.no_days_selected) + return@setOnClickListener } + + alarm.label = view.edit_alarm_label.value + + var alarmId = alarm.id + if (alarm.id == 0) { + alarmId = activity.dbHelper.insertAlarm(alarm) + if (alarmId == -1) { + activity.toast(R.string.unknown_error_occurred) + } + } else { + if (!activity.dbHelper.updateAlarm(alarm)) { + activity.toast(R.string.unknown_error_occurred) + } + } + + activity.config.alarmLastConfig = alarm + callback(alarmId) + dismiss() } } + } } private fun restoreLastAlarm() { @@ -139,6 +141,23 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba private fun updateAlarmTime() { view.edit_alarm_time.text = activity.getFormattedTime(alarm.timeInMinutes * 60, false, true) + checkDaylessAlarm() + } + + private fun checkDaylessAlarm() { + if (alarm.days == 0) { + val calendar = Calendar.getInstance() + val currentMinutesOfDay = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE) + + val textId = if (alarm.timeInMinutes > currentMinutesOfDay) { + R.string.tomorrow + } else { + R.string.today + } + + view.edit_alarm_dayless_label.text = "(${activity.getString(textId)})" + } + view.edit_alarm_dayless_label.beVisibleIf(alarm.days == 0) } private fun getProperDayDrawable(selected: Boolean): Drawable { diff --git a/app/src/main/res/layout/dialog_edit_alarm.xml b/app/src/main/res/layout/dialog_edit_alarm.xml index 2faa9ec..e475e42 100644 --- a/app/src/main/res/layout/dialog_edit_alarm.xml +++ b/app/src/main/res/layout/dialog_edit_alarm.xml @@ -1,6 +1,5 @@ - + tools:text="07:00" /> + + + android:orientation="horizontal" /> @@ -47,7 +58,7 @@ android:clickable="false" android:drawableLeft="@drawable/ic_vibrate_vector" android:drawablePadding="@dimen/normal_margin" - android:text="@string/vibrate"/> + android:text="@string/vibrate" /> @@ -59,11 +70,11 @@ android:background="?attr/selectableItemBackground" android:drawableLeft="@drawable/ic_bell_vector" android:drawablePadding="@dimen/normal_margin" - android:paddingLeft="@dimen/tiny_margin" + android:paddingStart="@dimen/tiny_margin" android:paddingTop="@dimen/normal_margin" android:paddingBottom="@dimen/normal_margin" android:textSize="@dimen/normal_text_size" - tools:text="Default alarm"/> + tools:text="Default alarm" /> + android:src="@drawable/ic_label_vector" /> + android:textSize="@dimen/normal_text_size" /> diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 8d4d088..a6d66ae 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -12,7 +12,7 @@ 70sp 60sp - 44sp + 48sp 60sp 48sp 14sp