Skip to content

Commit 2c54b9b

Browse files
committed
add connectivity check into saber sync interface
Issue: #541
1 parent bd41f20 commit 2c54b9b

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

Diff for: lib/components/home/syncing_button.dart

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22

33
import 'package:flutter/cupertino.dart';
44
import 'package:flutter/material.dart';
5+
import 'package:saber/components/notifs/snackbar.dart';
56
import 'package:saber/components/theming/adaptive_icon.dart';
67
import 'package:saber/data/nextcloud/saber_syncer.dart';
78
import 'package:saber/data/prefs.dart';
@@ -46,6 +47,11 @@ class _SyncingButtonState extends State<SyncingButton> {
4647
if (mounted) setState(() {});
4748
}
4849

50+
void _snackBarSyncOnlyOverWifi() {
51+
if (!mounted) return;
52+
SnackBarNotification.show(context, message: 'Wifi is not connected, disable "sync only over wifi" in settings to use mobile data.'); // fixme
53+
}
54+
4955
/// Returns a value between 0-1 representing the progress of the sync,
5056
/// or null if we're still refreshing.
5157
double? getPercentage() {
@@ -60,9 +66,14 @@ class _SyncingButtonState extends State<SyncingButton> {
6066
return filesTransferred / (filesTransferred + numPending);
6167
}
6268

63-
void onPressed() {
69+
void onPressed() async {
6470
assert(Prefs.loggedIn);
6571

72+
if (!(await SaberSyncInterface.shouldSync())) {
73+
_snackBarSyncOnlyOverWifi();
74+
return;
75+
}
76+
6677
// Don't refresh if we're already refreshing.
6778
if (syncer.downloader.isRefreshing) return;
6879

Diff for: lib/components/settings/nextcloud_profile.dart

+19-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter/cupertino.dart';
44
import 'package:flutter/material.dart';
55
import 'package:go_router/go_router.dart';
66
import 'package:nextcloud/provisioning_api.dart';
7+
import 'package:saber/components/notifs/snackbar.dart';
78
import 'package:saber/components/theming/adaptive_icon.dart';
89
import 'package:saber/data/file_manager/file_manager.dart';
910
import 'package:saber/data/nextcloud/nextcloud_client_extension.dart';
@@ -52,6 +53,19 @@ class _NextcloudProfileState extends State<NextcloudProfile> {
5253
if (mounted) setState(() {});
5354
}
5455

56+
void _resyncEverything() async {
57+
Prefs.fileSyncResyncEverythingDate.value = DateTime.now();
58+
final allFiles = await FileManager.getAllFiles(includeExtensions: true, includeAssets: true);
59+
for (final file in allFiles) {
60+
syncer.uploader.enqueueRel(file);
61+
}
62+
}
63+
64+
void _snackBarSyncOnlyOverWifi() {
65+
if (!mounted) return;
66+
SnackBarNotification.show(context, message: 'Wifi is not connected, disable "sync only over wifi" in settings to use mobile data.'); // fixme
67+
}
68+
5569
@override
5670
Widget build(BuildContext context) {
5771
final loginStep =
@@ -71,6 +85,7 @@ class _NextcloudProfileState extends State<NextcloudProfile> {
7185
};
7286

7387
var colorScheme = Theme.of(context).colorScheme;
88+
7489
return ListTile(
7590
onTap: () => context.push(RoutePaths.login),
7691
leading: ValueListenableBuilder(
@@ -140,12 +155,11 @@ class _NextcloudProfileState extends State<NextcloudProfile> {
140155
),
141156
tooltip: t.settings.resyncEverything,
142157
onPressed: () async {
143-
Prefs.fileSyncResyncEverythingDate.value = DateTime.now();
144-
final allFiles = await FileManager.getAllFiles(
145-
includeExtensions: true, includeAssets: true);
146-
for (final file in allFiles) {
147-
syncer.uploader.enqueueRel(file);
158+
if (!(await SaberSyncInterface.shouldSync())) {
159+
_snackBarSyncOnlyOverWifi();
160+
return;
148161
}
162+
_resyncEverything();
149163
},
150164
),
151165
],

Diff for: lib/data/nextcloud/saber_syncer.dart

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22
import 'dart:io';
33

44
import 'package:abstract_sync/abstract_sync.dart';
5+
import 'package:connectivity_plus/connectivity_plus.dart';
56
import 'package:encrypt/encrypt.dart';
67
import 'package:flutter/foundation.dart';
78
import 'package:logging/logging.dart';
@@ -22,6 +23,16 @@ class SaberSyncInterface
2223
extends AbstractSyncInterface<SaberSyncFile, File, WebDavFile> {
2324
const SaberSyncInterface();
2425

26+
static Future<bool> shouldSync() async {
27+
if (Prefs.onlySyncOverWifi.value) {
28+
final List<ConnectivityResult> connRes = await Connectivity().checkConnectivity();
29+
if (!connRes.contains(ConnectivityResult.wifi)) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+
2536
static final log = Logger('SaberSyncInterface');
2637

2738
@override

Diff for: lib/main_common.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ void setupBackgroundSync() {
161161
}
162162

163163
@pragma('vm:entry-point')
164-
void doBackgroundSync() {
164+
void doBackgroundSync() async {
165+
if (!(await SaberSyncInterface.shouldSync())) {
166+
return; // FIXME: should we warn user in this case?
167+
}
165168
Workmanager().executeTask((_, __) async {
166169
StrokeOptionsExtension.setDefaults();
167170
Prefs.init();

0 commit comments

Comments
 (0)