From 9807ee5f54f96f5dc6dd825cc76d874ba54a3ce0 Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Fri, 2 Mar 2018 20:56:09 +0100 Subject: [PATCH] fix link header handling in AccountActivityFragment - fixes douplication of users after undoing unblocks/unmutes - removes unnecessary loading when scrolled to end of list --- .../tusky/adapter/AccountAdapter.java | 12 ++-- .../tusky/fragment/AccountListFragment.java | 59 ++++++++----------- 2 files changed, 29 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/AccountAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/AccountAdapter.java index 699a33e3..65e36e8d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/AccountAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/AccountAdapter.java @@ -46,16 +46,14 @@ public abstract class AccountAdapter extends RecyclerView.Adapter { } public void update(@Nullable List newAccounts, @Nullable String fromId, - @Nullable String uptoId) { + @Nullable String uptoId) { if (newAccounts == null || newAccounts.isEmpty()) { return; } - if (fromId != null) { - bottomId = fromId; - } - if (uptoId != null) { - topId = uptoId; - } + + bottomId = fromId; + topId = uptoId; + if (accountList.isEmpty()) { accountList = ListUtils.removeDuplicates(newAccounts); } else { diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/AccountListFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/AccountListFragment.java index 679ab67f..57c5435f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/AccountListFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/AccountListFragment.java @@ -19,6 +19,7 @@ import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.Snackbar; import android.support.v7.widget.DividerItemDecoration; @@ -29,20 +30,19 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; - import com.keylesspalace.tusky.AccountActivity; +import com.keylesspalace.tusky.BaseActivity; +import com.keylesspalace.tusky.R; import com.keylesspalace.tusky.adapter.AccountAdapter; import com.keylesspalace.tusky.adapter.BlocksAdapter; import com.keylesspalace.tusky.adapter.FollowAdapter; import com.keylesspalace.tusky.adapter.FollowRequestsAdapter; import com.keylesspalace.tusky.adapter.FooterViewHolder; import com.keylesspalace.tusky.adapter.MutesAdapter; -import com.keylesspalace.tusky.BaseActivity; import com.keylesspalace.tusky.entity.Account; import com.keylesspalace.tusky.entity.Relationship; import com.keylesspalace.tusky.interfaces.AccountActionListener; import com.keylesspalace.tusky.network.MastodonApi; -import com.keylesspalace.tusky.R; import com.keylesspalace.tusky.util.HttpHeaderLink; import com.keylesspalace.tusky.util.ThemeUtils; import com.keylesspalace.tusky.view.EndlessOnScrollListener; @@ -107,7 +107,7 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi @Nullable @Override - public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_account_list, container, false); @@ -161,6 +161,9 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi }; recyclerView.addOnScrollListener(scrollListener); + + fetchAccounts(null, null, FetchEnd.BOTTOM); + } @Override @@ -182,7 +185,7 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi Callback callback = new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { onMuteSuccess(mute, id, position); } else { @@ -191,7 +194,7 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(@NonNull Call call, @NonNull Throwable t) { onMuteFailure(mute, id); } }; @@ -212,12 +215,9 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi } final MutesAdapter mutesAdapter = (MutesAdapter) adapter; final Account unmutedUser = mutesAdapter.removeItem(position); - View.OnClickListener listener = new View.OnClickListener() { - @Override - public void onClick(View v) { - mutesAdapter.addItem(unmutedUser, position); - onMute(true, id, position); - } + View.OnClickListener listener = v -> { + mutesAdapter.addItem(unmutedUser, position); + onMute(true, id, position); }; Snackbar.make(recyclerView, R.string.confirmation_unmuted, Snackbar.LENGTH_LONG) .setAction(R.string.action_undo, listener) @@ -246,7 +246,7 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi Callback cb = new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { onBlockSuccess(block, id, position); } else { @@ -255,7 +255,7 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(@NonNull Call call, @NonNull Throwable t) { onBlockFailure(block, id); } }; @@ -276,12 +276,9 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi } final BlocksAdapter blocksAdapter = (BlocksAdapter) adapter; final Account unblockedUser = blocksAdapter.removeItem(position); - View.OnClickListener listener = new View.OnClickListener() { - @Override - public void onClick(View v) { - blocksAdapter.addItem(unblockedUser, position); - onBlock(true, id, position); - } + View.OnClickListener listener = v -> { + blocksAdapter.addItem(unblockedUser, position); + onBlock(true, id, position); }; Snackbar.make(recyclerView, R.string.confirmation_unblocked, Snackbar.LENGTH_LONG) .setAction(R.string.action_undo, listener) @@ -311,7 +308,7 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi Callback callback = new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { onRespondToFollowRequestSuccess(position); } else { @@ -320,7 +317,7 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(@NonNull Call call, @NonNull Throwable t) { onRespondToFollowRequestFailure(accept, accountId); } }; @@ -351,11 +348,6 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi Log.e(TAG, message); } - private void jumpToTop() { - layoutManager.scrollToPositionWithOffset(0, 0); - scrollListener.reset(); - } - private enum FetchEnd { TOP, BOTTOM @@ -393,17 +385,12 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi /* When this is called by the EndlessScrollListener it cannot refresh the footer state * using adapter.notifyItemChanged. So its necessary to postpone doing so until a * convenient time for the UI thread using a Runnable. */ - recyclerView.post(new Runnable() { - @Override - public void run() { - adapter.setFooterState(FooterViewHolder.State.LOADING); - } - }); + recyclerView.post(() -> adapter.setFooterState(FooterViewHolder.State.LOADING)); } Callback> cb = new Callback>() { @Override - public void onResponse(Call> call, Response> response) { + public void onResponse(@NonNull Call> call, @NonNull Response> response) { if (response.isSuccessful()) { String linkHeader = response.headers().get("Link"); onFetchAccountsSuccess(response.body(), linkHeader, fetchEnd); @@ -413,7 +400,7 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi } @Override - public void onFailure(Call> call, Throwable t) { + public void onFailure(@NonNull Call> call, @NonNull Throwable t) { onFetchAccountsFailure((Exception) t, fetchEnd); } }; @@ -476,6 +463,8 @@ public class AccountListFragment extends BaseFragment implements AccountActionLi private void onLoadMore(RecyclerView recyclerView) { AccountAdapter adapter = (AccountAdapter) recyclerView.getAdapter(); + //if we do not have a bottom id, we know we do not need to load more + if(adapter.getBottomId() == null) return; fetchAccounts(adapter.getBottomId(), null, FetchEnd.BOTTOM); }