Skip to content

Trigger a Sync Automatically When the App Reconnects to the Backend #23

Description

@TheZupZup

Overview

Context: SettingsScreen has an auto-sync toggle that runs triggerSync() on a 5-minute Timer. The timer runs continuously regardless of whether the backend is reachable. There is no concept of "just reconnected" — the app doesn't know it was previously disconnected.

This issue adds a simple reconnect-triggered sync: when the app detects the backend has come back online after being unreachable, it triggers a sync immediately (rather than waiting up to 5 minutes for the timer).

What needs to be done

  • In AppState, add a _backendReachable boolean (default: true when connected)
  • In AppState, add a checkBackendHealth() method that calls ApiClient.ping()GET /health and updates _backendReachable
  • When an API call fails in AppState (any catch block), set _backendReachable = false and notifyListeners()
  • In the auto-sync timer callback (SettingsScreen._autoSyncTimer), before triggering sync: if _backendReachable was false, call checkBackendHealth() first — if it passes, set _backendReachable = true and immediately trigger sync
  • After a reconnect-triggered sync, also call loadNotebooks() and loadNotes() to refresh the UI with any remote changes

Goal

If the backend was unreachable and then comes back online, the next auto-sync timer tick detects the recovery, syncs immediately, and refreshes the notes list — so the user sees up-to-date content without having to tap "Sync now" manually.

Where to look

  • app/lib/services/app_state.dart — add _backendReachable, checkBackendHealth()
  • app/lib/screens/settings_screen.dart_autoSyncTimer callback
  • app/lib/services/api_client.dartping() method already exists

Edge cases

  • The auto-sync toggle is off: no timer runs, so no reconnect-triggered sync either (that's fine — manual sync button still works)
  • The backend comes back but WebDAV sync is not configured: trigger loadNotes() refresh but skip triggerSync()

Testing steps

Related to: #17, #22, #24, #26

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions