diff options
author | desperateCoder <echotodevnull@gmail.com> | 2019-10-19 20:06:41 +0300 |
---|---|---|
committer | desperateCoder <echotodevnull@gmail.com> | 2019-10-19 20:33:14 +0300 |
commit | c7e2c9b3059b9554261c0b706be8698e03d72ca0 (patch) | |
tree | a44bf5f006b51f8913649ff3355f5d27e05b3c1c | |
parent | 0f31fee9bd74ed937232428c05537f8efa7ce099 (diff) |
#180: enable SyncManager to save only when user tells us to
4 files changed, 46 insertions, 20 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/SyncManager.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/SyncManager.java index 7efea8845..e4d458ac7 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/SyncManager.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/SyncManager.java @@ -34,6 +34,7 @@ import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.DataBaseAdapter import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData; import it.niedermann.nextcloud.deck.persistence.sync.helpers.DataPropagationHelper; import it.niedermann.nextcloud.deck.persistence.sync.helpers.SyncHelper; +import it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.AbstractSyncDataProvider; import it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.ActivityDataProvider; import it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.BoardDataProvider; import it.niedermann.nextcloud.deck.persistence.sync.helpers.providers.CardDataProvider; @@ -416,28 +417,47 @@ public class SyncManager { return liveData; } - public MutableLiveData<FullCard> archiveCard(Card card) { - card.setArchived(true); + public MutableLiveData<FullCard> archiveCard(FullCard card) { + card.getCard().setArchived(true); return updateCard(card); } - public MutableLiveData<FullCard> updateCard(Card card) { + public MutableLiveData<FullCard> updateCard(FullCard card) { MutableLiveData<FullCard> liveData = new MutableLiveData<>(); doAsync(() -> { - FullCard fullCard = dataBaseAdapter.getFullCardByLocalIdDirectly(card.getAccountId(), card.getLocalId()); - if (fullCard == null) { + FullCard fullCardFromDB = dataBaseAdapter.getFullCardByLocalIdDirectly(card.getAccountId(), card.getLocalId()); + if (fullCardFromDB == null) { throw new IllegalArgumentException("card to update does not exist."); } + + List<User> deletedUsers = AbstractSyncDataProvider.findDelta(card.getAssignedUsers(), fullCardFromDB.getAssignedUsers()); + List<User> addedUsers = AbstractSyncDataProvider.findDelta(fullCardFromDB.getAssignedUsers(), card.getAssignedUsers()); + for (User addedUser : addedUsers) { + dataBaseAdapter.createJoinCardWithUser(addedUser.getLocalId(), card.getLocalId(), DBStatus.LOCAL_EDITED); + } + for (User deletedUser : deletedUsers) { + dataBaseAdapter.deleteJoinedUserForCard(card.getLocalId(), deletedUser.getLocalId()); + } + + List<Label> deletedLabels = AbstractSyncDataProvider.findDelta(card.getLabels(), fullCardFromDB.getLabels()); + List<Label> addedLabels = AbstractSyncDataProvider.findDelta(fullCardFromDB.getLabels(), card.getLabels()); + Account account = dataBaseAdapter.getAccountByIdDirectly(card.getAccountId()); - FullStack stack = dataBaseAdapter.getFullStackByLocalIdDirectly(card.getStackId()); + FullStack stack = dataBaseAdapter.getFullStackByLocalIdDirectly(card.getCard().getStackId()); Board board = dataBaseAdapter.getBoardByLocalIdDirectly(stack.getStack().getBoardId()); - fullCard.setCard(card); - new DataPropagationHelper(serverAdapter, dataBaseAdapter).updateEntity(new CardPropagationDataProvider(null, board, stack), fullCard, new IResponseCallback<FullCard>(account) { - @Override - public void onResponse(FullCard response) { - liveData.postValue(response); - } - }); + fullCardFromDB.setCard(card.getCard()); + card.getCard().setStatus(DBStatus.LOCAL_EDITED.getId()); + dataBaseAdapter.updateCard(card.getCard(), false); + + new SyncHelper(serverAdapter, dataBaseAdapter, null) + .setResponseCallback(new IResponseCallback<Boolean>(dataBaseAdapter.getAccountByIdDirectly(card.getAccountId())) { + @Override + public void onResponse(Boolean response) { + // do nothing + } + }).doUpSyncFor(new CardPropagationDataProvider(null, board, stack)); + +// new DataPropagationHelper(serverAdapter, dataBaseAdapter).updateEntity(); }); return liveData; } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AbstractSyncDataProvider.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AbstractSyncDataProvider.java index 65bae2a19..9e06d67c2 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AbstractSyncDataProvider.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/helpers/providers/AbstractSyncDataProvider.java @@ -30,18 +30,24 @@ public abstract class AbstractSyncDataProvider<T extends IRemoteEntity> { // do nothing as a default. } - protected static <T extends IRemoteEntity> List<T> findDelta(List<T> entitiesFromServer, List<T> localEntities){ + /** + * Searches each entry of <code>listB</code> in list <code>listA</code> and returns the missing ones + * @param listA List + * @param listB List + * @return all entries of <code>listB</code> missing in <code>listA</code> + */ + public static <T extends IRemoteEntity> List<T> findDelta(List<T> listA, List<T> listB){ List<T> delta = new ArrayList<>(); - for (T localEntity : localEntities) { + for (T b : listB) { boolean found = false; - for (T remoteEntity : entitiesFromServer) { - if (remoteEntity.getId().equals(localEntity.getId()) && localEntity.getAccountId() == remoteEntity.getAccountId()) { + for (T a : listA) { + if (a.getId().equals(b.getId()) && b.getAccountId() == a.getAccountId()) { found = true; break; } } if (!found){ - delta.add(localEntity); + delta.add(b); } } return delta; diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java index 9c2ab3749..4e60db677 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/EditActivity.java @@ -153,7 +153,7 @@ public class EditActivity extends AppCompatActivity { if (createMode) { observeOnce(syncManager.createFullCard(accountId, boardId, stackId, fullCard), EditActivity.this, (card) -> finish()); } else { - observeOnce(syncManager.updateCard(fullCard.card), EditActivity.this, (card) -> finish()); + observeOnce(syncManager.updateCard(fullCard), EditActivity.this, (card) -> finish()); } } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardAdapter.java index aaf53e5d2..5850ead12 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardAdapter.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/CardAdapter.java @@ -361,7 +361,7 @@ public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder return true; } case R.id.action_card_archive: { - syncManager.archiveCard(card.getCard()); + syncManager.archiveCard(card); return true; } case R.id.action_card_delete: { |