Search in sources :

Example 6 with StatusViewData

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);
}
Also used : Status(com.keylesspalace.tusky.entity.Status) StatusViewData(com.keylesspalace.tusky.viewdata.StatusViewData)

Example 7 with StatusViewData

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);
    }
}
Also used : StatusViewData(com.keylesspalace.tusky.viewdata.StatusViewData)

Example 8 with StatusViewData

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);
        }
    });
}
Also used : Status(com.keylesspalace.tusky.entity.Status) StatusViewData(com.keylesspalace.tusky.viewdata.StatusViewData) Pair(android.support.v4.util.Pair)

Example 9 with StatusViewData

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");
    }
}
Also used : Status(com.keylesspalace.tusky.entity.Status) StatusViewData(com.keylesspalace.tusky.viewdata.StatusViewData)

Example 10 with StatusViewData

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);
}
Also used : StatusViewData(com.keylesspalace.tusky.viewdata.StatusViewData) Pair(android.support.v4.util.Pair) Nullable(android.support.annotation.Nullable)

Aggregations

StatusViewData (com.keylesspalace.tusky.viewdata.StatusViewData)12 Status (com.keylesspalace.tusky.entity.Status)5 Pair (android.support.v4.util.Pair)3 NotificationViewData (com.keylesspalace.tusky.viewdata.NotificationViewData)2 Nullable (android.support.annotation.Nullable)1