From 8c8c886b917e46f31b60715bf8c384cec958aab0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 5 Feb 2017 16:29:35 +0100 Subject: [PATCH] update the event reminder picker at importing ICS files --- .../calendar/dialogs/ImportEventsDialog.kt | 51 ++++++------------- .../calendar/extensions/Context.kt | 28 ---------- .../calendar/fragments/WeekFragment.kt | 3 ++ .../calendar/helpers/Config.kt | 16 +----- .../calendar/helpers/Constants.kt | 6 --- .../main/res/layout/dialog_import_events.xml | 45 ++-------------- 6 files changed, 23 insertions(+), 126 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt index 785987fb7..0a797e5fc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/dialogs/ImportEventsDialog.kt @@ -3,33 +3,29 @@ package com.simplemobiletools.calendar.dialogs import android.app.Activity import android.support.v7.app.AlertDialog import android.view.LayoutInflater -import android.view.View -import android.widget.AdapterView import com.simplemobiletools.calendar.R -import com.simplemobiletools.calendar.extensions.getDefaultReminderTypeIndex -import com.simplemobiletools.calendar.extensions.setupReminderPeriod -import com.simplemobiletools.calendar.helpers.* +import com.simplemobiletools.calendar.extensions.config +import com.simplemobiletools.calendar.extensions.getReminderText +import com.simplemobiletools.calendar.helpers.IcsParser import com.simplemobiletools.calendar.helpers.IcsParser.ImportResult.* -import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.extensions.humanizePath +import com.simplemobiletools.commons.extensions.setupDialogStuff +import com.simplemobiletools.commons.extensions.toast import kotlinx.android.synthetic.main.dialog_import_events.view.* class ImportEventsDialog(val activity: Activity, val path: String, val callback: (refreshView: Boolean) -> Unit) : AlertDialog.Builder(activity) { + var reminderMinutes = 0 + init { val view = LayoutInflater.from(activity).inflate(R.layout.dialog_import_events, null).apply { + reminderMinutes = activity.config.defaultReminderMinutes import_events_filename.text = activity.humanizePath(path) - import_events_reminder.setSelection(context.getDefaultReminderTypeIndex()) - context.setupReminderPeriod(import_events_custom_reminder_other_period, import_events_custom_reminder_value) - - import_events_reminder.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { - override fun onItemSelected(p0: AdapterView<*>?, p1: View?, itemIndex: Int, p3: Long) { - import_events_custom_reminder_holder.beVisibleIf(itemIndex == 2) - if (itemIndex == 2) { - activity.showKeyboard(import_events_custom_reminder_value) - } - } - - override fun onNothingSelected(p0: AdapterView<*>?) { + import_events_reminder.text = activity.getReminderText(reminderMinutes) + import_events_reminder.setOnClickListener { + EventReminderDialog(activity, reminderMinutes) { + reminderMinutes = it + import_events_reminder.text = activity.getReminderText(it) } } } @@ -40,14 +36,8 @@ class ImportEventsDialog(val activity: Activity, val path: String, val callback: .create().apply { activity.setupDialogStuff(view, this, R.string.import_events) getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({ - val minutes = when (view.import_events_reminder.selectedItemPosition) { - 0 -> REMINDER_OFF - 1 -> REMINDER_AT_START - else -> getReminderMinutes(view) - } - Thread({ - val result = IcsParser().parseIcs(context, minutes, path) + val result = IcsParser().parseIcs(context, reminderMinutes, path) handleParseResult(result) dismiss() }).start() @@ -65,15 +55,4 @@ class ImportEventsDialog(val activity: Activity, val path: String, val callback: callback.invoke(result != IMPORT_FAIL) } } - - private fun getReminderMinutes(view: View): Int { - val multiplier = when (view.import_events_custom_reminder_other_period.selectedItemPosition) { - 1 -> HOUR_MINS - 2 -> DAY_MINS - else -> 1 - } - - val value = view.import_events_custom_reminder_value.value - return Integer.valueOf(if (value.isEmpty()) "0" else value) * multiplier - } } 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 d8dd7d5b0..231646945 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -9,8 +9,6 @@ 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 @@ -100,32 +98,6 @@ 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.setupReminderPeriod(otherPeriod: AppCompatSpinner, otherValue: EditText) { - val mins = config.defaultReminderMinutes - 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.getReminderText(minutes: Int) = when (minutes) { -1 -> getString(R.string.no_reminder) 0 -> getString(R.string.at_start) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt index 1c363d03f..ea76ded32 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/WeekFragment.kt @@ -291,6 +291,9 @@ class WeekFragment : Fragment(), WeeklyCalendar { mView.week_top_holder.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { + if (activity == null) + return + mView.week_top_holder.viewTreeObserver.removeOnGlobalLayoutListener(this) if (isFragmentVisible) { (activity as MainActivity).updateHoursTopMargin(mView.week_top_holder.height) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt index 0596d3e91..f36ac00f6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt @@ -41,23 +41,9 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getInt(VIEW, MONTHLY_VIEW) set(view) = prefs.edit().putInt(VIEW, view).apply() - var defaultReminderType: Int - get() = prefs.getInt(REMINDER_TYPE, REMINDER_AT_START) - set(type) { - var newType = type - if (newType == REMINDER_CUSTOM && defaultReminderMinutes == 0) - newType = REMINDER_AT_START - - prefs.edit().putInt(REMINDER_TYPE, newType).apply() - } - var defaultReminderMinutes: Int get() = prefs.getInt(REMINDER_MINUTES, 10) - set(mins) { - if (mins == 0) - defaultReminderType = REMINDER_AT_START - prefs.edit().putInt(REMINDER_MINUTES, mins).apply() - } + set(mins) = prefs.edit().putInt(REMINDER_MINUTES, mins).apply() var googleSync: Boolean get() = prefs.getBoolean(GOOGLE_SYNC, false) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt index 9af00eb17..c653ce252 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt @@ -17,8 +17,6 @@ val EVENTS_LIST_VIEW = 3 val WEEKLY_VIEW = 4 val REMINDER_OFF = -1 -val REMINDER_AT_START = 0 -val REMINDER_CUSTOM = 1 val DAY = 86400 val WEEK = 604800 @@ -26,9 +24,6 @@ val BIWEEK = 1209600 val MONTH = 2592000 // exact value not taken into account, Joda is used for adding months and years val YEAR = 31536000 -val HOUR_MINS = 60 -val DAY_MINS = 1440 - // Shared Preferences val SUNDAY_FIRST = "sunday_first" val WEEK_NUMBERS = "week_numbers" @@ -37,7 +32,6 @@ val END_WEEKLY_AT = "end_weekly_at" val VIBRATE = "vibrate" val REMINDER_SOUND = "reminder_sound" val VIEW = "view" -val REMINDER_TYPE = "reminder_type" val REMINDER_MINUTES = "reminder_minutes" val GOOGLE_SYNC = "google_sync" val SYNC_ACCOUNT_NAME = "sync_account_name" diff --git a/app/src/main/res/layout/dialog_import_events.xml b/app/src/main/res/layout/dialog_import_events.xml index 4b08c7ecc..4c2203b91 100644 --- a/app/src/main/res/layout/dialog_import_events.xml +++ b/app/src/main/res/layout/dialog_import_events.xml @@ -33,49 +33,12 @@ android:text="@string/event_reminder" android:textSize="@dimen/smaller_text_size"/> - - - + android:layout_marginEnd="@dimen/small_margin" + android:layout_marginRight="@dimen/small_margin" + android:padding="@dimen/normal_margin"/> - - - - - - -