Skip to content

Commit

Permalink
Merge pull request #183 from Pavel401/sync-update
Browse files Browse the repository at this point in the history
Added connectivity check and sync progress indicator
  • Loading branch information
Pavel401 authored Sep 12, 2023
2 parents 0e26d51 + d9563ba commit 6e00df2
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/drawer/nav_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class _NavDrawerState extends State<NavDrawer> {
color: AppSettings.isDarkMode ? Colors.white : Colors.black,
),
onTap: () {
storageWidget.synchronize(context);
Navigator.pop(context);
storageWidget.synchronize(context, true);
},
title: const Text("Refresh"),
),
Expand Down
76 changes: 64 additions & 12 deletions lib/model/storage/storage_widget.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// ignore_for_file: use_build_context_synchronously

import 'dart:collection';
import 'dart:io';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';

import 'package:loggy/loggy.dart';
Expand Down Expand Up @@ -241,18 +244,67 @@ class _StorageWidgetState extends State<StorageWidget> {
setState(() {});
}

Future<void> synchronize(BuildContext context) async {
Future<void> synchronize(BuildContext context, bool isDialogNeeded) async {
try {
var header = await storage.home.synchronize(await client());
_refreshTasks();
pendingTags = _pendingTags();
projects = _projects();
setState(() {});
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('${header['code']}: ${header['status']}'),
));
// ignore: avoid_catches_without_on_clauses
final connectivityResult = await Connectivity().checkConnectivity();
if (connectivityResult == ConnectivityResult.none) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'You are not connected to the internet. Please check your network connection.'),
),
);
} else {
if (isDialogNeeded) {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return Dialog(
elevation: 5,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Container(
padding: const EdgeInsets.all(16.0),
child: const Column(
mainAxisSize: MainAxisSize.min,
children: [
CircularProgressIndicator(),
SizedBox(height: 16.0),
Text(
"Syncing",
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8.0),
Text("Please wait..."),
],
),
),
);
},
);
}

var header = await storage.home.synchronize(await client());
_refreshTasks();
pendingTags = _pendingTags();
projects = _projects();
setState(() {});

if (isDialogNeeded) {
Navigator.pop(context);
}

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('${header['code']}: ${header['status']}'),
),
);
}
} catch (e, trace) {
logError(e, trace);
}
Expand Down Expand Up @@ -412,7 +464,7 @@ class InheritedStorage extends InheritedModel<String> {
final Set<String> selectedTags;
final Task Function(String) getTask;
final void Function(Task) mergeTask;
final void Function(BuildContext) synchronize;
final void Function(BuildContext, bool) synchronize;
final void Function() togglePendingFilter;
final void Function(String) toggleProjectFilter;
final void Function() toggleTagUnion;
Expand Down
4 changes: 2 additions & 2 deletions lib/views/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class _HomePageState extends State<HomePage> {

if (value) {
storageWidget = StorageWidget.of(context);
storageWidget.synchronize(context);
storageWidget.synchronize(context, false);
} else {}
}

Expand Down Expand Up @@ -129,7 +129,7 @@ class _HomePageState extends State<HomePage> {
Builder(
builder: (context) => IconButton(
icon: const Icon(Icons.refresh, color: Colors.white),
onPressed: () => storageWidget.synchronize(context),
onPressed: () => storageWidget.synchronize(context, true),
),
),
Builder(
Expand Down
Loading

0 comments on commit 6e00df2

Please sign in to comment.