Skip to content

Commit

Permalink
Add new module that contains convenience Kotlin Time accessors to And…
Browse files Browse the repository at this point in the history
…roid SDK APIs
  • Loading branch information
janseeger committed Dec 16, 2024
1 parent 09fc3cc commit a2e3f58
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 3 deletions.
16 changes: 16 additions & 0 deletions dachlatten-datetime-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
id("android-library-base")
id("kotlin-library-unit-test")
id("android-library-release")
}

dependencies {
api(project(":dachlatten-datetime"))

compileOnly(libs.kotlinx.datetime)
compileOnly(libs.androidx.core)
compileOnly(libs.androidx.compose.animation)
compileOnly(libs.android.support.annotations)

testImplementation(libs.kotlinx.datetime)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.sipgate.dachlatten.datetime

import androidx.compose.animation.core.StartOffsetType
import kotlin.time.Duration

@Suppress("unused")
fun StartOffset(offset: Duration, offsetType: StartOffsetType = StartOffsetType.Delay) =
androidx.compose.animation.core.StartOffset(
offsetMillis = offset.inWholeMilliseconds.toInt(),
offsetType = offsetType
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.sipgate.dachlatten.datetime

import android.os.Handler
import kotlin.time.Duration
import androidx.core.os.postDelayed

/**
* Version of [Handler.postDelayed] which re-orders the parameters, allowing the action to be placed
* outside of parentheses and takes {Duration}s.
*
* ```
* handler.postDelayed(200.milliseconds) {
* doSomething()
* }
* ```
*
* @return the created Runnable
*/
@Suppress("unused")
inline fun Handler.postDelayed(
delay: Duration,
token: Any? = null,
crossinline action: () -> Unit
) = postDelayed(
delayInMillis = delay.inWholeMilliseconds,
token = token,
action = action
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.sipgate.dachlatten.datetime

import androidx.core.app.NotificationCompat.Builder
import kotlin.time.Duration

@Suppress("unused")
fun Builder.setTimeoutAfter(duration: Duration): Builder =
setTimeoutAfter(duration.inWholeMilliseconds)

@Suppress("unused")
fun Builder.setVibrate(pattern: Array<Duration>?): Builder =
setVibrate(pattern?.map { it.inWholeMilliseconds }?.toLongArray())
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.sipgate.dachlatten.datetime

import android.media.ToneGenerator
import kotlin.time.Duration

@Suppress("unused")
fun ToneGenerator.startTone(toneType: Int, duration: Duration) = startTone(
toneType, duration.inWholeMilliseconds.toInt()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.sipgate.dachlatten.datetime

import android.animation.ValueAnimator
import kotlin.time.Duration

@Suppress("unused")
fun ValueAnimator.setDuration(duration: Duration): ValueAnimator =
setDuration(duration.inWholeMilliseconds)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.sipgate.dachlatten.datetime

import android.os.Build
import android.os.VibrationEffect.Composition
import androidx.annotation.FloatRange
import androidx.annotation.RequiresApi
import kotlin.time.Duration

@Suppress("unused")
@RequiresApi(Build.VERSION_CODES.R)
fun Composition.addPrimitive(
primitiveId: Int,
@FloatRange(from = 0.0, to = 1.0) scale: Float,
delay: Duration
) {
addPrimitive(
/* primitiveId = */ primitiveId,
/* scale = */ scale,
/* delay = */ delay.inWholeMilliseconds.toInt()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.sipgate.dachlatten.datetime

import android.os.Build
import android.os.VibrationEffect
import androidx.annotation.RequiresApi
import kotlin.time.Duration

@Suppress("unused")
@RequiresApi(Build.VERSION_CODES.O)
fun createWaveform(timings: Array<Duration>, amplitudes: IntArray, repeat: Int): VibrationEffect =
VibrationEffect.createWaveform(
/* timings = */ timings.map { it.inWholeMilliseconds }.toLongArray(),
/* amplitudes = */ amplitudes,
/* repeat = */ repeat
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.sipgate.dachlatten.datetime

import android.os.PowerManager.WakeLock
import kotlin.time.Duration

@Suppress("unused")
fun WakeLock.acquire(duration: Duration) = acquire(duration.inWholeMilliseconds)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package de.sipgate.dachlatten.datetime

import org.junit.jupiter.api.Test
import java.util.Date
import java.util.TimeZone
import kotlin.test.Test
import kotlin.test.assertEquals

class DateExtTest {
Expand Down
1 change: 0 additions & 1 deletion dachlatten-datetime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ plugins {

dependencies {
compileOnly(libs.kotlinx.datetime)
compileOnly(libs.android.support.annotations)

testImplementation(libs.kotlinx.datetime)
}
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ kotlin-gradle-plugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-p
android-support-annotations = { module = "com.android.support:support-annotations", version.ref = "android-support-annotations" }
androidx-activity = { group = "androidx.activity", name = "activity-compose", version.ref = "androidx-activity" }
androidx-annotation = { group = "androidx.annotation", name = "annotation-jvm", version.ref = "androidx-annotation" }
androidx-compose-animation = { group = "androidx.compose.animation", name = "animation", version.ref = "androidx-compose" }
androidx-compose-foundation = { group = "androidx.compose.foundation", name = "foundation", version.ref = "androidx-compose" }
androidx-compose-ui = { group = "androidx.compose.ui", name = "ui", version.ref = "androidx-compose" }
androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4", version.ref = "androidx-compose" }
androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest", version.ref = "androidx-compose" }
androidx-core = { module = "androidx.core:core", version.ref="androidx-core" }
androidx-core = { module = "androidx.core:core-ktx", version.ref="androidx-core" }
androidx-lifecycle-process = { group = "androidx.lifecycle", name = "lifecycle-process", version.ref = "androidx-lifecycle" }

jetbrains-markdown = { module = "org.jetbrains:markdown", version.ref = "jetbrains-markdown" }
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ rootProject.name = "Dachlatten"
include(":dachlatten-android")
include(":dachlatten-compose")
include(":dachlatten-datetime")
include(":dachlatten-datetime-android")
include(":dachlatten-debug")
include(":dachlatten-flow")
include(":dachlatten-google")
Expand Down

0 comments on commit a2e3f58

Please sign in to comment.