-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'android-handler-funcs'
- Loading branch information
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
plugins { | ||
id("android-library-base") | ||
id("android-library-unit-test") | ||
id("android-library-robolectric-test") | ||
id("android-library-release") | ||
} | ||
|
||
dependencies { | ||
implementation(libs.compose.ui) | ||
} | ||
|
||
android { | ||
composeOptions { | ||
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get() | ||
} | ||
|
||
buildFeatures { | ||
compose = true | ||
} | ||
} | ||
|
42 changes: 42 additions & 0 deletions
42
dachlatten-compose/src/main/kotlin/de/sipgate/dachlatten/compose/AndroidHandlerFunc.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package de.sipgate.dachlatten.compose | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.platform.LocalContext | ||
|
||
// This provides a parameterless handlerFunc that can be | ||
// passed around and invoked from anywhere. It will keep a | ||
// reference to the Context given during its creation. | ||
// Warning: | ||
// Depending on the situation this can easily leak the Context! | ||
|
||
typealias AndroidClickHandler = context (ContextProvider) () -> Unit | ||
|
||
@Composable | ||
inline fun withContext(crossinline target: AndroidClickHandler): ClickHandler { | ||
return LocalContext.current.withContext(target) | ||
} | ||
|
||
@Composable | ||
inline fun <reified T, R> withContext( | ||
crossinline target: context (ContextProvider) | ||
(T) -> R, | ||
): (T) -> R = LocalContext.current.withContext(target) | ||
|
||
inline fun <reified T, R> Context.withContext( | ||
crossinline target: context (ContextProvider) | ||
(T) -> R, | ||
): (T) -> R = { param -> | ||
with(ContextProviderImpl(this)) { | ||
target.invoke(this, param) | ||
} | ||
} | ||
|
||
inline fun <R> Context.withContext( | ||
crossinline target: context (ContextProvider) | ||
() -> R, | ||
): () -> R = { | ||
with(ContextProviderImpl(this)) { | ||
target(this) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
dachlatten-compose/src/test/kotlin/AndroidHandlerFuncTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package de.sipgate.dachlatten.compose | ||
|
||
import org.junit.Test | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.RuntimeEnvironment | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class AndroidHandlerFuncTest { | ||
|
||
@Test | ||
fun testAndroidHandlerFuncWillReceiveAContext() { | ||
val context = RuntimeEnvironment.getApplication().applicationContext | ||
val handlerFunc = context.withContext(::someFunctionThatAccessesTheAndroidContext) | ||
|
||
handlerFunc.invoke() | ||
} | ||
|
||
@Test | ||
fun testAndroidHandlerFuncWillReceiveAContextAndReturnValueIsPassedBack() { | ||
val context = RuntimeEnvironment.getApplication().applicationContext | ||
val handlerFunc = context.withContext(::someFunctionThatAccessesTheAndroidContextAndReturnsSomething) | ||
|
||
val result = handlerFunc.invoke() | ||
Assertions.assertTrue(result.isNotEmpty()) | ||
} | ||
|
||
context (ContextProvider) | ||
private fun someFunctionThatAccessesTheAndroidContext() { | ||
context.packageName | ||
} | ||
|
||
context (ContextProvider) | ||
private fun someFunctionThatAccessesTheAndroidContextAndReturnsSomething(): String { | ||
return context.packageName | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters