Skip to content

Commit

Permalink
feat: New sample app (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamglin0 authored Dec 13, 2024
1 parent 4fab980 commit e61a00c
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 204 deletions.
3 changes: 0 additions & 3 deletions .fleet/settings.json

This file was deleted.

2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ kotlin = "2.1.0"
binaryCompatibilityValidator = "0.16.3"
#compose
composeMultiplatform = "1.7.1"
colorpicker="1.1.2"
#other
dokka = "1.9.20"
mavenPublish = "0.30.0"
Expand All @@ -22,6 +23,7 @@ mavenPublish = "0.30.0"
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidxAppCompat" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidxActivity" }
androidx-annotation = { module = "androidx.annotation:annotation", version.ref = "androidxAnnotation" }
colorPicker = { module = "com.github.skydoves:colorpicker-compose", version.ref = "colorpicker" }
[plugins]
#android
android-applcation = { id = "com.android.application", version.ref = "androidGradlePlugin" }
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion sample/compose-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ kotlin {
implementation(compose.ui)
implementation(compose.uiUtil)
implementation(compose.material3)
implementation(compose.components.resources)
implementation(compose.components.uiToolingPreview)
implementation(project(":compose-shadow"))
implementation(libs.colorPicker)
}

desktopMain.dependencies {
Expand Down
40 changes: 20 additions & 20 deletions sample/compose-app/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name="com.adamglin.composeshadow.app.SampleApplication"
android:allowBackup="true"
android:enableOnBackInvokedCallback="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:usesCleartextTraffic="true"
tools:targetApi="31">
android:name="com.admaglin.composeshadow.app.SampleApplication"
android:allowBackup="true"
android:enableOnBackInvokedCallback="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".app.MainActivity"
android:exported="true"
android:label="@string/title_activity_main"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
android:name="com.admaglin.composeshadow.app.MainActivity"
android:exported="true"
android:label="@string/title_activity_main"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Expand Down
150 changes: 150 additions & 0 deletions sample/compose-app/src/commonMain/kotlin/OffsetIndicator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import androidx.compose.animation.core.*
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.draggable2D
import androidx.compose.foundation.gestures.rememberDraggable2DState
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.PointerIcon
import androidx.compose.ui.input.pointer.pointerHoverIcon
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch


@Composable
fun rememberOffsetIndicatorState(
onValueChange: (DpOffset) -> Unit,
): OffsetIndicatorState {
val onValueChangeState = rememberUpdatedState(onValueChange)
return remember {
OffsetIndicatorState(
onValueChange = { onValueChangeState.value.invoke(it) }
)
}
}

class OffsetIndicatorState(
val onValueChange: (DpOffset) -> Unit,
) {
private val animatable = Animatable(DpOffset.Zero, DpOffset.VectorConverter)
val targetValue get() = animatable.targetValue


suspend fun snapTo(offset: DpOffset) {
animatable.snapTo(offset)
onValueChange.invoke(offset)
}

suspend fun animateTo(offset: DpOffset) {
onValueChange.invoke(offset)
animatable.animateTo(offset, spring(stiffness = Spring.StiffnessHigh))
}
}

private val DpOffset.VectorConverter: TwoWayConverter<Offset, AnimationVector2D>
get() = TwoWayConverter(
convertToVector = { AnimationVector2D(it.x, it.y) },
convertFromVector = { Offset(it.v1, it.v2) }
)


@OptIn(ExperimentalFoundationApi::class)
@Composable
fun OffsetIndicator(
state: OffsetIndicatorState,
modifier: Modifier = Modifier
) {
val density = LocalDensity.current
val coroutineScope = rememberCoroutineScope()
Box(modifier) {
BoxWithConstraints(
modifier = Modifier.fillMaxSize()
.border(1.dp, color = OFFSET_INDICATOR_LINE_COLOR)
.drawBehind {
drawLine(
OFFSET_INDICATOR_LINE_COLOR,
strokeWidth = .5f,
start = size.TopCenterOffset,
end = size.BottomCenterOffset
)
drawLine(
OFFSET_INDICATOR_LINE_COLOR,
strokeWidth = .5f,
start = size.CenterLeftOffset,
end = size.CenterRightOffset,
)
}
) {
val boxWidth = maxWidth.value.dp

val realOffset =
DpOffset(
state.targetValue.x + boxWidth / 2 - MOVABLE_POINTER_SIZE / 2,
state.targetValue.y + boxWidth / 2 - MOVABLE_POINTER_SIZE / 2
)
val draggable2DState = rememberDraggable2DState {
with(density) {
DpOffset(
(state.targetValue.x + it.x.toDp()).coerceIn(-boxWidth / 2, boxWidth / 2),
(state.targetValue.y + it.y.toDp()).coerceIn(-boxWidth / 2, boxWidth / 2),
).let {
println("${state.targetValue}result $it")
coroutineScope.launch { state.snapTo(it) }
}
}
}
Box(
modifier = Modifier
.offset(realOffset.x, realOffset.y)
.size(MOVABLE_POINTER_SIZE)
.clip(CircleShape)
.pointerHoverIcon(PointerIcon.Hand)
.background(Color.Blue)
.draggable2D(draggable2DState)
)
}
}
}

private val OFFSET_INDICATOR_LINE_COLOR = Color.LightGray
private val MOVABLE_POINTER_SIZE = 10.dp

private val Size.TopLeftOffset: Offset
get() = Offset.Zero

private val Size.TopCenterOffset: Offset
get() = Offset(width / 2, 0f)

private val Size.TopRightOffset: Offset
get() = Offset(width, 0f)

private val Size.CenterLeftOffset: Offset
get() = Offset(0f, height / 2)

private val Size.CenterOffset: Offset
get() = Offset(width / 2, height / 2)

private val Size.CenterRightOffset: Offset
get() = Offset(width, height / 2)

private val Size.BottomLeftOffset: Offset
get() = Offset(0f, height)

private val Size.BottomCenterOffset: Offset
get() = Offset(width / 2, height)

private val Size.BottomRightOffset: Offset
get() = Offset(width, height)
Loading

0 comments on commit e61a00c

Please sign in to comment.