From 0df8282eb43d5c4884a434df86d23ec971c76a7b Mon Sep 17 00:00:00 2001 From: inventory69 Date: Mon, 26 Jan 2026 21:42:03 +0100 Subject: [PATCH] fix(sync): Add WiFi-only check for onSave and background sync - SyncWorker: Add central WiFi-only guard before all sync operations - NoteEditorViewModel: Add WiFi-only check before onSave sync trigger - Prevents notes from syncing over 5G/mobile when WiFi-only is enabled - Fixes: onSave sync ignored WiFi-only setting completely --- .../dettmer/simplenotes/sync/SyncWorker.kt | 27 +++++++++++++++++++ .../ui/editor/NoteEditorViewModel.kt | 10 +++++++ 2 files changed, 37 insertions(+) diff --git a/android/app/src/main/java/dev/dettmer/simplenotes/sync/SyncWorker.kt b/android/app/src/main/java/dev/dettmer/simplenotes/sync/SyncWorker.kt index 889112a..f30611d 100644 --- a/android/app/src/main/java/dev/dettmer/simplenotes/sync/SyncWorker.kt +++ b/android/app/src/main/java/dev/dettmer/simplenotes/sync/SyncWorker.kt @@ -9,6 +9,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.work.CoroutineWorker import androidx.work.WorkerParameters import dev.dettmer.simplenotes.BuildConfig +import dev.dettmer.simplenotes.utils.Constants import dev.dettmer.simplenotes.utils.Logger import dev.dettmer.simplenotes.utils.NotificationHelper import kotlinx.coroutines.CancellationException @@ -88,6 +89,32 @@ class SyncWorker( return@withContext Result.success() } + if (BuildConfig.DEBUG) { + Logger.d(TAG, "πŸ“ Step 2.5: Checking WiFi-only setting") + } + + // πŸ†• v1.7.0: WiFi-Only Check (zentral fΓΌr alle Sync-Arten) + val prefs = applicationContext.getSharedPreferences( + Constants.PREFS_NAME, + Context.MODE_PRIVATE + ) + val wifiOnlySync = prefs.getBoolean( + Constants.KEY_WIFI_ONLY_SYNC, + Constants.DEFAULT_WIFI_ONLY_SYNC + ) + + if (wifiOnlySync && !syncService.isOnWiFi()) { + Logger.d(TAG, "⏭️ WiFi-only mode enabled, but not on WiFi - skipping sync") + Logger.d(TAG, " User can still manually sync when on WiFi") + + if (BuildConfig.DEBUG) { + Logger.d(TAG, "βœ… SyncWorker.doWork() SUCCESS (WiFi-only skip)") + Logger.d(TAG, "═══════════════════════════════════════") + } + + return@withContext Result.success() + } + if (BuildConfig.DEBUG) { Logger.d(TAG, "πŸ“ Step 3: Checking server reachability (Pre-Check)") } diff --git a/android/app/src/main/java/dev/dettmer/simplenotes/ui/editor/NoteEditorViewModel.kt b/android/app/src/main/java/dev/dettmer/simplenotes/ui/editor/NoteEditorViewModel.kt index 77cf761..40da751 100644 --- a/android/app/src/main/java/dev/dettmer/simplenotes/ui/editor/NoteEditorViewModel.kt +++ b/android/app/src/main/java/dev/dettmer/simplenotes/ui/editor/NoteEditorViewModel.kt @@ -372,6 +372,16 @@ class NoteEditorViewModel( return } + // πŸ†• v1.7.0: WiFi-Only Check + val wifiOnlySync = prefs.getBoolean(Constants.KEY_WIFI_ONLY_SYNC, Constants.DEFAULT_WIFI_ONLY_SYNC) + if (wifiOnlySync) { + val syncService = WebDavSyncService(getApplication()) + if (!syncService.isOnWiFi()) { + Logger.d(TAG, "⏭️ onSave sync blocked: WiFi-only mode, not on WiFi") + return + } + } + // Check 3: Throttling (5 seconds) to prevent spam val lastOnSaveSyncTime = prefs.getLong(Constants.PREF_LAST_ON_SAVE_SYNC_TIME, 0) val now = System.currentTimeMillis()