2017-01-23 16:19:30 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* This file is a part of Tusky.
|
2017-01-23 16:19:30 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* 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.
|
2017-01-23 16:19:30 +11:00
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-04-10 10:12:31 +10:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
2017-01-23 16:19:30 +11:00
|
|
|
*
|
2017-04-10 10:12:31 +10:00
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2017-01-23 16:19:30 +11:00
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
2017-03-20 13:38:39 +11:00
|
|
|
import android.support.annotation.Nullable;
|
2017-01-23 16:19:30 +11:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2017-03-09 10:27:37 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Status;
|
|
|
|
|
2017-01-23 16:19:30 +11:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
class ThreadAdapter extends RecyclerView.Adapter implements AdapterItemRemover {
|
2017-01-23 16:19:30 +11:00
|
|
|
private List<Status> statuses;
|
|
|
|
private StatusActionListener statusActionListener;
|
|
|
|
private int statusIndex;
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
ThreadAdapter(StatusActionListener listener) {
|
2017-01-23 16:19:30 +11:00
|
|
|
this.statusActionListener = listener;
|
|
|
|
this.statuses = new ArrayList<>();
|
|
|
|
this.statusIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
View view = LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.item_status, parent, false);
|
|
|
|
return new StatusViewHolder(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
|
|
|
|
StatusViewHolder holder = (StatusViewHolder) viewHolder;
|
|
|
|
Status status = statuses.get(position);
|
2017-03-08 15:52:17 +11:00
|
|
|
holder.setupWithStatus(status, statusActionListener);
|
2017-01-23 16:19:30 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return statuses.size();
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:13:51 +11:00
|
|
|
Status getItem(int position) {
|
2017-01-23 16:19:30 +11:00
|
|
|
return statuses.get(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeItem(int position) {
|
|
|
|
statuses.remove(position);
|
|
|
|
notifyItemRemoved(position);
|
|
|
|
}
|
|
|
|
|
2017-04-15 20:28:22 +10:00
|
|
|
int setStatus(Status status) {
|
|
|
|
if (statuses.size() > 0 && statuses.get(statusIndex).equals(status)) {
|
|
|
|
// Do not add this status on refresh, it's already in there.
|
|
|
|
statuses.set(statusIndex, status);
|
|
|
|
return statusIndex;
|
|
|
|
}
|
2017-01-23 16:19:30 +11:00
|
|
|
int i = statusIndex;
|
|
|
|
statuses.add(i, status);
|
|
|
|
notifyItemInserted(i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-04-15 20:28:22 +10:00
|
|
|
void setContext(List<Status> ancestors, List<Status> descendants) {
|
|
|
|
Status mainStatus = null;
|
|
|
|
|
|
|
|
// In case of refresh, remove old ancestors and descendants first. We'll remove all blindly,
|
|
|
|
// as we have no guarantee on their order to be the same as before
|
|
|
|
int old_size = statuses.size();
|
|
|
|
if (old_size > 0) {
|
|
|
|
mainStatus = statuses.get(statusIndex);
|
|
|
|
statuses.clear();
|
|
|
|
notifyItemRangeRemoved(0, old_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert newly fetched ancestors
|
2017-01-23 16:19:30 +11:00
|
|
|
statusIndex = ancestors.size();
|
|
|
|
statuses.addAll(0, ancestors);
|
|
|
|
notifyItemRangeInserted(0, statusIndex);
|
|
|
|
|
2017-04-15 20:28:22 +10:00
|
|
|
if (mainStatus != null) {
|
|
|
|
// In case we needed to delete everything (which is way easier than deleting
|
|
|
|
// everything except one), re-insert the remaining status here.
|
|
|
|
statuses.add(statusIndex, mainStatus);
|
|
|
|
notifyItemInserted(statusIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert newly fetched descendants
|
2017-01-23 16:19:30 +11:00
|
|
|
int end = statuses.size();
|
|
|
|
statuses.addAll(descendants);
|
|
|
|
notifyItemRangeInserted(end, descendants.size());
|
|
|
|
}
|
|
|
|
}
|