From 436b6f9d48e04c4c33481ed7c6b9a87d63788e7b Mon Sep 17 00:00:00 2001 From: Atamert Rahma Date: Fri, 11 Oct 2024 12:23:14 +0200 Subject: [PATCH] add connectivity check into saber sync interface Issue: #541 --- lib/components/home/syncing_button.dart | 13 +++++++++- .../settings/nextcloud_profile.dart | 24 +++++++++++++++---- lib/data/nextcloud/saber_syncer.dart | 11 +++++++++ lib/main_common.dart | 5 +++- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/lib/components/home/syncing_button.dart b/lib/components/home/syncing_button.dart index bfd58b360..64e313482 100644 --- a/lib/components/home/syncing_button.dart +++ b/lib/components/home/syncing_button.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:saber/components/notifs/snackbar.dart'; import 'package:saber/components/theming/adaptive_icon.dart'; import 'package:saber/data/nextcloud/saber_syncer.dart'; import 'package:saber/data/prefs.dart'; @@ -46,6 +47,11 @@ class _SyncingButtonState extends State { if (mounted) setState(() {}); } + void _snackBarSyncOnlyOverWifi() { + if (!mounted) return; + SnackBarNotification.show(context, message: 'Wifi is not connected, disable "sync only over wifi" in settings to use mobile data.'); // fixme + } + /// Returns a value between 0-1 representing the progress of the sync, /// or null if we're still refreshing. double? getPercentage() { @@ -60,9 +66,14 @@ class _SyncingButtonState extends State { return filesTransferred / (filesTransferred + numPending); } - void onPressed() { + void onPressed() async { assert(Prefs.loggedIn); + if (!(await SaberSyncInterface.shouldSync())) { + _snackBarSyncOnlyOverWifi(); + return; + } + // Don't refresh if we're already refreshing. if (syncer.downloader.isRefreshing) return; diff --git a/lib/components/settings/nextcloud_profile.dart b/lib/components/settings/nextcloud_profile.dart index 653633c8a..dafa00ca8 100644 --- a/lib/components/settings/nextcloud_profile.dart +++ b/lib/components/settings/nextcloud_profile.dart @@ -4,6 +4,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:nextcloud/provisioning_api.dart'; +import 'package:saber/components/notifs/snackbar.dart'; import 'package:saber/components/theming/adaptive_icon.dart'; import 'package:saber/data/file_manager/file_manager.dart'; import 'package:saber/data/nextcloud/nextcloud_client_extension.dart'; @@ -52,6 +53,19 @@ class _NextcloudProfileState extends State { if (mounted) setState(() {}); } + void _resyncEverything() async { + Prefs.fileSyncResyncEverythingDate.value = DateTime.now(); + final allFiles = await FileManager.getAllFiles(includeExtensions: true, includeAssets: true); + for (final file in allFiles) { + syncer.uploader.enqueueRel(file); + } + } + + void _snackBarSyncOnlyOverWifi() { + if (!mounted) return; + SnackBarNotification.show(context, message: 'Wifi is not connected, disable "sync only over wifi" in settings to use mobile data.'); // fixme + } + @override Widget build(BuildContext context) { final loginStep = @@ -71,6 +85,7 @@ class _NextcloudProfileState extends State { }; var colorScheme = Theme.of(context).colorScheme; + return ListTile( onTap: () => context.push(RoutePaths.login), leading: ValueListenableBuilder( @@ -140,12 +155,11 @@ class _NextcloudProfileState extends State { ), tooltip: t.settings.resyncEverything, onPressed: () async { - Prefs.fileSyncResyncEverythingDate.value = DateTime.now(); - final allFiles = await FileManager.getAllFiles( - includeExtensions: true, includeAssets: true); - for (final file in allFiles) { - syncer.uploader.enqueueRel(file); + if (!(await SaberSyncInterface.shouldSync())) { + _snackBarSyncOnlyOverWifi(); + return; } + _resyncEverything(); }, ), ], diff --git a/lib/data/nextcloud/saber_syncer.dart b/lib/data/nextcloud/saber_syncer.dart index 1ca8315ab..8784df32e 100644 --- a/lib/data/nextcloud/saber_syncer.dart +++ b/lib/data/nextcloud/saber_syncer.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'dart:io'; import 'package:abstract_sync/abstract_sync.dart'; +import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:encrypt/encrypt.dart'; import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart'; @@ -22,6 +23,16 @@ class SaberSyncInterface extends AbstractSyncInterface { const SaberSyncInterface(); + static Future shouldSync() async { + if (Prefs.onlySyncOverWifi.value) { + final List connRes = await Connectivity().checkConnectivity(); + if (!connRes.contains(ConnectivityResult.wifi)) { + return false; + } + } + return true; + } + static final log = Logger('SaberSyncInterface'); @override diff --git a/lib/main_common.dart b/lib/main_common.dart index fb3affc93..b1351f8cc 100644 --- a/lib/main_common.dart +++ b/lib/main_common.dart @@ -159,7 +159,10 @@ void setupBackgroundSync() { } @pragma('vm:entry-point') -void doBackgroundSync() { +void doBackgroundSync() async { + if (!(await SaberSyncInterface.shouldSync())) { + return; // FIXME: should we warn user in this case? + } Workmanager().executeTask((_, __) async { StrokeOptionsExtension.setDefaults(); Prefs.init();