Last active
May 30, 2018 12:25
-
-
Save tinsukE/d8315cbf1ca3717fd1a458330c496612 to your computer and use it in GitHub Desktop.
Complicated Splash
This file contains hidden or 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
class SplashActivity : Activity() { | |
private var isUserSessionInitialized = false | |
private var hasBeenShownForMinTime = false | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
initializeApp() | |
scheduleProgressBar() | |
} | |
private fun initializeApp() { | |
initializeUserSession() | |
fetchSupportInfo() | |
} | |
private fun initializeUserSession() { | |
userSession.initialize(object : Callback<Unit> { | |
override fun onSuccess(result: Unit) = onUserSessionInitialized() | |
override fun onError(error: Error) = onUserSessionError(error) | |
}) | |
} | |
private fun onUserSessionInitialized() { | |
isUserSessionInitialized = true | |
checkForCompletion() | |
} | |
private fun onUserSessionError(error: Error) { | |
showRetryAlert(error, | |
onRetry = { initializeUserSession() }, | |
onCancel = { finish() }) | |
} | |
private fun fetchSupportInfo() { | |
supportInfoHelper.updateInfo(object : Callback<SupportInfo> { | |
override fun onSuccess(result: SupportInfo) = onSupportInfoFetched() | |
override fun onError(error: Error) = onSupportInfoError(error) | |
}) | |
} | |
private fun onSupportInfoFetched() { | |
checkForCompletion() | |
} | |
private fun onSupportInfoError(error: Error) { | |
showRetryAlert(error, | |
onRetry = { fetchSupportInfo() }, | |
onCancel = { finish() }) | |
} | |
private fun scheduleProgressBar() { | |
progressBar.postDelayed({ | |
hasBeenShownForMinTime = true | |
checkForCompletion() | |
progressBar.visibility = View.VISIBLE | |
}, MIN_SHOW_TIME) | |
} | |
private fun checkForCompletion() { | |
if (isUserSessionInitialized && supportInfoHelper.hasInfo && hasBeenShownForMinTime) { | |
setResult(Activity.RESULT_OK) | |
finish() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment