Skip to content

Commit

Permalink
Major refactoring of architecture #5
Browse files Browse the repository at this point in the history
  • Loading branch information
AmadeyKuspakov committed Jul 10, 2023
1 parent ba7566a commit 0e5bab9
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 166 deletions.
2 changes: 1 addition & 1 deletion oauth/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")

implementation("androidx.navigation:navigation-compose:2.5.3")
implementation("androidx.navigation:navigation-compose:2.6.0")
implementation("com.google.accompanist:accompanist-navigation-animation:0.30.1")

implementation( "com.google.dagger:hilt-android:$hiltVersion")
Expand Down
23 changes: 9 additions & 14 deletions oauth/src/main/java/jp/co/soramitsu/oauth/base/CardActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
Expand All @@ -33,11 +31,10 @@ import jp.co.soramitsu.oauth.base.sdk.contract.SoraCardContractData
import jp.co.soramitsu.oauth.base.sdk.toPayWingsType
import jp.co.soramitsu.oauth.common.navigation.coordinator.api.NavigationCoordinator
import jp.co.soramitsu.oauth.core.engines.activityresult.api.ActivityResult
import jp.co.soramitsu.oauth.core.engines.activityresult.api.SoraCardResult
import jp.co.soramitsu.oauth.core.engines.router.api.ComposeRouter
import jp.co.soramitsu.oauth.core.engines.router.api.SoraCardDestinations
import jp.co.soramitsu.oauth.theme.AuthSdkTheme
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -63,16 +60,8 @@ class CardActivity : AppCompatActivity(R.layout.card_activity) {
super.onCreate(savedInstanceState)
setContent {
AuthSdkTheme {
val startDestination = composeRouter.startDestination.collectAsState()

LaunchedEffect(Unit) {
composeRouter.startDestination.onEach {
println("This is checkpoint: composeRouter.startDestination - $it")
}.launchIn(this)
}

Box(modifier = Modifier.fillMaxSize()) {
val isLoading = remember(startDestination.value) {
val isLoading = remember(composeRouter.startDestination.value) {
derivedStateOf {
composeRouter.startDestination.value === SoraCardDestinations.Loading
}
Expand All @@ -88,7 +77,7 @@ class CardActivity : AppCompatActivity(R.layout.card_activity) {

SoraCardNavGraph(
navHostController = composeRouter.navController,
startDestination = startDestination.value,
startDestination = composeRouter.startDestination.value,
)
}
}
Expand Down Expand Up @@ -166,4 +155,10 @@ class CardActivity : AppCompatActivity(R.layout.card_activity) {
)
}
}

override fun onBackPressed() {
super.onBackPressed()
activityResult.setResult(SoraCardResult.Canceled)
finish()
}
}
196 changes: 85 additions & 111 deletions oauth/src/main/java/jp/co/soramitsu/oauth/base/SoraCardNavGraph.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
package jp.co.soramitsu.oauth.base

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.movableContentOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.dialog
import androidx.navigation.navArgument
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import jp.co.soramitsu.oauth.base.navigation.Argument
import jp.co.soramitsu.oauth.base.navigation.Destination
import jp.co.soramitsu.oauth.base.navigation.animatedComposable
import jp.co.soramitsu.oauth.base.navigation.path
import jp.co.soramitsu.oauth.base.navigation.requireArguments
import jp.co.soramitsu.oauth.base.navigation.requireString
import jp.co.soramitsu.oauth.common.navigation.flow.login.api.LoginDestination
import jp.co.soramitsu.oauth.common.navigation.flow.registration.api.RegistrationDestination
import jp.co.soramitsu.oauth.common.navigation.flow.verification.api.VerificationDestination
import jp.co.soramitsu.oauth.core.engines.router.api.SoraCardDestinations
import jp.co.soramitsu.oauth.feature.login.terms.TermsAndConditionsScreen
import jp.co.soramitsu.oauth.feature.login.web.WebPageScreen
import jp.co.soramitsu.oauth.feature.login.enterphone.EnterPhoneNumberScreen
import jp.co.soramitsu.oauth.feature.login.enterotp.VerifyPhoneNumberScreen
import jp.co.soramitsu.oauth.feature.registration.enternames.RegisterUserScreen
Expand All @@ -42,63 +36,56 @@ internal fun SoraCardNavGraph(
navHostController: NavHostController,
startDestination: SoraCardDestinations,
) {
println("This is checkpoint: SoraCardNavGraph recompose - ${startDestination.route}")

val isGraphVisible = remember(startDestination) {
startDestination !== SoraCardDestinations.Loading
}

val navGraph = remember(startDestination) {
println("This is checkpoint: navGraph remember recompose - ${startDestination.route}")
if (isGraphVisible) {
AnimatedNavHost(
navController = navHostController,
startDestination = startDestination.route
) {
animatedComposable(
route = LoginDestination.TermsAndConditions.route
) {
TermsAndConditionsScreen()
}

animatedComposable(
route = LoginDestination.EnterPhone.route
) {
EnterPhoneNumberScreen()
}

animatedComposable(
route = LoginDestination.EnterOtp.route
) {
VerifyPhoneNumberScreen()
}

animatedComposable(
route = RegistrationDestination.EnterFirstAndLastName.route
) {
RegisterUserScreen()
}

animatedComposable(
route = RegistrationDestination.EnterEmail.route,
) {
EnterEmailScreen()
}

movableContentOf {
println("This is checkpoint: navGraph movableContentOf recompose - ${startDestination.route}")
animatedComposable(
route = RegistrationDestination.EmailConfirmation.route
) {
VerifyEmailScreen()
}

AnimatedNavHost(
navController = navHostController,
startDestination = SoraCardDestinations.Loading.route
animatedComposable(
route = VerificationDestination.GetPrepared.route
) {
animatedComposable(
route = LoginDestination.TermsAndConditions.route
) {
TermsAndConditionsScreen()
}

animatedComposable(
route = LoginDestination.EnterPhone.route
) {
EnterPhoneNumberScreen()
}

animatedComposable(
route = LoginDestination.EnterOtp.route
) {
VerifyPhoneNumberScreen()
}

animatedComposable(
route = RegistrationDestination.EnterFirstAndLastName.route
) {
RegisterUserScreen()
}

animatedComposable(
route = RegistrationDestination.EnterEmail.route,
) {
EnterEmailScreen()
}

animatedComposable(
route = RegistrationDestination.EmailConfirmation.route
) {
VerifyEmailScreen()
}

animatedComposable(
route = VerificationDestination.GetPrepared.route
) {
GetPreparedScreen()
}
GetPreparedScreen()
}

// animatedComposable(
// route = Destination.WEB_PAGE.route
Expand All @@ -113,66 +100,53 @@ internal fun SoraCardNavGraph(
// )
// }

animatedComposable(
route = VerificationDestination.VerificationFailed.route
) {
VerificationFailedScreen()
}

animatedComposable(
route = VerificationDestination.VerificationRejected.route
) {
VerificationRejectedScreen()
}

animatedComposable(
route = VerificationDestination.VerificationInProgress.route
) {
VerificationInProgressScreen()
}

animatedComposable(
route = VerificationDestination.VerificationSuccessful.route
) {
VerificationSuccessfulScreen()
}

animatedComposable(
route = VerificationDestination.NotEnoughXor.route
) {
CardIssuanceScreen()
}

dialog(
route = VerificationDestination.GetMoreXor.route
) {
ChooseXorPurchaseMethodDialog()
}
animatedComposable(
route = VerificationDestination.VerificationFailed.route
) {
VerificationFailedScreen()
}

animatedComposable(
route = VerificationDestination.VerificationRejected.route
) {
VerificationRejectedScreen()
}

animatedComposable(
route = VerificationDestination.VerificationInProgress.route
) {
VerificationInProgressScreen()
}

animatedComposable(
route = VerificationDestination.VerificationSuccessful.route
) {
VerificationSuccessfulScreen()
}

animatedComposable(
route = VerificationDestination.NotEnoughXor.route
) {
CardIssuanceScreen()
}

println("This is checkpoint: navHostController.graph - ${navHostController.graph.nodes}")
dialog(
route = VerificationDestination.GetMoreXor.route
) {
ChooseXorPurchaseMethodDialog()
}
}
}

navGraph.invoke()
}

@Preview
@Composable
@OptIn(ExperimentalAnimationApi::class)
private fun PreviewSoraCardNavGraph() {
SoraCardNavGraph(
navHostController = rememberAnimatedNavController(),
startDestination = LoginDestination.TermsAndConditions
)
}




//
// animatedComposable(
// route = SoraCardDestinations) {
// ChangeEmailScreen()
// }
Box(modifier = Modifier.fillMaxSize()) {
SoraCardNavGraph(
navHostController = rememberAnimatedNavController(),
startDestination = LoginDestination.TermsAndConditions
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package jp.co.soramitsu.oauth.base.navigation
import android.os.Bundle
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.CubicBezierEasing
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandVertically
import androidx.compose.animation.scaleIn
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.runtime.Composable
Expand All @@ -27,24 +31,28 @@ fun NavGraphBuilder.animatedComposable(
arguments = arguments,
deepLinks = deepLinks,
enterTransition = {
println("This is checkpoint: enterTransition - ${this.initialState.destination.route} to ${this.targetState.destination.route}")
slideInHorizontally(
initialOffsetX = { it },
animationSpec = tween(TRANSITION_DURATION)
)
},
popEnterTransition = {
println("This is checkpoint: popEnterTransition - ${this.initialState.destination.route} to ${this.targetState.destination.route}")
slideInHorizontally(
initialOffsetX = { -it },
animationSpec = tween(TRANSITION_DURATION)
)
},
exitTransition = {
println("This is checkpoint: exitTransition - ${this.initialState.destination.route} to ${this.targetState.destination.route}")
slideOutHorizontally(
targetOffsetX = { -it },
animationSpec = tween(TRANSITION_DURATION)
)
},
popExitTransition = {
println("This is checkpoint: popExitTransition - ${this.initialState.destination.route} to ${this.targetState.destination.route}")
slideOutHorizontally(
targetOffsetX = { it },
animationSpec = tween(TRANSITION_DURATION)
Expand All @@ -53,11 +61,3 @@ fun NavGraphBuilder.animatedComposable(
content = content
)
}

fun NavBackStackEntry.requireArguments(): Bundle {
return arguments ?: throw IllegalStateException("Arguments is null")
}

fun Bundle.requireString(key: String): String {
return getString(key) ?: throw IllegalStateException("Argument is null")
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LoginFlowImpl @Inject constructor(
}
composeRouter.setNewStartDestination(destination)
}
}.run { composeRouter.clearBackStack() }
}

override fun onBack() {
composeRouter.popBack()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RegistrationFlowImpl @Inject constructor(
}
composeRouter.setNewStartDestination(destination)
}
}.run { composeRouter.clearBackStack() }
}

override fun onBack() {
composeRouter.popBack()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sealed interface VerificationDestination: SoraCardDestinations {
override val route: String = "VERIFICATION_REJECTED"

companion object: SoraCardDestinations {
override val route: String = "VERIFICATION_FAILED"
override val route: String = "VERIFICATION_REJECTED"

const val ADDITIONAL_INFO_KEY = "ADDITIONAL_INFO_KEY"
}
Expand Down
Loading

0 comments on commit 0e5bab9

Please sign in to comment.