Skip to content

Commit

Permalink
Add feather module
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaios Tsitsonis committed Jun 1, 2023
1 parent 2cb236b commit 728a245
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 1 deletion.
1 change: 1 addition & 0 deletions feather/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
44 changes: 44 additions & 0 deletions feather/build.gradle.kts
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")
}
21 changes: 21 additions & 0 deletions feather/proguard-rules.pro
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
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)
}
}
}
}
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)
}
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ dependencyResolutionManagement {
}
}
rootProject.name = "Feather"
include(":app")
include(":app")
include(":feather")

0 comments on commit 728a245

Please sign in to comment.