From fe9df0255dca57cda718648cb1a77c6b14d5ca7f Mon Sep 17 00:00:00 2001 From: Spike Sprague Date: Tue, 1 Sep 2020 19:33:02 -0700 Subject: [PATCH 01/10] add Kronos ntp clock lib add Kronos ntp clock lib change DiagnosisKeysToken to use ntp time --- app/build.gradle | 6 +++++- .../android/BaseCovidWatchApplication.kt | 8 ++++++++ .../java/org/covidwatch/android/data/NTPTime.kt | 16 ++++++++++++++++ .../diagnosiskeystoken/DiagnosisKeysToken.kt | 5 +++-- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/covidwatch/android/data/NTPTime.kt diff --git a/app/build.gradle b/app/build.gradle index ee3bb251..65c337d4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -288,6 +288,10 @@ dependencies { testImplementation "io.mockk:mockk:1.10.0" // Koin testImplementation "org.koin:koin-test:$koin_version" + + // Kronos ntp clock + def kronos_latest_version = "0.0.1-alpha09" + implementation "com.lyft.kronos:kronos-android:$kronos_latest_version" } -apply plugin: 'com.google.gms.google-services' \ No newline at end of file +apply plugin: 'com.google.gms.google-services' diff --git a/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt b/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt index f7b01472..8ae53fdc 100644 --- a/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt +++ b/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt @@ -1,6 +1,7 @@ package org.covidwatch.android import android.app.Application +import android.content.Context import kotlinx.coroutines.GlobalScope import org.covidwatch.android.di.appModule import org.covidwatch.android.di.flavorSpecificModule @@ -36,5 +37,12 @@ open class BaseCovidWatchApplication : Application() { launchUseCase(removeOldExposuresUseCase) launchUseCase(updateRegionsUseCase) } + + appContext = applicationContext + } + + // convenience object to get the applicationContext from anywhere + companion object { + lateinit var appContext : Context } } diff --git a/app/src/main/java/org/covidwatch/android/data/NTPTime.kt b/app/src/main/java/org/covidwatch/android/data/NTPTime.kt new file mode 100644 index 00000000..db154863 --- /dev/null +++ b/app/src/main/java/org/covidwatch/android/data/NTPTime.kt @@ -0,0 +1,16 @@ +package org.covidwatch.android.data + +import com.lyft.kronos.AndroidClockFactory +import com.lyft.kronos.KronosClock +import org.covidwatch.android.BaseCovidWatchApplication + +object NTPTime { + private val kronosClock : KronosClock = AndroidClockFactory.createKronosClock( + BaseCovidWatchApplication.appContext) + + init { + kronosClock.syncInBackground() + } + + fun currentTimeMillis() : Long = kronosClock.getCurrentTimeMs() +} diff --git a/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt b/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt index 827ac487..6591ffeb 100644 --- a/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt +++ b/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt @@ -4,6 +4,7 @@ import androidx.room.Entity import androidx.room.PrimaryKey import androidx.room.TypeConverters import org.covidwatch.android.data.CovidExposureConfiguration +import org.covidwatch.android.data.NTPTime import org.covidwatch.android.data.converter.ExposureConfigurationConverter @Entity(tableName = "diagnosis_keys_token") @@ -11,6 +12,6 @@ import org.covidwatch.android.data.converter.ExposureConfigurationConverter data class DiagnosisKeysToken( @PrimaryKey val token: String, val exposureConfiguration: CovidExposureConfiguration, - val providedTime: Long = System.currentTimeMillis(), + val providedTime: Long = NTPTime.currentTimeMillis(), val potentialExposure: Boolean = false -) \ No newline at end of file +) From bcc9950133365a82d27174baf62cc570154e1d6d Mon Sep 17 00:00:00 2001 From: Spike Sprague Date: Tue, 1 Sep 2020 22:24:23 -0700 Subject: [PATCH 02/10] make app context injectable, make NTPTime use it make app context injectable, make NTPTime use it removed app context access hack from BaseCovidWatchApplicaiton --- .../android/BaseCovidWatchApplication.kt | 8 -------- .../org/covidwatch/android/data/NTPTime.kt | 20 +++++++++++-------- .../org/covidwatch/android/di/AppContext.kt | 8 ++++++++ .../org/covidwatch/android/di/AppModule.kt | 5 +++++ 4 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/org/covidwatch/android/di/AppContext.kt diff --git a/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt b/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt index 8ae53fdc..f7b01472 100644 --- a/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt +++ b/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt @@ -1,7 +1,6 @@ package org.covidwatch.android import android.app.Application -import android.content.Context import kotlinx.coroutines.GlobalScope import org.covidwatch.android.di.appModule import org.covidwatch.android.di.flavorSpecificModule @@ -37,12 +36,5 @@ open class BaseCovidWatchApplication : Application() { launchUseCase(removeOldExposuresUseCase) launchUseCase(updateRegionsUseCase) } - - appContext = applicationContext - } - - // convenience object to get the applicationContext from anywhere - companion object { - lateinit var appContext : Context } } diff --git a/app/src/main/java/org/covidwatch/android/data/NTPTime.kt b/app/src/main/java/org/covidwatch/android/data/NTPTime.kt index db154863..0567a051 100644 --- a/app/src/main/java/org/covidwatch/android/data/NTPTime.kt +++ b/app/src/main/java/org/covidwatch/android/data/NTPTime.kt @@ -2,15 +2,19 @@ package org.covidwatch.android.data import com.lyft.kronos.AndroidClockFactory import com.lyft.kronos.KronosClock -import org.covidwatch.android.BaseCovidWatchApplication +import org.covidwatch.android.di.AppContext +import org.koin.core.KoinComponent +import org.koin.core.inject -object NTPTime { - private val kronosClock : KronosClock = AndroidClockFactory.createKronosClock( - BaseCovidWatchApplication.appContext) +class NTPTime { + companion object : KoinComponent { + val context : AppContext by inject() + val kronosClock : KronosClock = AndroidClockFactory.createKronosClock(context) - init { - kronosClock.syncInBackground() - } + init { + kronosClock.syncInBackground() + } - fun currentTimeMillis() : Long = kronosClock.getCurrentTimeMs() + fun currentTimeMillis(): Long = kronosClock.getCurrentTimeMs() + } } diff --git a/app/src/main/java/org/covidwatch/android/di/AppContext.kt b/app/src/main/java/org/covidwatch/android/di/AppContext.kt new file mode 100644 index 00000000..c25fe545 --- /dev/null +++ b/app/src/main/java/org/covidwatch/android/di/AppContext.kt @@ -0,0 +1,8 @@ +package org.covidwatch.android.di + +import android.content.Context +import android.content.ContextWrapper + +// exists solely to make the Android applicationContext injectable +class AppContext(context : Context) : ContextWrapper(context) { +} \ No newline at end of file diff --git a/app/src/main/java/org/covidwatch/android/di/AppModule.kt b/app/src/main/java/org/covidwatch/android/di/AppModule.kt index 7ba96ab5..1938e217 100644 --- a/app/src/main/java/org/covidwatch/android/di/AppModule.kt +++ b/app/src/main/java/org/covidwatch/android/di/AppModule.kt @@ -1,6 +1,7 @@ package org.covidwatch.android.di import android.content.Context +import android.content.ContextWrapper import androidx.lifecycle.SavedStateHandle import androidx.room.Room import androidx.work.WorkManager @@ -331,6 +332,10 @@ val appModule = module { .build() } + single { + AppContext(androidContext()) + } + // Onboarding start viewModel { From c7f3c82c39cc35a37297ce09072c0d7cc99139f8 Mon Sep 17 00:00:00 2001 From: Spike Sprague Date: Wed, 2 Sep 2020 14:28:48 -0700 Subject: [PATCH 03/10] refactor so NtpTime is injectable, change VerifyPositiveDiagnosisFragment to use NtpTime for date pickers refactor so NtpTime is injectable, change VerifyPositiveDiagnosisFragment to use NtpTime for date pickers remove AppContext class properly start ntpTime in Application onCreate() --- .../android/BaseCovidWatchApplication.kt | 4 ++++ .../org/covidwatch/android/data/NTPTime.kt | 20 ---------------- .../org/covidwatch/android/data/NtpTime.kt | 23 +++++++++++++++++++ .../diagnosiskeystoken/DiagnosisKeysToken.kt | 3 +-- .../org/covidwatch/android/di/AppContext.kt | 8 ------- .../org/covidwatch/android/di/AppModule.kt | 8 +++---- .../VerifyPositiveDiagnosisFragment.kt | 8 ++++--- 7 files changed, 37 insertions(+), 37 deletions(-) delete mode 100644 app/src/main/java/org/covidwatch/android/data/NTPTime.kt create mode 100644 app/src/main/java/org/covidwatch/android/data/NtpTime.kt delete mode 100644 app/src/main/java/org/covidwatch/android/di/AppContext.kt diff --git a/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt b/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt index f7b01472..33a49aec 100644 --- a/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt +++ b/app/src/main/java/org/covidwatch/android/BaseCovidWatchApplication.kt @@ -2,6 +2,7 @@ package org.covidwatch.android import android.app.Application import kotlinx.coroutines.GlobalScope +import org.covidwatch.android.data.NtpTime import org.covidwatch.android.di.appModule import org.covidwatch.android.di.flavorSpecificModule import org.covidwatch.android.domain.ProvideDiagnosisKeysUseCase @@ -18,6 +19,7 @@ open class BaseCovidWatchApplication : Application() { private val updateRegionsUseCase: UpdateRegionsUseCase by inject() private val removeOldExposuresUseCase: RemoveOldDataUseCase by inject() private val provideDiagnosisKeysUseCase: ProvideDiagnosisKeysUseCase by inject() + private val ntpTime: NtpTime by inject() override fun onCreate() { super.onCreate() @@ -36,5 +38,7 @@ open class BaseCovidWatchApplication : Application() { launchUseCase(removeOldExposuresUseCase) launchUseCase(updateRegionsUseCase) } + + ntpTime.syncInBackground() } } diff --git a/app/src/main/java/org/covidwatch/android/data/NTPTime.kt b/app/src/main/java/org/covidwatch/android/data/NTPTime.kt deleted file mode 100644 index 0567a051..00000000 --- a/app/src/main/java/org/covidwatch/android/data/NTPTime.kt +++ /dev/null @@ -1,20 +0,0 @@ -package org.covidwatch.android.data - -import com.lyft.kronos.AndroidClockFactory -import com.lyft.kronos.KronosClock -import org.covidwatch.android.di.AppContext -import org.koin.core.KoinComponent -import org.koin.core.inject - -class NTPTime { - companion object : KoinComponent { - val context : AppContext by inject() - val kronosClock : KronosClock = AndroidClockFactory.createKronosClock(context) - - init { - kronosClock.syncInBackground() - } - - fun currentTimeMillis(): Long = kronosClock.getCurrentTimeMs() - } -} diff --git a/app/src/main/java/org/covidwatch/android/data/NtpTime.kt b/app/src/main/java/org/covidwatch/android/data/NtpTime.kt new file mode 100644 index 00000000..d1b2a155 --- /dev/null +++ b/app/src/main/java/org/covidwatch/android/data/NtpTime.kt @@ -0,0 +1,23 @@ +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 nowAsLocalDate() : LocalDate = + Instant.ofEpochMilli(currentTimeMillis()).atZone(ZoneId.systemDefault()).toLocalDate() + + fun nowAsLocalDateTime() : LocalDateTime = + LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()) +} diff --git a/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt b/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt index 6591ffeb..03a29899 100644 --- a/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt +++ b/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt @@ -4,7 +4,6 @@ import androidx.room.Entity import androidx.room.PrimaryKey import androidx.room.TypeConverters import org.covidwatch.android.data.CovidExposureConfiguration -import org.covidwatch.android.data.NTPTime import org.covidwatch.android.data.converter.ExposureConfigurationConverter @Entity(tableName = "diagnosis_keys_token") @@ -12,6 +11,6 @@ import org.covidwatch.android.data.converter.ExposureConfigurationConverter data class DiagnosisKeysToken( @PrimaryKey val token: String, val exposureConfiguration: CovidExposureConfiguration, - val providedTime: Long = NTPTime.currentTimeMillis(), + val providedTime: Long = System.currentTimeMillis(), val potentialExposure: Boolean = false ) diff --git a/app/src/main/java/org/covidwatch/android/di/AppContext.kt b/app/src/main/java/org/covidwatch/android/di/AppContext.kt deleted file mode 100644 index c25fe545..00000000 --- a/app/src/main/java/org/covidwatch/android/di/AppContext.kt +++ /dev/null @@ -1,8 +0,0 @@ -package org.covidwatch.android.di - -import android.content.Context -import android.content.ContextWrapper - -// exists solely to make the Android applicationContext injectable -class AppContext(context : Context) : ContextWrapper(context) { -} \ No newline at end of file diff --git a/app/src/main/java/org/covidwatch/android/di/AppModule.kt b/app/src/main/java/org/covidwatch/android/di/AppModule.kt index 1938e217..c40ea680 100644 --- a/app/src/main/java/org/covidwatch/android/di/AppModule.kt +++ b/app/src/main/java/org/covidwatch/android/di/AppModule.kt @@ -182,6 +182,10 @@ val appModule = module { ) } + single { + NtpTime(androidApplication()) + } + factory { ProvideDiagnosisKeysUseCase( workManager = get(), @@ -332,10 +336,6 @@ val appModule = module { .build() } - single { - AppContext(androidContext()) - } - // Onboarding start viewModel { diff --git a/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt b/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt index 038e05d3..ef750007 100644 --- a/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt +++ b/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt @@ -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.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() { override val viewModel: VerifyPositiveDiagnosisViewModel by stateViewModel() + private val ntpTime: NtpTime by inject() override fun bind( inflater: LayoutInflater, @@ -119,13 +121,13 @@ class VerifyPositiveDiagnosisFragment : val builder = MaterialDatePicker.Builder.datePicker() val constraints = CalendarConstraints.Builder() - val twoWeeksAgo = LocalDate.now().plusDays(-14).toInstant() + val twoWeeksAgo = ntpTime.nowAsLocalDate().plusDays(-14).toInstant() .toEpochMilli() // Day in the past or 14 days back val fromWhen = dayInPast ?: twoWeeksAgo - val untilNow = LocalDate.now().toInstant().toEpochMilli() + val untilNow = ntpTime.nowAsLocalDate().toInstant().toEpochMilli() constraints.setValidator(BaseDateValidator { it in fromWhen..untilNow }) From d04e54235d46133aa42a2c1d13115178e74c6615 Mon Sep 17 00:00:00 2001 From: Spike Sprague Date: Wed, 2 Sep 2020 14:31:56 -0700 Subject: [PATCH 04/10] remove unused function from NtpTime remove unused function from NtpTime --- app/src/main/java/org/covidwatch/android/data/NtpTime.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/src/main/java/org/covidwatch/android/data/NtpTime.kt b/app/src/main/java/org/covidwatch/android/data/NtpTime.kt index d1b2a155..e6af9385 100644 --- a/app/src/main/java/org/covidwatch/android/data/NtpTime.kt +++ b/app/src/main/java/org/covidwatch/android/data/NtpTime.kt @@ -5,7 +5,6 @@ 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) { @@ -17,7 +16,4 @@ class NtpTime (context : Context) { fun nowAsLocalDate() : LocalDate = Instant.ofEpochMilli(currentTimeMillis()).atZone(ZoneId.systemDefault()).toLocalDate() - - fun nowAsLocalDateTime() : LocalDateTime = - LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis()), ZoneId.systemDefault()) } From 385e199e2c6799d0e25f26727f85d81ef9ea564e Mon Sep 17 00:00:00 2001 From: Spike Sprague Date: Wed, 2 Sep 2020 14:43:35 -0700 Subject: [PATCH 05/10] remove unused import remove unused import --- app/src/main/java/org/covidwatch/android/di/AppModule.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/org/covidwatch/android/di/AppModule.kt b/app/src/main/java/org/covidwatch/android/di/AppModule.kt index c40ea680..09b8f9ea 100644 --- a/app/src/main/java/org/covidwatch/android/di/AppModule.kt +++ b/app/src/main/java/org/covidwatch/android/di/AppModule.kt @@ -1,7 +1,6 @@ package org.covidwatch.android.di import android.content.Context -import android.content.ContextWrapper import androidx.lifecycle.SavedStateHandle import androidx.room.Room import androidx.work.WorkManager From fdff16166bbb4ec2ec93a8ae2dbc04594822e6c7 Mon Sep 17 00:00:00 2001 From: "Richard \"spike\" Sprague" Date: Wed, 2 Sep 2020 14:48:43 -0700 Subject: [PATCH 06/10] remove minor change to DiagnosisKeysToken.kt to remove from this commit removed trailing newline (the only change to this file) to remove from this commit From 2f31082957ebd165cc1550f50b21c5c681fc8780 Mon Sep 17 00:00:00 2001 From: Spike Sprague Date: Thu, 3 Sep 2020 11:58:41 -0700 Subject: [PATCH 07/10] remove DiagnonsisKeysToken.kt from commit remove DiagnonsisKeysToken.kt from commit --- .../android/data/diagnosiskeystoken/DiagnosisKeysToken.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt b/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt index 03a29899..827ac487 100644 --- a/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt +++ b/app/src/main/java/org/covidwatch/android/data/diagnosiskeystoken/DiagnosisKeysToken.kt @@ -13,4 +13,4 @@ data class DiagnosisKeysToken( val exposureConfiguration: CovidExposureConfiguration, val providedTime: Long = System.currentTimeMillis(), val potentialExposure: Boolean = false -) +) \ No newline at end of file From 1a90ab6cc4b731681b623d0888b87bdbf635aa05 Mon Sep 17 00:00:00 2001 From: Spike Sprague Date: Fri, 4 Sep 2020 12:48:02 -0700 Subject: [PATCH 08/10] removed "LocalTime" from NtpTime, add plus/minus days extenstion to Instant removed "LocalTime" from NtpTime add plus/minus days extenstion to Instant --- app/build.gradle | 8 ++++---- app/src/main/java/org/covidwatch/android/data/NtpTime.kt | 5 +++-- .../java/org/covidwatch/android/extension/Instant.kt | 9 +++++++++ .../ui/reporting/VerifyPositiveDiagnosisFragment.kt | 7 +++---- 4 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/org/covidwatch/android/extension/Instant.kt diff --git a/app/build.gradle b/app/build.gradle index 65c337d4..fc5f5346 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -250,6 +250,10 @@ dependencies { implementation "com.github.lisawray.groupie:groupie:$groupie_version" implementation "com.github.lisawray.groupie:groupie-viewbinding:$groupie_version" + // Kronos ntp clock + def kronos_latest_version = "0.0.1-alpha09" + implementation "com.lyft.kronos:kronos-android:$kronos_latest_version" + //// TEST RELATED DEPENDENCIES testImplementation "org.jetbrains.kotlin:kotlin-test-junit5:$kotlin_version" @@ -288,10 +292,6 @@ dependencies { testImplementation "io.mockk:mockk:1.10.0" // Koin testImplementation "org.koin:koin-test:$koin_version" - - // Kronos ntp clock - def kronos_latest_version = "0.0.1-alpha09" - implementation "com.lyft.kronos:kronos-android:$kronos_latest_version" } apply plugin: 'com.google.gms.google-services' diff --git a/app/src/main/java/org/covidwatch/android/data/NtpTime.kt b/app/src/main/java/org/covidwatch/android/data/NtpTime.kt index e6af9385..a412709d 100644 --- a/app/src/main/java/org/covidwatch/android/data/NtpTime.kt +++ b/app/src/main/java/org/covidwatch/android/data/NtpTime.kt @@ -5,6 +5,7 @@ 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) { @@ -14,6 +15,6 @@ class NtpTime (context : Context) { fun syncInBackground() = kronosClock.syncInBackground() - fun nowAsLocalDate() : LocalDate = - Instant.ofEpochMilli(currentTimeMillis()).atZone(ZoneId.systemDefault()).toLocalDate() + fun nowAsInstant() : Instant = + Instant.ofEpochMilli(currentTimeMillis()) } diff --git a/app/src/main/java/org/covidwatch/android/extension/Instant.kt b/app/src/main/java/org/covidwatch/android/extension/Instant.kt new file mode 100644 index 00000000..8aeaeeb0 --- /dev/null +++ b/app/src/main/java/org/covidwatch/android/extension/Instant.kt @@ -0,0 +1,9 @@ +package org.covidwatch.android.extension + +import java.time.Instant + +// 1d * (24h/d) * (60m/h) * (60s/m) +fun Instant.plusDays(daysToAdd : Long) : Instant = + plusSeconds(daysToAdd * 24 * 60 * 60) + +fun Instant.minusDays(daysToSubtract : Long) : Instant = plusDays(-daysToSubtract) \ No newline at end of file diff --git a/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt b/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt index ef750007..300dd303 100644 --- a/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt +++ b/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt @@ -16,10 +16,10 @@ 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 @@ -121,13 +121,12 @@ class VerifyPositiveDiagnosisFragment : val builder = MaterialDatePicker.Builder.datePicker() val constraints = CalendarConstraints.Builder() - val twoWeeksAgo = ntpTime.nowAsLocalDate().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 = ntpTime.nowAsLocalDate().toInstant().toEpochMilli() + val untilNow = ntpTime.nowAsInstant().toEpochMilli() constraints.setValidator(BaseDateValidator { it in fromWhen..untilNow }) From f2f9b99b1314213a668b6a7a07f04a2748fe2a19 Mon Sep 17 00:00:00 2001 From: Spike Sprague Date: Fri, 4 Sep 2020 13:12:35 -0700 Subject: [PATCH 09/10] force date picker to open on today's month force date picker to open on today's month --- .../android/ui/reporting/VerifyPositiveDiagnosisFragment.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt b/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt index 300dd303..51c2b9c4 100644 --- a/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt +++ b/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt @@ -128,7 +128,9 @@ class VerifyPositiveDiagnosisFragment : val untilNow = ntpTime.nowAsInstant().toEpochMilli() - constraints.setValidator(BaseDateValidator { it in fromWhen..untilNow }) + constraints + .setValidator(BaseDateValidator { it in fromWhen..untilNow }) + .setOpenAt(untilNow) val datePicker = builder .setSelection(selection) From e626be86df7118240375be8b873e347e4f27143d Mon Sep 17 00:00:00 2001 From: rasprague Date: Mon, 7 Sep 2020 15:34:39 -0700 Subject: [PATCH 10/10] minor changes / cleanup minor changes / cleanup --- app/build.gradle | 2 +- app/src/main/java/org/covidwatch/android/data/NtpTime.kt | 5 ++--- .../main/java/org/covidwatch/android/extension/Instant.kt | 7 +++---- .../ui/reporting/VerifyPositiveDiagnosisFragment.kt | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index fc5f5346..237ebe43 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -294,4 +294,4 @@ dependencies { testImplementation "org.koin:koin-test:$koin_version" } -apply plugin: 'com.google.gms.google-services' +apply plugin: 'com.google.gms.google-services' \ No newline at end of file diff --git a/app/src/main/java/org/covidwatch/android/data/NtpTime.kt b/app/src/main/java/org/covidwatch/android/data/NtpTime.kt index a412709d..e05c72e9 100644 --- a/app/src/main/java/org/covidwatch/android/data/NtpTime.kt +++ b/app/src/main/java/org/covidwatch/android/data/NtpTime.kt @@ -11,10 +11,9 @@ import java.time.ZoneId class NtpTime (context : Context) { private val kronosClock : KronosClock = AndroidClockFactory.createKronosClock(context) - private fun currentTimeMillis() : Long = kronosClock.getCurrentTimeMs() + fun currentTimeMillis() : Long = kronosClock.getCurrentTimeMs() fun syncInBackground() = kronosClock.syncInBackground() - fun nowAsInstant() : Instant = - Instant.ofEpochMilli(currentTimeMillis()) + fun nowAsInstant() : Instant = Instant.ofEpochMilli(currentTimeMillis()) } diff --git a/app/src/main/java/org/covidwatch/android/extension/Instant.kt b/app/src/main/java/org/covidwatch/android/extension/Instant.kt index 8aeaeeb0..e152a1d2 100644 --- a/app/src/main/java/org/covidwatch/android/extension/Instant.kt +++ b/app/src/main/java/org/covidwatch/android/extension/Instant.kt @@ -1,9 +1,8 @@ package org.covidwatch.android.extension import java.time.Instant +import java.time.temporal.ChronoUnit -// 1d * (24h/d) * (60m/h) * (60s/m) -fun Instant.plusDays(daysToAdd : Long) : Instant = - plusSeconds(daysToAdd * 24 * 60 * 60) +fun Instant.plusDays(daysToAdd : Long) : Instant = plus(daysToAdd, ChronoUnit.DAYS) -fun Instant.minusDays(daysToSubtract : Long) : Instant = plusDays(-daysToSubtract) \ No newline at end of file +fun Instant.minusDays(daysToSubtract : Long) : Instant = minus(daysToSubtract, ChronoUnit.DAYS) diff --git a/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt b/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt index 51c2b9c4..279d2658 100644 --- a/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt +++ b/app/src/main/java/org/covidwatch/android/ui/reporting/VerifyPositiveDiagnosisFragment.kt @@ -126,7 +126,7 @@ class VerifyPositiveDiagnosisFragment : // Day in the past or 14 days back val fromWhen = dayInPast ?: twoWeeksAgo - val untilNow = ntpTime.nowAsInstant().toEpochMilli() + val untilNow = ntpTime.currentTimeMillis() constraints .setValidator(BaseDateValidator { it in fromWhen..untilNow })