From c44940dd0a1a105a6e246059a4f278b5ae69c432 Mon Sep 17 00:00:00 2001 From: Phil Shadlyn Date: Fri, 23 Aug 2019 14:12:27 -0400 Subject: [PATCH] GH-36: merge Reminder.displayTime and Reminder.displayString properties --- .../remindme/reminders/ReminderViewModel.kt | 20 ++---------------- .../reminders/list/ReminderListAdapter.kt | 15 +++++++++---- .../android/remindme/util/ReminderExt.kt | 19 +++++++++++++++++ .../remindme/util/ReminderExtensions.kt | 21 ------------------- 4 files changed, 32 insertions(+), 43 deletions(-) create mode 100644 app/src/main/java/com/physphil/android/remindme/util/ReminderExt.kt delete mode 100644 app/src/main/java/com/physphil/android/remindme/util/ReminderExtensions.kt diff --git a/app/src/main/java/com/physphil/android/remindme/reminders/ReminderViewModel.kt b/app/src/main/java/com/physphil/android/remindme/reminders/ReminderViewModel.kt index 7d54e5a..00f00e5 100644 --- a/app/src/main/java/com/physphil/android/remindme/reminders/ReminderViewModel.kt +++ b/app/src/main/java/com/physphil/android/remindme/reminders/ReminderViewModel.kt @@ -14,13 +14,10 @@ import com.physphil.android.remindme.models.Recurrence import com.physphil.android.remindme.models.Reminder import com.physphil.android.remindme.util.SingleLiveEvent import com.physphil.android.remindme.util.ViewString -import com.physphil.android.remindme.util.isNow -import com.physphil.android.remindme.util.isToday -import com.physphil.android.remindme.util.isTomorrow +import com.physphil.android.remindme.util.displayDate +import com.physphil.android.remindme.util.displayTime import com.physphil.android.remindme.util.millis import org.threeten.bp.LocalDateTime -import org.threeten.bp.format.DateTimeFormatter -import org.threeten.bp.format.FormatStyle /** * Copyright (c) 2017 Phil Shadlyn @@ -201,19 +198,6 @@ class ReminderViewModel( recurrence = recurrence.displayString ) - private val Reminder.displayTime: ViewString - get() = when { - time.isNow() -> ViewString.Integer(R.string.reminder_time_now) - else -> ViewString.String(time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT))) - } - - private val Reminder.displayDate: ViewString - get() = when { - time.isToday() -> ViewString.Integer(R.string.reminder_repeat_today) - time.isTomorrow() -> ViewString.Integer(R.string.reminder_repeat_tomorrow) - else -> ViewString.String(time.format(DateTimeFormatter.ofPattern("EEE MMM d, yyyy"))) - } - data class Time(val hour: Int, val minute: Int) data class Date(val year: Int, val month: Int, val day: Int) diff --git a/app/src/main/java/com/physphil/android/remindme/reminders/list/ReminderListAdapter.kt b/app/src/main/java/com/physphil/android/remindme/reminders/list/ReminderListAdapter.kt index b6b8a4b..48cbf0a 100644 --- a/app/src/main/java/com/physphil/android/remindme/reminders/list/ReminderListAdapter.kt +++ b/app/src/main/java/com/physphil/android/remindme/reminders/list/ReminderListAdapter.kt @@ -8,8 +8,9 @@ import androidx.recyclerview.widget.RecyclerView import com.physphil.android.remindme.R import com.physphil.android.remindme.models.Recurrence import com.physphil.android.remindme.models.Reminder -import com.physphil.android.remindme.util.getDisplayDate -import com.physphil.android.remindme.util.getDisplayTime +import com.physphil.android.remindme.util.ViewString +import com.physphil.android.remindme.util.displayDate +import com.physphil.android.remindme.util.displayTime import com.physphil.android.remindme.util.setVisibility import kotlinx.android.synthetic.main.view_header_reminder_list.view.* import kotlinx.android.synthetic.main.view_row_reminder_list.view.* @@ -54,8 +55,14 @@ class ReminderListAdapter : RecyclerView.Adapter listener?.onDeleteReminder(reminder) } - reminderItemDateView.text = reminder.getDisplayDate(context) - reminderItemTimeView.text = reminder.getDisplayTime(context) + reminderItemDateView.text = when (val date = reminder.displayDate) { + is ViewString.String -> date.value + is ViewString.Integer -> context.getString(date.resId) + } + reminderItemTimeView.text = when (val time = reminder.displayTime) { + is ViewString.String -> time.value + is ViewString.Integer -> context.getString(time.resId) + } reminderItemTitleView.text = reminder.title // Hide description if not entered diff --git a/app/src/main/java/com/physphil/android/remindme/util/ReminderExt.kt b/app/src/main/java/com/physphil/android/remindme/util/ReminderExt.kt new file mode 100644 index 0000000..a0e7927 --- /dev/null +++ b/app/src/main/java/com/physphil/android/remindme/util/ReminderExt.kt @@ -0,0 +1,19 @@ +package com.physphil.android.remindme.util + +import com.physphil.android.remindme.R +import com.physphil.android.remindme.models.Reminder +import org.threeten.bp.format.DateTimeFormatter +import org.threeten.bp.format.FormatStyle + +val Reminder.displayTime: ViewString + get() = when { + time.isNow() -> ViewString.Integer(R.string.reminder_time_now) + else -> ViewString.String(time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT))) + } + +val Reminder.displayDate: ViewString + get() = when { + time.isToday() -> ViewString.Integer(R.string.reminder_repeat_today) + time.isTomorrow() -> ViewString.Integer(R.string.reminder_repeat_tomorrow) + else -> ViewString.String(time.format(DateTimeFormatter.ofPattern("EEE MMM d, yyyy"))) + } diff --git a/app/src/main/java/com/physphil/android/remindme/util/ReminderExtensions.kt b/app/src/main/java/com/physphil/android/remindme/util/ReminderExtensions.kt deleted file mode 100644 index 7080d87..0000000 --- a/app/src/main/java/com/physphil/android/remindme/util/ReminderExtensions.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.physphil.android.remindme.util - -import android.annotation.SuppressLint -import android.content.Context -import com.physphil.android.remindme.R -import com.physphil.android.remindme.models.Reminder -import java.text.DateFormat -import java.text.SimpleDateFormat - -fun Reminder.getDisplayTime(context: Context): String = when { - time.isNow() -> context.getString(R.string.reminder_time_now) - else -> SimpleDateFormat.getTimeInstance(DateFormat.SHORT).format(time.millis) -} - -@SuppressLint("SimpleDateFormat") -fun Reminder.getDisplayDate(context: Context): String = when { - time.isToday() -> context.getString(R.string.reminder_repeat_today) - time.isTomorrow() -> context.getString(R.string.reminder_repeat_tomorrow) - else -> SimpleDateFormat("EEE MMM d, yyyy").format(time.millis) -} -