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++; position++;
} }
if (accounts.size() == 0) { if (accounts.size() == 0 && allAccountsRemoved != null) {
allAccountsRemoved.onAllAccountsRemoved(); allAccountsRemoved.onAllAccountsRemoved();
} }
} }

View File

@ -54,7 +54,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction; private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad; private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout; private SwipeRefreshLayout swipeRefreshLayout;
private boolean swiped;
private RecyclerView lv_accounts; private RecyclerView lv_accounts;
private View rootView; private View rootView;
private RetrofitPeertubeAPI.DataType accountFetch; private RetrofitPeertubeAPI.DataType accountFetch;
@ -75,7 +74,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
max_id = null; max_id = null;
firstLoad = true; firstLoad = true;
flag_loading = true; flag_loading = true;
swiped = false;
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer); swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
@ -167,7 +165,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
if (apiResponse.getError() != null) { if (apiResponse.getError() != null) {
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show(); Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
swiped = false;
flag_loading = false; flag_loading = false;
return; 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); textviewNoAction.setVisibility(View.VISIBLE);
else else
textviewNoAction.setVisibility(View.GONE); textviewNoAction.setVisibility(View.GONE);
max_id = apiResponse.getMax_id(); 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) { if (accounts != null && accounts.size() > 0) {
int previousPosition = this.accounts.size();
int currentPosition = this.accounts.size(); int currentPosition = this.accounts.size();
this.accounts.addAll(accounts); 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); swipeRefreshLayout.setRefreshing(false);
firstLoad = false; firstLoad = false;
@ -206,7 +204,6 @@ public class DisplayAccountsFragment extends Fragment implements AccountsListAda
accounts = new ArrayList<>(); accounts = new ArrayList<>();
firstLoad = true; firstLoad = true;
flag_loading = true; flag_loading = true;
swiped = true;
swipeRefreshLayout.setRefreshing(true); swipeRefreshLayout.setRefreshing(true);
AccountsVM viewModel = new ViewModelProvider(this).get(AccountsVM.class); AccountsVM viewModel = new ViewModelProvider(this).get(AccountsVM.class);
viewModel.getAccounts(RetrofitPeertubeAPI.DataType.MUTED, null).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); 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 RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private SwipeRefreshLayout swipeRefreshLayout; private SwipeRefreshLayout swipeRefreshLayout;
private String name; private String name;
private boolean swiped;
private RecyclerView lv_channels; private RecyclerView lv_channels;
private View rootView; private View rootView;
private FloatingActionButton action_button; private FloatingActionButton action_button;
@ -83,7 +82,6 @@ public class DisplayChannelsFragment extends Fragment implements ChannelListAdap
myChannels = bundle.getBoolean("myChannels", true); myChannels = bundle.getBoolean("myChannels", true);
} }
swiped = false;
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer); swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
@ -165,26 +163,25 @@ public class DisplayChannelsFragment extends Fragment implements ChannelListAdap
if (apiResponse.getError() != null) { if (apiResponse.getError() != null) {
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show(); Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
swiped = false;
return; return;
} }
List<ChannelData.Channel> channels = apiResponse.getChannels(); List<ChannelData.Channel> channels = apiResponse.getChannels();
if (!swiped && (channels == null || channels.size() == 0)) if ((channels == null || channels.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE); textviewNoAction.setVisibility(View.VISIBLE);
else else
textviewNoAction.setVisibility(View.GONE); 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) { if (channels != null && channels.size() > 0) {
int currentPosition = this.channels.size(); int currentPosition = this.channels.size();
this.channels.addAll(channels); 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); swipeRefreshLayout.setRefreshing(false);
} }
@ -192,7 +189,6 @@ public class DisplayChannelsFragment extends Fragment implements ChannelListAdap
public void pullToRefresh() { public void pullToRefresh() {
channels = new ArrayList<>(); channels = new ArrayList<>();
swiped = true;
swipeRefreshLayout.setRefreshing(true); swipeRefreshLayout.setRefreshing(true);
ChannelsVM viewModel = new ViewModelProvider(this).get(ChannelsVM.class); ChannelsVM viewModel = new ViewModelProvider(this).get(ChannelsVM.class);
if (name != null) { if (name != null) {

View File

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

View File

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