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
This commit is contained in:
inventory69
2026-01-26 21:42:03 +01:00
parent b70bc4d8f6
commit 0df8282eb4
2 changed files with 37 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.work.CoroutineWorker import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import dev.dettmer.simplenotes.BuildConfig import dev.dettmer.simplenotes.BuildConfig
import dev.dettmer.simplenotes.utils.Constants
import dev.dettmer.simplenotes.utils.Logger import dev.dettmer.simplenotes.utils.Logger
import dev.dettmer.simplenotes.utils.NotificationHelper import dev.dettmer.simplenotes.utils.NotificationHelper
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
@@ -88,6 +89,32 @@ class SyncWorker(
return@withContext Result.success() 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) { if (BuildConfig.DEBUG) {
Logger.d(TAG, "📍 Step 3: Checking server reachability (Pre-Check)") Logger.d(TAG, "📍 Step 3: Checking server reachability (Pre-Check)")
} }

View File

@@ -372,6 +372,16 @@ class NoteEditorViewModel(
return 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 // Check 3: Throttling (5 seconds) to prevent spam
val lastOnSaveSyncTime = prefs.getLong(Constants.PREF_LAST_ON_SAVE_SYNC_TIME, 0) val lastOnSaveSyncTime = prefs.getLong(Constants.PREF_LAST_ON_SAVE_SYNC_TIME, 0)
val now = System.currentTimeMillis() val now = System.currentTimeMillis()