Last active
March 7, 2020 21:12
-
-
Save Elvis10ten/ebbb230435d9a03318140f5840be1a4f to your computer and use it in GitHub Desktop.
This file contains 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
import android.content.Context | |
import android.os.Bundle | |
import android.os.Handler | |
import android.support.v7.app.AppCompatActivity | |
class SplashActivity: AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_splash) | |
scheduleSplashScreen() | |
} | |
private fun scheduleSplashScreen() { | |
val splashScreenDuration = getSplashScreenDuration() | |
Handler().postDelayed( | |
{ | |
// After the splash screen duration, route to the right activities | |
val user = UserDb.getCurrentUser() | |
routeToAppropriatePage(user) | |
finish() | |
}, | |
splashScreenDuration | |
) | |
} | |
private fun getSplashScreenDuration(): Long { | |
val sp = getPreferences(Context.MODE_PRIVATE) | |
val prefKeyFirstLaunch = "pref_first_launch" | |
return when(sp.getBoolean(prefKeyFirstLaunch, true)) { | |
true -> { | |
// If this is the first launch, make it slow (> 3 seconds) and set flag to false | |
sp.edit().putBoolean(prefKeyFirstLaunch, false).apply() | |
5000 | |
} | |
false -> { | |
// If the user has launched the app, make the splash screen fast (<= 1 seconds) | |
1000 | |
} | |
} | |
} | |
private fun routeToAppropriatePage(user: User) { | |
// Example routing | |
when { | |
user == null -> OnboardingActivity.start(this) | |
user.hasPhoneNumber() -> EditProfileActivity.start(this) | |
user.hasSubscriptionExpired() -> PaymentPlansActivity.start(this) | |
else -> HomeActivity.start(this) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment