-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
integrate kronos-android ntp clock library into project #150
base: develop
Are you sure you want to change the base?
Changes from 9 commits
fe9df02
bcc9950
c7f3c82
d04e542
385e199
fdff161
2f31082
1a90ab6
f2f9b99
e626be8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.covidwatch.android.data | ||
|
||
import android.content.Context | ||
import com.lyft.kronos.AndroidClockFactory | ||
import com.lyft.kronos.KronosClock | ||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
|
||
class NtpTime (context : Context) { | ||
private val kronosClock : KronosClock = AndroidClockFactory.createKronosClock(context) | ||
|
||
private fun currentTimeMillis() : Long = kronosClock.getCurrentTimeMs() | ||
|
||
fun syncInBackground() = kronosClock.syncInBackground() | ||
|
||
fun nowAsInstant() : Instant = | ||
Instant.ofEpochMilli(currentTimeMillis()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.covidwatch.android.extension | ||
|
||
import java.time.Instant | ||
|
||
// 1d * (24h/d) * (60m/h) * (60s/m) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating extensions is nice but why then don't use existing API inside of them? minus(15, ChronoUnit.DAYS) add(Duration.ofDays(15)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed |
||
fun Instant.plusDays(daysToAdd : Long) : Instant = | ||
plusSeconds(daysToAdd * 24 * 60 * 60) | ||
|
||
fun Instant.minusDays(daysToSubtract : Long) : Instant = plusDays(-daysToSubtract) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,19 +14,21 @@ import com.google.android.material.datepicker.CalendarConstraints | |
import com.google.android.material.datepicker.MaterialDatePicker | ||
import com.google.android.material.textfield.TextInputLayout | ||
import org.covidwatch.android.R | ||
import org.covidwatch.android.data.NtpTime | ||
import org.covidwatch.android.databinding.FragmentVerifyPositiveDiagnosisBinding | ||
import org.covidwatch.android.extension.minusDays | ||
import org.covidwatch.android.extension.observe | ||
import org.covidwatch.android.extension.observeEvent | ||
import org.covidwatch.android.extension.observeNullableEvent | ||
import org.covidwatch.android.extension.toInstant | ||
import org.covidwatch.android.ui.BaseViewModelFragment | ||
import org.koin.android.ext.android.inject | ||
import org.koin.androidx.viewmodel.ext.android.stateViewModel | ||
import java.time.LocalDate | ||
|
||
class VerifyPositiveDiagnosisFragment : | ||
BaseViewModelFragment<FragmentVerifyPositiveDiagnosisBinding, VerifyPositiveDiagnosisViewModel>() { | ||
|
||
override val viewModel: VerifyPositiveDiagnosisViewModel by stateViewModel() | ||
private val ntpTime: NtpTime by inject() | ||
|
||
override fun bind( | ||
inflater: LayoutInflater, | ||
|
@@ -119,15 +121,16 @@ class VerifyPositiveDiagnosisFragment : | |
val builder = MaterialDatePicker.Builder.datePicker() | ||
val constraints = CalendarConstraints.Builder() | ||
|
||
val twoWeeksAgo = LocalDate.now().plusDays(-14).toInstant() | ||
.toEpochMilli() | ||
val twoWeeksAgo = ntpTime.nowAsInstant().minusDays(14).toEpochMilli() | ||
|
||
// Day in the past or 14 days back | ||
val fromWhen = dayInPast ?: twoWeeksAgo | ||
|
||
val untilNow = LocalDate.now().toInstant().toEpochMilli() | ||
val untilNow = ntpTime.nowAsInstant().toEpochMilli() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the conversion is redundant because we use the milliseconds anyway. We could make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. calling currentTimeMillis() directly |
||
|
||
constraints.setValidator(BaseDateValidator { it in fromWhen..untilNow }) | ||
constraints | ||
.setValidator(BaseDateValidator { it in fromWhen..untilNow }) | ||
.setOpenAt(untilNow) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 👍 |
||
|
||
val datePicker = builder | ||
.setSelection(selection) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use the same formatter from AS in the future to avoid this kind of changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed