Skip to content

Commit

Permalink
Include SplashScreenController, new Composables and Extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluchu committed Aug 25, 2021
1 parent 6ad10cb commit 3f3aa01
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 19 deletions.
30 changes: 16 additions & 14 deletions app/src/main/java/com/jeluchu/compposecomponents/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ class MainActivity : ComponentActivity() {
StoryCard(
modifier = Modifier.animateItem(),
title = item.toString(),
iconMainUrl = "https://i.picsum.photos/id/1003/1181/1772.jpg?hmac=oN9fHMXiqe9Zq2RM6XT-RVZkojgPnECWwyEF1RvvTZk",
iconMainUrl = "https://i.picsum.photos/id/1016/3844/2563.jpg?hmac=WEryKFRvTdeae2aUrY-DHscSmZuyYI9jd_-p94stBvc",
circleImage = R.drawable.ic_btnfavourite,
navigateToScreen = { context.shortToast("Clicked!") }
)
}
}

if (imageUriState.value != null) {
if (false && imageUriState.value != null) {

NetworkImage(
modifier = Modifier
Expand All @@ -76,22 +76,24 @@ class MainActivity : ComponentActivity() {
url = imageUriState.value!!
)

}
Button(
onClick = { selectImageLauncher.launch("image/*") },
modifier = Modifier.padding(vertical = 8.dp)
) {
androidx.compose.material.Text("Open Gallery")
}

Button(
onClick = { selectImageLauncher.launch("image/*") },
modifier = Modifier.padding(vertical = 8.dp)
) {
androidx.compose.material.Text("Open Gallery")
}
DoubleTapAnimation(
"https://i.picsum.photos/id/1016/3844/2563.jpg?hmac=WEryKFRvTdeae2aUrY-DHscSmZuyYI9jd_-p94stBvc",
iconResource = R.drawable.ic_btnfavourite
) {
// Action when double tap
}

DoubleTapAnimation(
"https://i.picsum.photos/id/1016/3844/2563.jpg?hmac=WEryKFRvTdeae2aUrY-DHscSmZuyYI9jd_-p94stBvc",
iconResource = R.drawable.ic_btnfavourite
) {
// Action when double tap
}



}
}
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
classpath 'com.android.tools.build:gradle:7.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
}
}
Expand Down
1 change: 1 addition & 0 deletions jchucomponentscompose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
implementation 'androidx.compose.material:material:1.0.1'
implementation 'androidx.compose.ui:ui-tooling-preview:1.0.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation 'androidx.core:core-splashscreen:1.0.0-alpha01'
debugImplementation 'androidx.compose.ui:ui-tooling:1.0.1'
implementation 'androidx.compose.material:material-icons-extended:1.0.1'
implementation 'androidx.compose.foundation:foundation:1.0.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package com.jeluchu.jchucomponentscompose.ui.animations

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.graphics.Path
import android.view.View
import android.view.animation.AnticipateInterpolator
import androidx.core.animation.doOnEnd
import androidx.core.splashscreen.SplashScreen

/**
*
* Author: @Jeluchu
*
* This class allows you to quickly and easily
* implement a Splash Screen with the new Jetpack API
*
* @param splashScreen you must pass by parameter Activity.installSplashScreen() from [SplashScreen]
* @param defaultExitDuration you will be able to spend a certain amount of time
* for the duration of the animation. By default it is set to 300
*
*/

