Introduce self-written SectionedAdapter

This commit is contained in:
topjohnwu 2017-07-23 18:32:52 +08:00
parent 295334d3ac
commit 085fba050a
2 changed files with 169 additions and 113 deletions

View File

@ -6,6 +6,7 @@ import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -30,116 +31,103 @@ import java.util.Map;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
public class ReposAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { public class ReposAdapter extends SectionedAdapter<ReposAdapter.SectionHolder, ReposAdapter.RepoHolder> {
private static final int SECTION_TYPE = 0; private static final int UPDATES = 0;
private static final int REPO_TYPE = 1; private static final int INSTALLED = 1;
private static final int OTHERS = 2;
private List<Repo> mUpdateRepos, mInstalledRepos, mOthersRepos;
private int[] sectionList;
private int size;
private Cursor repoCursor = null; private Cursor repoCursor = null;
private Map<String, Module> moduleMap; private Map<String, Module> moduleMap;
private RepoDatabaseHelper repoDB; private RepoDatabaseHelper repoDB;
private List<Pair<Integer, List<Repo>>> repoPairs;
public ReposAdapter(RepoDatabaseHelper db, Map<String, Module> map) { public ReposAdapter(RepoDatabaseHelper db, Map<String, Module> map) {
repoDB = db; repoDB = db;
moduleMap = map; moduleMap = map;
mUpdateRepos = new ArrayList<>(); repoPairs = new ArrayList<>();
mInstalledRepos = new ArrayList<>(); }
mOthersRepos = new ArrayList<>();
sectionList = new int[3];
size = 0; @Override
public int getSectionCount() {
return repoPairs.size();
} }
@Override @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public int getItemCount(int section) {
Context context = parent.getContext(); return repoPairs.get(section).second.size();
View v; }
RecyclerView.ViewHolder holder = null;
switch (viewType) { @Override
case SECTION_TYPE: public SectionHolder onCreateSectionViewHolder(ViewGroup parent) {
v = LayoutInflater.from(context).inflate(R.layout.section, parent, false); View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.section, parent, false);
holder = new SectionHolder(v); return new SectionHolder(v);
}
@Override
public RepoHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_repo, parent, false);
return new RepoHolder(v);
}
@Override
public void onBindSectionViewHolder(SectionHolder holder, int section) {
switch (repoPairs.get(section).first) {
case UPDATES:
holder.sectionText.setText(R.string.update_available);
break; break;
case REPO_TYPE: case INSTALLED:
v = LayoutInflater.from(context).inflate(R.layout.list_item_repo, parent, false); holder.sectionText.setText(R.string.installed);
holder = new RepoHolder(v); break;
case OTHERS:
holder.sectionText.setText(R.string.not_installed);
break; break;
} }
return holder;
} }
@Override @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { public void onBindItemViewHolder(RepoHolder holder, int section, int position) {
Repo repo = repoPairs.get(section).second.get(position);
Context context = holder.itemView.getContext(); Context context = holder.itemView.getContext();
switch (getItemViewType(position)) {
case SECTION_TYPE:
SectionHolder section = (SectionHolder) holder;
if (position == sectionList[0]) {
section.sectionText.setText(context.getString(R.string.update_available));
} else if (position == sectionList[1]) {
section.sectionText.setText(context.getString(R.string.installed));
} else {
section.sectionText.setText(context.getString(R.string.not_installed));
}
break;
case REPO_TYPE:
RepoHolder repoHolder = (RepoHolder) holder;
Repo repo = getRepo(position);
repoHolder.title.setText(repo.getName());
repoHolder.versionName.setText(repo.getVersion());
String author = repo.getAuthor();
repoHolder.author.setText(TextUtils.isEmpty(author) ? null : context.getString(R.string.author, author));
repoHolder.description.setText(repo.getDescription());
repoHolder.infoLayout.setOnClickListener(v -> new MarkDownWindow(null, repo.getDetailUrl(), context)); holder.title.setText(repo.getName());
holder.versionName.setText(repo.getVersion());
String author = repo.getAuthor();
holder.author.setText(TextUtils.isEmpty(author) ? null : context.getString(R.string.author, author));
holder.description.setText(repo.getDescription());
repoHolder.downloadImage.setOnClickListener(v -> { holder.infoLayout.setOnClickListener(v -> new MarkDownWindow(null, repo.getDetailUrl(), context));
String filename = repo.getName() + "-" + repo.getVersion() + ".zip";
new AlertDialogBuilder(context)
.setTitle(context.getString(R.string.repo_install_title, repo.getName()))
.setMessage(context.getString(R.string.repo_install_msg, filename))
.setCancelable(true)
.setPositiveButton(R.string.install, (d, i) -> Utils.dlAndReceive(
context,
new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
new ProcessRepoZip((Activity) context, uri, true).exec();
}
},
repo.getZipUrl(),
Utils.getLegalFilename(filename)))
.setNeutralButton(R.string.download, (d, i) -> Utils.dlAndReceive(
context,
new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
new ProcessRepoZip((Activity) context, uri, false).exec();
}
},
repo.getZipUrl(),
Utils.getLegalFilename(filename)))
.setNegativeButton(R.string.no_thanks, null)
.show();
});
break;
}
}
@Override holder.downloadImage.setOnClickListener(v -> {
public int getItemViewType(int position) { String filename = repo.getName() + "-" + repo.getVersion() + ".zip";
for (int i : sectionList) { new AlertDialogBuilder(context)
if (position == i) .setTitle(context.getString(R.string.repo_install_title, repo.getName()))
return SECTION_TYPE; .setMessage(context.getString(R.string.repo_install_msg, filename))
} .setCancelable(true)
return REPO_TYPE; .setPositiveButton(R.string.install, (d, i) -> Utils.dlAndReceive(
} context,
new DownloadReceiver() {
@Override @Override
public int getItemCount() { public void onDownloadDone(Uri uri) {
return size; new ProcessRepoZip((Activity) context, uri, true).exec();
}
},
repo.getZipUrl(),
Utils.getLegalFilename(filename)))
.setNeutralButton(R.string.download, (d, i) -> Utils.dlAndReceive(
context,
new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
new ProcessRepoZip((Activity) context, uri, false).exec();
}
},
repo.getZipUrl(),
Utils.getLegalFilename(filename)))
.setNegativeButton(R.string.no_thanks, null)
.show();
});
} }
public void notifyDBChanged() { public void notifyDBChanged() {
@ -150,53 +138,43 @@ public class ReposAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
} }
public void filter(String s) { public void filter(String s) {
mUpdateRepos.clear(); List<Repo> updates = new ArrayList<>();
mInstalledRepos.clear(); List<Repo> installed = new ArrayList<>();
mOthersRepos.clear(); List<Repo> others = new ArrayList<>();
sectionList[0] = sectionList[1] = sectionList[2] = 0;
repoPairs.clear();
while (repoCursor.moveToNext()) { while (repoCursor.moveToNext()) {
Repo repo = new Repo(repoCursor); Repo repo = new Repo(repoCursor);
if (repo.getName().toLowerCase().contains(s.toLowerCase()) if (repo.getName().toLowerCase().contains(s.toLowerCase())
|| repo.getAuthor().toLowerCase().contains(s.toLowerCase()) || repo.getAuthor().toLowerCase().contains(s.toLowerCase())
|| repo.getDescription().toLowerCase().contains(s.toLowerCase()) || repo.getDescription().toLowerCase().contains(s.toLowerCase())
) { ) {
// Passed the filter // Passed the repoFilter
Module module = moduleMap.get(repo.getId()); Module module = moduleMap.get(repo.getId());
if (module != null) { if (module != null) {
if (repo.getVersionCode() > module.getVersionCode()) { if (repo.getVersionCode() > module.getVersionCode()) {
// Updates // Updates
mUpdateRepos.add(repo); updates.add(repo);
} else { } else {
mInstalledRepos.add(repo); installed.add(repo);
} }
} else { } else {
mOthersRepos.add(repo); others.add(repo);
} }
} }
} }
repoCursor.moveToFirst(); repoCursor.moveToFirst();
sectionList[0] = mUpdateRepos.isEmpty() ? -1 : 0; if (!updates.isEmpty())
size = mUpdateRepos.isEmpty() ? 0 : mUpdateRepos.size() + 1; repoPairs.add(new Pair<>(UPDATES, updates));
sectionList[1] = mInstalledRepos.isEmpty() ? -1 : size; if (!installed.isEmpty())
size += mInstalledRepos.isEmpty() ? 0 : mInstalledRepos.size() + 1; repoPairs.add(new Pair<>(INSTALLED, installed));
sectionList[2] = mOthersRepos.isEmpty() ? -1 : size; if (!others.isEmpty())
size += mOthersRepos.isEmpty() ? 0 : mOthersRepos.size() + 1; repoPairs.add(new Pair<>(OTHERS, others));
notifyDataSetChanged(); notifyDataSetChanged();
} }
private Repo getRepo(int position) {
if (!mUpdateRepos.isEmpty()) position -= 1;
if (position < mUpdateRepos.size()) return mUpdateRepos.get(position);
position -= mUpdateRepos.size();
if (!mInstalledRepos.isEmpty()) position -= 1;
if (position < mInstalledRepos.size()) return mInstalledRepos.get(position);
position -= mInstalledRepos.size();
if (!mOthersRepos.isEmpty()) position -= 1;
return mOthersRepos.get(position);
}
static class SectionHolder extends RecyclerView.ViewHolder { static class SectionHolder extends RecyclerView.ViewHolder {
@BindView(R.id.section_text) TextView sectionText; @BindView(R.id.section_text) TextView sectionText;

View File

@ -0,0 +1,78 @@
package com.topjohnwu.magisk.adapters;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
public abstract class SectionedAdapter<S extends RecyclerView.ViewHolder, C extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int SECTION_TYPE = Integer.MIN_VALUE;
@Override
final public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == SECTION_TYPE)
return onCreateSectionViewHolder(parent);
return onCreateItemViewHolder(parent, viewType);
}
@Override
@SuppressWarnings("unchecked")
final public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
PositionInfo info = getPositionInfo(position);
if (info.position == -1)
onBindSectionViewHolder((S) holder, info.section);
else
onBindItemViewHolder((C) holder, info.section, info.position);
}
@Override
final public int getItemCount() {
int size, sec;
size = sec = getSectionCount();
for (int i = 0; i < sec; ++i){
size += getItemCount(i);
}
return size;
}
@Override
final public int getItemViewType(int position) {
PositionInfo info = getPositionInfo(position);
if (info.position == -1)
return SECTION_TYPE;
else
return getItemViewType(info.section, info.position);
}
public int getItemViewType(int section, int position) {
return 0;
}
private PositionInfo getPositionInfo(int position) {
int section = 0;
while (true) {
if (position == 0)
return new PositionInfo(section, -1);
position -= 1;
if (position < getItemCount(section))
return new PositionInfo(section, position);
position -= getItemCount(section++);
}
}
private static class PositionInfo {
int section;
int position;
PositionInfo(int section, int position) {
this.section = section;
this.position = position;
}
}
public abstract int getSectionCount();
public abstract int getItemCount(int section);
public abstract S onCreateSectionViewHolder(ViewGroup parent);
public abstract C onCreateItemViewHolder(ViewGroup parent, int viewType);
public abstract void onBindSectionViewHolder(S holder, int section);
public abstract void onBindItemViewHolder(C holder, int section, int position);
}