persist mib sessions on disk instead of refreshing token and fix contact picker render issue #37
Auto Tag on Version Change / check-version (push) Failing after 15m8s

This commit is contained in:
2026-06-10 00:17:43 +05:00
parent 26dcb20f7f
commit 4b1c2419ec
5 changed files with 117 additions and 7 deletions
@@ -151,7 +151,7 @@ class ContactPickerSheetFragment : BottomSheetDialogFragment() {
viewModel.accounts.observe(viewLifecycleOwner) { pagerAdapter.rebuildAll() }
viewModel.hideAmounts.observe(viewLifecycleOwner) { pagerAdapter.rebuildAll() }
(activity as? HomeActivity)?.triggerRefresh()
(activity as? HomeActivity)?.loadAllContacts()
}
private fun attachMediator(pages: List<TabDef>) {
@@ -734,13 +734,75 @@ fun applyNavLabelVisibility() {
val mibJobs = mibLoginIds.mapNotNull { loginId ->
val creds = store.loadMibCredentials(loginId) ?: return@mapNotNull null
loginId to async(Dispatchers.IO) {
val app = application as BasedBankApp
// Prefer in-memory session; fall back to persisted session on restart.
var existingSession = app.mibSessions[loginId]
var existingFlow = app.mibLoginFlows[loginId]
val cachedProfiles = app.mibProfilesMap[loginId]?.takeIf { it.isNotEmpty() }
?: store.loadMibProfiles(loginId)
if (existingSession == null) {
val persisted = store.loadMibSession(loginId)
if (persisted != null && cachedProfiles.isNotEmpty()) {
val flow = MibLoginFlow(CredentialStore(this@HomeActivity))
flow.restoreSession(persisted, creds.username, creds.passwordHash, creds.otpSeed)
existingSession = persisted
existingFlow = flow
app.mibSessions[loginId] = persisted
app.mibLoginFlows[loginId] = flow
app.mibProfilesMap[loginId] = cachedProfiles
}
}
// Try the existing session first — avoids a full re-login on every refresh.
// doRequest() inside fetchAllProfiles() handles 419 (session expired) by
// automatically re-logging in and retrying, so this is safe to call directly.
if (existingSession != null && existingFlow != null && cachedProfiles.isNotEmpty()) {
existingFlow.onSessionRefreshed = { newSession, newProfiles ->
app.mibSessions[loginId] = newSession
app.mibProfilesMap[loginId] = newProfiles
store.saveMibSession(loginId, newSession)
store.saveMibProfiles(loginId, newProfiles)
}
try {
val visibleProfiles = cachedProfiles.filterVisibleProfiles(loginId)
val accounts = existingFlow.fetchAllProfiles(existingSession, visibleProfiles, "mib_${creds.username}")
val freshSession = existingFlow.lastSession ?: existingSession
app.mibSessions[loginId] = freshSession
store.saveMibSession(loginId, freshSession)
if (existingFlow.lastProfiles.isNotEmpty()) {
app.mibProfilesMap[loginId] = existingFlow.lastProfiles
store.saveMibProfiles(loginId, existingFlow.lastProfiles)
}
AccountCache.save(this@HomeActivity, accounts)
return@async accounts
} catch (e: java.io.IOException) {
refreshErrors.add("NO_INTERNET")
return@async AccountCache.load(this@HomeActivity).filter { it.loginTag == "mib_$loginId" }
} catch (e: BankServerException) {
refreshErrors.add("SERVER:${e.bankName}")
return@async AccountCache.load(this@HomeActivity).filter { it.loginTag == "mib_$loginId" }
} catch (_: Exception) {
// Session completely unrecoverable — fall through to full re-login
}
}
// Full re-login: first start with no persisted session, or unrecoverable above
try {
val flow = MibLoginFlow(CredentialStore(this@HomeActivity))
flow.onSessionRefreshed = { newSession, newProfiles ->
app.mibSessions[loginId] = newSession
app.mibProfilesMap[loginId] = newProfiles
store.saveMibSession(loginId, newSession)
store.saveMibProfiles(loginId, newProfiles)
}
val accounts = flow.login(creds.username, creds.passwordHash, creds.otpSeed)
val app = application as BasedBankApp
app.mibSessions[loginId] = flow.lastSession!!
val newSession = flow.lastSession!!
app.mibSessions[loginId] = newSession
app.mibProfilesMap[loginId] = flow.lastProfiles
app.mibLoginFlows[loginId] = flow
store.saveMibSession(loginId, newSession)
store.saveMibProfiles(loginId, flow.lastProfiles)
accounts
} catch (e: java.io.IOException) {