Use ListAdapter instead of FastAdadpter, fixes some update bugs

This commit is contained in:
Shinokuni 2019-03-09 17:41:47 +01:00
parent 95a50a4787
commit f8054d14a9
3 changed files with 186 additions and 218 deletions

View File

@ -19,7 +19,7 @@ import com.mikepenz.fastadapter.commons.utils.DiffCallback;
import com.mikepenz.fastadapter.commons.utils.FastAdapterDiffUtil;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.views.EditFeedDialog;
import com.readrops.app.views.FeedWithFolderItem;
import com.readrops.app.views.FeedsAdapter;
import com.readrops.app.viewmodels.ManageFeedsViewModel;
import com.readrops.app.R;
import com.readrops.app.database.pojo.FeedWithFolder;
@ -34,8 +34,8 @@ import io.reactivex.schedulers.Schedulers;
public class ManageFeedsActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ModelAdapter<FeedWithFolder, FeedWithFolderItem> itemAdapter;
private FastAdapter fastAdapter;
private FeedsAdapter adapter;
private ManageFeedsViewModel viewModel;
@Override
@ -45,85 +45,24 @@ public class ManageFeedsActivity extends AppCompatActivity {
recyclerView = findViewById(R.id.feeds_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
itemAdapter = new ModelAdapter<>(feedWithFolder -> {
FeedWithFolderItem folderItem = new FeedWithFolderItem(feedWithFolder);
folderItem.setListener(new FeedWithFolderItem.ManageFeedsListener() {
@Override
public void onEdit(FeedWithFolder feedWithFolder) {
openEditFeedDialog(feedWithFolder);
}
adapter = new FeedsAdapter(new FeedsAdapter.ManageFeedsListener() {
@Override
public void onEdit(FeedWithFolder feedWithFolder) {
openEditFeedDialog(feedWithFolder);
}
@Override
public void onDelete(FeedWithFolder feedWithFolder) {
deleteFolder(feedWithFolder.getFeed().getId());
}
});
return folderItem;
@Override
public void onDelete(FeedWithFolder feedWithFolder) {
deleteFolder(feedWithFolder.getFeed().getId());
}
});
fastAdapter = FastAdapter.with(itemAdapter);
recyclerView.setAdapter(fastAdapter);
recyclerView.setAdapter(adapter);
viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()).create(ManageFeedsViewModel.class);
viewModel.getFeedsWithFolder().observe(this, feedWithFolders -> {
List<FeedWithFolderItem> items = new ArrayList<>();
for (FeedWithFolder feedWithFolder : feedWithFolders) {
FeedWithFolderItem folderItem = new FeedWithFolderItem(feedWithFolder);
folderItem.setListener(new FeedWithFolderItem.ManageFeedsListener() {
@Override
public void onEdit(FeedWithFolder feedWithFolder) {
openEditFeedDialog(feedWithFolder);
}
@Override
public void onDelete(FeedWithFolder feedWithFolder) {
deleteFolder(feedWithFolder.getFeed().getId());
}
});
items.add(folderItem);
}
if (items.size() > 0) {
DiffUtil.DiffResult diffResult = FastAdapterDiffUtil.calculateDiff(itemAdapter, items, new DiffCallback<FeedWithFolderItem>(){
@Override
public boolean areItemsTheSame(FeedWithFolderItem oldItem, FeedWithFolderItem newItem) {
return oldItem.getModel().getFeed().getId() == newItem.getModel().getFeed().getId();
}
@Override
public boolean areContentsTheSame(FeedWithFolderItem oldItem, FeedWithFolderItem newItem) {
FeedWithFolder feedWithFolder1 = oldItem.getModel();
FeedWithFolder feedWithFolder2 = newItem.getModel();
return feedWithFolder1.getFolder().getName().equals(feedWithFolder2.getFolder().getName()) &&
feedWithFolder1.getFeed().getName().equals(feedWithFolder2.getFeed().getName()) &&
feedWithFolder1.getFeed().getUrl().equals(feedWithFolder2.getFeed().getUrl());
}
@Nullable
@Override
public Object getChangePayload(FeedWithFolderItem oldItem, int oldItemPosition, FeedWithFolderItem newItem, int newItemPosition) {
Bundle bundle = new Bundle();
if (!oldItem.getModel().getFeed().getName().equals(newItem.getModel().getFeed().getName()))
bundle.putString(FeedWithFolderItem.FEED_NAME_KEY, newItem.getModel().getFeed().getName());
if (!oldItem.getModel().getFolder().getName().equals(newItem.getModel().getFolder().getName()))
bundle.putString(FeedWithFolderItem.FOLDER_NAME_KEY, newItem.getModel().getFolder().getName());
if (bundle.size() > 0)
return bundle;
else
return null;
}
});
FastAdapterDiffUtil.set(itemAdapter, diffResult);
}
adapter.submitList(feedWithFolders);
});
}
@ -160,24 +99,6 @@ public class ManageFeedsActivity extends AppCompatActivity {
transaction.add(editFeedDialog, "").commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.feeds_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.add_folder:
addFolder();
return true;
}
return super.onOptionsItemSelected(item);
}
private void addFolder() {
new MaterialDialog.Builder(this)
@ -203,6 +124,26 @@ public class ManageFeedsActivity extends AppCompatActivity {
.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.feeds_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.add_folder:
addFolder();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
finish();

View File

@ -1,125 +0,0 @@
package com.readrops.app.views;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.mikepenz.fastadapter.items.ModelAbstractItem;
import com.readrops.app.R;
import com.readrops.app.database.pojo.FeedWithFolder;
import com.readrops.app.utils.GlideApp;
import java.util.List;
public class FeedWithFolderItem extends ModelAbstractItem<FeedWithFolder, FeedWithFolderItem, FeedWithFolderItem.ViewHolder> {
public static final String FEED_NAME_KEY = "name";
public static final String FOLDER_NAME_KEY = "folderName";
public static final String FOLDER_ID_KEY = "folderKey";
private ManageFeedsListener listener;
public FeedWithFolderItem(FeedWithFolder feedWithFolder) {
super(feedWithFolder);
}
@NonNull
@Override
public ViewHolder getViewHolder(View v) {
return new ViewHolder(v);
}
@Override
public int getType() {
return R.id.feed_layout;
}
@Override
public int getLayoutRes() {
return R.layout.feed_layout;
}
@Override
public long getIdentifier() {
return getModel().getFeed().getId();
}
@Override
public void bindView(ViewHolder holder, List<Object> payloads) {
super.bindView(holder, payloads);
if (!payloads.isEmpty()) {
Bundle bundle = (Bundle)payloads.get(0);
if (bundle.getString(FEED_NAME_KEY) != null) {
holder.feedName.setText(bundle.getString(FEED_NAME_KEY));
}
if (bundle.getString(FOLDER_NAME_KEY) != null) {
holder.folderName.setText(bundle.getString(FOLDER_NAME_KEY));
}
} else {
FeedWithFolder feedWithFolder = getModel();
if (feedWithFolder.getFeed().getIconUrl() != null) {
GlideApp.with(holder.itemView.getContext())
.load(feedWithFolder.getFeed().getIconUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.ic_rss_feed)
.into(holder.feedIcon);
} else
holder.feedIcon.setImageResource(R.drawable.ic_rss_feed);
holder.feedName.setText(feedWithFolder.getFeed().getName());
if (feedWithFolder.getFeed().getDescription() != null) {
holder.feedDescription.setVisibility(View.VISIBLE);
holder.feedDescription.setText(feedWithFolder.getFeed().getDescription());
} else
holder.feedDescription.setVisibility(View.GONE);
if (feedWithFolder.getFolder().getId() != 1)
holder.folderName.setText(feedWithFolder.getFolder().getName());
else
holder.folderName.setText(holder.itemView.getResources().getString(R.string.no_folder));
holder.editFeed.setOnClickListener(v -> listener.onEdit(getModel()));
holder.deleteFeed.setOnClickListener(v -> listener.onDelete(getModel()));
}
}
public void setListener(ManageFeedsListener listener) {
this.listener = listener;
}
public interface ManageFeedsListener {
void onEdit(FeedWithFolder feedWithFolder);
void onDelete(FeedWithFolder feedWithFolder);
}
protected static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView feedIcon;
private TextView feedName;
private TextView feedDescription;
private TextView folderName;
private ImageView editFeed;
private ImageView deleteFeed;
public ViewHolder(View itemView) {
super(itemView);
feedIcon = itemView.findViewById(R.id.feed_layout_icon);
feedName = itemView.findViewById(R.id.feed_layout_name);
feedDescription = itemView.findViewById(R.id.feed_layout_description);
folderName = itemView.findViewById(R.id.feed_layout_folder);
editFeed = itemView.findViewById(R.id.feed_layout_edit);
deleteFeed = itemView.findViewById(R.id.feed_layout_delete);
}
}
}

View File

@ -0,0 +1,152 @@
package com.readrops.app.views;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.recyclerview.extensions.AsyncListDiffer;
import android.support.v7.recyclerview.extensions.ListAdapter;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.readrops.app.R;
import com.readrops.app.database.pojo.FeedWithFolder;
import com.readrops.app.utils.GlideApp;
import java.util.List;
public class FeedsAdapter extends ListAdapter<FeedWithFolder, FeedsAdapter.ViewHolder> {
public static final String FEED_NAME_KEY = "name";
public static final String FOLDER_NAME_KEY = "folderName";
private ManageFeedsListener listener;
private static final DiffUtil.ItemCallback<FeedWithFolder> DIFF_CALLBACK = new DiffUtil.ItemCallback<FeedWithFolder>() {
@Override
public boolean areItemsTheSame(@NonNull FeedWithFolder feedWithFolder, @NonNull FeedWithFolder t1) {
Log.d("", "areItemsTheSame: ");
return feedWithFolder.getFeed().getId() == t1.getFeed().getId();
}
@Override
public boolean areContentsTheSame(@NonNull FeedWithFolder feedWithFolder, @NonNull FeedWithFolder t1) {
return feedWithFolder.getFeed().getName().equals(t1.getFeed().getName())
&& feedWithFolder.getFolder().getName().equals(t1.getFolder().getName());
}
@Nullable
@Override
public Object getChangePayload(@NonNull FeedWithFolder oldItem, @NonNull FeedWithFolder newItem) {
Bundle bundle = new Bundle();
if (!oldItem.getFeed().getName().equals(newItem.getFeed().getName()))
bundle.putString(FeedsAdapter.FEED_NAME_KEY, newItem.getFeed().getName());
if (!oldItem.getFolder().getName().equals(newItem.getFolder().getName()))
bundle.putString(FeedsAdapter.FOLDER_NAME_KEY, newItem.getFolder().getName());
if (bundle.size() > 0)
return bundle;
else
return null;
}
};
public FeedsAdapter(ManageFeedsListener listener) {
super(DIFF_CALLBACK);
this.listener = listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.feed_layout, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
FeedWithFolder feedWithFolder = getItem(i);
if (feedWithFolder.getFeed().getIconUrl() != null) {
GlideApp.with(viewHolder.itemView.getContext())
.load(feedWithFolder.getFeed().getIconUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.ic_rss_feed)
.into(viewHolder.feedIcon);
} else
viewHolder.feedIcon.setImageResource(R.drawable.ic_rss_feed);
viewHolder.feedName.setText(feedWithFolder.getFeed().getName());
if (feedWithFolder.getFeed().getDescription() != null) {
viewHolder.feedDescription.setVisibility(View.VISIBLE);
viewHolder.feedDescription.setText(feedWithFolder.getFeed().getDescription());
} else
viewHolder.feedDescription.setVisibility(View.GONE);
if (feedWithFolder.getFolder().getId() != 1)
viewHolder.folderName.setText(feedWithFolder.getFolder().getName());
else
viewHolder.folderName.setText(viewHolder.itemView.getResources().getString(R.string.no_folder));
viewHolder.editFeed.setOnClickListener(v -> listener.onEdit(getItem(i)));
viewHolder.deleteFeed.setOnClickListener(v -> listener.onDelete(getItem(i)));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
if (!payloads.isEmpty()) {
Bundle bundle = (Bundle) payloads.get(0);
if (bundle.getString(FEED_NAME_KEY) != null) {
holder.feedName.setText(bundle.getString(FEED_NAME_KEY));
}
if (bundle.getString(FOLDER_NAME_KEY) != null) {
holder.folderName.setText(bundle.getString(FOLDER_NAME_KEY));
}
} else
onBindViewHolder(holder, position);
}
public interface ManageFeedsListener {
void onEdit(FeedWithFolder feedWithFolder);
void onDelete(FeedWithFolder feedWithFolder);
}
protected static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView feedIcon;
private TextView feedName;
private TextView feedDescription;
private TextView folderName;
private ImageView editFeed;
private ImageView deleteFeed;
public ViewHolder(View itemView) {
super(itemView);
feedIcon = itemView.findViewById(R.id.feed_layout_icon);
feedName = itemView.findViewById(R.id.feed_layout_name);
feedDescription = itemView.findViewById(R.id.feed_layout_description);
folderName = itemView.findViewById(R.id.feed_layout_folder);
editFeed = itemView.findViewById(R.id.feed_layout_edit);
deleteFeed = itemView.findViewById(R.id.feed_layout_delete);
}
}
}