From 0c1119c15085d12b1ba6c3af0b1e6657af18aaf2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 2 Apr 2017 17:03:35 +0200 Subject: [PATCH] replace EventRepeatInterval with the generic radiobutton dialog --- .../calendar/activities/EventActivity.kt | 3 +- .../calendar/activities/SimpleActivity.kt | 40 ++++++++++ .../dialogs/EventRepeatIntervalDialog.kt | 73 ------------------- 3 files changed, 41 insertions(+), 75 deletions(-) delete mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/EventRepeatIntervalDialog.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt index 7d346c57c..fd4ceb5b8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/EventActivity.kt @@ -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.EventRepeatIntervalDialog import com.simplemobiletools.calendar.dialogs.SelectEventTypeDialog import com.simplemobiletools.calendar.extensions.* import com.simplemobiletools.calendar.helpers.* @@ -139,7 +138,7 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener { } private fun showRepeatIntervalDialog() { - EventRepeatIntervalDialog(this, mRepeatInterval) { + showEventRepeatIntervalDialog(mRepeatInterval) { mRepeatInterval = it updateRepetitionText() checkRepeatLimit(it) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SimpleActivity.kt index 03a8ee3f7..ec13b6986 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SimpleActivity.kt @@ -3,7 +3,10 @@ package com.simplemobiletools.calendar.activities import android.os.Bundle import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.dialogs.CustomEventReminderDialog +import com.simplemobiletools.calendar.dialogs.CustomEventRepeatIntervalDialog 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.dialogs.RadioGroupDialog 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() + seconds.apply { + add(0) + add(DAY) + add(WEEK) + add(BIWEEK) + add(MONTH) + add(YEAR) + add(curSeconds) + } + + val items = ArrayList(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) + } + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/EventRepeatIntervalDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/EventRepeatIntervalDialog.kt deleted file mode 100644 index 9127caaf3..000000000 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/EventRepeatIntervalDialog.kt +++ /dev/null @@ -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() - 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() - } - } -}