diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-04-27 14:06:34 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-04-27 14:06:34 +0300 |
commit | eeab776d3b674efd63828930e2a38375f35d8dd8 (patch) | |
tree | f159a059cb6e2bd717966ec17b0bc0df67aafd77 /app | |
parent | 5c04619df984c7afaf632d740392b1df17bae11d (diff) | |
parent | 2fd3a741f6216f9f8717f4db2ab77e40353d86a1 (diff) |
Merge branch '421-delete-list-card-counter'
Diffstat (limited to 'app')
5 files changed, 35 insertions, 11 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 97daca151..0552aeb13 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 @@ -1150,6 +1150,12 @@ public class SyncManager { } } + /** + * FIXME clean up on error + * When uploading the exact same attachment 2 times to the same card, the server starts burning and gets mad and returns status 500 + * The problem is, that the attachment is still in our local database and everytime one tries to sync, the log is spammed with 500 errors + * Also this leads to the attachment being present in the card forever with a DBStatus.LOCAL_EDITED + */ public WrappedLiveData<Attachment> addAttachmentToCard(long accountId, long localCardId, @NonNull String mimeType, @NonNull File file) { WrappedLiveData<Attachment> liveData = new WrappedLiveData<>(); doAsync(() -> { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentAdapter.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentAdapter.java index aa7e44ecc..28e473bf4 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentAdapter.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentAdapter.java @@ -199,12 +199,8 @@ public class CardAttachmentAdapter extends RecyclerView.Adapter<AttachmentViewHo } public void removeAttachment(Attachment a) { - for (int i = 0; i < this.attachments.size(); i++) { - if (this.attachments.get(i).equals(a)) { - this.attachments.remove(i); - notifyItemRemoved(i); - return; - } - } + final int index = this.attachments.indexOf(a); + this.attachments.remove(a); + notifyItemRemoved(index); } } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java index 6dd53c1ea..95089e4de 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/card/attachments/CardAttachmentsFragment.java @@ -20,6 +20,8 @@ import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import com.google.android.material.snackbar.Snackbar; + import java.io.File; import java.util.Date; import java.util.List; @@ -31,10 +33,12 @@ import it.niedermann.nextcloud.deck.databinding.FragmentCardEditTabAttachmentsBi import it.niedermann.nextcloud.deck.model.Attachment; import it.niedermann.nextcloud.deck.model.enums.DBStatus; import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; +import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData; import it.niedermann.nextcloud.deck.ui.branding.BrandedFragment; import it.niedermann.nextcloud.deck.ui.card.EditCardViewModel; import it.niedermann.nextcloud.deck.util.FileUtils; +import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce; import static it.niedermann.nextcloud.deck.ui.branding.BrandedActivity.applyBrandToFAB; import static it.niedermann.nextcloud.deck.ui.card.attachments.CardAttachmentAdapter.VIEW_TYPE_DEFAULT; import static it.niedermann.nextcloud.deck.ui.card.attachments.CardAttachmentAdapter.VIEW_TYPE_IMAGE; @@ -158,6 +162,13 @@ public class CardAttachmentsFragment extends BrandedFragment implements Attachme DeckLog.warn("path to file is null"); return; } + for (Attachment existingAttachment : viewModel.getFullCard().getAttachments()) { + final String existingPath = existingAttachment.getLocalPath(); + if (existingPath != null && existingPath.equals(path)) { + Snackbar.make(binding.coordinatorLayout, R.string.attachment_already_exists, Snackbar.LENGTH_LONG).show(); + return; + } + } final File uploadFile = new File(path); final Date now = new Date(); final Attachment a = new Attachment(); @@ -173,7 +184,19 @@ public class CardAttachmentsFragment extends BrandedFragment implements Attachme viewModel.getFullCard().getAttachments().add(a); adapter.addAttachment(a); if (!viewModel.isCreateMode()) { - syncManager.addAttachmentToCard(viewModel.getAccount().getId(), viewModel.getFullCard().getLocalId(), Attachment.getMimetypeForUri(getContext(), uri), uploadFile); + WrappedLiveData<Attachment> liveData = syncManager.addAttachmentToCard(viewModel.getAccount().getId(), viewModel.getFullCard().getLocalId(), Attachment.getMimetypeForUri(getContext(), uri), uploadFile); + observeOnce(liveData, getViewLifecycleOwner(), (next) -> { + if (liveData.hasError()) { + viewModel.getFullCard().getAttachments().remove(a); + adapter.removeAttachment(a); + Snackbar.make(binding.coordinatorLayout, R.string.attachment_already_exists, Snackbar.LENGTH_LONG).show(); + } else { + viewModel.getFullCard().getAttachments().remove(a); + adapter.removeAttachment(a); + viewModel.getFullCard().getAttachments().add(next); + adapter.addAttachment(next); + } + }); } updateEmptyContentView(); } diff --git a/app/src/main/res/layout/item_attachment_default.xml b/app/src/main/res/layout/item_attachment_default.xml index a8cc154e3..da3dfc831 100644 --- a/app/src/main/res/layout/item_attachment_default.xml +++ b/app/src/main/res/layout/item_attachment_default.xml @@ -38,9 +38,7 @@ android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" - android:hint="Filename" - android:textSize="@dimen/text_size_attachments" - tools:ignore="HardcodedText" /> + android:textSize="@dimen/text_size_attachments" /> <LinearLayout android:layout_width="wrap_content" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c568079a1..79832bdf4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -208,4 +208,5 @@ <string name="archived_cards">Archived cards</string> <string name="action_card_dearchive">Undo card archiving</string> <string name="action_archived_cards">Browse archived cards</string> + <string name="attachment_already_exists">Attachment already exists</string> </resources> |