-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
b58346e
commit 4ea5c40
Showing
9 changed files
with
134 additions
and
8 deletions.
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
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
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
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
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
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
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
54 changes: 54 additions & 0 deletions
54
appcues/src/main/java/com/appcues/trait/appcues/EffectsTrait.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,54 @@ | ||
package com.appcues.trait.appcues | ||
|
||
import androidx.compose.foundation.layout.BoxScope | ||
import androidx.compose.runtime.Composable | ||
import com.appcues.data.model.AppcuesConfigMap | ||
import com.appcues.data.model.getConfig | ||
import com.appcues.data.model.getConfigDouble | ||
import com.appcues.data.model.getConfigInt | ||
import com.appcues.data.model.getConfigStyle | ||
import com.appcues.trait.AppcuesTraitException | ||
import com.appcues.trait.BackdropDecoratingTrait | ||
import com.appcues.trait.appcues.EffectsTrait.PresentationStyle.CONFETTI | ||
import com.appcues.trait.appcues.effects.ConfettiEffect | ||
|
||
internal class EffectsTrait( | ||
override val config: AppcuesConfigMap, | ||
) : BackdropDecoratingTrait { | ||
|
||
companion object { | ||
|
||
const val TYPE = "@appcues/effects" | ||
const val DEFAULT_DURATION = 2000 | ||
const val DEFAULT_INTENSITY = 1.0 | ||
} | ||
|
||
private enum class PresentationStyle { | ||
CONFETTI | ||
} | ||
|
||
private val presentationStyle = config.getConfig<String?>("presentationStyle").toPresentationStyle() | ||
|
||
private val duration = config.getConfigInt("duration") ?: DEFAULT_DURATION | ||
|
||
private val intensity = config.getConfigDouble("intensity") ?: DEFAULT_INTENSITY | ||
|
||
private val style = config.getConfigStyle("style") | ||
|
||
@Composable | ||
override fun BoxScope.BackdropDecorate(content: @Composable BoxScope.() -> Unit) { | ||
// other backdrop decorate traits renders first (putting this one on top) | ||
content() | ||
|
||
when (presentationStyle) { | ||
CONFETTI -> ConfettiEffect(style, duration, intensity) | ||
} | ||
} | ||
|
||
private fun String?.toPresentationStyle(): PresentationStyle { | ||
return when (this) { | ||
"confetti" -> CONFETTI | ||
else -> throw AppcuesTraitException("invalid effects presentation style: $this") | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
appcues/src/main/java/com/appcues/trait/appcues/effects/ConfettiEffects.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,47 @@ | ||
package com.appcues.trait.appcues.effects | ||
|
||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import com.appcues.data.mapper.styling.mapToColors | ||
import com.appcues.data.model.styling.ComponentStyle | ||
import nl.dionsegijn.konfetti.compose.KonfettiView | ||
import nl.dionsegijn.konfetti.core.Party | ||
import nl.dionsegijn.konfetti.core.Position.Relative | ||
import nl.dionsegijn.konfetti.core.emitter.Emitter | ||
import nl.dionsegijn.konfetti.core.models.Size | ||
import java.util.concurrent.TimeUnit.MILLISECONDS | ||
|
||
private const val EMITTER_INTENSITY_RAW = 200 | ||
|
||
@Composable | ||
internal fun ConfettiEffect(style: ComponentStyle?, duration: Int, intensity: Double) { | ||
// default colors in case colors style property is empty | ||
val colors = remember { | ||
val styleColors = style?.colors ?: listOf() | ||
|
||
styleColors.ifEmpty { listOf("#5C5CFF", "#20E0D6", "#FF5290").mapToColors() }.map { it.toInt() } | ||
} | ||
|
||
// this is a center point on the top of the screen, that will disperse particles | ||
// in a 180 angle spread before it start falling straight down | ||
val party = Party( | ||
speed = 0f, | ||
maxSpeed = 60f, | ||
damping = 0.9f, | ||
spread = 180, | ||
size = listOf(Size(sizeInDp = 10), Size(sizeInDp = 12), Size(sizeInDp = 16)), | ||
angle = 270, | ||
timeToLive = 5000, | ||
fadeOutEnabled = true, | ||
colors = colors, | ||
position = Relative(x = 0.5, y = -0.05), | ||
emitter = Emitter(duration = duration.toLong(), MILLISECONDS).perSecond((EMITTER_INTENSITY_RAW * intensity).toInt()) | ||
) | ||
|
||
KonfettiView( | ||
modifier = Modifier.fillMaxSize(), | ||
parties = listOf(party), | ||
) | ||
} |