update the Alarm enabled state at toggling

This commit is contained in:
tibbi 2018-03-05 23:04:53 +01:00
parent 0f8d61c0d0
commit eee9458d4e
4 changed files with 33 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import android.view.ViewGroup
import com.simplemobiletools.clock.R import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.SimpleActivity import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.extensions.formatAlarmTime import com.simplemobiletools.clock.extensions.formatAlarmTime
import com.simplemobiletools.clock.interfaces.ToggleAlarmInterface
import com.simplemobiletools.clock.models.Alarm import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
@ -14,8 +15,8 @@ import com.simplemobiletools.commons.views.MyRecyclerView
import kotlinx.android.synthetic.main.item_alarm.view.* import kotlinx.android.synthetic.main.item_alarm.view.*
import java.util.* import java.util.*
class AlarmsAdapter(activity: SimpleActivity, var alarms: ArrayList<Alarm>, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : class AlarmsAdapter(activity: SimpleActivity, var alarms: ArrayList<Alarm>, val toggleAlarmInterface: ToggleAlarmInterface,
MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) { recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
private val adjustedPrimaryColor = activity.getAdjustedPrimaryColor() private val adjustedPrimaryColor = activity.getAdjustedPrimaryColor()
override fun getActionMenuId() = R.menu.cab_alarms override fun getActionMenuId() = R.menu.cab_alarms
@ -79,6 +80,9 @@ class AlarmsAdapter(activity: SimpleActivity, var alarms: ArrayList<Alarm>, recy
alarm_switch.isChecked = alarm.isEnabled alarm_switch.isChecked = alarm.isEnabled
alarm_switch.setColors(textColor, adjustedPrimaryColor, backgroundColor) alarm_switch.setColors(textColor, adjustedPrimaryColor, backgroundColor)
alarm_switch.setOnClickListener {
toggleAlarmInterface.alarmToggled(alarm.id, alarm_switch.isChecked)
}
} }
} }
} }

View File

@ -11,13 +11,16 @@ import com.simplemobiletools.clock.adapters.AlarmsAdapter
import com.simplemobiletools.clock.dialogs.EditAlarmDialog import com.simplemobiletools.clock.dialogs.EditAlarmDialog
import com.simplemobiletools.clock.extensions.createNewAlarm import com.simplemobiletools.clock.extensions.createNewAlarm
import com.simplemobiletools.clock.extensions.dbHelper import com.simplemobiletools.clock.extensions.dbHelper
import com.simplemobiletools.clock.interfaces.ToggleAlarmInterface
import com.simplemobiletools.clock.models.Alarm import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.extensions.updateTextColors
import kotlinx.android.synthetic.main.fragment_alarm.view.* import kotlinx.android.synthetic.main.fragment_alarm.view.*
class AlarmFragment : Fragment() { class AlarmFragment : Fragment(), ToggleAlarmInterface {
private val DEFAULT_ALARM_MINUTES = 480 private val DEFAULT_ALARM_MINUTES = 480
private var alarms = ArrayList<Alarm>()
lateinit var view: ViewGroup lateinit var view: ViewGroup
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
@ -43,10 +46,10 @@ class AlarmFragment : Fragment() {
} }
private fun setupAlarms() { private fun setupAlarms() {
val alarms = context!!.dbHelper.getAlarms() alarms = context!!.dbHelper.getAlarms()
val currAdapter = view.alarms_list.adapter val currAdapter = view.alarms_list.adapter
if (currAdapter == null) { if (currAdapter == null) {
val alarmsAdapter = AlarmsAdapter(activity as SimpleActivity, alarms, view.alarms_list) { val alarmsAdapter = AlarmsAdapter(activity as SimpleActivity, alarms, this, view.alarms_list) {
openEditAlarm(it as Alarm) openEditAlarm(it as Alarm)
} }
view.alarms_list.adapter = alarmsAdapter view.alarms_list.adapter = alarmsAdapter
@ -60,4 +63,12 @@ class AlarmFragment : Fragment() {
setupAlarms() setupAlarms()
} }
} }
override fun alarmToggled(id: Int, isEnabled: Boolean) {
if (context!!.dbHelper.updateAlarmEnabledState(id, isEnabled)) {
alarms.firstOrNull { it.id == id }?.isEnabled = isEnabled
} else {
activity!!.toast(R.string.unknown_error_occurred)
}
}
} }

View File

@ -67,6 +67,14 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return mDb.update(ALARMS_TABLE_NAME, values, selection, selectionArgs) == 1 return mDb.update(ALARMS_TABLE_NAME, values, selection, selectionArgs) == 1
} }
fun updateAlarmEnabledState(id: Int, isEnabled: Boolean): Boolean {
val selectionArgs = arrayOf(id.toString())
val values = ContentValues()
values.put(COL_IS_ENABLED, isEnabled)
val selection = "$COL_ID = ?"
return mDb.update(ALARMS_TABLE_NAME, values, selection, selectionArgs) == 1
}
private fun fillAlarmContentValues(alarm: Alarm): ContentValues { private fun fillAlarmContentValues(alarm: Alarm): ContentValues {
return ContentValues().apply { return ContentValues().apply {
put(COL_TIME_IN_MINUTES, alarm.timeInMinutes) put(COL_TIME_IN_MINUTES, alarm.timeInMinutes)

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.clock.interfaces
interface ToggleAlarmInterface {
fun alarmToggled(id: Int, isEnabled: Boolean)
}