Skip to content

Commit

Permalink
add connectivity check into saber sync interface
Browse files Browse the repository at this point in the history
Issue: #541
  • Loading branch information
AlphaGergedan committed Nov 7, 2024
1 parent 681949c commit 436b6f9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
13 changes: 12 additions & 1 deletion lib/components/home/syncing_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -46,6 +47,11 @@ class _SyncingButtonState extends State<SyncingButton> {
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() {
Expand All @@ -60,9 +66,14 @@ class _SyncingButtonState extends State<SyncingButton> {
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;

Expand Down
24 changes: 19 additions & 5 deletions lib/components/settings/nextcloud_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -52,6 +53,19 @@ class _NextcloudProfileState extends State<NextcloudProfile> {
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 =
Expand All @@ -71,6 +85,7 @@ class _NextcloudProfileState extends State<NextcloudProfile> {
};

var colorScheme = Theme.of(context).colorScheme;

return ListTile(
onTap: () => context.push(RoutePaths.login),
leading: ValueListenableBuilder(
Expand Down Expand Up @@ -140,12 +155,11 @@ class _NextcloudProfileState extends State<NextcloudProfile> {
),
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();
},
),
],
Expand Down
11 changes: 11 additions & 0 deletions lib/data/nextcloud/saber_syncer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -22,6 +23,16 @@ class SaberSyncInterface
extends AbstractSyncInterface<SaberSyncFile, File, WebDavFile> {
const SaberSyncInterface();

static Future<bool> shouldSync() async {
if (Prefs.onlySyncOverWifi.value) {
final List<ConnectivityResult> connRes = await Connectivity().checkConnectivity();
if (!connRes.contains(ConnectivityResult.wifi)) {
return false;
}
}
return true;
}

static final log = Logger('SaberSyncInterface');

@override
Expand Down
5 changes: 4 additions & 1 deletion lib/main_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 436b6f9

Please sign in to comment.