diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/adapters/TimerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/clock/adapters/TimerAdapter.kt index cd86c346..e5f864db 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/adapters/TimerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/adapters/TimerAdapter.kt @@ -1,8 +1,6 @@ package com.simplemobiletools.clock.adapters import android.graphics.Color -import android.media.AudioManager -import android.media.RingtoneManager import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -13,19 +11,17 @@ import com.simplemobiletools.clock.R import com.simplemobiletools.clock.activities.SimpleActivity import com.simplemobiletools.clock.dialogs.MyTimePickerDialogDialog import com.simplemobiletools.clock.extensions.* -import com.simplemobiletools.clock.helpers.PICK_AUDIO_FILE_INTENT_ID import com.simplemobiletools.clock.models.Timer import com.simplemobiletools.clock.models.TimerEvent import com.simplemobiletools.clock.models.TimerState -import com.simplemobiletools.commons.dialogs.SelectAlarmSoundDialog import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.models.AlarmSound import kotlinx.android.synthetic.main.item_timer.view.* import org.greenrobot.eventbus.EventBus class TimerAdapter( private val activity: SimpleActivity, private val onRefresh: () -> Unit, + private val onClick: (Timer) -> Unit, ) : ListAdapter(diffUtil) { companion object { @@ -42,12 +38,7 @@ class TimerAdapter( private val config = activity.config private var textColor = config.textColor - private var primaryColor = config.primaryColor private var adjustedPrimaryColor = activity.getAdjustedPrimaryColor() - private var contrastColor = adjustedPrimaryColor.getContrastColor() - private var backgroundColor = config.backgroundColor - - private var selectedTimer: Timer? = null override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TimerViewHolder { return TimerViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_timer, parent, false)) @@ -62,29 +53,11 @@ class TimerAdapter( onRefresh.invoke() } - fun updatePrimaryColor(primaryColor: Int) { - this.primaryColor = primaryColor - adjustedPrimaryColor = activity.getAdjustedPrimaryColor() - contrastColor = adjustedPrimaryColor.getContrastColor() - onRefresh.invoke() - } - inner class TimerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { - init { - itemView.timer_label.onTextChangeListener { text -> - updateTimer(getItem(adapterPosition).copy(label = text), false) - } - - } - fun bind(timer: Timer) { itemView.apply { post { - timer_initial_time.colorLeftDrawable(textColor) - timer_vibrate.colorLeftDrawable(textColor) - timer_label_image.applyColorFilter(textColor) - timer_sound.colorLeftDrawable(textColor) timer_play_pause.background = activity.resources.getColoredDrawableWithColor(R.drawable.circle_background_filled, adjustedPrimaryColor) timer_play_pause.applyColorFilter(if (adjustedPrimaryColor == Color.WHITE) Color.BLACK else Color.WHITE) timer_reset.applyColorFilter(textColor) @@ -97,16 +70,6 @@ class TimerAdapter( timer_label.setText(timer.label) } - timer_initial_time.text = timer.seconds.getFormattedDuration() - timer_initial_time.setTextColor(textColor) - - timer_vibrate.isChecked = timer.vibrate - timer_vibrate.setTextColor(textColor) - timer_vibrate.setColors(textColor, adjustedPrimaryColor, backgroundColor) - timer_vibrate_holder.setOnClickListener { - timer_vibrate.toggle() - updateTimer(timer.copy(vibrate = timer_vibrate.isChecked, channelId = null), false) - } timer_time.setTextColor(textColor) timer_time.text = when (timer.state) { @@ -119,33 +82,6 @@ class TimerAdapter( changeDuration(timer) } - timer_initial_time.setTextColor(textColor) - timer_initial_time.setOnClickListener { - changeDuration(timer) - } - - - timer_sound.text = timer.soundTitle - timer_sound.setTextColor(textColor) - timer_sound.setOnClickListener { - selectedTimer = timer - SelectAlarmSoundDialog(activity, timer.soundUri, AudioManager.STREAM_ALARM, PICK_AUDIO_FILE_INTENT_ID, - RingtoneManager.TYPE_ALARM, true, - onAlarmPicked = { sound -> - if (sound != null) { - updateAlarmSound(timer, sound) - } - }, - onAlarmSoundDeleted = { sound -> - if (timer.soundUri == sound.uri) { - val defaultAlarm = context.getDefaultAlarmSound(RingtoneManager.TYPE_ALARM) - updateAlarmSound(timer, defaultAlarm) - } - - context.checkAlarmsWithDeletedSoundUri(sound.uri) - }) - } - timer_delete.applyColorFilter(textColor) timer_delete.setOnClickListener { activity.timerHelper.deleteTimer(timer.id!!) { @@ -167,7 +103,12 @@ class TimerAdapter( is TimerState.Finished -> EventBus.getDefault().post(TimerEvent.Start(timer.id!!, timer.seconds.secondsToMillis)) } } + updateViewStates(timer.state) + + setOnClickListener { + onClick.invoke(timer) + } } } @@ -188,14 +129,6 @@ class TimerAdapter( } } - fun updateAlarmSoundForSelectedTimer(alarmSound: AlarmSound) { - selectedTimer?.let { updateAlarmSound(it, alarmSound) } - } - - fun updateAlarmSound(timer: Timer, alarmSound: AlarmSound) { - updateTimer(timer.copy(soundTitle = alarmSound.title, soundUri = alarmSound.uri, channelId = null)) - } - private fun updateTimer(timer: Timer, refresh: Boolean = true) { activity.timerHelper.insertOrUpdateTimer(timer) { if (refresh) { diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditTimerDialog.kt b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditTimerDialog.kt new file mode 100644 index 00000000..0057e9ab --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/clock/dialogs/EditTimerDialog.kt @@ -0,0 +1,115 @@ +package com.simplemobiletools.clock.dialogs + +import android.media.AudioManager +import android.media.RingtoneManager +import androidx.appcompat.app.AlertDialog +import com.simplemobiletools.clock.R +import com.simplemobiletools.clock.activities.SimpleActivity +import com.simplemobiletools.clock.extensions.* +import com.simplemobiletools.clock.helpers.PICK_AUDIO_FILE_INTENT_ID +import com.simplemobiletools.clock.models.Timer +import com.simplemobiletools.commons.dialogs.SelectAlarmSoundDialog +import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.models.AlarmSound +import kotlinx.android.synthetic.main.dialog_edit_timer.view.* + +class EditTimerDialog(val activity: SimpleActivity, val timer: Timer, val callback: () -> Unit) { + private val view = activity.layoutInflater.inflate(R.layout.dialog_edit_timer, null) + private val textColor = activity.config.textColor + + init { + restoreLastAlarm() + updateAlarmTime() + + view.apply { + + edit_timer_initial_time.text = timer.seconds.getFormattedDuration() + edit_timer_initial_time.setTextColor(textColor) + edit_timer_initial_time.setOnClickListener { + changeDuration(timer) + } + + edit_timer_vibrate.colorLeftDrawable(textColor) + edit_timer_vibrate.isChecked = timer.vibrate + edit_timer_vibrate.setTextColor(textColor) + edit_timer_vibrate_holder.setOnClickListener { + edit_timer_vibrate.toggle() + timer.vibrate = edit_timer_vibrate.isChecked + timer.channelId = null + } + + edit_timer_sound.colorLeftDrawable(textColor) + edit_timer_sound.text = timer.soundTitle + edit_timer_sound.setOnClickListener { + SelectAlarmSoundDialog(activity, timer.soundUri, AudioManager.STREAM_ALARM, PICK_AUDIO_FILE_INTENT_ID, + RingtoneManager.TYPE_ALARM, true, + onAlarmPicked = { sound -> + if (sound != null) { + updateAlarmSound(sound) + } + }, + onAlarmSoundDeleted = { sound -> + if (timer.soundUri == sound.uri) { + val defaultAlarm = context.getDefaultAlarmSound(RingtoneManager.TYPE_ALARM) + updateAlarmSound(defaultAlarm) + } + + context.checkAlarmsWithDeletedSoundUri(sound.uri) + }) + } + + + edit_timer_label_image.applyColorFilter(textColor) + edit_timer_label.setText(timer.label) + + } + + AlertDialog.Builder(activity) + .setPositiveButton(R.string.ok, null) + .setNegativeButton(R.string.cancel, null) + .create().apply { + activity.setupDialogStuff(view, this) { + getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { + timer.label = view.edit_timer_label.value + activity.timerHelper.insertOrUpdateTimer(timer){ + activity.config.timerLastConfig = timer + callback() + dismiss() + } + } + } + } + } + + private fun restoreLastAlarm() { + if (timer.id == null) { + activity.config.timerLastConfig?.let { lastConfig -> + timer.label = lastConfig.label + timer.seconds = lastConfig.seconds + timer.soundTitle = lastConfig.soundTitle + timer.soundUri = lastConfig.soundUri + timer.vibrate = lastConfig.vibrate + } + } + } + + private fun updateAlarmTime() { + view.edit_timer_initial_time.text = activity.getFormattedTime(timer.seconds * 60, false, true) + } + + private fun changeDuration(timer: Timer) { + MyTimePickerDialogDialog(activity, timer.seconds) { seconds -> + val timerSeconds = if (seconds <= 0) 10 else seconds + timer.seconds = timerSeconds + view.edit_timer_initial_time.text = timerSeconds.getFormattedDuration() + } + } + + fun updateAlarmSound(alarmSound: AlarmSound) { + timer.soundTitle = alarmSound.title + timer.soundUri = alarmSound.uri + timer.channelId = null + view.edit_timer_sound.text = alarmSound.title + } + +} 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 a30e1598..cfe71eee 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/extensions/Context.kt @@ -85,6 +85,10 @@ fun Context.createNewAlarm(timeInMinutes: Int, weekDays: Int): Alarm { return Alarm(0, timeInMinutes, weekDays, false, false, defaultAlarmSound.title, defaultAlarmSound.uri, "") } +fun Context.createNewTimer(): Timer { + return Timer(null, config.timerSeconds, config.timerState, config.timerVibrate, config.timerSoundUri, config.timerSoundTitle, config.timerLabel ?: "", System.currentTimeMillis(), config.timerChannelId, ) +} + fun Context.scheduleNextAlarm(alarm: Alarm, showToast: Boolean) { val calendar = Calendar.getInstance() calendar.firstDayOfWeek = Calendar.MONDAY @@ -166,7 +170,8 @@ fun Context.hideNotification(id: Int) { fun Context.hideTimerNotification() = hideNotification(TIMER_NOTIF_ID) fun Context.updateWidgets() { - val widgetsCnt = AppWidgetManager.getInstance(applicationContext)?.getAppWidgetIds(ComponentName(applicationContext, MyWidgetDateTimeProvider::class.java)) ?: return + val widgetsCnt = + AppWidgetManager.getInstance(applicationContext)?.getAppWidgetIds(ComponentName(applicationContext, MyWidgetDateTimeProvider::class.java)) ?: return if (widgetsCnt.isNotEmpty()) { val ids = intArrayOf(R.xml.widget_date_time_info) Intent(applicationContext, MyWidgetDateTimeProvider::class.java).apply { @@ -179,7 +184,8 @@ fun Context.updateWidgets() { @SuppressLint("NewApi") fun Context.scheduleNextWidgetUpdate() { - val widgetsCnt = AppWidgetManager.getInstance(applicationContext)?.getAppWidgetIds(ComponentName(applicationContext, MyWidgetDateTimeProvider::class.java)) ?: return + val widgetsCnt = + AppWidgetManager.getInstance(applicationContext)?.getAppWidgetIds(ComponentName(applicationContext, MyWidgetDateTimeProvider::class.java)) ?: return if (widgetsCnt.isEmpty()) { return } 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 ef8e7fd8..72c1aea5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/TimerFragment.kt @@ -8,8 +8,11 @@ import androidx.fragment.app.Fragment import com.simplemobiletools.clock.R import com.simplemobiletools.clock.activities.SimpleActivity import com.simplemobiletools.clock.adapters.TimerAdapter +import com.simplemobiletools.clock.dialogs.EditTimerDialog import com.simplemobiletools.clock.extensions.config +import com.simplemobiletools.clock.extensions.createNewTimer import com.simplemobiletools.clock.extensions.timerHelper +import com.simplemobiletools.clock.models.Timer import com.simplemobiletools.clock.models.TimerEvent import com.simplemobiletools.commons.extensions.hideKeyboard import com.simplemobiletools.commons.extensions.updateTextColors @@ -27,6 +30,7 @@ class TimerFragment : Fragment() { private lateinit var timerAdapter: TimerAdapter private var timerPositionToScrollTo = INVALID_POSITION private var storedTextColor = 0 + private var currentEditAlarmDialog: EditTimerDialog? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -40,9 +44,7 @@ class TimerFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { view = (inflater.inflate(R.layout.fragment_timer, container, false) as ViewGroup).apply { - timerAdapter = TimerAdapter(requireActivity() as SimpleActivity) { - refreshTimers() - } + timerAdapter = TimerAdapter(requireActivity() as SimpleActivity, ::refreshTimers, ::openEditTimer) storeStateVariables() @@ -50,9 +52,9 @@ class TimerFragment : Fragment() { timers_list.itemAnimator = null timer_add.setOnClickListener { - activity?.hideKeyboard(it) - activity?.timerHelper?.insertNewTimer { - refreshTimers(true) + activity?.run { + hideKeyboard() + openEditTimer(createNewTimer()) } } @@ -75,7 +77,6 @@ class TimerFragment : Fragment() { storeStateVariables() } - private fun refreshTimers(scrollToLatest: Boolean = false) { activity?.timerHelper?.getTimers { timers -> timerAdapter.submitList(timers) { @@ -101,7 +102,7 @@ class TimerFragment : Fragment() { } fun updateAlarmSound(alarmSound: AlarmSound) { - timerAdapter.updateAlarmSoundForSelectedTimer(alarmSound) + currentEditAlarmDialog?.updateAlarmSound(alarmSound) } fun updatePosition(timerId: Long) { @@ -118,4 +119,11 @@ class TimerFragment : Fragment() { } } } + + private fun openEditTimer(timer: Timer) { + currentEditAlarmDialog = EditTimerDialog(activity as SimpleActivity, timer) { + currentEditAlarmDialog = null + refreshTimers() + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Config.kt index 67c5ec41..3fb71c78 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Config.kt @@ -5,6 +5,7 @@ import android.media.RingtoneManager import com.simplemobiletools.clock.extensions.gson.gson import com.simplemobiletools.clock.models.Alarm import com.simplemobiletools.clock.models.StateWrapper +import com.simplemobiletools.clock.models.Timer import com.simplemobiletools.clock.models.TimerState import com.simplemobiletools.commons.extensions.getDefaultAlarmSound import com.simplemobiletools.commons.extensions.getDefaultAlarmTitle @@ -79,6 +80,12 @@ class Config(context: Context) : BaseConfig(context) { } set(alarm) = prefs.edit().putString(ALARM_LAST_CONFIG, gson.toJson(alarm)).apply() + var timerLastConfig: Timer? + get() = prefs.getString(TIMER_LAST_CONFIG, null)?.let { lastAlarm -> + gson.fromJson(lastAlarm, Timer::class.java) + } + set(alarm) = prefs.edit().putString(TIMER_LAST_CONFIG, gson.toJson(alarm)).apply() + var timerChannelId: String? get() = prefs.getString(TIMER_CHANNEL_ID, null) set(id) = prefs.edit().putString(TIMER_CHANNEL_ID, id).apply() 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 0bf22bc3..c99fe5a1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/Constants.kt @@ -20,6 +20,7 @@ const val TIMER_LABEL = "timer_label" const val TIMER_MAX_REMINDER_SECS = "timer_max_reminder_secs" const val ALARM_MAX_REMINDER_SECS = "alarm_max_reminder_secs" const val ALARM_LAST_CONFIG = "alarm_last_config" +const val TIMER_LAST_CONFIG = "timer_last_config" const val USE_TEXT_SHADOW = "use_text_shadow" const val INCREASE_VOLUME_GRADUALLY = "increase_volume_gradually" const val ALARMS_SORT_BY = "alarms_sort_by" diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/TimerHelper.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/TimerHelper.kt index fb6771d4..ba255986 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/TimerHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/TimerHelper.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.clock.helpers import android.content.Context -import com.simplemobiletools.clock.extensions.config import com.simplemobiletools.clock.extensions.timerDb import com.simplemobiletools.clock.models.Timer import com.simplemobiletools.commons.helpers.ensureBackgroundThread @@ -34,24 +33,4 @@ class TimerHelper(val context: Context) { callback.invoke() } } - - fun insertNewTimer(callback: () -> Unit = {}) { - ensureBackgroundThread { - val config = context.config - timerDao.insertOrUpdateTimer( - Timer( - id = null, - seconds = config.timerSeconds, - state = config.timerState, - vibrate = config.timerVibrate, - soundUri = config.timerSoundUri, - soundTitle = config.timerSoundTitle, - label = config.timerLabel ?: "", - createdAt = System.currentTimeMillis(), - channelId = config.timerChannelId, - ) - ) - callback.invoke() - } - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/models/Timer.kt b/app/src/main/kotlin/com/simplemobiletools/clock/models/Timer.kt index bd098589..17062640 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/models/Timer.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/models/Timer.kt @@ -5,13 +5,13 @@ import androidx.room.PrimaryKey @Entity(tableName = "timers") data class Timer( - @PrimaryKey(autoGenerate = true) val id: Long?, - val seconds: Int, + @PrimaryKey(autoGenerate = true) var id: Long?, + var seconds: Int, val state: TimerState, - val vibrate: Boolean, - val soundUri: String, - val soundTitle: String, - val label: String, - val createdAt: Long, - val channelId: String? = null, + var vibrate: Boolean, + var soundUri: String, + var soundTitle: String, + var label: String, + var createdAt: Long, + var channelId: String? = null, ) diff --git a/app/src/main/res/layout/dialog_edit_timer.xml b/app/src/main/res/layout/dialog_edit_timer.xml new file mode 100644 index 00000000..f72ba1b4 --- /dev/null +++ b/app/src/main/res/layout/dialog_edit_timer.xml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/item_timer.xml b/app/src/main/res/layout/item_timer.xml index 1b6b4c39..96ea9bdf 100644 --- a/app/src/main/res/layout/item_timer.xml +++ b/app/src/main/res/layout/item_timer.xml @@ -1,145 +1,91 @@ - + android:layout_height="wrap_content" + android:background="?selectableItemBackground" + android:clickable="true" + android:focusable="true" + android:foreground="@drawable/selector"> - + android:layout_height="wrap_content"> - - - - - + android:layout_marginTop="@dimen/bigger_margin" + android:background="?attr/selectableItemBackground" + android:gravity="center_horizontal" + android:padding="@dimen/small_margin" + android:textSize="@dimen/stopwatch_text_size" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" + tools:text="00:00" /> - - - - - - - - - - + android:paddingStart="@dimen/small_margin" + android:paddingEnd="@dimen/small_margin" + android:textSize="@dimen/bigger_text_size" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/timer_time" + tools:text="Cook rice" /> - - + - + - + + + +