use of com.keylesspalace.tusky.viewdata.StatusViewData in project Tusky by tuskyapp.
the class ViewThreadFragment method setContext.
private 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 oldSize = statuses.size();
if (oldSize > 1) {
mainStatus = statuses.get(statusIndex);
statuses.clear();
adapter.clearItems();
}
// Insert newly fetched ancestors
statusIndex = ancestors.size();
adapter.setDetailedStatusPosition(statusIndex);
statuses.addAll(0, ancestors);
List<StatusViewData.Concrete> ancestorsViewDatas = statuses.getPairedCopy().subList(0, statusIndex);
if (BuildConfig.DEBUG && ancestors.size() != ancestorsViewDatas.size()) {
String error = String.format(Locale.getDefault(), "Incorrectly got statusViewData sublist." + " ancestors.size == %d ancestorsViewDatas.size == %d," + " statuses.size == %d", ancestors.size(), ancestorsViewDatas.size(), statuses.size());
throw new AssertionError(error);
}
adapter.addAll(0, ancestorsViewDatas);
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);
StatusViewData.Concrete viewData = statuses.getPairedItem(statusIndex);
if (viewData.getCard() == null && card != null) {
viewData = new StatusViewData.Builder(viewData).setCard(card).createStatusViewData();
statuses.setPairedItem(statusIndex, viewData);
}
adapter.addItem(statusIndex, viewData);
}
// Insert newly fetched descendants
statuses.addAll(descendants);
List<StatusViewData.Concrete> descendantsViewData;
descendantsViewData = statuses.getPairedCopy().subList(statuses.size() - descendants.size(), statuses.size());
if (BuildConfig.DEBUG && descendants.size() != descendantsViewData.size()) {
String error = String.format(Locale.getDefault(), "Incorrectly got statusViewData sublist." + " descendants.size == %d descendantsViewData.size == %d," + " statuses.size == %d", descendants.size(), descendantsViewData.size(), statuses.size());
throw new AssertionError(error);
}
adapter.addAll(descendantsViewData);
}
use of com.keylesspalace.tusky.viewdata.StatusViewData in project Tusky by tuskyapp.
the class TimelineAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
if (position < statuses.size()) {
StatusViewData status = statuses.get(position);
if (status instanceof StatusViewData.Placeholder) {
PlaceholderViewHolder holder = (PlaceholderViewHolder) viewHolder;
holder.setup(!((StatusViewData.Placeholder) status).isLoading(), statusListener);
} else {
StatusViewHolder holder = (StatusViewHolder) viewHolder;
holder.setupWithStatus((StatusViewData.Concrete) status, statusListener, mediaPreviewEnabled);
}
} else {
FooterViewHolder holder = (FooterViewHolder) viewHolder;
holder.setState(footerState);
}
}
use of com.keylesspalace.tusky.viewdata.StatusViewData in project Tusky by tuskyapp.
the class TimelineFragment method onReblog.
@Override
public void onReblog(final boolean reblog, final int position) {
final Status status = statuses.get(position).getAsRight();
timelineCases.reblogWithCallback(status, reblog, new Callback<Status>() {
@Override
public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
if (response.isSuccessful()) {
status.setReblogged(reblog);
if (status.getReblog() != null) {
status.getReblog().setReblogged(reblog);
}
Pair<StatusViewData.Concrete, Integer> actual = findStatusAndPosition(position, status);
if (actual == null)
return;
StatusViewData newViewData = new StatusViewData.Builder(actual.first).setReblogged(reblog).createStatusViewData();
statuses.setPairedItem(actual.second, newViewData);
adapter.changeItem(actual.second, newViewData, false);
}
}
@Override
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
Log.d(TAG, "Failed to reblog status " + status.getId(), t);
}
});
}
use of com.keylesspalace.tusky.viewdata.StatusViewData in project Tusky by tuskyapp.
the class TimelineFragment method onLoadMore.
@Override
public void onLoadMore(int position) {
// check bounds before accessing list,
if (statuses.size() >= position && position > 0) {
Status fromStatus = statuses.get(position - 1).getAsRightOrNull();
Status toStatus = statuses.get(position + 1).getAsRightOrNull();
if (fromStatus == null || toStatus == null) {
Log.e(TAG, "Failed to load more at " + position + ", wrong placeholder position");
return;
}
sendFetchTimelineRequest(fromStatus.getId(), toStatus.getId(), FetchEnd.MIDDLE, position);
StatusViewData newViewData = new StatusViewData.Placeholder(true);
statuses.setPairedItem(position, newViewData);
adapter.changeItem(position, newViewData, false);
} else {
Log.e(TAG, "error loading more");
}
}
use of com.keylesspalace.tusky.viewdata.StatusViewData in project Tusky by tuskyapp.
the class TimelineFragment method findStatusAndPosition.
@Nullable
private Pair<StatusViewData.Concrete, Integer> findStatusAndPosition(int position, Status status) {
StatusViewData.Concrete statusToUpdate;
int positionToUpdate;
StatusViewData someOldViewData = statuses.getPairedItem(position);
// Unlikely, but data could change between the request and response
if ((someOldViewData instanceof StatusViewData.Placeholder) || !((StatusViewData.Concrete) someOldViewData).getId().equals(status.getId())) {
// try to find the status we need to update
int foundPos = statuses.indexOf(Either.<Placeholder, Status>right(status));
// okay, it's hopeless, give up
if (foundPos < 0)
return null;
statusToUpdate = ((StatusViewData.Concrete) statuses.getPairedItem(foundPos));
positionToUpdate = position;
} else {
statusToUpdate = (StatusViewData.Concrete) someOldViewData;
positionToUpdate = position;
}
return new Pair<>(statusToUpdate, positionToUpdate);
}
Aggregations