Skip to content
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

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -290,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'
Copy link
Member

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -36,5 +38,7 @@ open class BaseCovidWatchApplication : Application() {
launchUseCase(removeOldExposuresUseCase)
launchUseCase(updateRegionsUseCase)
}

ntpTime.syncInBackground()
}
}
20 changes: 20 additions & 0 deletions app/src/main/java/org/covidwatch/android/data/NtpTime.kt
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())
}
4 changes: 4 additions & 0 deletions app/src/main/java/org/covidwatch/android/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ val appModule = module {
)
}

single {
NtpTime(androidApplication())
}

factory {
ProvideDiagnosisKeysUseCase(
workManager = get(),
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/org/covidwatch/android/extension/Instant.kt
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)
Copy link
Member

Choose a reason for hiding this comment

The 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?
Like this:

minus(15, ChronoUnit.DAYS)
add(Duration.ofDays(15))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The 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 NtpTime.currentTimeMillis public and use it directly here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍


val datePicker = builder
.setSelection(selection)
Expand Down