SplashScreen animation.
This commit is contained in:
parent
fa2af6dd90
commit
9cde3f6404
7 changed files with 181 additions and 50 deletions
|
|
@ -1,55 +1,121 @@
|
|||
package com.pixelized.biblib.ui.composable.screen
|
||||
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.animation.*
|
||||
import androidx.compose.animation.core.tween
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material.MaterialTheme
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.pixelized.biblib.BuildConfig
|
||||
import com.pixelized.biblib.R
|
||||
import com.pixelized.biblib.ui.theme.BibLibTheme
|
||||
import com.pixelized.biblib.ui.viewmodel.initialisation.IInitialisation
|
||||
import com.pixelized.biblib.ui.viewmodel.initialisation.InitialisationViewModel
|
||||
import com.pixelized.biblib.ui.viewmodel.navigation.INavigation
|
||||
import com.pixelized.biblib.ui.viewmodel.navigation.NavigationViewModel
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.*
|
||||
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
fun SplashScreenComposablePreview() {
|
||||
BibLibTheme {
|
||||
SplashScreenComposable(INavigation.Mock())
|
||||
SplashScreenComposable(IInitialisation.Mock(), INavigation.Mock())
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalAnimationApi::class)
|
||||
@Composable
|
||||
fun SplashScreenComposable(
|
||||
initialisation: IInitialisation = viewModel<InitialisationViewModel>(),
|
||||
navigation: INavigation = viewModel<NavigationViewModel>()
|
||||
) {
|
||||
val duration = 1000
|
||||
val typography = MaterialTheme.typography
|
||||
initialisation.LoadApplication {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxHeight()
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.width(240.dp)
|
||||
.align(Alignment.Center)
|
||||
) {
|
||||
AnimatedVisibility(
|
||||
visible = it != IInitialisation.State.Finished,
|
||||
initiallyVisible = false,
|
||||
enter = fadeIn(animationSpec = tween(duration))
|
||||
+ slideInVertically(
|
||||
initialOffsetY = { height -> -height },
|
||||
animationSpec = tween(duration)
|
||||
),
|
||||
exit = fadeOut(animationSpec = tween(duration))
|
||||
+ slideOutVertically(
|
||||
targetOffsetY = { height -> -height },
|
||||
animationSpec = tween(duration)
|
||||
),
|
||||
) {
|
||||
Text(
|
||||
style = typography.h4,
|
||||
text = "Welcome to"
|
||||
)
|
||||
}
|
||||
AnimatedVisibility(
|
||||
modifier = Modifier.align(Alignment.End),
|
||||
visible = it != IInitialisation.State.Finished,
|
||||
initiallyVisible = false,
|
||||
enter = fadeIn(animationSpec = tween(duration))
|
||||
+ slideInVertically(
|
||||
initialOffsetY = { height -> height },
|
||||
animationSpec = tween(duration)
|
||||
),
|
||||
exit = fadeOut(animationSpec = tween(duration))
|
||||
+ slideOutVertically(
|
||||
targetOffsetY = { height -> height },
|
||||
animationSpec = tween(duration)
|
||||
),
|
||||
) {
|
||||
Text(
|
||||
style = typography.h4,
|
||||
text = stringResource(id = R.string.app_name)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxHeight()
|
||||
.fillMaxWidth(),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Text(
|
||||
style = typography.h4,
|
||||
text = "Welcome to BibLib"
|
||||
)
|
||||
}
|
||||
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
LaunchedEffect(key1 = "loading", block = {
|
||||
coroutineScope.launch {
|
||||
delay(1000)
|
||||
navigation.navigateTo(INavigation.Screen.LoginScreen)
|
||||
AnimatedVisibility(
|
||||
modifier = Modifier.align(Alignment.BottomEnd),
|
||||
visible = it != IInitialisation.State.Finished,
|
||||
enter = fadeIn(animationSpec = tween(duration)),
|
||||
exit = fadeOut(animationSpec = tween(duration)),
|
||||
) {
|
||||
Text(
|
||||
style = typography.caption,
|
||||
text = stringResource(
|
||||
R.string.app_version,
|
||||
BuildConfig.BUILD_TYPE.toUpperCase(Locale.getDefault()),
|
||||
BuildConfig.VERSION_NAME,
|
||||
BuildConfig.VERSION_CODE
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (it == IInitialisation.State.Finished) {
|
||||
LaunchedEffect(key1 = "navigateTo(INavigation.Screen.LoginScreen)") {
|
||||
delay(1000)
|
||||
navigation.navigateTo(INavigation.Screen.LoginScreen)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -124,11 +124,4 @@ class AuthenticationViewModel : ViewModel(), IAuthentication {
|
|||
_state.postValue(State.Loading)
|
||||
launcher?.launch(googleSignIn.client.signInIntent)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val SHARED_PREF = "BIB_LIB_SHARED_PREF"
|
||||
const val REMEMBER_CREDENTIAL = "REMEMBER_CREDENTIAL"
|
||||
const val REMEMBER_USER = "REMEMBER_USER"
|
||||
const val REMEMBER_PASSWORD = "REMEMBER_PASSWORD"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.pixelized.biblib.ui.viewmodel.initialisation
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
|
||||
interface IInitialisation {
|
||||
|
||||
@Composable
|
||||
fun LoadApplication(content: @Composable (State) -> Unit)
|
||||
|
||||
sealed class State {
|
||||
abstract fun proceed(): State
|
||||
|
||||
object Initial : State() {
|
||||
override fun proceed() = Loading
|
||||
}
|
||||
object Loading : State() {
|
||||
override fun proceed() = Finished
|
||||
}
|
||||
object Finished : State() {
|
||||
override fun proceed() = Finished
|
||||
}
|
||||
}
|
||||
|
||||
class Mock(private val state: State = State.Loading) : IInitialisation {
|
||||
@Composable
|
||||
override fun LoadApplication(content: (State) -> Unit) = content(state)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package com.pixelized.biblib.ui.viewmodel.initialisation
|
||||
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.pixelized.biblib.network.client.IBibLibClient
|
||||
import com.pixelized.biblib.repository.credential.ICredentialRepository
|
||||
import com.pixelized.biblib.ui.viewmodel.initialisation.IInitialisation.State.*
|
||||
import com.pixelized.biblib.utils.injection.inject
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
class InitialisationViewModel : ViewModel(), IInitialisation {
|
||||
private val credentialRepository: ICredentialRepository by inject()
|
||||
private val client: IBibLibClient by inject()
|
||||
|
||||
@Composable
|
||||
override fun LoadApplication(content: @Composable (IInitialisation.State) -> Unit) {
|
||||
val state: MutableState<IInitialisation.State> = remember { mutableStateOf(Initial) }
|
||||
|
||||
LaunchedEffect(key1 = "LoadApplication") {
|
||||
state.value = Loading
|
||||
delay(2000)
|
||||
|
||||
val bearerToken = credentialRepository.bearer
|
||||
if (bearerToken != null) {
|
||||
client.updateBearerToken(bearerToken)
|
||||
state.value = Finished
|
||||
} else {
|
||||
state.value = Finished
|
||||
}
|
||||
}
|
||||
|
||||
content(state.value)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue