Fix issue #62 - Errors with pull to refresh

This commit is contained in:
Thomas 2020-11-11 11:13:29 +01:00
parent 55bbafdbb5
commit c63048730f
5 changed files with 52 additions and 50 deletions

View File

@ -130,7 +130,7 @@ public class AccountsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
}
position++;
}
if (accounts.size() == 0) {
if (accounts.size() == 0 && allAccountsRemoved != null) {
allAccountsRemoved.onAllAccountsRemoved();
}
}

View File

@ -54,7 +54,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout;
private boolean swiped;
private RecyclerView lv_accounts;
private View rootView;
private RetrofitPeertubeAPI.DataType accountFetch;
@ -75,7 +74,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
max_id = null;
firstLoad = true;
flag_loading = true;
swiped = false;
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
@ -167,7 +165,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
if (apiResponse.getError() != null) {
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
swipeRefreshLayout.setRefreshing(false);
swiped = false;
flag_loading = false;
return;
}
@ -181,21 +178,22 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
}
}
if (!swiped && firstLoad && (accounts == null || accounts.size() == 0))
if (firstLoad && (accounts == null || accounts.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
max_id = apiResponse.getMax_id();
if (swiped) {
accountsListAdapter = new AccountsListAdapter(accountFetch, this.accounts);
lv_accounts.setAdapter(accountsListAdapter);
swiped = false;
}
if (accounts != null && accounts.size() > 0) {
int previousPosition = this.accounts.size();
int currentPosition = this.accounts.size();
this.accounts.addAll(accounts);
accountsListAdapter.notifyItemRangeChanged(currentPosition, accounts.size());
if (previousPosition == 0) {
accountsListAdapter = new AccountsListAdapter(accountFetch, this.accounts);
accountsListAdapter.allAccountsRemoved = this;
lv_accounts.setAdapter(accountsListAdapter);
} else
accountsListAdapter.notifyItemRangeChanged(currentPosition, accounts.size());
}
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
@ -206,7 +204,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
accounts = new ArrayList<>();
firstLoad = true;
flag_loading = true;
swiped = true;
swipeRefreshLayout.setRefreshing(true);
AccountsVM viewModel = new ViewModelProvider(this).get(AccountsVM.class);
viewModel.getAccounts(RetrofitPeertubeAPI.DataType.MUTED, null).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts);

View File

@ -63,7 +63,6 @@ public class DisplayChannelsFragment extends Fragment implements ChannelListAdap
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private SwipeRefreshLayout swipeRefreshLayout;
private String name;
private boolean swiped;
private RecyclerView lv_channels;
private View rootView;
private FloatingActionButton action_button;
@ -83,7 +82,6 @@ public class DisplayChannelsFragment extends Fragment implements ChannelListAdap
myChannels = bundle.getBoolean("myChannels", true);
}
swiped = false;
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
@ -165,26 +163,25 @@ public class DisplayChannelsFragment extends Fragment implements ChannelListAdap
if (apiResponse.getError() != null) {
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
swipeRefreshLayout.setRefreshing(false);
swiped = false;
return;
}
List<ChannelData.Channel> channels = apiResponse.getChannels();
if (!swiped && (channels == null || channels.size() == 0))
if ((channels == null || channels.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
if (swiped) {
channelListAdapter = new ChannelListAdapter(this.channels, myChannels);
channelListAdapter.allChannelRemoved = DisplayChannelsFragment.this;
channelListAdapter.editAlertDialog = DisplayChannelsFragment.this;
lv_channels.setAdapter(channelListAdapter);
swiped = false;
}
if (channels != null && channels.size() > 0) {
int currentPosition = this.channels.size();
this.channels.addAll(channels);
channelListAdapter.notifyItemRangeChanged(currentPosition, channels.size());
if (currentPosition == 0) {
channelListAdapter = new ChannelListAdapter(this.channels, myChannels);
channelListAdapter.allChannelRemoved = DisplayChannelsFragment.this;
channelListAdapter.editAlertDialog = DisplayChannelsFragment.this;
lv_channels.setAdapter(channelListAdapter);
} else {
channelListAdapter.notifyItemRangeChanged(currentPosition, channels.size());
}
}
swipeRefreshLayout.setRefreshing(false);
}
@ -192,7 +189,6 @@ public class DisplayChannelsFragment extends Fragment implements ChannelListAdap
public void pullToRefresh() {
channels = new ArrayList<>();
swiped = true;
swipeRefreshLayout.setRefreshing(true);
ChannelsVM viewModel = new ViewModelProvider(this).get(ChannelsVM.class);
if (name != null) {

View File

@ -72,9 +72,9 @@ public class DisplayNotificationsFragment extends Fragment {
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout;
private boolean swiped;
private RecyclerView lv_notifications;
private View rootView;
private NotificationsVM viewModel;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -86,10 +86,10 @@ public class DisplayNotificationsFragment extends Fragment {
max_id = "0";
firstLoad = true;
flag_loading = true;
swiped = false;
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
viewModel = new ViewModelProvider(this).get(NotificationsVM.class);
lv_notifications = rootView.findViewById(R.id.lv_elements);
lv_notifications.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
@ -115,7 +115,6 @@ public class DisplayNotificationsFragment extends Fragment {
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) {
flag_loading = true;
NotificationsVM viewModel = new ViewModelProvider(DisplayNotificationsFragment.this).get(NotificationsVM.class);
viewModel.getNotifications(null, max_id).observe(DisplayNotificationsFragment.this.requireActivity(), apiResponse -> manageVIewNotifications(apiResponse));
nextElementLoader.setVisibility(View.VISIBLE);
}
@ -127,8 +126,7 @@ public class DisplayNotificationsFragment extends Fragment {
});
swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh);
NotificationsVM viewModel = new ViewModelProvider(this).get(NotificationsVM.class);
viewModel.getNotifications(null, null).observe(DisplayNotificationsFragment.this.requireActivity(), this::manageVIewNotifications);
viewModel.getNotifications(null, "0").observe(DisplayNotificationsFragment.this.requireActivity(), this::manageVIewNotifications);
return rootView;
}
@ -138,9 +136,22 @@ public class DisplayNotificationsFragment extends Fragment {
rootView = null;
}
@Override
public void onPause() {
super.onPause();
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setEnabled(false);
swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.clearAnimation();
}
}
@Override
public void onResume() {
super.onResume();
swipeRefreshLayout.setEnabled(true);
if (getActivity() != null && getActivity() != null) {
View action_button = getActivity().findViewById(R.id.action_button);
if (action_button != null) {
@ -172,15 +183,15 @@ public class DisplayNotificationsFragment extends Fragment {
public void pullToRefresh() {
max_id = "0";
int size = notifications.size();
notifications.clear();
notifications = new ArrayList<>();
max_id = "0";
peertubeNotificationsListAdapter.notifyItemRangeRemoved(0, size);
firstLoad = true;
flag_loading = true;
swiped = true;
swipeRefreshLayout.setRefreshing(true);
NotificationsVM viewModel = new ViewModelProvider(this).get(NotificationsVM.class);
viewModel.getNotifications(null, null).observe(DisplayNotificationsFragment.this.requireActivity(), this::manageVIewNotifications);
viewModel.getNotifications(null, "0").observe(DisplayNotificationsFragment.this.requireActivity(), this::manageVIewNotifications);
}
private void manageVIewNotifications(APIResponse apiResponse) {
@ -190,28 +201,25 @@ public class DisplayNotificationsFragment extends Fragment {
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
flag_loading = false;
swipeRefreshLayout.setRefreshing(false);
swiped = false;
return;
}
int previousPosition = notifications.size();
max_id = String.valueOf(Integer.parseInt(max_id) + 20);
List<Notification> notifications = apiResponse.getPeertubeNotifications();
if (!swiped && firstLoad && (notifications == null || notifications.size() == 0))
if (firstLoad && (notifications == null || notifications.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
if (swiped) {
if (previousPosition > 0) {
this.notifications.subList(0, previousPosition).clear();
peertubeNotificationsListAdapter.notifyItemRangeRemoved(0, previousPosition);
}
swiped = false;
}
if (notifications != null && notifications.size() > 0) {
this.notifications.addAll(notifications);
peertubeNotificationsListAdapter.notifyItemRangeInserted(previousPosition, notifications.size());
if (previousPosition == 0) {
peertubeNotificationsListAdapter = new PeertubeNotificationsListAdapter(this.notifications);
lv_notifications.setAdapter(peertubeNotificationsListAdapter);
} else
peertubeNotificationsListAdapter.notifyItemRangeInserted(previousPosition, notifications.size());
} else {
if (firstLoad)
textviewNoAction.setVisibility(View.VISIBLE);

View File

@ -245,6 +245,12 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta
return rootView;
}
@Override
public void onResume() {
super.onResume();
swipeRefreshLayout.setEnabled(true);
}
@Override
public void onPause() {
@ -434,11 +440,6 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta
rootView = null;
}
@Override
public void onResume() {
super.onResume();
swipeRefreshLayout.setEnabled(true);
}
public void scrollToTop() {