2017-06-30 01:55:39 +10:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.adapter;
|
|
|
|
|
2017-07-06 00:36:14 +10:00
|
|
|
import android.content.Context;
|
2017-06-30 01:55:39 +10:00
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.keylesspalace.tusky.R;
|
|
|
|
import com.keylesspalace.tusky.db.TootEntity;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class SavedTootAdapter extends RecyclerView.Adapter {
|
|
|
|
private List<TootEntity> list;
|
2017-07-06 00:36:14 +10:00
|
|
|
private SavedTootAction handler;
|
2017-06-30 01:55:39 +10:00
|
|
|
|
2017-07-06 00:36:14 +10:00
|
|
|
public SavedTootAdapter(Context context) {
|
2017-06-30 01:55:39 +10:00
|
|
|
super();
|
|
|
|
list = new ArrayList<>();
|
2017-07-06 00:36:14 +10:00
|
|
|
handler = (SavedTootAction) context;
|
2017-06-30 01:55:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
View view = LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.item_saved_toot, parent, false);
|
|
|
|
return new TootViewHolder(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
|
|
|
|
TootViewHolder holder = (TootViewHolder) viewHolder;
|
2017-12-04 07:56:47 +11:00
|
|
|
holder.bind(getItem(position));
|
2017-06-30 01:55:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
2017-07-06 00:36:14 +10:00
|
|
|
return list.size();
|
2017-06-30 01:55:39 +10:00
|
|
|
}
|
|
|
|
|
2017-07-07 20:32:04 +10:00
|
|
|
public void setItems(List<TootEntity> newToot) {
|
|
|
|
list = new ArrayList<>();
|
|
|
|
list.addAll(newToot);
|
|
|
|
}
|
|
|
|
|
2017-06-30 01:55:39 +10:00
|
|
|
public void addItems(List<TootEntity> newToot) {
|
|
|
|
int end = list.size();
|
|
|
|
list.addAll(newToot);
|
|
|
|
notifyItemRangeInserted(end, newToot.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
public TootEntity removeItem(int position) {
|
|
|
|
if (position < 0 || position >= list.size()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
TootEntity toot = list.remove(position);
|
|
|
|
notifyItemRemoved(position);
|
|
|
|
return toot;
|
|
|
|
}
|
|
|
|
|
2017-10-18 09:20:26 +11:00
|
|
|
private TootEntity getItem(int position) {
|
2017-06-30 01:55:39 +10:00
|
|
|
if (position >= 0 && position < list.size()) {
|
|
|
|
return list.get(position);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-06 00:36:14 +10:00
|
|
|
// handler saved toot
|
|
|
|
public interface SavedTootAction {
|
|
|
|
void delete(int position, TootEntity item);
|
|
|
|
|
|
|
|
void click(int position, TootEntity item);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class TootViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
View view;
|
|
|
|
TextView content;
|
|
|
|
ImageButton suppr;
|
2017-06-30 01:55:39 +10:00
|
|
|
|
|
|
|
TootViewHolder(View view) {
|
|
|
|
super(view);
|
2017-07-06 00:36:14 +10:00
|
|
|
this.view = view;
|
2017-10-18 09:20:26 +11:00
|
|
|
this.content = view.findViewById(R.id.content);
|
|
|
|
this.suppr = view.findViewById(R.id.suppr);
|
2017-06-30 01:55:39 +10:00
|
|
|
}
|
|
|
|
|
2017-12-04 07:56:47 +11:00
|
|
|
void bind(final TootEntity item) {
|
|
|
|
suppr.setEnabled(true);
|
|
|
|
|
2017-07-06 00:36:14 +10:00
|
|
|
if (item != null) {
|
2017-12-04 07:56:47 +11:00
|
|
|
content.setText(item.getText());
|
|
|
|
|
|
|
|
suppr.setOnClickListener(v -> {
|
|
|
|
v.setEnabled(false);
|
|
|
|
handler.delete(getAdapterPosition(), item);
|
2017-07-06 00:36:14 +10:00
|
|
|
});
|
2017-12-04 07:56:47 +11:00
|
|
|
view.setOnClickListener(v -> handler.click(getAdapterPosition(), item));
|
2017-07-06 00:36:14 +10:00
|
|
|
}
|
2017-06-30 01:55:39 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|