-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vaios Tsitsonis
committed
Jun 1, 2023
1 parent
2cb236b
commit 728a245
Showing
6 changed files
with
176 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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,44 @@ | ||
plugins { | ||
id("com.android.library") | ||
kotlin("android") | ||
} | ||
|
||
android { | ||
namespace = "com.quadible.feather" | ||
compileSdk = 33 | ||
|
||
defaultConfig { | ||
minSdk = 24 | ||
targetSdk = 33 | ||
|
||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
isMinifyEnabled = false | ||
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") | ||
} | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
|
||
buildFeatures { | ||
compose = true | ||
} | ||
composeOptions { | ||
kotlinCompilerExtensionVersion = "1.3.2" | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(platform("androidx.compose:compose-bom:2022.10.00")) | ||
implementation("androidx.compose.foundation:foundation") | ||
} |
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle.kts. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
63 changes: 63 additions & 0 deletions
63
feather/src/main/java/com/quadible/feather/FloatingDraggableItem.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,63 @@ | ||
package com.quadible.feather | ||
|
||
import androidx.compose.foundation.gestures.detectDragGestures | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.BoxScope | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.offset | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.input.pointer.pointerInput | ||
import androidx.compose.ui.layout.onGloballyPositioned | ||
import androidx.compose.ui.unit.IntOffset | ||
import kotlin.math.roundToInt | ||
|
||
@Composable | ||
fun FloatingDraggableItem( | ||
initialOffset: (FloatingDraggableItemState) -> IntOffset, | ||
content: @Composable BoxScope.(FloatingDraggableItemState) -> Unit, | ||
) { | ||
val state = remember { mutableStateOf(FloatingDraggableItemState()) } | ||
|
||
Box( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.onGloballyPositioned { state.updateContainerSize(size = it.size) }, | ||
) { | ||
Box( | ||
modifier = Modifier | ||
.offset { state.value.offset } | ||
.onGloballyPositioned { | ||
state.updateContentSizeSize( | ||
size = it.size, | ||
initialOffset = initialOffset, | ||
) | ||
} | ||
.pointerInput(Unit) { | ||
detectDragGestures { _, dragAmount -> | ||
val calculatedX = state.value.offset.x + dragAmount.x.roundToInt() | ||
val calculatedY = state.value.offset.y + dragAmount.y.roundToInt() | ||
|
||
val offset = IntOffset( | ||
calculatedX.coerceIn(0, state.value.dragAreaSize.width), | ||
calculatedY.coerceIn(0, state.value.dragAreaSize.height), | ||
) | ||
state.updateOffset(offset = offset) | ||
} | ||
}, | ||
) { | ||
content(state.value) | ||
} | ||
|
||
DisposableEffect(true) { | ||
// Reset position on hide | ||
onDispose { | ||
val offset = initialOffset(state.value) | ||
state.updateOffset(offset = offset) | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
feather/src/main/java/com/quadible/feather/FloatingDraggableItemState.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,45 @@ | ||
package com.quadible.feather | ||
|
||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.ui.unit.IntOffset | ||
import androidx.compose.ui.unit.IntSize | ||
|
||
data class FloatingDraggableItemState( | ||
val contentSize: IntSize = IntSize(width = 0, height = 0), | ||
val containerSize: IntSize = IntSize(width = 0, height = 0), | ||
val offset: IntOffset = IntOffset(x = 0, y = 0), | ||
) { | ||
|
||
val dragAreaSize: IntSize | ||
get() = IntSize( | ||
width = containerSize.width - contentSize.width, | ||
height = containerSize.height - contentSize.height, | ||
) | ||
} | ||
|
||
internal fun MutableState<FloatingDraggableItemState>.updateContainerSize(size: IntSize) { | ||
value = value.copy(containerSize = size) | ||
} | ||
|
||
internal fun MutableState<FloatingDraggableItemState>.updateContentSizeSize( | ||
size: IntSize, | ||
initialOffset: (FloatingDraggableItemState) -> IntOffset, | ||
) { | ||
val wasNotRenderedBefore = size.isNotEmpty() && value.contentSize.isEmpty() | ||
val offset = if (wasNotRenderedBefore) { | ||
val stateWithUpdatedSize = value.copy(contentSize = size) | ||
initialOffset(stateWithUpdatedSize) | ||
} else { | ||
value.offset | ||
} | ||
|
||
value = value.copy(contentSize = size, offset = offset) | ||
} | ||
|
||
private fun IntSize.isEmpty(): Boolean = this == IntSize(width = 0, height = 0) | ||
|
||
private fun IntSize.isNotEmpty(): Boolean = !isEmpty() | ||
|
||
internal fun MutableState<FloatingDraggableItemState>.updateOffset(offset: IntOffset) { | ||
value = value.copy(offset = offset) | ||
} |
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