@SuppressLint("CustomSplashScreen")
class SplashScreenController(
private val splashScreen: SplashScreen,
private val defaultExitDuration: Long = 300,
) {

fun customizeSplashScreenExit(keys: List<SplashAnimations>, onExitExtraActions: () -> Unit = {}) =
splashScreen.setOnExitAnimationListener { splashScreenViewProvider ->
val onExit = {
splashScreenViewProvider.remove()
onExitExtraActions()
}
showSplashExitAnimator(splashScreenViewProvider.view, keys, onExit)
showSplashIconExitAnimator(splashScreenViewProvider.iconView, onExit)
}

private fun showSplashExitAnimator(
splashScreenView: View,
keys: List<SplashAnimations>,
onExit: () -> Unit = {}) {

AnimatorSet().run {
duration = defaultExitDuration
interpolator = AnticipateInterpolator()
splashScreenView.apply {
playTogether(getAnimation(keys, splashScreenView).toList())
}
doOnEnd { onExit() }
start()
}

}

private fun showSplashIconExitAnimator(iconView: View, onExit: () -> Unit = {}) {

val alphaOut = ObjectAnimator.ofFloat(
iconView,
View.ALPHA,
1f,
0f
)

val scaleOut = ObjectAnimator.ofFloat(
iconView,
View.SCALE_X,
View.SCALE_Y,
Path().apply {
moveTo(1.0f, 1.0f)
lineTo(0.3f, 0.3f)
}
)

val slideUp = ObjectAnimator.ofFloat(
iconView,
View.TRANSLATION_Y,
0f,
-(iconView.height).toFloat() * 2.25f
)

AnimatorSet().run {
interpolator = AnticipateInterpolator()
duration = defaultExitDuration

playTogether(alphaOut, scaleOut, slideUp)
doOnEnd { onExit() }
start()
}

}

private fun getAnimation(keys: List<SplashAnimations>, splashScreenView: View): MutableList<ObjectAnimator> {

val listAnimations: MutableList<ObjectAnimator> = mutableListOf()

for (key in keys) {
val animation = when(key) {
SplashAnimations.SlideUp -> ObjectAnimator.ofFloat(
splashScreenView,
View.TRANSLATION_Y,
0f,
-splashScreenView.height.toFloat()
)
SplashAnimations.SlideLeft -> ObjectAnimator.ofFloat(
splashScreenView,
View.TRANSLATION_X,
0f,
-splashScreenView.width.toFloat()
)
SplashAnimations.ScaleXOut -> ObjectAnimator.ofFloat(
splashScreenView,
View.SCALE_X,
1.0f,
0f
)
SplashAnimations.AlphaOut -> ObjectAnimator.ofFloat(
splashScreenView,
View.ALPHA,
1f,
0f
)
SplashAnimations.ScaleOut -> ObjectAnimator.ofFloat(
splashScreenView,
View.SCALE_X,
View.SCALE_Y,
Path().apply {
moveTo(1.0f, 1.0f)
lineTo(0f, 0f)
}
)
}

listAnimations.add(animation)

}

return listAnimations

}

}

enum class SplashAnimations {
SlideUp,
SlideLeft,
ScaleXOut,
AlphaOut,
ScaleOut
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import com.jeluchu.jchucomponentscompose.ui.images.NetworkImage

@Composable
fun DebutCard(
modifier: Modifier,
modifier: Modifier = Modifier,
title: String,
image: String,
iconDebut: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ fun StoryCard(
NetworkImage(
url = iconMainUrl,
modifier = Modifier
.size(23.dp)
.padding(start = 7.dp)
.constrainAs(storyImg) {
linkTo(parent.start, parent.end)
linkTo(parent.top, parent.bottom)
},
contentScale = ContentScale.Fit,
contentScale = ContentScale.Crop,
)

} else if (iconMainResource.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.jeluchu.jchucomponentscompose.ui.chips

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jeluchu.jchucomponentscompose.ui.theme.green200
import com.jeluchu.jchucomponentscompose.ui.theme.green700
import com.jeluchu.jchucomponentscompose.ui.theme.typography

/**
*
* Author: @Jeluchu
*
* This component is similar to the Chips,
* in which you can display a text or a text and an icon
*
* @sample ChipTagViewPreview
*
* @param modifier modifier that will be used to change the color, size...
* @param title text to be displayed on the chip
* @param colors color of the text and background
* @param shape type of shape desired for this chip
* @param style style of text to be displayed
* @param navigateToScreen action to be performed after pressing
*
*/

@Composable
fun InterestTag(
modifier: Modifier = Modifier,
title: String,
colors: TagColors = TagDefaults.tagColors(),
shape: Shape = RoundedCornerShape(4.dp),
style: TextStyle = typography.body2.copy(fontWeight = FontWeight.Bold),
navigateToScreen: () -> Unit = {}
) {
val tagModifier = modifier
.padding(4.dp)
.clickable(onClick = navigateToScreen)
.clip(shape = shape)
.background(colors.backgroundColor(enabled = true).value)
.padding(horizontal = 8.dp, vertical = 4.dp)
Text(
text = title,
color = colors.contentColor(enabled = true).value,
modifier = tagModifier,
style = style
)
}

@Stable
interface TagColors {
@Composable
fun backgroundColor(enabled: Boolean): State<Color>

@Composable
fun contentColor(enabled: Boolean): State<Color>
}

@Immutable
private class DefaultTagColors(
private val backgroundColor: Color,
private val contentColor: Color
) : TagColors {
@Composable
override fun backgroundColor(enabled: Boolean): State<Color> {
return rememberUpdatedState(newValue = backgroundColor)
}

@Composable
override fun contentColor(enabled: Boolean): State<Color> {
return rememberUpdatedState(newValue = contentColor)
}
}

object TagDefaults {
@Composable
fun tagColors(
backgroundColor: Color = green200.copy(alpha = .2f),
contentColor: Color = green700
): TagColors = DefaultTagColors(backgroundColor = backgroundColor, contentColor = contentColor)
}


@Preview
@Composable
fun InterestTagPreview() {
InterestTag(title = "Name")
}
Loading

0 comments on commit 3f3aa01

Please sign in to comment.