Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat 541 add a setting to only sync over wifi #1368

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 25 additions & 0 deletions lib/components/notifs/snackbar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';

class SnackBarNotification extends StatelessWidget {
final String message;

const SnackBarNotification({
super.key,
required this.message,
});

@override
Widget build(BuildContext context) {
return SnackBar(
content: Text(message),
);
}

static void show(BuildContext context, {required String message}) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
),
);
}
}
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
2 changes: 2 additions & 0 deletions lib/data/prefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ abstract class Prefs {

static late final PlainPref<Uint8List?> pfp;
static late final PlainPref<bool> syncInBackground;
static late final PlainPref<bool> onlySyncOverWifi;

static late final PlainPref<ThemeMode> appTheme;

Expand Down Expand Up @@ -168,6 +169,7 @@ abstract class Prefs {

pfp = PlainPref('pfp', null);
syncInBackground = PlainPref('syncInBackground', true);
onlySyncOverWifi = PlainPref('onlySyncOverWifi', true);

appTheme = PlainPref('appTheme', ThemeMode.system);
platform = PlainPref('platform', defaultTargetPlatform);
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
5 changes: 2 additions & 3 deletions lib/pages/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:saber/components/canvas/canvas_preview.dart';
import 'package:saber/components/canvas/image/editor_image.dart';
import 'package:saber/components/canvas/save_indicator.dart';
import 'package:saber/components/navbar/responsive_navbar.dart';
import 'package:saber/components/notifs/snackbar.dart';
import 'package:saber/components/theming/adaptive_alert_dialog.dart';
import 'package:saber/components/theming/adaptive_icon.dart';
import 'package:saber/components/theming/dynamic_material_app.dart';
Expand Down Expand Up @@ -1722,9 +1723,7 @@ class EditorState extends State<Editor> {

void snackBarNeedsToSaveBeforeExiting() {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(t.editor.needsToSaveBeforeExiting),
));
SnackBarNotification.show(context, message: t.editor.needsToSaveBeforeExiting);
}

Widget bottomSheet(BuildContext context) {
Expand Down
9 changes: 9 additions & 0 deletions lib/pages/home/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,15 @@ class _SettingsPageState extends State<SettingsPage> {
},
pref: Prefs.hyperlegibleFont,
),
SettingsSwitch(
title: 'Sync notes only over WiFi',
subtitle: 'If disabled mobile data may be used to sync notes',
iconBuilder: (i) => switch (Prefs.platform.value) {
TargetPlatform.iOS || TargetPlatform.macOS => Icons.wifi_rounded,
_ => Icons.wifi_sharp,
},
pref: Prefs.onlySyncOverWifi,
),
SettingsSubtitle(subtitle: t.settings.prefCategories.writing),
SettingsSwitch(
title: t.settings.prefLabels.preferGreyscale,
Expand Down
8 changes: 6 additions & 2 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import FlutterMacOS
import Foundation

import audioplayers_darwin
import connectivity_plus
import desktop_webview_window
import device_info_plus
import dynamic_color
Expand All @@ -14,7 +15,8 @@ import flutter_web_auth_2
import irondash_engine_context
import path_provider_foundation
import printing
import screen_retriever
import quill_native_bridge_macos
import screen_retriever_macos
import share_plus
import shared_preferences_foundation
import super_native_extensions
Expand All @@ -24,6 +26,7 @@ import window_to_front

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
ConnectivityPlusPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlusPlugin"))
DesktopWebviewWindowPlugin.register(with: registry.registrar(forPlugin: "DesktopWebviewWindowPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
DynamicColorPlugin.register(with: registry.registrar(forPlugin: "DynamicColorPlugin"))
Expand All @@ -32,7 +35,8 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
IrondashEngineContextPlugin.register(with: registry.registrar(forPlugin: "IrondashEngineContextPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
PrintingPlugin.register(with: registry.registrar(forPlugin: "PrintingPlugin"))
ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin"))
QuillNativeBridgePlugin.register(with: registry.registrar(forPlugin: "QuillNativeBridgePlugin"))
ScreenRetrieverMacosPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverMacosPlugin"))
SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SuperNativeExtensionsPlugin.register(with: registry.registrar(forPlugin: "SuperNativeExtensionsPlugin"))
Expand Down
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.18.0"
connectivity_plus:
dependency: "direct main"
description:
name: connectivity_plus
sha256: "876849631b0c7dc20f8b471a2a03142841b482438e3b707955464f5ffca3e4c3"
url: "https://pub.dev"
source: hosted
version: "6.1.0"
connectivity_plus_platform_interface:
dependency: transitive
description:
name: connectivity_plus_platform_interface
sha256: "42657c1715d48b167930d5f34d00222ac100475f73d10162ddf43e714932f204"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -917,6 +933,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "8.1.0"
nm:
dependency: transitive
description:
name: nm
sha256: "2c9aae4127bdc8993206464fcc063611e0e36e72018696cd9631023a31b24254"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
one_dollar_unistroke_recognizer:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ dependencies:
args: ^2.5.0

flutter_web_auth_2: ^4.0.0
connectivity_plus: ^6.0.5

pdfrx: ^1.0.85

Expand Down
9 changes: 6 additions & 3 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
#include "generated_plugin_registrant.h"

#include <audioplayers_windows/audioplayers_windows_plugin.h>
#include <connectivity_plus/connectivity_plus_windows_plugin.h>
#include <desktop_webview_window/desktop_webview_window_plugin.h>
#include <dynamic_color/dynamic_color_plugin_c_api.h>
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
#include <irondash_engine_context/irondash_engine_context_plugin_c_api.h>
#include <permission_handler_windows/permission_handler_windows_plugin.h>
#include <printing/printing_plugin.h>
#include <screen_retriever/screen_retriever_plugin.h>
#include <screen_retriever_windows/screen_retriever_windows_plugin_c_api.h>
#include <share_plus/share_plus_windows_plugin_c_api.h>
#include <super_native_extensions/super_native_extensions_plugin_c_api.h>
#include <url_launcher_windows/url_launcher_windows.h>
Expand All @@ -23,6 +24,8 @@
void RegisterPlugins(flutter::PluginRegistry* registry) {
AudioplayersWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("AudioplayersWindowsPlugin"));
ConnectivityPlusWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("ConnectivityPlusWindowsPlugin"));
DesktopWebviewWindowPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DesktopWebviewWindowPlugin"));
DynamicColorPluginCApiRegisterWithRegistrar(
Expand All @@ -35,8 +38,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
PrintingPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PrintingPlugin"));
ScreenRetrieverPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("ScreenRetrieverPlugin"));
ScreenRetrieverWindowsPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("ScreenRetrieverWindowsPluginCApi"));
SharePlusWindowsPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("SharePlusWindowsPluginCApi"));
SuperNativeExtensionsPluginCApiRegisterWithRegistrar(
Expand Down
3 changes: 2 additions & 1 deletion windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

list(APPEND FLUTTER_PLUGIN_LIST
audioplayers_windows
connectivity_plus
desktop_webview_window
dynamic_color
flutter_secure_storage_windows
irondash_engine_context
permission_handler_windows
printing
screen_retriever
screen_retriever_windows
share_plus
super_native_extensions
url_launcher_windows
Expand Down
Loading