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 b6f1bd14..0a37cfa5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditAlarmDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditAlarmDialog.kt @@ -7,10 +7,7 @@ import android.support.v7.app.AlertDialog import android.widget.TextView import com.simplemobiletools.clock.R import com.simplemobiletools.clock.activities.SimpleActivity -import com.simplemobiletools.clock.extensions.colorLeftDrawable -import com.simplemobiletools.clock.extensions.config -import com.simplemobiletools.clock.extensions.dbHelper -import com.simplemobiletools.clock.extensions.getFormattedTime +import com.simplemobiletools.clock.extensions.* import com.simplemobiletools.clock.models.Alarm import com.simplemobiletools.clock.models.AlarmSound import com.simplemobiletools.commons.extensions.* @@ -31,11 +28,22 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba edit_alarm_sound.colorLeftDrawable(textColor) edit_alarm_sound.text = alarm.soundTitle edit_alarm_sound.setOnClickListener { - SelectAlarmSoundDialog(activity, alarm.soundUri, AudioManager.STREAM_ALARM) { + SelectAlarmSoundDialog(activity, alarm.soundUri, AudioManager.STREAM_ALARM, onAlarmPicked = { if (it != null) { - alarmSoundUpdated(it) + updateSelectedAlarmSound(it) } - } + }, onAlarmSoundDeleted = { + val defaultAlarm = AlarmSound(0, context.getDefaultAlarmTitle(), context.getDefaultAlarmUri().toString()) + if (alarm.soundUri == it.uri) { + updateSelectedAlarmSound(defaultAlarm) + } + + activity.dbHelper.getAlarmsWithUri(it.uri).forEach { + it.soundTitle = defaultAlarm.title + it.soundUri = defaultAlarm.uri + activity.dbHelper.updateAlarm(it) + } + }) } edit_alarm_vibrate.colorLeftDrawable(textColor) @@ -123,13 +131,9 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba return drawable } - private fun alarmSoundUpdated(alarmSound: AlarmSound) { + fun updateSelectedAlarmSound(alarmSound: AlarmSound) { alarm.soundTitle = alarmSound.title alarm.soundUri = alarmSound.uri view.edit_alarm_sound.text = alarmSound.title } - - fun updateSelectedAlarmSound(alarmSound: AlarmSound) { - alarmSoundUpdated(alarmSound) - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt index ba18a56b..6c0c1faf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/SelectAlarmSoundDialog.kt @@ -11,9 +11,9 @@ import android.widget.RadioGroup import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.simplemobiletools.clock.R -import com.simplemobiletools.clock.activities.MainActivity import com.simplemobiletools.clock.activities.SimpleActivity -import com.simplemobiletools.clock.extensions.* +import com.simplemobiletools.clock.extensions.config +import com.simplemobiletools.clock.extensions.getAlarmSounds import com.simplemobiletools.clock.helpers.PICK_AUDIO_FILE_INTENT_ID import com.simplemobiletools.clock.models.AlarmSound import com.simplemobiletools.commons.dialogs.RadioGroupDialog @@ -25,7 +25,8 @@ import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.views.MyCompatRadioButton import kotlinx.android.synthetic.main.dialog_select_alarm_sound.view.* -class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: String, val audioStream: Int, val callback: (alarmSound: AlarmSound?) -> Unit) { +class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: String, val audioStream: Int, val onAlarmPicked: (alarmSound: AlarmSound?) -> Unit, + val onAlarmSoundDeleted: (alarmSound: AlarmSound) -> Unit) { private val ADD_NEW_SOUND_ID = -2 private val view = activity.layoutInflater.inflate(R.layout.dialog_select_alarm_sound, null) @@ -144,29 +145,16 @@ class SelectAlarmSoundDialog(val activity: SimpleActivity, val currentUri: Strin view.dialog_select_alarm_system_radio.check(systemAlarmSounds.firstOrNull()?.id ?: 0) } - val defaultAlarm = AlarmSound(0, activity.getDefaultAlarmTitle(), activity.getDefaultAlarmUri().toString()) - val defaultTitle = defaultAlarm.title - val defaultUri = defaultAlarm.uri - if (config.timerSoundUri == alarmSound.uri) { - config.timerSoundTitle = defaultTitle - config.timerSoundUri = defaultUri - (activity as MainActivity).updateTimerTabAlarmSound(defaultAlarm) - } - - activity.dbHelper.getAlarmsWithUri(alarmSound.uri).forEach { - it.soundTitle = defaultTitle - it.soundUri = defaultUri - activity.dbHelper.updateAlarm(it) - } + onAlarmSoundDeleted(alarmSound) } private fun dialogConfirmed() { if (view.dialog_select_alarm_your_radio.checkedRadioButtonId != -1) { val checkedId = view.dialog_select_alarm_your_radio.checkedRadioButtonId - callback(yourAlarmSounds.firstOrNull { it.id == checkedId }) + onAlarmPicked(yourAlarmSounds.firstOrNull { it.id == checkedId }) } else { val checkedId = view.dialog_select_alarm_system_radio.checkedRadioButtonId - callback(systemAlarmSounds.firstOrNull { it.id == checkedId }) + onAlarmPicked(systemAlarmSounds.firstOrNull { it.id == checkedId }) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt index e9ad85a1..1502d553 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt @@ -66,11 +66,16 @@ class TimerFragment : Fragment() { } timer_sound.setOnClickListener { - SelectAlarmSoundDialog(activity as SimpleActivity, config.timerSoundUri, AudioManager.STREAM_SYSTEM) { + SelectAlarmSoundDialog(activity as SimpleActivity, config.timerSoundUri, AudioManager.STREAM_SYSTEM, onAlarmPicked = { if (it != null) { updateAlarmSound(it) } - } + }, onAlarmSoundDeleted = { + if (config.timerSoundUri == it.uri) { + val defaultAlarm = AlarmSound(0, context.getDefaultAlarmTitle(), context.getDefaultAlarmUri().toString()) + updateAlarmSound(defaultAlarm) + } + }) } }