Skip to content

Commit

Permalink
Use LiveData for empty content view
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Niedermann <info@niedermann.it>
  • Loading branch information
stefan-niedermann committed Nov 1, 2020
1 parent 7eb093c commit ea7895c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import androidx.annotation.Nullable;
import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
Expand All @@ -28,6 +30,7 @@
import it.niedermann.nextcloud.deck.ui.branding.Branded;
import it.niedermann.nextcloud.deck.util.MimeTypeUtil;

import static androidx.lifecycle.Transformations.distinctUntilChanged;
import static androidx.recyclerview.widget.RecyclerView.NO_ID;
import static it.niedermann.nextcloud.deck.util.AttachmentUtil.openAttachmentInBrowser;

Expand All @@ -37,6 +40,9 @@ public class CardAttachmentAdapter extends RecyclerView.Adapter<AttachmentViewHo
public static final int VIEW_TYPE_DEFAULT = 2;
public static final int VIEW_TYPE_IMAGE = 1;

@NonNull
private MutableLiveData<Boolean> isEmpty = new MutableLiveData<>(true);
@NonNull
private final MenuInflater menuInflater;
@ColorInt
private int mainColor;
Expand Down Expand Up @@ -126,22 +132,34 @@ public int getItemCount() {
return attachments.size();
}

private void updateIsEmpty() {
this.isEmpty.postValue(getItemCount() <= 0);
}

@NonNull
public LiveData<Boolean> isEmpty() {
return distinctUntilChanged(this.isEmpty);
}

public void setAttachments(@NonNull List<Attachment> attachments, @Nullable Long cardRemoteId) {
this.cardRemoteId = cardRemoteId;
this.attachments.clear();
this.attachments.addAll(attachments);
notifyDataSetChanged();
this.updateIsEmpty();
}

public void addAttachment(Attachment a) {
this.attachments.add(0, a);
notifyItemInserted(this.attachments.size());
this.updateIsEmpty();
}

public void removeAttachment(Attachment a) {
final int index = this.attachments.indexOf(a);
this.attachments.remove(a);
notifyItemRemoved(index);
this.updateIsEmpty();
}

public void replaceAttachment(Attachment toReplace, Attachment with) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.M;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static androidx.core.content.PermissionChecker.PERMISSION_GRANTED;
import static androidx.core.content.PermissionChecker.checkSelfPermission;
import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce;
Expand Down Expand Up @@ -106,7 +108,15 @@ public View onCreateView(@NonNull LayoutInflater inflater,
viewModel.getFullCard().getLocalId());
binding.attachmentsList.setAdapter(adapter);

updateEmptyContentView();
adapter.isEmpty().observe(getViewLifecycleOwner(), (isEmpty) -> {
if (isEmpty) {
this.binding.emptyContentView.setVisibility(VISIBLE);
this.binding.attachmentsList.setVisibility(GONE);
} else {
this.binding.emptyContentView.setVisibility(GONE);
this.binding.attachmentsList.setVisibility(VISIBLE);
}
});

final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int spanCount = (int) ((displayMetrics.widthPixels / displayMetrics.density) / getResources().getInteger(R.integer.max_dp_attachment_column));
Expand Down Expand Up @@ -138,7 +148,6 @@ public void onMapSharedElements(List<String> names, Map<String, View> sharedElem
}
});
adapter.setAttachments(viewModel.getFullCard().getAttachments(), viewModel.getFullCard().getId());
updateEmptyContentView();
}

if (viewModel.canEdit()) {
Expand Down Expand Up @@ -287,7 +296,6 @@ private void uploadNewAttachmentFromUri(@NonNull Uri sourceUri, String mimeType)
}
});
}
updateEmptyContentView();
break;
}
default: {
Expand Down Expand Up @@ -344,24 +352,13 @@ public void onAttachmentDeleted(Attachment attachment) {
}
});
}
updateEmptyContentView();
}

@Override
public void onAttachmentClicked(int position) {
this.clickedItemPosition = position;
}

private void updateEmptyContentView() {
if (this.adapter == null || this.adapter.getItemCount() == 0) {
this.binding.emptyContentView.setVisibility(View.VISIBLE);
this.binding.attachmentsList.setVisibility(View.GONE);
} else {
this.binding.emptyContentView.setVisibility(View.GONE);
this.binding.attachmentsList.setVisibility(View.VISIBLE);
}
}

@Override
public void applyBrand(int mainColor) {
applyBrandToFAB(mainColor, binding.fab);
Expand Down

0 comments on commit ea7895c

Please sign in to comment.