Skip to content

Commit

Permalink
migrate FlexibleItems to native view bindings
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Scherzinger <[email protected]>
  • Loading branch information
AndyScherzinger authored and mahibi committed Feb 25, 2022
1 parent d4f9f99 commit 0677607
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Nextcloud Talk application
*
* @author Mario Danic
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <[email protected]>
* Copyright (C) 2017 Mario Danic ([email protected])
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -24,17 +26,12 @@
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.nextcloud.talk.R;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.databinding.AccountItemBinding;
import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.models.json.participants.Participant;
import com.nextcloud.talk.utils.ApiUtils;
Expand All @@ -44,9 +41,6 @@
import java.util.regex.Pattern;

import androidx.annotation.Nullable;
import androidx.emoji.widget.EmojiTextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import eu.davidea.flexibleadapter.items.IFilterable;
Expand All @@ -56,10 +50,10 @@
public class AdvancedUserItem extends AbstractFlexibleItem<AdvancedUserItem.UserItemViewHolder> implements
IFilterable<String> {

private Participant participant;
private UserEntity userEntity;
private final Participant participant;
private final UserEntity userEntity;
@Nullable
private Account account;
private final Account account;

public AdvancedUserItem(Participant participant, UserEntity userEntity, @Nullable Account account) {
this.participant = participant;
Expand Down Expand Up @@ -110,68 +104,70 @@ public UserItemViewHolder createViewHolder(View view, FlexibleAdapter adapter) {

@Override
public void bindViewHolder(FlexibleAdapter adapter, UserItemViewHolder holder, int position, List payloads) {
holder.avatarImageView.setController(null);
holder.binding.userIcon.setController(null);

if (adapter.hasFilter()) {
FlexibleUtils.highlightText(
holder.contactDisplayName,
holder.binding.userName,
participant.getDisplayName(),
String.valueOf(adapter.getFilter(String.class)),
NextcloudTalkApplication.Companion.getSharedApplication()
.getResources()
.getColor(R.color.colorPrimary));
} else {
holder.contactDisplayName.setText(participant.getDisplayName());
holder.binding.userName.setText(participant.getDisplayName());
}

if (userEntity != null && !TextUtils.isEmpty(userEntity.getBaseUrl())) {
String host = Uri.parse(userEntity.getBaseUrl()).getHost();
if (!TextUtils.isEmpty(host)) {
holder.serverUrl.setText(Uri.parse(userEntity.getBaseUrl()).getHost());
holder.binding.account.setText(Uri.parse(userEntity.getBaseUrl()).getHost());
} else {
holder.serverUrl.setText(userEntity.getBaseUrl());
holder.binding.account.setText(userEntity.getBaseUrl());
}
}

holder.avatarImageView.getHierarchy().setPlaceholderImage(R.drawable.account_circle_48dp);
holder.avatarImageView.getHierarchy().setFailureImage(R.drawable.account_circle_48dp);
holder.binding.userIcon.getHierarchy().setPlaceholderImage(R.drawable.account_circle_48dp);
holder.binding.userIcon.getHierarchy().setFailureImage(R.drawable.account_circle_48dp);

if (userEntity != null && userEntity.getBaseUrl() != null &&
userEntity.getBaseUrl().startsWith("http://") ||
userEntity.getBaseUrl().startsWith("https://")) {

DraweeController draweeController = Fresco.newDraweeControllerBuilder()
.setOldController(holder.avatarImageView.getController())
.setOldController(holder.binding.userIcon.getController())
.setAutoPlayAnimations(true)
.setImageRequest(DisplayUtils.getImageRequestForUrl(ApiUtils.getUrlForAvatarWithName(userEntity.getBaseUrl(),
participant.getActorId(), R.dimen.small_item_height), null))
.setImageRequest(
DisplayUtils.getImageRequestForUrl(
ApiUtils.getUrlForAvatarWithName(
userEntity.getBaseUrl(),
participant.getActorId(),
R.dimen.small_item_height),
null))
.build();
holder.avatarImageView.setController(draweeController);
holder.binding.userIcon.setController(draweeController);
}
}

@Override
public boolean filter(String constraint) {
return participant.getDisplayName() != null &&
Pattern.compile(constraint, Pattern.CASE_INSENSITIVE | Pattern.LITERAL).matcher(participant.getDisplayName().trim()).find();
Pattern
.compile(constraint, Pattern.CASE_INSENSITIVE | Pattern.LITERAL)
.matcher(participant.getDisplayName().trim())
.find();
}


static class UserItemViewHolder extends FlexibleViewHolder {

@BindView(R.id.user_name)
public EmojiTextView contactDisplayName;
@BindView(R.id.account)
public TextView serverUrl;
@BindView(R.id.user_icon)
public SimpleDraweeView avatarImageView;
public AccountItemBinding binding;

/**
* Default constructor.
*/
UserItemViewHolder(View view, FlexibleAdapter adapter) {
super(view, adapter);
ButterKnife.bind(this, view);
binding = AccountItemBinding.bind(view);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Nextcloud Talk application
*
* @author Mario Danic
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <[email protected]>
* Copyright (C) 2017-2018 Mario Danic <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -24,20 +26,14 @@
import android.text.format.Formatter;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.content.res.AppCompatResources;
import autodagger.AutoInjector;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.nextcloud.talk.R;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.components.filebrowser.models.BrowserFile;
import com.nextcloud.talk.databinding.RvItemBrowserFileBinding;
import com.nextcloud.talk.interfaces.SelectionInterface;
import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.utils.ApiUtils;
Expand All @@ -49,22 +45,21 @@

import javax.inject.Inject;

import androidx.appcompat.content.res.AppCompatResources;
import autodagger.AutoInjector;
import butterknife.BindView;
import butterknife.ButterKnife;
import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
import eu.davidea.flexibleadapter.items.IFilterable;
import eu.davidea.flexibleadapter.items.IFlexible;
import eu.davidea.viewholders.FlexibleViewHolder;

@AutoInjector(NextcloudTalkApplication.class)
public class BrowserFileItem extends AbstractFlexibleItem<BrowserFileItem.ViewHolder> implements IFilterable<String> {
public class BrowserFileItem extends AbstractFlexibleItem<BrowserFileItem.BrowserFileItemViewHolder> implements IFilterable<String> {
@Inject
Context context;
private BrowserFile browserFile;
private UserEntity activeUser;
private SelectionInterface selectionInterface;
private final BrowserFile browserFile;
private final UserEntity activeUser;
private final SelectionInterface selectionInterface;
private boolean selected;

public BrowserFileItem(BrowserFile browserFile, UserEntity activeUser, SelectionInterface selectionInterface) {
Expand Down Expand Up @@ -94,9 +89,8 @@ public int getLayoutRes() {
}

@Override
public ViewHolder createViewHolder(View view, FlexibleAdapter<IFlexible> adapter) {
return new ViewHolder(view, adapter);

public BrowserFileItemViewHolder createViewHolder(View view, FlexibleAdapter<IFlexible> adapter) {
return new BrowserFileItemViewHolder(view, adapter);
}

private boolean isSelected() {
Expand All @@ -108,8 +102,11 @@ private void setSelected(boolean selected) {
}

@Override
public void bindViewHolder(FlexibleAdapter<IFlexible> adapter, ViewHolder holder, int position, List<Object> payloads) {
holder.fileIconImageView.setController(null);
public void bindViewHolder(FlexibleAdapter<IFlexible> adapter,
BrowserFileItemViewHolder holder,
int position,
List<Object> payloads) {
holder.binding.fileIcon.setController(null);
if (!browserFile.isAllowedToReShare() || browserFile.isEncrypted()) {
holder.itemView.setEnabled(false);
holder.itemView.setAlpha(0.38f);
Expand All @@ -119,31 +116,32 @@ public void bindViewHolder(FlexibleAdapter<IFlexible> adapter, ViewHolder holder
}

if (browserFile.isEncrypted()) {
holder.fileEncryptedImageView.setVisibility(View.VISIBLE);
holder.binding.fileEncryptedImageView.setVisibility(View.VISIBLE);

} else {
holder.fileEncryptedImageView.setVisibility(View.GONE);
holder.binding.fileEncryptedImageView.setVisibility(View.GONE);
}

if (browserFile.isFavorite()) {
holder.fileFavoriteImageView.setVisibility(View.VISIBLE);
holder.binding.fileFavoriteImageView.setVisibility(View.VISIBLE);
} else {
holder.fileFavoriteImageView.setVisibility(View.GONE);
holder.binding.fileFavoriteImageView.setVisibility(View.GONE);
}

if (selectionInterface.shouldOnlySelectOneImageFile()) {
if (browserFile.isFile && browserFile.mimeType.startsWith("image/")) {
holder.selectFileCheckbox.setVisibility(View.VISIBLE);
holder.binding.selectFileCheckbox.setVisibility(View.VISIBLE);
} else {
holder.selectFileCheckbox.setVisibility(View.GONE);
holder.binding.selectFileCheckbox.setVisibility(View.GONE);
}
} else {
holder.selectFileCheckbox.setVisibility(View.VISIBLE);
holder.binding.selectFileCheckbox.setVisibility(View.VISIBLE);
}

if (context != null) {
holder
.fileIconImageView
.binding
.fileIcon
.getHierarchy()
.setPlaceholderImage(
AppCompatResources.getDrawable(
Expand All @@ -160,25 +158,28 @@ public void bindViewHolder(FlexibleAdapter<IFlexible> adapter, ViewHolder holder
.setAutoPlayAnimations(true)
.setImageRequest(DisplayUtils.getImageRequestForUrl(path, null))
.build();
holder.fileIconImageView.setController(draweeController);
holder.binding.fileIcon.setController(draweeController);
}
}

holder.filenameTextView.setText(browserFile.getDisplayName());
holder.fileModifiedTextView.setText(String.format(context.getString(R.string.nc_last_modified),
holder.binding.filenameTextView.setText(browserFile.getDisplayName());
holder.binding.fileModifiedInfo.setText(String.format(context.getString(R.string.nc_last_modified),
Formatter.formatShortFileSize(context, browserFile.getSize()),
DateUtils.INSTANCE.getLocalDateTimeStringFromTimestamp(browserFile.getModifiedTimestamp())));
setSelected(selectionInterface.isPathSelected(browserFile.getPath()));
holder.selectFileCheckbox.setChecked(isSelected());
holder.binding.selectFileCheckbox.setChecked(isSelected());

if (!browserFile.isEncrypted()) {
holder.selectFileCheckbox.setOnClickListener(new View.OnClickListener() {
holder.binding.selectFileCheckbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!browserFile.isAllowedToReShare()) {
((CheckBox) v).setChecked(false);
Toast.makeText(context, context.getResources().getString(R.string.nc_file_browser_reshare_forbidden),
Toast.LENGTH_LONG).show();
Toast.makeText(
context,
context.getResources().getString(R.string.nc_file_browser_reshare_forbidden),
Toast.LENGTH_LONG)
.show();
} else if (((CheckBox) v).isChecked() != isSelected()) {
setSelected(((CheckBox) v).isChecked());
selectionInterface.toggleBrowserItemSelection(browserFile.getPath());
Expand All @@ -187,33 +188,22 @@ public void onClick(View v) {
});
}

holder.filenameTextView.setSelected(true);
holder.fileModifiedTextView.setSelected(true);
holder.binding.filenameTextView.setSelected(true);
holder.binding.fileModifiedInfo.setSelected(true);
}

@Override
public boolean filter(String constraint) {
return false;
}

static class ViewHolder extends FlexibleViewHolder {

@BindView(R.id.file_icon)
public SimpleDraweeView fileIconImageView;
@BindView(R.id.file_modified_info)
public TextView fileModifiedTextView;
@BindView(R.id.filename_text_view)
public TextView filenameTextView;
@BindView(R.id.select_file_checkbox)
public CheckBox selectFileCheckbox;
@BindView(R.id.fileEncryptedImageView)
public ImageView fileEncryptedImageView;
@BindView(R.id.fileFavoriteImageView)
public ImageView fileFavoriteImageView;

ViewHolder(View view, FlexibleAdapter adapter) {
static class BrowserFileItemViewHolder extends FlexibleViewHolder {

RvItemBrowserFileBinding binding;

BrowserFileItemViewHolder(View view, FlexibleAdapter adapter) {
super(view, adapter);
ButterKnife.bind(this, view);
binding = RvItemBrowserFileBinding.bind(view);
}
}
}

0 comments on commit 0677607

Please sign in to comment.