2020-06-26 14:34:39 +02:00
|
|
|
package app.fedilab.fedilabtube.fragment;
|
2020-07-01 16:36:08 +02:00
|
|
|
/* Copyright 2020 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of TubeLab
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2020-06-26 14:34:39 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
2020-07-09 17:57:01 +02:00
|
|
|
import android.graphics.Rect;
|
2020-06-26 14:34:39 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2020-09-03 18:57:10 +02:00
|
|
|
import android.widget.Button;
|
2020-06-26 14:34:39 +02:00
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
2020-06-27 11:21:25 +02:00
|
|
|
|
2020-06-26 14:34:39 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2020-09-03 18:57:26 +02:00
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
2020-06-26 14:34:39 +02:00
|
|
|
import androidx.fragment.app.Fragment;
|
2020-09-07 11:15:43 +02:00
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2020-07-09 17:57:01 +02:00
|
|
|
import androidx.recyclerview.widget.GridLayoutManager;
|
2020-06-26 14:34:39 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
2020-06-27 11:21:25 +02:00
|
|
|
|
2020-06-26 14:34:39 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2020-06-27 11:21:25 +02:00
|
|
|
|
2020-06-26 14:34:39 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import app.fedilab.fedilabtube.R;
|
|
|
|
import app.fedilab.fedilabtube.client.APIResponse;
|
2020-09-03 18:56:48 +02:00
|
|
|
import app.fedilab.fedilabtube.client.entities.Account;
|
2020-06-26 14:34:39 +02:00
|
|
|
import app.fedilab.fedilabtube.client.entities.Peertube;
|
2020-09-03 18:56:48 +02:00
|
|
|
import app.fedilab.fedilabtube.drawer.AccountsHorizontalListAdapter;
|
2020-06-26 14:34:39 +02:00
|
|
|
import app.fedilab.fedilabtube.drawer.PeertubeAdapter;
|
|
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
2020-09-07 14:14:36 +02:00
|
|
|
import app.fedilab.fedilabtube.viewmodel.AccountsVM;
|
2020-09-07 11:15:43 +02:00
|
|
|
import app.fedilab.fedilabtube.viewmodel.FeedsVM;
|
2020-09-07 14:14:36 +02:00
|
|
|
import app.fedilab.fedilabtube.viewmodel.SearchVM;
|
2020-06-26 14:34:39 +02:00
|
|
|
import es.dmoral.toasty.Toasty;
|
|
|
|
|
2020-09-07 11:15:43 +02:00
|
|
|
import static app.fedilab.fedilabtube.viewmodel.FeedsVM.Type.POVERVIEW;
|
|
|
|
import static app.fedilab.fedilabtube.viewmodel.FeedsVM.Type.PSUBSCRIPTIONS;
|
2020-06-27 14:16:45 +02:00
|
|
|
|
2020-06-26 14:34:39 +02:00
|
|
|
|
2020-09-08 10:11:11 +02:00
|
|
|
public class DisplayStatusFragment extends Fragment implements AccountsHorizontalListAdapter.EventListener {
|
2020-06-26 14:34:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
private LinearLayoutManager mLayoutManager;
|
2020-07-09 17:57:01 +02:00
|
|
|
private GridLayoutManager gLayoutManager;
|
2020-06-26 14:34:39 +02:00
|
|
|
private boolean flag_loading;
|
|
|
|
private Context context;
|
|
|
|
private PeertubeAdapter peertubeAdapater;
|
2020-09-03 18:56:48 +02:00
|
|
|
private AccountsHorizontalListAdapter accountsHorizontalListAdapter;
|
|
|
|
private String max_id, max_id_accounts;
|
2020-06-26 14:34:39 +02:00
|
|
|
private List<Peertube> peertubes;
|
2020-09-03 18:56:48 +02:00
|
|
|
private List<Account> accounts;
|
2020-09-07 11:15:43 +02:00
|
|
|
private FeedsVM.Type type;
|
2020-06-26 14:34:39 +02:00
|
|
|
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
|
|
|
|
private boolean firstLoad;
|
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
|
|
|
private SharedPreferences sharedpreferences;
|
|
|
|
private String search_peertube;
|
|
|
|
private TextView textviewNoActionText;
|
|
|
|
private boolean ischannel;
|
|
|
|
private View rootView;
|
|
|
|
private RecyclerView lv_status;
|
2020-06-27 19:08:52 +02:00
|
|
|
private String targetedId;
|
2020-09-03 18:56:48 +02:00
|
|
|
private boolean check_ScrollingUp;
|
2020-09-03 18:57:10 +02:00
|
|
|
private String forAccount;
|
2020-09-03 18:57:26 +02:00
|
|
|
private ConstraintLayout top_account_container;
|
2020-06-26 14:34:39 +02:00
|
|
|
|
|
|
|
public DisplayStatusFragment() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
rootView = inflater.inflate(R.layout.fragment_video, container, false);
|
|
|
|
|
2020-06-26 18:17:17 +02:00
|
|
|
|
2020-06-26 14:34:39 +02:00
|
|
|
peertubes = new ArrayList<>();
|
2020-09-03 18:56:48 +02:00
|
|
|
accounts = new ArrayList<>();
|
2020-06-26 14:34:39 +02:00
|
|
|
context = getContext();
|
|
|
|
Bundle bundle = this.getArguments();
|
|
|
|
|
|
|
|
if (bundle != null) {
|
|
|
|
search_peertube = bundle.getString("search_peertube", null);
|
2020-06-27 19:08:52 +02:00
|
|
|
targetedId = bundle.getString("targetedid", null);
|
2020-06-26 14:34:39 +02:00
|
|
|
ischannel = bundle.getBoolean("ischannel", false);
|
2020-09-07 11:15:43 +02:00
|
|
|
type = (FeedsVM.Type) bundle.get("type");
|
2020-07-03 17:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getArguments() != null && type == null) {
|
|
|
|
type = DisplayStatusFragmentArgs.fromBundle(getArguments()).getType();
|
2020-06-26 14:34:39 +02:00
|
|
|
}
|
2020-07-03 17:35:28 +02:00
|
|
|
|
|
|
|
|
2020-06-26 14:34:39 +02:00
|
|
|
if (ischannel) {
|
2020-09-07 11:15:43 +02:00
|
|
|
type = FeedsVM.Type.CHANNEL;
|
2020-06-26 14:34:39 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 18:57:10 +02:00
|
|
|
forAccount = null;
|
2020-06-26 14:34:39 +02:00
|
|
|
lv_status = rootView.findViewById(R.id.lv_status);
|
2020-09-03 18:57:26 +02:00
|
|
|
RecyclerView lv_accounts = rootView.findViewById(R.id.lv_accounts);
|
|
|
|
Button display_all = rootView.findViewById(R.id.display_all);
|
|
|
|
top_account_container = rootView.findViewById(R.id.top_account_container);
|
2020-06-26 14:34:39 +02:00
|
|
|
max_id = null;
|
2020-09-03 18:56:48 +02:00
|
|
|
max_id_accounts = null;
|
2020-06-26 14:34:39 +02:00
|
|
|
flag_loading = true;
|
|
|
|
firstLoad = true;
|
2020-09-03 18:56:48 +02:00
|
|
|
check_ScrollingUp = false;
|
2020-06-26 14:34:39 +02:00
|
|
|
|
|
|
|
assert context != null;
|
|
|
|
sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
|
|
|
|
|
|
|
|
mainLoader = rootView.findViewById(R.id.loader);
|
|
|
|
nextElementLoader = rootView.findViewById(R.id.loading_next_status);
|
|
|
|
textviewNoAction = rootView.findViewById(R.id.no_action);
|
|
|
|
textviewNoActionText = rootView.findViewById(R.id.no_action_text);
|
|
|
|
mainLoader.setVisibility(View.VISIBLE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
peertubeAdapater = new PeertubeAdapter(this.peertubes);
|
|
|
|
lv_status.setAdapter(peertubeAdapater);
|
|
|
|
|
2020-09-03 18:57:10 +02:00
|
|
|
accountsHorizontalListAdapter = new AccountsHorizontalListAdapter(this.accounts, this);
|
2020-09-03 18:56:48 +02:00
|
|
|
LinearLayoutManager layoutManager
|
|
|
|
= new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
|
|
|
|
lv_accounts.setLayoutManager(layoutManager);
|
|
|
|
lv_accounts.setAdapter(accountsHorizontalListAdapter);
|
|
|
|
|
2020-07-09 17:57:01 +02:00
|
|
|
if (!Helper.isTablet(context)) {
|
|
|
|
mLayoutManager = new LinearLayoutManager(context);
|
|
|
|
lv_status.setLayoutManager(mLayoutManager);
|
|
|
|
} else {
|
|
|
|
gLayoutManager = new GridLayoutManager(context, 2);
|
|
|
|
int spanCount = (int) Helper.convertDpToPixel(2, context);
|
|
|
|
int spacing = (int) Helper.convertDpToPixel(5, context);
|
|
|
|
lv_status.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, true));
|
|
|
|
lv_status.setLayoutManager(gLayoutManager);
|
|
|
|
}
|
2020-06-26 14:34:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh);
|
2020-09-07 14:14:36 +02:00
|
|
|
//Load data depending of the value
|
|
|
|
if (search_peertube == null) { //Not a Peertube search
|
|
|
|
FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class);
|
|
|
|
viewModel.getVideos(type, "0", targetedId, null).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIewVideos);
|
2020-06-26 14:34:39 +02:00
|
|
|
} else {
|
2020-09-07 14:14:36 +02:00
|
|
|
SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(SearchVM.class);
|
|
|
|
viewModel.getVideos("0", search_peertube).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIewVideos);
|
2020-06-26 14:34:39 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 18:56:48 +02:00
|
|
|
lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
|
|
|
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
|
|
|
|
if (dy > 0) {
|
|
|
|
int visibleItemCount = mLayoutManager.getChildCount();
|
|
|
|
int totalItemCount = mLayoutManager.getItemCount();
|
|
|
|
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
|
2020-09-07 14:14:36 +02:00
|
|
|
AccountsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(AccountsVM.class);
|
2020-09-07 15:03:22 +02:00
|
|
|
viewModel.getAccounts(max_id_accounts, null, AccountsVM.accountFetch.SUBSCRIPTION).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse));
|
2020-09-03 18:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2020-06-27 19:08:52 +02:00
|
|
|
if (type != POVERVIEW) {
|
2020-06-27 14:16:45 +02:00
|
|
|
lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
2020-09-03 18:56:48 +02:00
|
|
|
if (type == PSUBSCRIPTIONS) {
|
|
|
|
if (dy > 0) {
|
|
|
|
if (check_ScrollingUp) {
|
2020-09-03 18:57:26 +02:00
|
|
|
top_account_container.setVisibility(View.GONE);
|
2020-09-03 18:56:48 +02:00
|
|
|
final Handler handler = new Handler();
|
|
|
|
handler.postDelayed(() -> check_ScrollingUp = false, 300);
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!check_ScrollingUp) {
|
2020-09-03 18:57:26 +02:00
|
|
|
top_account_container.setVisibility(View.VISIBLE);
|
2020-09-03 18:56:48 +02:00
|
|
|
final Handler handler = new Handler();
|
|
|
|
handler.postDelayed(() -> check_ScrollingUp = true, 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 17:57:01 +02:00
|
|
|
if (mLayoutManager != null) {
|
|
|
|
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
|
|
|
|
if (dy > 0) {
|
|
|
|
int visibleItemCount = mLayoutManager.getChildCount();
|
|
|
|
int totalItemCount = mLayoutManager.getItemCount();
|
|
|
|
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
|
|
|
|
if (!flag_loading) {
|
|
|
|
flag_loading = true;
|
|
|
|
if (search_peertube == null) { //Not a Peertube search
|
2020-09-07 11:15:43 +02:00
|
|
|
FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class);
|
2020-09-07 14:14:36 +02:00
|
|
|
viewModel.getVideos(type, max_id, null, null).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse));
|
2020-07-09 17:57:01 +02:00
|
|
|
} else {
|
2020-09-07 14:14:36 +02:00
|
|
|
SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(SearchVM.class);
|
|
|
|
viewModel.getVideos(max_id, search_peertube).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse));
|
2020-07-09 17:57:01 +02:00
|
|
|
}
|
|
|
|
nextElementLoader.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (gLayoutManager != null) {
|
|
|
|
int firstVisibleItem = gLayoutManager.findFirstVisibleItemPosition();
|
|
|
|
if (dy > 0) {
|
|
|
|
int visibleItemCount = gLayoutManager.getChildCount();
|
|
|
|
int totalItemCount = gLayoutManager.getItemCount();
|
|
|
|
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
|
|
|
|
if (!flag_loading) {
|
|
|
|
flag_loading = true;
|
|
|
|
if (search_peertube == null) { //Not a Peertube search
|
2020-09-07 11:15:43 +02:00
|
|
|
FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class);
|
2020-09-07 14:14:36 +02:00
|
|
|
viewModel.getVideos(type, max_id, null, null).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse));
|
2020-07-09 17:57:01 +02:00
|
|
|
} else {
|
2020-09-07 14:14:36 +02:00
|
|
|
SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(SearchVM.class);
|
|
|
|
viewModel.getVideos(max_id, search_peertube).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse));
|
2020-07-09 17:57:01 +02:00
|
|
|
}
|
|
|
|
nextElementLoader.setVisibility(View.VISIBLE);
|
2020-06-27 14:16:45 +02:00
|
|
|
}
|
2020-07-09 17:57:01 +02:00
|
|
|
} else {
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2020-06-26 14:34:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-27 14:16:45 +02:00
|
|
|
});
|
|
|
|
}
|
2020-09-03 18:56:48 +02:00
|
|
|
if (type == PSUBSCRIPTIONS) {
|
2020-09-07 14:14:36 +02:00
|
|
|
AccountsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(AccountsVM.class);
|
2020-09-07 15:03:22 +02:00
|
|
|
viewModel.getAccounts(max_id, null, AccountsVM.accountFetch.SUBSCRIPTION).observe(DisplayStatusFragment.this.requireActivity(), this::manageViewAccounts);
|
2020-09-03 18:56:48 +02:00
|
|
|
}
|
2020-06-26 14:34:39 +02:00
|
|
|
|
2020-09-03 18:57:10 +02:00
|
|
|
display_all.setOnClickListener(v -> {
|
|
|
|
forAccount = null;
|
|
|
|
pullToRefresh();
|
|
|
|
});
|
2020-06-26 14:34:39 +02:00
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
if (swipeRefreshLayout != null) {
|
|
|
|
swipeRefreshLayout.setEnabled(false);
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
swipeRefreshLayout.clearAnimation();
|
|
|
|
}
|
|
|
|
if (getActivity() != null) {
|
|
|
|
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
if (imm != null && getView() != null) {
|
|
|
|
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle saveInstance) {
|
|
|
|
super.onCreate(saveInstance);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(@NotNull Context context) {
|
|
|
|
super.onAttach(context);
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2020-09-03 18:56:48 +02:00
|
|
|
|
2020-09-07 14:14:36 +02:00
|
|
|
private void manageViewAccounts(APIResponse apiResponse) {
|
2020-09-03 18:56:48 +02:00
|
|
|
if (apiResponse != null && apiResponse.getAccounts() != null && apiResponse.getAccounts().size() > 0) {
|
2020-09-03 18:57:26 +02:00
|
|
|
if (top_account_container.getVisibility() == View.GONE) {
|
|
|
|
top_account_container.setVisibility(View.VISIBLE);
|
2020-09-03 18:56:48 +02:00
|
|
|
}
|
|
|
|
int previousPosition = accounts.size();
|
|
|
|
accounts.addAll(apiResponse.getAccounts());
|
|
|
|
accountsHorizontalListAdapter.notifyItemRangeInserted(previousPosition, apiResponse.getAccounts().size());
|
2020-09-03 18:57:26 +02:00
|
|
|
if (max_id_accounts == null) {
|
2020-09-03 18:56:48 +02:00
|
|
|
max_id_accounts = "0";
|
2020-09-03 18:57:26 +02:00
|
|
|
}
|
|
|
|
//max_id_accounts needs to work like an offset
|
2020-09-03 18:56:48 +02:00
|
|
|
int tootPerPage = sharedpreferences.getInt(Helper.SET_VIDEOS_PER_PAGE, Helper.VIDEOS_PER_PAGE);
|
|
|
|
max_id_accounts = String.valueOf(Integer.parseInt(max_id_accounts) + tootPerPage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 11:15:43 +02:00
|
|
|
|
2020-09-07 14:14:36 +02:00
|
|
|
private void manageVIewVideos(APIResponse apiResponse) {
|
2020-06-26 14:34:39 +02:00
|
|
|
//hide loaders
|
|
|
|
mainLoader.setVisibility(View.GONE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
|
|
|
//handle other API error but discards 404 - error which can often happen due to toots which have been deleted
|
|
|
|
if (this.peertubes == null || apiResponse == null || (apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404 && apiResponse.getError().getStatusCode() != 501)) {
|
|
|
|
if (apiResponse == null)
|
|
|
|
Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show();
|
|
|
|
else {
|
|
|
|
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
flag_loading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int previousPosition = this.peertubes.size();
|
|
|
|
if (max_id == null)
|
|
|
|
max_id = "0";
|
|
|
|
//max_id needs to work like an offset
|
|
|
|
int tootPerPage = sharedpreferences.getInt(Helper.SET_VIDEOS_PER_PAGE, Helper.VIDEOS_PER_PAGE);
|
|
|
|
max_id = String.valueOf(Integer.parseInt(max_id) + tootPerPage);
|
|
|
|
if (apiResponse.getPeertubes() == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.peertubes.addAll(apiResponse.getPeertubes());
|
|
|
|
//If no item were inserted previously the adapter is created
|
|
|
|
if (previousPosition == 0) {
|
|
|
|
peertubeAdapater = new PeertubeAdapter(this.peertubes);
|
|
|
|
lv_status.setAdapter(peertubeAdapater);
|
|
|
|
} else
|
|
|
|
peertubeAdapater.notifyItemRangeInserted(previousPosition, apiResponse.getPeertubes().size());
|
|
|
|
//remove handlers
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
textviewNoAction.setVisibility(View.GONE);
|
|
|
|
if (firstLoad && (apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0)) {
|
|
|
|
textviewNoActionText.setText(R.string.no_video_to_display);
|
|
|
|
textviewNoAction.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
flag_loading = false;
|
|
|
|
firstLoad = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
if (lv_status != null) {
|
|
|
|
try {
|
|
|
|
lv_status.setAdapter(null);
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.onDestroyView();
|
|
|
|
rootView = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
swipeRefreshLayout.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void scrollToTop() {
|
|
|
|
if (mLayoutManager != null) {
|
|
|
|
mLayoutManager.scrollToPositionWithOffset(0, 0);
|
2020-07-09 17:57:01 +02:00
|
|
|
} else if (gLayoutManager != null) {
|
|
|
|
gLayoutManager.scrollToPositionWithOffset(0, 0);
|
2020-06-26 14:34:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void pullToRefresh() {
|
2020-09-03 18:57:10 +02:00
|
|
|
int size = peertubes.size();
|
|
|
|
peertubes.clear();
|
|
|
|
peertubes = new ArrayList<>();
|
|
|
|
max_id = "0";
|
|
|
|
peertubeAdapater.notifyItemRangeRemoved(0, size);
|
2020-09-03 18:57:26 +02:00
|
|
|
if (forAccount == null) {
|
|
|
|
for (Account account : accounts) {
|
|
|
|
account.setSelected(false);
|
|
|
|
}
|
|
|
|
accountsHorizontalListAdapter.notifyItemRangeRemoved(0, accounts.size());
|
|
|
|
}
|
2020-09-03 18:57:10 +02:00
|
|
|
if (search_peertube == null) { //Not a Peertube search
|
2020-09-07 11:15:43 +02:00
|
|
|
FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class);
|
2020-09-07 14:14:36 +02:00
|
|
|
viewModel.getVideos(type, "0", targetedId, null).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIewVideos);
|
2020-09-03 18:57:10 +02:00
|
|
|
} else {
|
2020-09-07 14:14:36 +02:00
|
|
|
SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(SearchVM.class);
|
|
|
|
viewModel.getVideos("0", search_peertube).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIewVideos);
|
2020-06-26 14:34:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-03 18:57:10 +02:00
|
|
|
@Override
|
|
|
|
public void click(String forAccount) {
|
|
|
|
this.forAccount = forAccount;
|
|
|
|
pullToRefresh();
|
|
|
|
}
|
2020-07-09 17:57:01 +02:00
|
|
|
|
|
|
|
static class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
|
|
|
|
|
|
|
|
private int spanCount;
|
|
|
|
private int spacing;
|
|
|
|
private boolean includeEdge;
|
|
|
|
|
|
|
|
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
|
|
|
|
this.spanCount = spanCount;
|
|
|
|
this.spacing = spacing;
|
|
|
|
this.includeEdge = includeEdge;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, RecyclerView parent, @NotNull RecyclerView.State state) {
|
|
|
|
int position = parent.getChildAdapterPosition(view);
|
|
|
|
int column = position % spanCount;
|
|
|
|
|
|
|
|
if (includeEdge) {
|
|
|
|
outRect.left = spacing - column * spacing / spanCount;
|
|
|
|
outRect.right = (column + 1) * spacing / spanCount;
|
|
|
|
|
|
|
|
if (position < spanCount) {
|
|
|
|
outRect.top = spacing;
|
|
|
|
}
|
|
|
|
outRect.bottom = spacing;
|
|
|
|
} else {
|
|
|
|
outRect.left = column * spacing / spanCount;
|
|
|
|
outRect.right = spacing - (column + 1) * spacing / spanCount;
|
|
|
|
if (position >= spanCount) {
|
|
|
|
outRect.top = spacing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-26 14:34:39 +02:00
|
|
|
}
|