Skip to content

Commit

Permalink
GH-36: create localDateTimeFromMillis function
Browse files Browse the repository at this point in the history
  • Loading branch information
physphil committed Aug 23, 2019
1 parent 2703544 commit 7692fbc
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import com.physphil.android.remindme.data.ReminderRepo
import com.physphil.android.remindme.models.Recurrence
import com.physphil.android.remindme.reminders.ReminderActivity
import com.physphil.android.remindme.util.Notification
import com.physphil.android.remindme.util.localDateTimeFromMillis
import com.physphil.android.remindme.util.millis
import org.threeten.bp.Instant
import org.threeten.bp.LocalDateTime
import org.threeten.bp.ZoneId

/**
* Copyright (c) 2017 Phil Shadlyn
Expand Down Expand Up @@ -89,7 +87,7 @@ class ShowNotificationJob(
text: String,
recurrence: Recurrence
) {
val newTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).apply {
val newTime = localDateTimeFromMillis(time).apply {
when (recurrence) {
Recurrence.HOURLY -> this.plusHours(1)
Recurrence.DAILY -> this.plusDays(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ReminderViewModel(
fun updateDate(year: Int, month: Int, dayOfMonth: Int) {
val newTime = reminder.time
.withYear(year)
.withMonth(month + 1) // Month is 0-11
.withMonth(month + 1) // Month is 0-11 in DatePicker
.withDayOfMonth(dayOfMonth)
reminder = reminder.copy(time = newTime)
_reminderDateLiveData.postValue(reminder.displayDate)
Expand All @@ -136,7 +136,7 @@ class ReminderViewModel(
_openDatePickerEvent.postValue(
Date(
year = year,
month = monthValue - 1, // Month is 0-11
month = monthValue - 1, // Month is 0-11 in DatePicker
day = dayOfMonth
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import com.physphil.android.remindme.REMINDER_COLUMN_TITLE
import com.physphil.android.remindme.TABLE_REMINDERS
import com.physphil.android.remindme.models.Recurrence
import com.physphil.android.remindme.models.Reminder
import org.threeten.bp.Instant
import org.threeten.bp.LocalDateTime
import org.threeten.bp.ZoneId
import com.physphil.android.remindme.util.localDateTimeFromMillis

/**
* Entity to store Reminder objects in Room
Expand Down Expand Up @@ -71,12 +69,9 @@ data class ReminderEntity(
id = id,
title = title,
body = body,
time = fromMillis(this@ReminderEntity.time),
time = localDateTimeFromMillis(this@ReminderEntity.time),
recurrence = Recurrence.fromId(recurrence),
externalId = externalId,
notificationId = notificationId
)

private fun fromMillis(millis: Long): LocalDateTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault())
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.physphil.android.remindme.util

import org.threeten.bp.Instant
import org.threeten.bp.LocalDateTime
import org.threeten.bp.ZoneId
import org.threeten.bp.ZonedDateTime
Expand Down Expand Up @@ -56,4 +57,7 @@ fun LocalDateTime.tomorrowMorning(): LocalDateTime =
private fun LocalDateTime.advanceDay(): LocalDateTime = plusDays(1)

val LocalDateTime.millis: Long
get() = ZonedDateTime.of(this, ZoneId.systemDefault()).toInstant().toEpochMilli()
get() = ZonedDateTime.of(this, ZoneId.systemDefault()).toInstant().toEpochMilli()

fun localDateTimeFromMillis(millis: Long): LocalDateTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault())
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ 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)
}
}

0 comments on commit 7692fbc

Please sign in to comment.