Skip to content

Commit

Permalink
Support multiplatform
Browse files Browse the repository at this point in the history
Signed-off-by: Martmists <[email protected]>
  • Loading branch information
Martmists-GH committed Nov 7, 2023
1 parent a5c124b commit b69227e
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 85 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ buildscript {
classpath Dependencies.dokka
classpath Dependencies.kotlinBinaryValidator
classpath Dependencies.hiltPlugin
classpath Dependencies.composePlugin
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.skydoves.orbital

object Configuration {
const val compileSdk = 33
const val compileSdk = 34
const val targetSdk = 33
const val minSdk = 21
const val majorVersion = 0
Expand Down
10 changes: 6 additions & 4 deletions buildSrc/src/main/kotlin/com/skydoves/orbital/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ object Versions {
internal const val ANDROID_GRADLE_PLUGIN = "7.4.1"
internal const val ANDROID_GRADLE_SPOTLESS = "6.7.0"
internal const val GRADLE_NEXUS_PUBLISH_PLUGIN = "1.3.0"
internal const val KOTLIN = "1.8.10"
internal const val KOTLIN_GRADLE_DOKKA = "1.8.10"
internal const val KOTLIN = "1.9.10"
internal const val KOTLIN_GRADLE_DOKKA = "1.9.10"
internal const val KOTLIN_BINARY_VALIDATOR = "0.13.0"

internal const val COMPOSE = "1.3.1"
internal const val COMPOSE_COMPILER = "1.4.4"
internal const val COMPOSE = "1.5.3"
const val COMPOSE_COMPILER = "1.5.3"
internal const val COROUTINES = "1.6.4"

internal const val RULES = "1.4.0"
Expand Down Expand Up @@ -39,6 +39,8 @@ object Dependencies {
"com.diffplug.spotless:spotless-plugin-gradle:${Versions.ANDROID_GRADLE_SPOTLESS}"
const val dokka = "org.jetbrains.dokka:dokka-gradle-plugin:${Versions.KOTLIN_GRADLE_DOKKA}"
const val hiltPlugin = "com.google.dagger:hilt-android-gradle-plugin:${Versions.HILT}"
const val composePlugin =
"org.jetbrains.compose:compose-gradle-plugin:${Versions.COMPOSE_COMPILER}"
const val kotlinBinaryValidator =
"org.jetbrains.kotlinx:binary-compatibility-validator:${Versions.KOTLIN_BINARY_VALIDATOR}"

Expand Down
61 changes: 0 additions & 61 deletions orbital/build.gradle

This file was deleted.

62 changes: 62 additions & 0 deletions orbital/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import com.skydoves.orbital.Configuration
import com.skydoves.orbital.Dependencies
import com.skydoves.orbital.Versions

plugins {
id("com.android.library")
kotlin("multiplatform")
id("org.jetbrains.compose")
id("org.jetbrains.dokka")
id("binary-compatibility-validator")
}

kotlin {
androidTarget()
jvm()

sourceSets {
val commonMain by getting {
dependencies {
implementation(compose.ui)
implementation(compose.animation)
}
}
}
}


android {
compileSdk = Configuration.compileSdk
defaultConfig {
minSdk = Configuration.minSdk
targetSdk = Configuration.targetSdk
}

buildFeatures {
compose = true
buildConfig = false
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

composeOptions {
kotlinCompilerExtensionVersion = Versions.COMPOSE_COMPILER
}

lint {
abortOnError = false
}
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += listOf(
"-Xexplicit-api=strict",
"-Xopt-in=androidx.compose.ui.ExperimentalComposeUiApi"
)
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.layout.intermediateLayout
import androidx.compose.ui.layout.onPlaced
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.round

Expand All @@ -39,6 +42,7 @@ import androidx.compose.ui.unit.round
*
* @param animationSpec An [FiniteAnimationSpec] which has [IntOffset] as a generic type.
*/
@OptIn(ExperimentalComposeUiApi::class)
public fun Modifier.animateMovement(
orbitalScope: OrbitalScope,
animationSpec: FiniteAnimationSpec<IntOffset> = spring(
Expand All @@ -53,14 +57,14 @@ public fun Modifier.animateMovement(
}
with(orbitalScope) {
this@composed
.onPlaced { lookaheadScopeCoordinates, layoutCoordinates ->
.onPlaced { layoutCoordinates ->
// This block of code has the LookaheadCoordinates of the LookaheadLayout
// as the first parameter, and the coordinates of this modifier as the second
// parameter.

// localLookaheadPositionOf returns the *target* position of this
// modifier in the LookaheadLayout's local coordinates.
val targetOffset = lookaheadScopeCoordinates
val targetOffset = layoutCoordinates.parentLayoutCoordinates!!
.localLookaheadPositionOf(
layoutCoordinates
)
Expand All @@ -69,7 +73,7 @@ public fun Modifier.animateMovement(

// localPositionOf returns the *current* position of this
// modifier in the LookaheadLayout's local coordinates.
placementOffset = lookaheadScopeCoordinates
placementOffset = layoutCoordinates.parentLayoutCoordinates!!
.localPositionOf(
layoutCoordinates, Offset.Zero
)
Expand All @@ -79,7 +83,7 @@ public fun Modifier.animateMovement(
// intermediateLayout is expected to produce intermediate stages of a layout
// transform. When the measure block is invoked after lookahead pass, the lookahead
// size of the child will be accessible as a parameter to the measure block.
.intermediateLayout { measurable, constraints, _ ->
.intermediateLayout { measurable, constraints ->
val placeable = measurable.measure(constraints)
layout(placeable.width, placeable.height) {
// offsetAnimation will animate the target position whenever it changes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.layout.intermediateLayout
import androidx.compose.ui.layout.onPlaced
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
Expand All @@ -40,6 +43,7 @@ import androidx.compose.ui.unit.round
* @param movementSpec An [FiniteAnimationSpec] which has [IntOffset] as a generic type.
* @param transformSpec An [FiniteAnimationSpec] which has [IntSize] as a generic type.
*/
@OptIn(ExperimentalComposeUiApi::class)
public fun Modifier.animateSharedElementTransition(
orbitalScope: OrbitalScope,
movementSpec: FiniteAnimationSpec<IntOffset> = spring(
Expand All @@ -66,7 +70,7 @@ public fun Modifier.animateSharedElementTransition(
// child will be accessible as a parameter to the measure block.
with(orbitalScope) {
this@composed
.intermediateLayout { measurable, constraints, lookaheadSize ->
.intermediateLayout { measurable, constraints ->
val (width, height) = sizeAnimation.value ?: lookaheadSize
measurable
.measure(constraints)
Expand All @@ -76,14 +80,14 @@ public fun Modifier.animateSharedElementTransition(
}
}
}
.onPlaced { lookaheadScopeCoordinates, layoutCoordinates ->
.onPlaced { layoutCoordinates ->
// This block of code has the LookaheadCoordinates of the LookaheadLayout
// as the first parameter, and the coordinates of this modifier as the second
// parameter.

// localLookaheadPositionOf returns the *target* position of this
// modifier in the LookaheadLayout's local coordinates.
val targetOffset = lookaheadScopeCoordinates
val targetOffset = layoutCoordinates.parentLayoutCoordinates!!
.localLookaheadPositionOf(
layoutCoordinates
)
Expand All @@ -92,13 +96,13 @@ public fun Modifier.animateSharedElementTransition(

// localPositionOf returns the *current* position of this
// modifier in the LookaheadLayout's local coordinates.
placementOffset = lookaheadScopeCoordinates
placementOffset = layoutCoordinates.parentLayoutCoordinates!!
.localPositionOf(
layoutCoordinates, Offset.Zero
)
.round()
}
.intermediateLayout { measurable, _, lookaheadSize ->
.intermediateLayout { measurable, _ ->
// When layout changes, the lookahead pass will calculate a new final size for the
// child modifier. This lookahead size can be used to animate the size
// change, such that the animation starts from the current size and gradually
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package com.skydoves.orbital

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.LookaheadLayout
import androidx.compose.ui.layout.LookaheadScope
import androidx.compose.ui.layout.MeasurePolicy
import java.lang.Integer.max

Expand All @@ -32,20 +35,23 @@ import java.lang.Integer.max
* @param measurePolicy The function that defines the measurement and layout.
* @param content A Composable that receives [OrbitalScope].
*/
@OptIn(ExperimentalComposeUiApi::class)
@Composable
public fun Orbital(
modifier: Modifier = Modifier,
measurePolicy: MeasurePolicy = internalMeasurePolicy,
content: @Composable OrbitalScope.() -> Unit
) {
LookaheadLayout(
content = {
val orbitalScope = remember { OrbitalScope(this) }
orbitalScope.content()
},
modifier = modifier,
measurePolicy = measurePolicy
)
LookaheadScope {
Layout(
content = {
val orbitalScope = remember { OrbitalScope(this) }
orbitalScope.content()
},
modifier = modifier,
measurePolicy = measurePolicy
)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package com.skydoves.orbital

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.layout.LookaheadLayoutScope
import androidx.compose.ui.layout.LookaheadScope

/**
* OrbitalScope is a scope that wraps [LookaheadLayoutScope] to apply animations.
*/
public class OrbitalScope internal constructor(lookaheadLayoutScope: LookaheadLayoutScope) :
LookaheadLayoutScope by lookaheadLayoutScope
@OptIn(ExperimentalComposeUiApi::class)
public class OrbitalScope internal constructor(lookaheadScope: LookaheadScope) :
LookaheadScope by lookaheadScope

0 comments on commit b69227e

Please sign in to comment.