From 863602195256f1cc0ff7aef5ad977ab2f75dfe7c Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Fri, 1 May 2020 11:05:45 +0200 Subject: [PATCH] SyncButton: Show number of unpushed changes Related to #123 --- lib/appstate.dart | 1 + lib/core/git_repo.dart | 8 ++++++++ lib/state_container.dart | 24 ++++++++++++++++++++++++ lib/widgets/sync_button.dart | 24 +++++++++++++++++++----- pubspec.lock | 4 ++-- pubspec.yaml | 2 +- 6 files changed, 55 insertions(+), 8 deletions(-) diff --git a/lib/appstate.dart b/lib/appstate.dart index 17ad0169..68df6429 100644 --- a/lib/appstate.dart +++ b/lib/appstate.dart @@ -25,6 +25,7 @@ class AppState { bool onBoardingCompleted = false; SyncStatus syncStatus = SyncStatus.Unknown; + int numChanges = 0; // // Temporary diff --git a/lib/core/git_repo.dart b/lib/core/git_repo.dart index 6fb84270..2c8b22b8 100644 --- a/lib/core/git_repo.dart +++ b/lib/core/git_repo.dart @@ -169,6 +169,14 @@ class GitNoteRepository { rethrow; } } + + Future numChanges() async { + try { + var repo = await git.GitRepository.load(gitDirPath); + return repo.numChangesToPush(); + } catch (_) {} + return 0; + } } const ignoredMessages = [ diff --git a/lib/state_container.dart b/lib/state_container.dart index 95bc7a5d..f25f6fdd 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -87,6 +87,9 @@ class StateContainer with ChangeNotifier { return _loadLock.synchronized(() async { await appState.notesFolder.loadRecursively(); await _notesCache.buildCache(appState.notesFolder); + + appState.numChanges = await _gitRepo.numChanges(); + notifyListeners(); }); } @@ -113,6 +116,7 @@ class StateContainer with ChangeNotifier { Log.d("Synced!"); appState.syncStatus = SyncStatus.Done; + appState.numChanges = 0; notifyListeners(); } catch (e, stacktrace) { Log.d("Failed to Sync"); @@ -148,6 +152,8 @@ class StateContainer with ChangeNotifier { _gitRepo.addFolder(newFolder).then((NoteRepoResult _) { _syncNotes(); + appState.numChanges += 1; + notifyListeners(); }); }); } @@ -161,6 +167,8 @@ class StateContainer with ChangeNotifier { folder.parentFS.removeFolder(folder); _gitRepo.removeFolder(folder).then((NoteRepoResult _) { _syncNotes(); + appState.numChanges += 1; + notifyListeners(); }); }); } @@ -177,6 +185,8 @@ class StateContainer with ChangeNotifier { .renameFolder(oldFolderPath, folder.folderPath) .then((NoteRepoResult _) { _syncNotes(); + appState.numChanges += 1; + notifyListeners(); }); }); } @@ -190,6 +200,8 @@ class StateContainer with ChangeNotifier { _gitRepo.renameNote(oldNotePath, note.filePath).then((NoteRepoResult _) { _syncNotes(); + appState.numChanges += 1; + notifyListeners(); }); }); } @@ -206,6 +218,8 @@ class StateContainer with ChangeNotifier { _gitRepo.moveNote(oldNotePath, note.filePath).then((NoteRepoResult _) { _syncNotes(); + appState.numChanges += 1; + notifyListeners(); }); }); } @@ -219,6 +233,8 @@ class StateContainer with ChangeNotifier { note.updateModified(); _gitRepo.addNote(note).then((NoteRepoResult _) { _syncNotes(); + appState.numChanges += 1; + notifyListeners(); }); }); } @@ -230,6 +246,8 @@ class StateContainer with ChangeNotifier { // FIXME: What if the Note hasn't yet been saved? note.parent.remove(note); _gitRepo.removeNote(note).then((NoteRepoResult _) async { + appState.numChanges += 1; + notifyListeners(); // FIXME: Is there a way of figuring this amount dynamically? // The '4 seconds' is taken from snack_bar.dart -> _kSnackBarDisplayDuration // We wait an aritfical amount of time, so that the user has a change to undo @@ -247,6 +265,8 @@ class StateContainer with ChangeNotifier { note.parent.add(note); _gitRepo.resetLastCommit().then((NoteRepoResult _) { _syncNotes(); + appState.numChanges -= 1; + notifyListeners(); }); }); } @@ -259,6 +279,8 @@ class StateContainer with ChangeNotifier { note.updateModified(); _gitRepo.updateNote(note).then((NoteRepoResult _) { _syncNotes(); + appState.numChanges += 1; + notifyListeners(); }); }); } @@ -274,6 +296,8 @@ class StateContainer with ChangeNotifier { await config.saveToFS(); _gitRepo.addFolderConfig(config).then((NoteRepoResult _) { _syncNotes(); + appState.numChanges += 1; + notifyListeners(); }); }); } diff --git a/lib/widgets/sync_button.dart b/lib/widgets/sync_button.dart index 3e9fd488..eefcb785 100644 --- a/lib/widgets/sync_button.dart +++ b/lib/widgets/sync_button.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:connectivity/connectivity.dart'; import 'package:git_bindings/git_bindings.dart'; +import 'package:badges/badges.dart'; import 'package:gitjournal/appstate.dart'; import 'package:gitjournal/state_container.dart'; @@ -60,11 +61,24 @@ class _SyncButtonState extends State { ); } - return IconButton( - icon: Icon(_syncStatusIcon()), - onPressed: () async { - _syncRepo(); - }, + var theme = Theme.of(context); + var darkMode = theme.brightness == Brightness.dark; + var style = theme.textTheme.caption.copyWith( + fontSize: 6.0, + color: darkMode ? Colors.black : Colors.white, + ); + + return Badge( + badgeContent: Text(appState.numChanges.toString(), style: style), + showBadge: appState.numChanges != 0, + badgeColor: theme.iconTheme.color, + position: BadgePosition.topRight(top: 10.0, right: 4.0), + child: IconButton( + icon: Icon(_syncStatusIcon()), + onPressed: () async { + _syncRepo(); + }, + ), ); } diff --git a/pubspec.lock b/pubspec.lock index 257a0062..83902105 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -56,7 +56,7 @@ packages: name: badges url: "https://pub.dartlang.org" source: hosted - version: "0.0.6" + version: "1.1.1" benchmark_harness: dependency: "direct dev" description: @@ -146,7 +146,7 @@ packages: description: path: "." ref: HEAD - resolved-ref: "4c530f3a63b0de22d62ce854da22f2db8f0209ce" + resolved-ref: "27fe7ee9b5713c72dd7a3db3018ff16e6b06945b" url: "https://github.com/GitJournal/dart_git.git" source: git version: "0.0.1" diff --git a/pubspec.yaml b/pubspec.yaml index 2c2e3cd0..19e91b6f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ dependencies: dots_indicator: ^0.0.3 package_info: ^0.4.0+13 http: ^0.12.0+1 - badges: ^0.0.6 + badges: ^1.1.1 share: ^0.6.3+5 launch_review: ^2.0.0 device_info: ^0.4.1+4