Skip to content

Commit

Permalink
Changed the way of opening the first screen - make it much simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
PStrelchenko committed Dec 28, 2020
1 parent d6684da commit 2e573b4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@ package com.aaglobal.jnc_playground.ui.auth

import android.os.Bundle
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.navigation.fragment.findNavController
import com.aaglobal.jnc_playground.R
import kotlinx.android.synthetic.main.fragment_finish_auth.*


class FinishAuthFragment : Fragment(R.layout.fragment_finish_auth) {

companion object {
const val AUTH_FLOW_RESULT_KEY = "auth_flow_result"
}

private val finishAuthViewModel: FinishAuthViewModel by viewModels()


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

Expand All @@ -27,13 +22,13 @@ class FinishAuthFragment : Fragment(R.layout.fragment_finish_auth) {
finishAuthViewModel.setFinishAuthFlag()

// Navigate back from auth flow
Navigation.findNavController(
requireActivity(),
R.id.activity_root__fragment__nav_host
).popBackStack(R.id.auth__nav_graph, true)

// Send signal about finishing flow
findNavController().currentBackStackEntry?.savedStateHandle?.set(AUTH_FLOW_RESULT_KEY, true)
val result = findNavController().popBackStack(R.id.auth__nav_graph, true)
if (result.not()) {
// we can't open new destination with this action
// --> we opened Auth flow from splash
// --> need to open main graph
findNavController().navigate(R.id.MainFragment)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.navigation.Navigation
import com.aaglobal.jnc_playground.R
import com.aaglobal.jnc_playground.ui.auth.FinishAuthFragment
import com.aaglobal.jnc_playground.ui.auth.StartAuthFragmentArgs


class SplashFragment : Fragment(R.layout.fragment_splash) {
Expand All @@ -19,32 +17,21 @@ class SplashFragment : Fragment(R.layout.fragment_splash) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val authResult = findNavController().currentBackStackEntry
?.savedStateHandle
?.remove<Boolean>(FinishAuthFragment.AUTH_FLOW_RESULT_KEY) == true

if (authResult) {
navigateToMainScreen()
return
}

// Navigate with SingleLiveEvent from Splash screen
splashViewModel.splashNavCommand.observe(viewLifecycleOwner, Observer { splashNavCommand ->
when (splashNavCommand) {
SplashNavCommand.NAVIGATE_TO_MAIN -> navigateToMainScreen()
SplashNavCommand.NAVIGATE_TO_AUTH -> navigateToAuthFlow()
null -> {
// do nothing
}
val navController = Navigation.findNavController(requireActivity(), R.id.activity_root__fragment__nav_host)

val mainGraph = navController.navInflater.inflate(R.navigation.app_nav_graph)

// Way to change first screen at runtime.
mainGraph.startDestination = when (splashNavCommand) {
SplashNavCommand.NAVIGATE_TO_MAIN -> R.id.MainFragment
SplashNavCommand.NAVIGATE_TO_AUTH -> R.id.auth__nav_graph
null -> throw IllegalArgumentException("Illegal splash navigation command")
}
})
}

private fun navigateToAuthFlow() {
findNavController().navigate(R.id.action__SplashFragment__to__AuthFlow, StartAuthFragmentArgs(isFromSplashScreen = true).toBundle())
navController.graph = mainGraph
})
}

private fun navigateToMainScreen() {
findNavController().navigate(R.id.action__SplashFragment__to__MainFragment)
}
}

0 comments on commit 2e573b4

Please sign in to comment.