Skip to content

Commit

Permalink
Android, Ios, Desktop targets added
Browse files Browse the repository at this point in the history
  • Loading branch information
ismail committed Jan 3, 2024
1 parent 184f6ca commit a401226
Show file tree
Hide file tree
Showing 26 changed files with 1,068 additions and 29 deletions.
19 changes: 18 additions & 1 deletion lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,29 @@ kotlin {
}
}

val ktor = "2.3.7"

sourceSets {
val commonMain by getting {
dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material)
implementation(compose.material3)
implementation(compose.materialIconsExtended)
implementation(compose.ui)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)

implementation("io.ktor:ktor-client-core:$ktor")
}
}
val androidMain by getting {
dependencies {
api("androidx.activity:activity-compose:1.7.2")
api("androidx.appcompat:appcompat:1.6.1")
api("androidx.core:core-ktx:1.10.1")
implementation("com.airbnb.android:lottie-compose:6.2.0")
implementation("io.ktor:ktor-client-android:$ktor")
}
}
val iosX64Main by getting
Expand All @@ -46,9 +59,13 @@ kotlin {
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
dependencies {
implementation("io.ktor:ktor-client-darwin:$ktor")
}
}
val desktopMain by getting {
dependencies {
implementation("io.ktor:ktor-client-java:$ktor")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package image_animation

import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.repeatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.animateLottieCompositionAsState
import com.airbnb.lottie.compose.rememberLottieComposition
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.Resource
import org.jetbrains.compose.resources.orEmpty
import org.jetbrains.compose.resources.rememberImageBitmap
import kotlin.math.roundToInt

@OptIn(ExperimentalResourceApi::class)
@Composable
actual fun KottieAnimation(
modifier: Modifier,
image: Resource,
speed: Float,
iterations: Int,
completed: () -> Unit
) {

val time = remember { Animatable(0f) }

LaunchedEffect(Unit) {
time.animateTo(
targetValue = 1f,
animationSpec = repeatable(
iterations = iterations,
animation = tween(
durationMillis = (1000 / speed).roundToInt(),
easing = FastOutLinearInEasing
),
repeatMode = RepeatMode.Restart
)
)
}

val value = image.rememberImageBitmap()

Image(
bitmap = value.orEmpty(),
contentDescription = "",
modifier = modifier
.alpha(time.value)
)

LaunchedEffect(
key1 = time.isRunning,
){
if (time.isRunning){
println("animation running!!")
}else{
if (iterations == 1){
println("animation completed!!")
completed.invoke()
}
}
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package json_animation

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.animateLottieCompositionAsState
import com.airbnb.lottie.compose.rememberLottieComposition
import com.airbnb.lottie.compose.LottieAnimation
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.Resource


@OptIn(ExperimentalResourceApi::class)
@SuppressLint("DiscouragedApi")
@Composable
actual fun KottieAnimation(
modifier: Modifier,
fileName: Resource,
speed: Float,
iterations: Int,
completed: () -> Unit
) {

var animation by remember { mutableStateOf("") }

LaunchedEffect(Unit){
animation = fileName.readBytes().decodeToString()
}

val composition by rememberLottieComposition(
LottieCompositionSpec.JsonString(animation)
)

val animationState = animateLottieCompositionAsState(
composition = composition,
speed = speed,
iterations = iterations
)

LaunchedEffect(key1 = animationState.progress){
if (animationState.progress == 1f){
completed()
}
}

LottieAnimation(
composition = composition,
progress = { animationState.progress },
modifier = Modifier.fillMaxSize(),
)

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package text_animation


import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.repeatable
import androidx.compose.animation.core.tween
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.text.TextStyle
import kotlin.math.roundToInt


@Composable
actual fun KottieAnimation(
modifier: Modifier,
text: String,
speed: Float,
iterations: Int,
textStyle: TextStyle,
completed: () -> Unit,
) {


val time = remember { Animatable(0f) }

LaunchedEffect(Unit) {
time.animateTo(
targetValue = 1f,
animationSpec = repeatable(
iterations = iterations,
animation = tween(
durationMillis = (1000 / speed).roundToInt(),
easing = FastOutLinearInEasing
),
repeatMode = RepeatMode.Restart
)
)
}

Text(
text = text,
style = textStyle,
modifier = modifier
.alpha(time.value)
)

LaunchedEffect(
key1 = time.isRunning,
){
if (time.isRunning){
println("animation running!!")
}else{
if (iterations == 1){
println("animation completed!!")
completed.invoke()
}
}
}


}


Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package url_animation

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.animateLottieCompositionAsState
import com.airbnb.lottie.compose.rememberLottieComposition

@Composable
actual fun KottieAnimation(
modifier: Modifier,
url: String,
speed: Float,
iterations: Int,
completed: () -> Unit
) {

val composition by rememberLottieComposition(
LottieCompositionSpec.Url(url)
)

val animationState = animateLottieCompositionAsState(
composition = composition,
speed = speed,
iterations = iterations
)

LaunchedEffect(key1 = animationState.progress){
if (animationState.progress == 1f){
completed()
}
}

LottieAnimation(
composition = composition,
progress = { animationState.progress },
modifier = Modifier.fillMaxSize(),
)

}
18 changes: 18 additions & 0 deletions lib/src/commonMain/kotlin/image_animation/KottieAnimation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package image_animation

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.Resource


@OptIn(ExperimentalResourceApi::class)
@Composable
expect fun KottieAnimation(
modifier: Modifier = Modifier,
image: Resource,
speed: Float = 1f,
iterations: Int = 1,
completed: () -> Unit = {}
)

19 changes: 19 additions & 0 deletions lib/src/commonMain/kotlin/json_animation/KottieAnimation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package json_animation

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.Resource



@OptIn(ExperimentalResourceApi::class)
@Composable
expect fun KottieAnimation(
modifier: Modifier = Modifier,
fileName: Resource,
speed: Float = 1f,
iterations: Int = 1,
completed: () -> Unit = {}
)

20 changes: 20 additions & 0 deletions lib/src/commonMain/kotlin/text_animation/KottieAnimation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package text_animation

import androidx.compose.material3.LocalTextStyle
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle



@Composable
expect fun KottieAnimation(
modifier: Modifier = Modifier,
text: String,
speed: Float = 1f,
iterations: Int = 1,
textStyle: TextStyle = LocalTextStyle.current,
completed: () -> Unit = {}
)


16 changes: 16 additions & 0 deletions lib/src/commonMain/kotlin/url_animation/KottieAnimation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package url_animation

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier


@Composable
expect fun KottieAnimation(
modifier: Modifier = Modifier,
url: String,
speed: Float = 1f,
iterations: Int = 1,
completed: () -> Unit = {}
)


7 changes: 7 additions & 0 deletions lib/src/commonMain/kotlin/utils/KottieConstants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package utils



object KottieConstants {
val IterateForever = Int.MAX_VALUE
}
Loading

0 comments on commit a401226

Please sign in to comment.