QML UI: implement undelete

This code is very similar to the undo code in the desktop UI, but
untangling that from the desktop seemed massive overkill; we don't have
lists of dives to delete and undelete here - so this is actually much
simpler and easier to maintain (I hope).

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2016-02-29 06:53:26 -08:00
parent e39e9eee3b
commit abab031ed2
3 changed files with 59 additions and 3 deletions

View file

@ -63,11 +63,12 @@ MobileComponents.Page {
text: "Delete dive"
iconName: "trash-empty"
onTriggered: {
manager.deleteDive(diveDetailsListView.currentItem.modelData.dive.id)
var deletedId = diveDetailsListView.currentItem.modelData.dive.id
manager.deleteDive(deletedId)
var notification = notificationComponent.createObject(contentItem.parent);
notification.showNotification("Dive deleted", 3000, "Undo",
function() {
print("now I need to undo!")
manager.undoDelete(deletedId)
});
contextDrawer.close()
stackView.pop()

View file

@ -43,7 +43,9 @@ extern "C" int gitProgressCB(int percent)
QMLManager::QMLManager() : m_locationServiceEnabled(false),
m_verboseEnabled(false),
m_credentialStatus(UNKNOWN),
reply(0)
reply(0),
deletedDive(0),
deletedTrip(0)
{
m_instance = this;
appendTextToLog(getUserAgent());
@ -610,6 +612,34 @@ void QMLManager::saveChanges()
mark_divelist_changed(false);
}
void QMLManager::undoDelete(int id)
{
if (!deletedDive || deletedDive->id != id) {
qDebug() << "can't find the deleted dive";
return;
}
if (deletedTrip)
insert_trip(&deletedTrip);
if (deletedDive->divetrip) {
struct dive_trip *trip = deletedDive->divetrip;
tripflag_t tripflag = deletedDive->tripflag; // this gets overwritten in add_dive_to_trip()
deletedDive->divetrip = NULL;
deletedDive->next = NULL;
deletedDive->pprev = NULL;
add_dive_to_trip(deletedDive, trip);
deletedDive->tripflag = tripflag;
}
record_dive(deletedDive);
DiveListModel::instance()->addDive(deletedDive);
prefs.cloud_background_sync = false;
prefs.git_local_only = true;
saveChanges();
prefs.cloud_background_sync = true;
prefs.git_local_only = false;
deletedDive = NULL;
deletedTrip = NULL;
}
void QMLManager::deleteDive(int id)
{
struct dive *d = get_dive_by_uniq_id(id);
@ -617,6 +647,28 @@ void QMLManager::deleteDive(int id)
qDebug() << "oops, trying to delete non-existing dive";
return;
}
// clean up (or create) the storage for the deleted dive and trip (if applicable)
if (!deletedDive)
deletedDive = alloc_dive();
else
clear_dive(deletedDive);
copy_dive(d, deletedDive);
if (!deletedTrip) {
deletedTrip = (struct dive_trip *)calloc(1, sizeof(struct dive_trip));
} else {
free(deletedTrip->location);
free(deletedTrip->notes);
memset(deletedTrip, 0, sizeof(struct dive_trip));
}
// if this is the last dive in that trip, remember the trip as well
if (d->divetrip && d->divetrip->nrdives == 1) {
deletedTrip = (struct dive_trip *)calloc(1, sizeof(struct dive_trip));
*deletedTrip = *d->divetrip;
deletedTrip->location = copy_string(d->divetrip->location);
deletedTrip->notes = copy_string(d->divetrip->notes);
deletedTrip->nrdives = 0;
deletedDive->divetrip = deletedTrip;
}
DiveListModel::instance()->removeDiveById(id);
delete_single_dive(get_idx_by_uniq_id(id));
prefs.cloud_background_sync = false;

View file

@ -92,6 +92,7 @@ public slots:
void saveChanges();
void deleteDive(int id);
void undoDelete(int id);
QString addDive();
void addDiveAborted(int id);
void applyGpsData();
@ -125,6 +126,8 @@ private:
static QMLManager *m_instance;
QNetworkReply *reply;
QNetworkRequest request;
struct dive *deletedDive;
struct dive_trip *deletedTrip;
credentialStatus_t m_credentialStatus;