/AndroidStopwatch

Simple realisation of StopWatch

Primary LanguageKotlinMIT LicenseMIT

AndroidStopwatch

Simple realisation of Stopwatch.

demo

Install

add jitpack.io repository to the top build.gradle or settings.gradle

repositories {
    // ...
    maven { url 'https://jitpack.io' }
}

add the dependency to your module build.gradle

dependencies {
    //...
    implementation('com.github.fmaxx:AndroidStopwatch:0.0.2')
}

Using

Common case

// initialization
val stopwatch = Stopwatch.Builder(scope = YOUR_SCOPE).build()

// collect data with Flow
YOUR_SCOPE.launch {
    stopwatch.collect { elapsedMilliseconds ->
        println("elapsed: $elapsedMilliseconds")
    }
}

stopwatch.start()

Activity case

 override fun onCreate(savedInstanceState: Bundle?) {
    val stopwatch = Stopwatch.Builder(scope = lifecycleScope).build()
    lifecycleScope.launch {
        repeatOnLifecycle(Lifecycle.State.STARTED) {
            stopwatch.collect { elapsedMilliseconds ->
                println("elapsed: $elapsedMilliseconds")
            }
        }
    }
    stopwatch.start()
}

Settings

Stopwatch.Builder has additional methods setTickDelay(milliseconds: Long) - milliseconds between each time tick (should be more than zero) setStartMilliseconds(milliseconds: Long) - milliseconds started at (should be more than zero)

val stopwatch = Stopwatch.Builder(scope = YOUR_SCOPE)
    .setTickDelay(20)
    .setStartMilliseconds(10_000)
    .build()