replace EventRepeatInterval with the generic radiobutton dialog

This commit is contained in:
tibbi 2017-04-02 17:03:35 +02:00
parent 6e88c8a425
commit 0c1119c150
3 changed files with 41 additions and 75 deletions

View File

@ -9,7 +9,6 @@ import android.view.MenuItem
import android.view.WindowManager import android.view.WindowManager
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.dialogs.DeleteEventDialog import com.simplemobiletools.calendar.dialogs.DeleteEventDialog
import com.simplemobiletools.calendar.dialogs.EventRepeatIntervalDialog
import com.simplemobiletools.calendar.dialogs.SelectEventTypeDialog import com.simplemobiletools.calendar.dialogs.SelectEventTypeDialog
import com.simplemobiletools.calendar.extensions.* import com.simplemobiletools.calendar.extensions.*
import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.*
@ -139,7 +138,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
} }
private fun showRepeatIntervalDialog() { private fun showRepeatIntervalDialog() {
EventRepeatIntervalDialog(this, mRepeatInterval) { showEventRepeatIntervalDialog(mRepeatInterval) {
mRepeatInterval = it mRepeatInterval = it
updateRepetitionText() updateRepetitionText()
checkRepeatLimit(it) checkRepeatLimit(it)

View File

@ -3,7 +3,10 @@ package com.simplemobiletools.calendar.activities
import android.os.Bundle import android.os.Bundle
import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.dialogs.CustomEventReminderDialog import com.simplemobiletools.calendar.dialogs.CustomEventReminderDialog
import com.simplemobiletools.calendar.dialogs.CustomEventRepeatIntervalDialog
import com.simplemobiletools.calendar.extensions.getReminderText import com.simplemobiletools.calendar.extensions.getReminderText
import com.simplemobiletools.calendar.extensions.getRepetitionText
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
@ -49,4 +52,41 @@ open class SimpleActivity : BaseSimpleActivity() {
} }
} }
} }
protected fun showEventRepeatIntervalDialog(curSeconds: Int, callback: (minutes: Int) -> Unit) {
val seconds = TreeSet<Int>()
seconds.apply {
add(0)
add(DAY)
add(WEEK)
add(BIWEEK)
add(MONTH)
add(YEAR)
add(curSeconds)
}
val items = ArrayList<RadioItem>(seconds.size + 1)
seconds.mapIndexedTo(items, {
index, value ->
RadioItem(index, getRepetitionText(value), value)
})
var selectedIndex = 0
seconds.forEachIndexed { index, value ->
if (value == curSeconds)
selectedIndex = index
}
items.add(RadioItem(-1, getString(R.string.custom)))
RadioGroupDialog(this, items, selectedIndex) {
if (it == -1) {
CustomEventRepeatIntervalDialog(this) {
callback(it)
}
} else {
callback(it as Int)
}
}
}
} }

View File

@ -1,73 +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.getRepetitionText
import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.commons.extensions.setupDialogStuff
import kotlinx.android.synthetic.main.dialog_radio_group.view.*
import java.util.*
class EventRepeatIntervalDialog(val activity: Activity, val repeatSeconds: Int, val callback: (mins: Int) -> Unit) : AlertDialog.Builder(activity),
RadioGroup.OnCheckedChangeListener {
val dialog: AlertDialog?
var wasInit = false
var seconds = 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)
seconds.apply {
add(0)
add(DAY)
add(WEEK)
add(BIWEEK)
add(MONTH)
add(YEAR)
add(repeatSeconds)
}
seconds.forEachIndexed { index, value ->
addRadioButton(activity.getRepetitionText(value), value, index)
}
addRadioButton(activity.getString(R.string.custom), -1, seconds.size)
dialog = AlertDialog.Builder(activity)
.create().apply {
activity.setupDialogStuff(view, this, R.string.select_repeat_interval)
}
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 == repeatSeconds
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 == seconds.size) {
CustomEventRepeatIntervalDialog(activity) {
callback.invoke(it)
dialog?.dismiss()
}
} else {
callback.invoke(seconds.elementAt(checkedId))
dialog?.dismiss()
}
}
}