replace the EventReminderDialog with the generic radiobutton dialog

This commit is contained in:
tibbi 2017-04-02 15:34:59 +02:00
parent d5a4a02726
commit 6e88c8a425
4 changed files with 46 additions and 76 deletions

View File

@ -9,7 +9,6 @@ import android.view.MenuItem
import android.view.WindowManager
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.dialogs.DeleteEventDialog
import com.simplemobiletools.calendar.dialogs.EventReminderDialog
import com.simplemobiletools.calendar.dialogs.EventRepeatIntervalDialog
import com.simplemobiletools.calendar.dialogs.SelectEventTypeDialog
import com.simplemobiletools.calendar.extensions.*
@ -119,21 +118,21 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
}
private fun showReminder1Dialog() {
EventReminderDialog(this, mReminder1Minutes) {
showEventReminderDialog(mReminder1Minutes) {
mReminder1Minutes = it
checkReminderTexts()
}
}
private fun showReminder2Dialog() {
EventReminderDialog(this, mReminder2Minutes) {
showEventReminderDialog(mReminder2Minutes) {
mReminder2Minutes = it
checkReminderTexts()
}
}
private fun showReminder3Dialog() {
EventReminderDialog(this, mReminder3Minutes) {
showEventReminderDialog(mReminder3Minutes) {
mReminder3Minutes = it
checkReminderTexts()
}

View File

@ -10,7 +10,6 @@ import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.dialogs.EventReminderDialog
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.extensions.getReminderText
@ -196,7 +195,7 @@ class SettingsActivity : SimpleActivity() {
var reminderMinutes = config.defaultReminderMinutes
settings_default_reminder.text = getReminderText(reminderMinutes)
settings_default_reminder_holder.setOnClickListener {
EventReminderDialog(this, reminderMinutes) {
showEventReminderDialog(reminderMinutes) {
config.defaultReminderMinutes = it
reminderMinutes = it
settings_default_reminder.text = getReminderText(it)

View File

@ -1,10 +1,52 @@
package com.simplemobiletools.calendar.activities
import android.os.Bundle
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.dialogs.CustomEventReminderDialog
import com.simplemobiletools.calendar.extensions.getReminderText
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.models.RadioItem
import java.util.TreeSet
import kotlin.collections.ArrayList
open class SimpleActivity : BaseSimpleActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
protected fun showEventReminderDialog(curMinutes: Int, callback: (minutes: Int) -> Unit) {
val minutes = TreeSet<Int>()
minutes.apply {
add(-1)
add(0)
add(10)
add(30)
add(curMinutes)
}
val items = ArrayList<RadioItem>(minutes.size + 1)
minutes.mapIndexedTo(items, {
index, value ->
RadioItem(index, getReminderText(value), value)
})
var selectedIndex = 0
minutes.forEachIndexed { index, value ->
if (value == curMinutes)
selectedIndex = index
}
items.add(RadioItem(-2, getString(R.string.custom)))
RadioGroupDialog(this, items, selectedIndex) {
if (it == -2) {
CustomEventReminderDialog(this) {
callback(it)
}
} else {
callback(it as Int)
}
}
}
}

View File

@ -1,70 +0,0 @@
package com.simplemobiletools.calendar.dialogs
import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.ViewGroup
import android.widget.RadioButton
import android.widget.RadioGroup
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.extensions.getReminderText
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_radio_group.view.*
import java.util.*
class EventReminderDialog(val activity: Activity, val reminderMinutes: Int, val callback: (minutes: Int) -> Unit) : AlertDialog.Builder(activity),
RadioGroup.OnCheckedChangeListener {
val dialog: AlertDialog?
var wasInit = false
var minutes = TreeSet<Int>()
var radioGroup: RadioGroup
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null)
radioGroup = view.dialog_radio_group
radioGroup.setOnCheckedChangeListener(this)
minutes.apply {
add(-1)
add(0)
add(10)
add(30)
add(reminderMinutes)
}
minutes.forEachIndexed { index, value ->
addRadioButton(activity.getReminderText(value), value, index)
}
addRadioButton(activity.getString(R.string.custom), -2, minutes.size)
dialog = AlertDialog.Builder(activity)
.create().apply {
activity.setupDialogStuff(view, this, R.string.select_event_reminder)
}
wasInit = true
}
private fun addRadioButton(textValue: String, value: Int, index: Int) {
val radioButton = (activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton).apply {
text = textValue
isChecked = value == reminderMinutes
id = index
}
radioGroup.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
}
override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
if (!wasInit)
return
if (checkedId == minutes.size) {
CustomEventReminderDialog(activity) {
callback.invoke(it)
dialog?.dismiss()
}
} else {
callback.invoke(minutes.elementAt(checkedId))
dialog?.dismiss()
}
}
}