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:
@@ -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)")
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user