fedilab-Android-App/app/src/main/java/app/fedilab/android/fragments/DisplayAccountsFragment.java

305 lines
15 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-05-05 16:36:04 +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-05-05 16:36:04 +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-05-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
2018-11-25 10:45:16 +01:00
2017-05-05 16:36:04 +02:00
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
2019-11-15 16:32:25 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
2019-08-14 18:55:45 +02:00
2019-06-11 19:38:26 +02: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;
2019-11-15 16:32:25 +01:00
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
2017-05-05 16:36:04 +02:00
import java.util.ArrayList;
import java.util.List;
2017-09-17 12:25:21 +02:00
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
2019-08-24 10:53:59 +02:00
import app.fedilab.android.activities.TootInfoActivity;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.asynctasks.RetrieveAccountsAsyncTask;
import app.fedilab.android.asynctasks.RetrieveManyRelationshipsAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
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.client.Entities.Relationship;
import app.fedilab.android.drawers.AccountsListAdapter;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnRetrieveAccountsInterface;
import app.fedilab.android.interfaces.OnRetrieveManyRelationshipsInterface;
2019-11-15 16:32:25 +01:00
import es.dmoral.toasty.Toasty;
2017-05-05 16:36:04 +02:00
/**
* Created by Thomas on 27/04/2017.
* Fragment to display content related to accounts
*/
2017-09-17 12:25:21 +02:00
public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccountsInterface, OnRetrieveManyRelationshipsInterface {
2017-05-05 16:36:04 +02:00
private boolean flag_loading;
private Context context;
private AsyncTask<Void, Void, Void> asyncTask;
private AccountsListAdapter accountsListAdapter;
private String max_id;
private List<Account> accounts;
private RetrieveAccountsAsyncTask.Type type;
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout;
2018-10-22 17:09:25 +02:00
private String targetedId, instance, name;
2017-06-11 17:14:54 +02:00
private boolean swiped;
2017-10-24 11:44:07 +02:00
private RecyclerView lv_accounts;
2020-04-08 12:42:15 +02:00
private View rootView;
2017-05-05 16:36:04 +02:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2017-05-05 16:36:04 +02:00
2020-04-08 12:42:15 +02:00
rootView = inflater.inflate(R.layout.fragment_accounts, container, false);
2017-05-05 16:36:04 +02:00
context = getContext();
Bundle bundle = this.getArguments();
2017-05-26 17:20:36 +02:00
accounts = new ArrayList<>();
2017-05-05 16:36:04 +02:00
if (bundle != null) {
type = (RetrieveAccountsAsyncTask.Type) bundle.get("type");
2019-08-14 18:55:45 +02:00
if (bundle.containsKey("tag"))
2019-03-31 14:25:40 +02:00
targetedId = bundle.getString("tag", null);
else
targetedId = bundle.getString("targetedid", null);
2018-10-22 17:09:25 +02:00
instance = bundle.getString("instance", null);
name = bundle.getString("name", null);
2017-05-05 16:36:04 +02:00
}
max_id = null;
firstLoad = true;
flag_loading = true;
2017-06-11 17:14:54 +02:00
swiped = false;
2017-05-05 16:36:04 +02:00
2017-10-21 17:15:52 +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
);
2017-10-21 17:15:52 +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-21 17:15:52 +02:00
mainLoader = rootView.findViewById(R.id.loader);
nextElementLoader = rootView.findViewById(R.id.loading_next_accounts);
textviewNoAction = rootView.findViewById(R.id.no_action);
2017-05-05 16:36:04 +02:00
mainLoader.setVisibility(View.VISIBLE);
nextElementLoader.setVisibility(View.GONE);
2019-08-19 09:11:23 +02:00
accountsListAdapter = new AccountsListAdapter(type, targetedId, this.accounts);
2017-05-05 16:36:04 +02:00
lv_accounts.setAdapter(accountsListAdapter);
final LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(context);
lv_accounts.setLayoutManager(mLayoutManager);
lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() {
2020-04-08 12:42:15 +02:00
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
2019-08-14 18:55:45 +02:00
if (dy > 0) {
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) {
flag_loading = true;
2019-08-14 18:55:45 +02:00
if (type == RetrieveAccountsAsyncTask.Type.SEARCH || type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING || type == RetrieveAccountsAsyncTask.Type.REBLOGGED || type == RetrieveAccountsAsyncTask.Type.FAVOURITED)
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).execute();
2018-10-22 17:09:25 +02:00
else if (type == RetrieveAccountsAsyncTask.Type.CHANNELS)
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, instance, name, DisplayAccountsFragment.this).execute();
2018-10-22 17:09:25 +02:00
else
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).execute();
nextElementLoader.setVisibility(View.VISIBLE);
2017-08-26 19:39:11 +02:00
}
} else {
nextElementLoader.setVisibility(View.GONE);
2017-08-26 19:39:11 +02:00
}
}
}
});
2020-04-08 12:42:15 +02:00
swipeRefreshLayout.setOnRefreshListener(() -> {
max_id = null;
accounts = new ArrayList<>();
firstLoad = true;
flag_loading = true;
swiped = true;
if (type == RetrieveAccountsAsyncTask.Type.SEARCH || type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING || type == RetrieveAccountsAsyncTask.Type.REBLOGGED || type == RetrieveAccountsAsyncTask.Type.FAVOURITED)
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).execute();
2020-04-08 12:42:15 +02:00
else if (type == RetrieveAccountsAsyncTask.Type.CHANNELS)
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, instance, name, DisplayAccountsFragment.this).execute();
2020-04-08 12:42:15 +02:00
else
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).execute();
});
2019-08-14 18:55:45 +02:00
if (type == RetrieveAccountsAsyncTask.Type.SEARCH || type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING || type == RetrieveAccountsAsyncTask.Type.REBLOGGED || type == RetrieveAccountsAsyncTask.Type.FAVOURITED)
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).execute();
2018-10-22 17:09:25 +02:00
else if (type == RetrieveAccountsAsyncTask.Type.CHANNELS)
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, instance, name, DisplayAccountsFragment.this).execute();
2018-10-22 17:09:25 +02:00
else
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).execute();
2017-05-05 16:36:04 +02:00
return rootView;
}
2020-04-08 12:42:15 +02:00
@Override
public void onDestroyView() {
super.onDestroyView();
rootView = null;
}
2017-05-05 16:36:04 +02:00
@Override
2019-08-14 18:55:45 +02:00
public void onCreate(Bundle saveInstance) {
2017-05-05 16:36:04 +02:00
super.onCreate(saveInstance);
}
@Override
2020-04-08 12:42:15 +02:00
public void onAttach(@NonNull Context context) {
2017-05-05 16:36:04 +02:00
super.onAttach(context);
this.context = context;
}
public void onDestroy() {
super.onDestroy();
2019-08-14 18:55:45 +02:00
if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
2017-05-05 16:36:04 +02:00
asyncTask.cancel(true);
}
2019-08-14 18:55:45 +02:00
public void scrollToTop() {
if (lv_accounts != null)
lv_accounts.setAdapter(accountsListAdapter);
}
2017-05-05 16:36:04 +02:00
@Override
2017-06-03 18:18:27 +02:00
public void onRetrieveAccounts(APIResponse apiResponse) {
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
2019-08-14 18:55:45 +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
}
swipeRefreshLayout.setRefreshing(false);
swiped = false;
2017-08-19 19:10:43 +02:00
flag_loading = false;
return;
}
2019-08-14 18:55:45 +02:00
flag_loading = (apiResponse.getMax_id() == null);
2019-03-31 14:25:40 +02:00
List<Account> accounts;
2019-08-14 18:55:45 +02:00
if (apiResponse.getResults() != null && apiResponse.getResults().getAccounts() != null)
2019-03-31 14:25:40 +02:00
accounts = apiResponse.getResults().getAccounts();
else
accounts = apiResponse.getAccounts();
2019-08-14 18:55:45 +02:00
if (!swiped && firstLoad && (accounts == null || accounts.size() == 0))
2017-05-05 16:36:04 +02:00
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
2019-03-31 15:15:46 +02:00
2019-08-14 18:55:45 +02:00
if (type == RetrieveAccountsAsyncTask.Type.SEARCH) {
if (max_id == null)
2019-03-31 15:15:46 +02:00
max_id = "0";
2020-04-08 12:42:15 +02:00
max_id = String.valueOf(Integer.parseInt(max_id) + 20);
2019-08-14 18:55:45 +02:00
} else {
2019-03-31 15:15:46 +02:00
max_id = apiResponse.getMax_id();
}
2019-08-14 18:55:45 +02:00
if (swiped) {
2019-08-19 09:11:23 +02:00
accountsListAdapter = new AccountsListAdapter(type, targetedId, this.accounts);
2017-06-16 18:54:46 +02:00
lv_accounts.setAdapter(accountsListAdapter);
swiped = false;
}
2019-08-14 18:55:45 +02:00
if (accounts != null && accounts.size() > 0) {
2019-02-12 17:29:18 +01:00
int currentPosition = this.accounts.size();
2017-10-28 11:02:37 +02:00
this.accounts.addAll(accounts);
2019-08-14 18:55:45 +02:00
accountsListAdapter.notifyItemRangeChanged(currentPosition, accounts.size());
2017-05-05 16:36:04 +02:00
}
2019-09-06 17:55:14 +02:00
if (context instanceof TootInfoActivity && this.accounts != null) {
if (type == RetrieveAccountsAsyncTask.Type.REBLOGGED) {
2019-08-24 10:53:59 +02:00
((TootInfoActivity) context).updateBoostCount(this.accounts.size());
2019-09-06 17:55:14 +02:00
} else if (type == RetrieveAccountsAsyncTask.Type.FAVOURITED) {
2019-08-24 10:53:59 +02:00
((TootInfoActivity) context).updateFavCount(this.accounts.size());
}
}
2017-05-05 16:36:04 +02:00
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
if (type != RetrieveAccountsAsyncTask.Type.BLOCKED && (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED))
2020-08-01 11:11:39 +02:00
new RetrieveManyRelationshipsAsyncTask(context, accounts, DisplayAccountsFragment.this).execute();
2017-09-17 12:25:21 +02:00
}
@Override
public void onRetrieveRelationship(APIResponse apiResponse) {
2019-08-14 18:55:45 +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-09-17 12:25:21 +02:00
return;
}
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
List<Relationship> relationships = apiResponse.getRelationships();
2019-08-14 18:55:45 +02:00
if (relationships != null && relationships.size() > 0) {
for (Relationship relationship : relationships) {
for (Account account : accounts) {
if (account.getId().equals(userId)) {
2017-09-17 12:25:21 +02:00
account.setFollowType(Account.followAction.NOTHING);
continue;
}
2019-08-14 18:55:45 +02:00
if (account.getId().equals(relationship.getId())) {
account.setMuting_notifications(relationship.isMuting_notifications());
2019-08-14 18:55:45 +02:00
if (relationship.isFollowing())
2017-09-17 12:25:21 +02:00
account.setFollowType(Account.followAction.FOLLOW);
else
account.setFollowType(Account.followAction.NOT_FOLLOW);
2019-08-14 18:55:45 +02:00
if (relationship.isBlocking())
2017-09-17 12:25:21 +02:00
account.setFollowType(Account.followAction.BLOCK);
2019-08-14 18:55:45 +02:00
else if (relationship.isMuting()) {
2017-09-17 12:25:21 +02:00
account.setFollowType(Account.followAction.MUTE);
2019-08-14 18:55:45 +02:00
} else if (relationship.isRequested())
2017-09-17 12:25:21 +02:00
account.setFollowType(Account.followAction.REQUEST_SENT);
break;
}
}
}
accountsListAdapter.notifyDataSetChanged();
}
2017-05-05 16:36:04 +02:00
}
2019-08-14 18:55:45 +02:00
2019-09-06 17:55:14 +02:00
public void pullToRefresh() {
2019-08-14 18:55:45 +02:00
max_id = null;
accounts = new ArrayList<>();
firstLoad = true;
flag_loading = true;
swiped = true;
swipeRefreshLayout.setRefreshing(true);
if (type == RetrieveAccountsAsyncTask.Type.SEARCH || type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING || type == RetrieveAccountsAsyncTask.Type.REBLOGGED || type == RetrieveAccountsAsyncTask.Type.FAVOURITED)
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).execute();
2019-08-14 18:55:45 +02:00
else if (type == RetrieveAccountsAsyncTask.Type.CHANNELS)
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, instance, name, DisplayAccountsFragment.this).execute();
2019-08-14 18:55:45 +02:00
else
2020-08-01 11:11:39 +02:00
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).execute();
2019-08-14 18:55:45 +02:00
}
2017-05-05 16:36:04 +02:00
}