Skip to content

Commit

Permalink
Replacing mainHandler with lifecycleScope
Browse files Browse the repository at this point in the history
  • Loading branch information
this-Aditya committed Dec 2, 2024
1 parent 284c9d5 commit f15575c
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions app/src/main/java/org/radarcns/detail/LoginActivityImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.textfield.TextInputLayout
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
Expand All @@ -60,7 +64,7 @@ import java.net.MalformedURLException
import java.net.URI

class LoginActivityImpl : LoginActivity(), PrivacyPolicyFragment.OnFragmentInteractionListener {
private var startActivityFuture: Runnable? = null
private var startActivityFuture: Job? = null
private var didModifyBaseUrl: Boolean = false
private var canLogin: Boolean = false

Expand All @@ -73,12 +77,10 @@ class LoginActivityImpl : LoginActivity(), PrivacyPolicyFragment.OnFragmentInter
private lateinit var qrCodeScanner: QrCodeScanner
private lateinit var dialog: Dialog

private lateinit var mainHandler: Handler

override fun onCreate(savedInstanceBundle: Bundle?) {
didCreate = false
didModifyBaseUrl = false
mainHandler = Handler(Looper.getMainLooper())
super.onCreate(savedInstanceBundle)
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)
Expand Down Expand Up @@ -206,7 +208,7 @@ class LoginActivityImpl : LoginActivity(), PrivacyPolicyFragment.OnFragmentInter
canLogin = true

logger.error("Failed to log in with {}", manager, ex)
mainHandler.post {
lifecycleScope.launch {
onDoneProcessing()
val res: Int = when (ex) {
is QrException -> R.string.login_failed_qr
Expand All @@ -222,28 +224,28 @@ class LoginActivityImpl : LoginActivity(), PrivacyPolicyFragment.OnFragmentInter

override fun loginSucceeded(manager: LoginManager?, authState: AppAuthState) {

mainHandler.post {
startActivityFuture?.let {
mainHandler.removeCallbacks(it)
startActivityFuture = null
}
val runnable = Runnable {
onDoneProcessing()
if (supportFragmentManager.fragments.any { it.id == R.id.privacy_policy_fragment }) {
logger.info("Privacy policy fragment already started.")
} else if (authState.isPrivacyPolicyAccepted) {
super.loginSucceeded(manager, authState)
if (!didCreate) {
overridePendingTransition(0, 0)
}
} else {
logger.info("Login succeeded. Calling privacy-policy fragment")
startPrivacyPolicyFragment(authState)
startActivityFuture?.let {
it.cancel()
startActivityFuture = null
}
val runnable = lifecycleScope.launch(start = CoroutineStart.LAZY) {
onDoneProcessing()
if (supportFragmentManager.fragments.any { it.id == R.id.privacy_policy_fragment }) {
logger.info("Privacy policy fragment already started.")
} else if (authState.isPrivacyPolicyAccepted) {
super.loginSucceeded(manager, authState)
if (!didCreate) {
overridePendingTransition(0, 0)
}
startActivityFuture = null
} else {
logger.info("Login succeeded. Calling privacy-policy fragment")
startPrivacyPolicyFragment(authState)
}
startActivityFuture = runnable
mainHandler.postDelayed(runnable, 100L)
startActivityFuture = null
}
lifecycleScope.launch {
delay(100)
runnable.start()
}
}

Expand Down

0 comments on commit f15575c

Please sign in to comment.