allow creating alarms without a day selected

This commit is contained in:
tibbi 2020-11-05 10:11:40 +01:00
parent c73d714e7b
commit 9b55486462
3 changed files with 83 additions and 53 deletions

View file

@ -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 {

View file

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/edit_alarm_scrollview"
android:layout_width="match_parent"
@ -18,7 +17,19 @@
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:textSize="@dimen/alarm_text_size"
tools:text="07:00"/>
tools:text="07:00" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/edit_alarm_dayless_label"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignTop="@+id/edit_alarm_time"
android:layout_alignBottom="@+id/edit_alarm_time"
android:layout_marginStart="@dimen/normal_margin"
android:layout_toEndOf="@+id/edit_alarm_time"
android:gravity="center_vertical"
android:textSize="@dimen/bigger_text_size"
tools:text="@string/tomorrow" />
<LinearLayout
android:id="@+id/edit_alarm_days_holder"
@ -27,7 +38,7 @@
android:layout_below="@+id/edit_alarm_time"
android:layout_marginTop="@dimen/medium_margin"
android:layout_marginBottom="@dimen/medium_margin"
android:orientation="horizontal"/>
android:orientation="horizontal" />
<RelativeLayout
android:id="@+id/edit_alarm_vibrate_holder"
@ -35,7 +46,7 @@
android:layout_height="wrap_content"
android:layout_below="@+id/edit_alarm_days_holder"
android:background="?attr/selectableItemBackground"
android:paddingLeft="@dimen/tiny_margin"
android:paddingStart="@dimen/tiny_margin"
android:paddingTop="@dimen/normal_margin"
android:paddingBottom="@dimen/normal_margin">
@ -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" />
</RelativeLayout>
@ -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" />
<ImageView
android:id="@+id/edit_alarm_label_image"
@ -72,24 +83,24 @@
android:layout_below="@+id/edit_alarm_sound"
android:layout_alignTop="@+id/edit_alarm_label"
android:layout_alignBottom="@+id/edit_alarm_label"
android:layout_marginLeft="@dimen/tiny_margin"
android:layout_marginRight="@dimen/tiny_margin"
android:layout_marginStart="@dimen/tiny_margin"
android:layout_marginEnd="@dimen/tiny_margin"
android:scaleType="fitCenter"
android:src="@drawable/ic_label_vector"/>
android:src="@drawable/ic_label_vector" />
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/edit_alarm_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_alarm_sound"
android:layout_marginLeft="@dimen/medium_margin"
android:layout_marginStart="@dimen/medium_margin"
android:layout_marginTop="@dimen/small_margin"
android:layout_toRightOf="@+id/edit_alarm_label_image"
android:layout_toEndOf="@+id/edit_alarm_label_image"
android:hint="@string/label"
android:maxLines="1"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/normal_text_size"/>
android:textSize="@dimen/normal_text_size" />
</RelativeLayout>
</ScrollView>

View file

@ -12,7 +12,7 @@
<dimen name="clock_text_size">70sp</dimen>
<dimen name="clock_text_size_smaller">60sp</dimen>
<dimen name="alarm_text_size">44sp</dimen>
<dimen name="alarm_text_size">48sp</dimen>
<dimen name="stopwatch_text_size">60sp</dimen>
<dimen name="widget_time_text_size_small">48sp</dimen>
<dimen name="widget_details_text_size">14sp</dimen>