From 7e491869bc494d507a45eeda35cb03bb940c0984 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 25 Jan 2017 23:05:30 +0100 Subject: [PATCH] move some reminder related things to context extensions --- .../calendar/activities/SettingsActivity.kt | 43 ++++--------------- .../calendar/extensions/Context.kt | 35 +++++++++++++++ 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt index ca84f2cfd..a67c768d9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt @@ -11,7 +11,12 @@ import android.widget.AdapterView import android.widget.ArrayAdapter import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.extensions.config -import com.simplemobiletools.calendar.helpers.* +import com.simplemobiletools.calendar.extensions.getDefaultReminderTypeIndex +import com.simplemobiletools.calendar.extensions.getDefaultReminderValue +import com.simplemobiletools.calendar.extensions.setupReminderPeriod +import com.simplemobiletools.calendar.helpers.DAY_MINS +import com.simplemobiletools.calendar.helpers.HOUR_MINS +import com.simplemobiletools.calendar.helpers.REMINDER_CUSTOM import com.simplemobiletools.commons.extensions.* import kotlinx.android.synthetic.main.activity_settings.* @@ -132,9 +137,9 @@ class SettingsActivity : SimpleActivity() { private fun setupEventReminder() { val reminderType = config.defaultReminderType val reminderMinutes = config.defaultReminderMinutes - settings_default_reminder.setSelection(getDefaultReminderTypeIndex(reminderType)) + settings_default_reminder.setSelection(getDefaultReminderTypeIndex()) custom_reminder_save.setTextColor(custom_reminder_other_val.currentTextColor) - setupReminderPeriod(reminderMinutes) + setupReminderPeriod(reminderMinutes, custom_reminder_other_period, custom_reminder_value) settings_custom_reminder_holder.beVisibleIf(reminderType == REMINDER_CUSTOM) custom_reminder_save.setOnClickListener { saveReminder() } @@ -157,22 +162,6 @@ class SettingsActivity : SimpleActivity() { } } - private fun getDefaultReminderTypeIndex(type: Int): Int { - return when (type) { - REMINDER_OFF -> 0 - REMINDER_AT_START -> 1 - else -> 2 - } - } - - private fun getDefaultReminderValue(index: Int): Int { - return when (index) { - 0 -> REMINDER_OFF - 1 -> REMINDER_AT_START - else -> REMINDER_CUSTOM - } - } - private fun saveReminder() { val value = custom_reminder_value.value val multiplier = when (custom_reminder_other_period.selectedItemPosition) { @@ -187,22 +176,6 @@ class SettingsActivity : SimpleActivity() { hideKeyboard() } - private fun setupReminderPeriod(mins: Int) { - var value = mins - if (mins == 0) { - custom_reminder_other_period.setSelection(0) - } else if (mins % DAY_MINS == 0) { - value = mins / DAY_MINS - custom_reminder_other_period.setSelection(2) - } else if (mins % HOUR_MINS == 0) { - value = mins / HOUR_MINS - custom_reminder_other_period.setSelection(1) - } else { - custom_reminder_other_period.setSelection(0) - } - custom_reminder_value.setText(value.toString()) - } - private fun getWeeklyAdapter(): ArrayAdapter { val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item) for (i in 0..24) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt index 004f12382..6e018a2d9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -9,6 +9,8 @@ import android.content.Intent import android.graphics.Color import android.os.Build import android.os.SystemClock +import android.support.v7.widget.AppCompatSpinner +import android.widget.EditText import com.simplemobiletools.calendar.R import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.models.Event @@ -98,6 +100,39 @@ fun Context.getAppropriateTheme(): Int { return if (config.backgroundColor.getContrastColor() == Color.WHITE) R.style.MyDialogTheme_Dark else R.style.MyDialogTheme } +fun Context.getDefaultReminderTypeIndex(): Int { + val reminderType = config.defaultReminderType + return when (reminderType) { + REMINDER_OFF -> 0 + REMINDER_AT_START -> 1 + else -> 2 + } +} + +fun Context.getDefaultReminderValue(index: Int): Int { + return when (index) { + 0 -> REMINDER_OFF + 1 -> REMINDER_AT_START + else -> REMINDER_CUSTOM + } +} + +fun Context.setupReminderPeriod(mins: Int, otherPeriod: AppCompatSpinner, otherValue: EditText) { + var value = mins + if (mins == 0) { + otherPeriod.setSelection(0) + } else if (mins % DAY_MINS == 0) { + value = mins / DAY_MINS + otherPeriod.setSelection(2) + } else if (mins % HOUR_MINS == 0) { + value = mins / HOUR_MINS + otherPeriod.setSelection(1) + } else { + otherPeriod.setSelection(0) + } + otherValue.setText(value.toString()) +} + fun Context.getNewEventTimestampFromCode(dayCode: String) = Formatter.getLocalDateTimeFromCode(dayCode).withTime(13, 0, 0, 0).seconds() val Context.config: Config get() = Config.newInstance(this)