From 00ac1c12db347d7a63c4c44a62f41b373cb84706 Mon Sep 17 00:00:00 2001
From: Dirk Hohndel <dirk@hohndel.org>
Date: Fri, 15 Apr 2016 04:58:09 -0700
Subject: [PATCH] QML UI: fix the save to cloud logic

There were several logical flaws here. Ugh.

Don't save things if there are no unsaved changes, if we haven't
initialized this repository from the cloud or if we are already saving
things.

Then, once we decide that we should save, first always save to the local
cache and then check if we should save to the cloud and do so if
requested.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
---
 mobile-widgets/qmlmanager.cpp | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp
index 4fa32bdd9..95db4ad41 100644
--- a/mobile-widgets/qmlmanager.cpp
+++ b/mobile-widgets/qmlmanager.cpp
@@ -794,21 +794,27 @@ void QMLManager::saveChangesLocal()
 
 void QMLManager::saveChangesCloud(bool forceRemoteSync)
 {
-	if (prefs.git_local_only && !forceRemoteSync)
+	if (!unsaved_changes()) {
+		appendTextToLog("asked to save changes but no unsaved changes");
 		return;
-
-	git_storage_update_progress(true, "start save change to cloud");
+	}
 	if (!loadFromCloud()) {
 		appendTextToLog("Don't save dives without loading from the cloud, first.");
 		return;
 	}
-	bool glo = prefs.git_local_only;
-	// first we need to store any unsaved changes to the local repo
-	saveChangesLocal();
 	if (alreadySaving) {
-		appendTextToLog("save operation in progress already, can't sync with server");
+		appendTextToLog("save operation in progress already");
 		return;
 	}
+	// first we need to store any unsaved changes to the local repo
+	saveChangesLocal();
+
+	// if the user asked not to push to the cloud we are done
+	if (prefs.git_local_only && !forceRemoteSync)
+		return;
+
+	bool glo = prefs.git_local_only;
+	git_storage_update_progress(false, "start save change to cloud");
 	prefs.git_local_only = false;
 	alreadySaving = true;
 	loadDivesWithValidCredentials();
@@ -816,7 +822,6 @@ void QMLManager::saveChangesCloud(bool forceRemoteSync)
 	git_storage_update_progress(false, "finished syncing dive list to cloud server");
 	setAccessingCloud(-1);
 	prefs.git_local_only = glo;
-	alreadySaving = false;
 }
 
 bool QMLManager::undoDelete(int id)