Skip to content

fix(sync): dispatch API calls to IO dispatcher and resolve sync race - #21

Merged
GothwadTech merged 1 commit into
Bansify:mainfrom
krinry:fix/mail-sync-issue
Nov 23, 2026
Merged

GothwadTech merged 1 commit into
Bansify:mainfrom
krinry:fix/mail-sync-issue

Conversation

@krinry

@krinry krinry commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes the "Could not sync Bansify Mail" error when launching the app or attempting to refresh emails on device.

Root Cause

  1. NetworkOnMainThreadException: MailApiClient.executeRaw() executed OkHttp network calls synchronously on whatever coroutine context called it. In MailViewModel, viewModelScope.launch defaults to Dispatchers.Main.immediate, causing OkHttp's client.newCall(request).execute() to run directly on Android's main thread and trigger android.os.NetworkOnMainThreadException. Because the exception message was null, the UI fell back to "Could not sync Bansify Mail".
  2. Sync Race Condition on Startup: In MailViewModel.init, when activeAccountFlow emitted the active account on startup, refreshCurrentFolder() checked val account = uiState.value.activeAccount ?: return. Since uiState's combination and subscription had not yet evaluated at that exact instant, uiState.value.activeAccount was null and the initial sync was silently aborted while lastSyncedAccount was already marked.
  3. Status Bar Clipping: The error banner in MailNavGraph did not have statusBarsPadding(), causing it to render underneath the status bar / device notch and preventing user interaction with the "Retry" button.

Changes

  • MailApiClient.kt: Wrapped executeRaw in withContext(Dispatchers.IO) so all API network requests and response parsing execute on the I/O thread pool.
  • MailViewModel.kt:
    • refreshCurrentFolder now accepts an optional targetAccount: MailAccountEntity? parameter and falls back to repository.activeAccount().
    • Pass the emitted account directly in activeAccountFlow.collect.
    • Update lastSyncedAccount only on success and reset on failure to allow retries.
    • Surface error code or exception class name if error.message is null or blank.
  • MailNavGraph.kt: Added .statusBarsPadding() to the error banner Surface.

Testing

  • Tested locally on physical device (realme NARZO 70 Turbo 5G): 16 emails successfully fetched from Cloudflare D1 production and displayed in the inbox.
  • All 40 JVM unit tests passed (./gradlew testDebugUnitTest).

- Run OkHttp call execution on Dispatchers.IO in MailApiClient to avoid NetworkOnMainThreadException
- Accept target account and fallback to activeAccount in MailViewModel.refreshCurrentFolder
- Update lastSyncedAccount on success and clear on failure to allow retries
- Apply statusBarsPadding to syncError banner Surface
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@GothwadTech
GothwadTech merged commit 26b18e8 into Bansify:main Nov 23, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants