2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.fragments;
|
2017-06-07 11:12:40 +02:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2017-06-07 11:12:40 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-06-07 11:12:40 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
2017-06-07 11:12:40 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
2018-11-25 10:45:16 +01:00
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
|
|
2021-01-19 17:43:51 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2017-06-07 11:12:40 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2017-10-29 09:30:08 +01:00
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
import app.fedilab.android.R;
|
|
|
|
import app.fedilab.android.asynctasks.RetrieveFollowRequestSentAsyncTask;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.client.APIResponse;
|
|
|
|
import app.fedilab.android.client.Entities.Account;
|
|
|
|
import app.fedilab.android.drawers.AccountsFollowRequestAdapter;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.interfaces.OnRetrieveAccountsInterface;
|
2019-11-15 16:32:25 +01:00
|
|
|
import es.dmoral.toasty.Toasty;
|
2017-06-07 11:12:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 07/06/2017.
|
|
|
|
* Fragment to display follow requests for the authenticated account
|
|
|
|
*/
|
|
|
|
public class DisplayFollowRequestSentFragment extends Fragment implements OnRetrieveAccountsInterface {
|
|
|
|
|
|
|
|
|
|
|
|
private boolean flag_loading;
|
|
|
|
private Context context;
|
|
|
|
private AccountsFollowRequestAdapter accountsFollowRequestAdapter;
|
|
|
|
private String max_id;
|
|
|
|
private List<Account> accounts;
|
|
|
|
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
|
|
|
|
private boolean firstLoad;
|
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
|
|
|
private int accountPerPage;
|
|
|
|
private TextView no_action_text;
|
2017-06-11 17:14:54 +02:00
|
|
|
private boolean swiped;
|
2017-10-29 09:30:08 +01:00
|
|
|
private RecyclerView lv_accounts;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2017-06-07 11:12:40 +02:00
|
|
|
@Override
|
2017-12-17 14:20:04 +01:00
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2017-06-07 11:12:40 +02:00
|
|
|
|
|
|
|
//View for fragment is the same that fragment accounts
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_accounts, container, false);
|
|
|
|
|
|
|
|
context = getContext();
|
|
|
|
accounts = new ArrayList<>();
|
|
|
|
max_id = null;
|
|
|
|
firstLoad = true;
|
|
|
|
flag_loading = true;
|
2017-06-11 17:14:54 +02:00
|
|
|
swiped = false;
|
2017-06-07 11:12:40 +02:00
|
|
|
|
2017-10-28 11:02:37 +02:00
|
|
|
swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);
|
2019-11-12 21:27:02 +01:00
|
|
|
int c1 = getResources().getColor(R.color.cyanea_accent);
|
|
|
|
int c2 = getResources().getColor(R.color.cyanea_primary_dark);
|
|
|
|
int c3 = getResources().getColor(R.color.cyanea_primary);
|
|
|
|
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(c3);
|
|
|
|
swipeRefreshLayout.setColorSchemeColors(
|
|
|
|
c1, c2, c1
|
|
|
|
);
|
2019-07-13 15:05:46 +02:00
|
|
|
accountPerPage = Helper.ACCOUNTS_PER_PAGE;
|
2017-10-28 11:02:37 +02:00
|
|
|
lv_accounts = rootView.findViewById(R.id.lv_accounts);
|
2017-10-29 09:30:08 +01:00
|
|
|
lv_accounts.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
|
2017-10-28 11:02:37 +02:00
|
|
|
no_action_text = rootView.findViewById(R.id.no_action_text);
|
|
|
|
mainLoader = rootView.findViewById(R.id.loader);
|
|
|
|
nextElementLoader = rootView.findViewById(R.id.loading_next_accounts);
|
|
|
|
textviewNoAction = rootView.findViewById(R.id.no_action);
|
2017-06-07 11:12:40 +02:00
|
|
|
mainLoader.setVisibility(View.VISIBLE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2019-08-19 09:11:23 +02:00
|
|
|
accountsFollowRequestAdapter = new AccountsFollowRequestAdapter(this.accounts);
|
2017-06-07 11:12:40 +02:00
|
|
|
lv_accounts.setAdapter(accountsFollowRequestAdapter);
|
2017-10-29 09:30:08 +01:00
|
|
|
final LinearLayoutManager mLayoutManager;
|
|
|
|
mLayoutManager = new LinearLayoutManager(context);
|
2017-10-29 10:20:08 +01:00
|
|
|
lv_accounts.setLayoutManager(mLayoutManager);
|
2017-10-29 09:30:08 +01:00
|
|
|
lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
2021-01-19 17:43:51 +01:00
|
|
|
public void onScrolled(@NotNull RecyclerView recyclerView, int dx, int dy) {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (dy > 0) {
|
2017-10-29 09:30:08 +01:00
|
|
|
int visibleItemCount = mLayoutManager.getChildCount();
|
|
|
|
int totalItemCount = mLayoutManager.getItemCount();
|
|
|
|
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
|
|
|
|
if (firstVisibleItem + visibleItemCount == totalItemCount) {
|
|
|
|
if (!flag_loading) {
|
|
|
|
flag_loading = true;
|
2021-01-19 17:43:51 +01:00
|
|
|
new RetrieveFollowRequestSentAsyncTask(context, max_id, DisplayFollowRequestSentFragment.this);
|
2017-10-29 09:30:08 +01:00
|
|
|
nextElementLoader.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2017-06-07 11:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-01-19 17:43:51 +01:00
|
|
|
swipeRefreshLayout.setOnRefreshListener(() -> {
|
|
|
|
max_id = null;
|
|
|
|
accounts = new ArrayList<>();
|
|
|
|
firstLoad = true;
|
|
|
|
flag_loading = true;
|
|
|
|
swiped = true;
|
|
|
|
new RetrieveFollowRequestSentAsyncTask(context, max_id, DisplayFollowRequestSentFragment.this);
|
2017-06-07 11:12:40 +02:00
|
|
|
});
|
|
|
|
|
2021-01-19 17:43:51 +01:00
|
|
|
new RetrieveFollowRequestSentAsyncTask(context, max_id, DisplayFollowRequestSentFragment.this);
|
2017-06-07 11:12:40 +02:00
|
|
|
return rootView;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-09-06 17:55:14 +02:00
|
|
|
public void onCreate(Bundle saveInstance) {
|
2017-06-07 11:12:40 +02:00
|
|
|
super.onCreate(saveInstance);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2021-01-19 17:43:51 +01:00
|
|
|
public void onAttach(@NotNull Context context) {
|
2017-06-07 11:12:40 +02:00
|
|
|
super.onAttach(context);
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:12:50 +02:00
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2017-06-07 11:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRetrieveAccounts(APIResponse apiResponse) {
|
|
|
|
|
|
|
|
mainLoader.setVisibility(View.GONE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (apiResponse.getError() != null) {
|
2019-11-15 16:32:25 +01:00
|
|
|
if (apiResponse.getError().getError().length() < 100) {
|
2019-09-30 18:14:46 +02:00
|
|
|
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
|
2019-11-15 16:32:25 +01:00
|
|
|
} else {
|
|
|
|
Toasty.error(context, getString(R.string.long_api_error, "\ud83d\ude05"), Toast.LENGTH_LONG).show();
|
2019-09-30 18:14:46 +02:00
|
|
|
}
|
2017-06-17 07:49:47 +02:00
|
|
|
flag_loading = false;
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
swiped = false;
|
2017-06-07 11:12:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
List<Account> accounts = apiResponse.getAccounts();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (!swiped && firstLoad && (accounts == null || accounts.size() == 0)) {
|
2017-06-07 11:12:40 +02:00
|
|
|
no_action_text.setText(context.getString(R.string.no_follow_request));
|
|
|
|
textviewNoAction.setVisibility(View.VISIBLE);
|
2019-09-06 17:55:14 +02:00
|
|
|
} else
|
2017-06-07 11:12:40 +02:00
|
|
|
textviewNoAction.setVisibility(View.GONE);
|
|
|
|
max_id = apiResponse.getMax_id();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (swiped) {
|
2019-08-19 09:11:23 +02:00
|
|
|
accountsFollowRequestAdapter = new AccountsFollowRequestAdapter(this.accounts);
|
2017-06-16 18:54:46 +02:00
|
|
|
lv_accounts.setAdapter(accountsFollowRequestAdapter);
|
|
|
|
swiped = false;
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
if (accounts != null && accounts.size() > 0) {
|
2017-10-28 11:02:37 +02:00
|
|
|
this.accounts.addAll(accounts);
|
2017-06-07 11:12:40 +02:00
|
|
|
accountsFollowRequestAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
firstLoad = false;
|
|
|
|
flag_loading = accounts != null && accounts.size() < accountPerPage;
|
|
|
|
}
|
|
|
|
}
|