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

288 lines
14 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-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.core.content.ContextCompat;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2017-05-05 16:36:04 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
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-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;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.asynctasks.RetrieveAccountsAsyncTask;
import app.fedilab.android.asynctasks.RetrieveManyRelationshipsAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
import app.fedilab.android.interfaces.OnRetrieveAccountsInterface;
import app.fedilab.android.interfaces.OnRetrieveManyRelationshipsInterface;
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;
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
View rootView = inflater.inflate(R.layout.fragment_accounts, container, false);
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-03-31 14:25:40 +02:00
if( bundle.containsKey("tag"))
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);
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);
accountsListAdapter = new AccountsListAdapter(context, 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() {
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
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-03-31 14:25:40 +02:00
if (type == RetrieveAccountsAsyncTask.Type.SEARCH || type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING || type == RetrieveAccountsAsyncTask.Type.REBLOGGED || type == RetrieveAccountsAsyncTask.Type.FAVOURITED)
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2018-10-22 17:09:25 +02:00
else if (type == RetrieveAccountsAsyncTask.Type.CHANNELS)
asyncTask = new RetrieveAccountsAsyncTask(context, instance, name, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
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
}
}
}
});
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
max_id = null;
accounts = new ArrayList<>();
firstLoad = true;
flag_loading = true;
swiped = true;
2019-03-31 14:25:40 +02:00
if (type == RetrieveAccountsAsyncTask.Type.SEARCH || type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING|| type == RetrieveAccountsAsyncTask.Type.REBLOGGED || type == RetrieveAccountsAsyncTask.Type.FAVOURITED)
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2018-10-22 17:09:25 +02:00
else if (type == RetrieveAccountsAsyncTask.Type.CHANNELS)
asyncTask = new RetrieveAccountsAsyncTask(context, instance, name, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
2018-05-11 16:33:16 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
switch (theme){
case Helper.THEME_LIGHT:
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
R.color.mastodonC2,
R.color.mastodonC3);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.white));
break;
case Helper.THEME_DARK:
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4__,
R.color.mastodonC4,
R.color.mastodonC4);
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.mastodonC1_));
break;
case Helper.THEME_BLACK:
swipeRefreshLayout.setColorSchemeResources(R.color.dark_icon,
R.color.mastodonC2,
R.color.mastodonC3);
2018-05-12 12:06:43 +02:00
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(context, R.color.black_3));
2018-05-11 16:33:16 +02:00
break;
}
2019-03-31 14:25:40 +02:00
if (type == RetrieveAccountsAsyncTask.Type.SEARCH || type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING|| type == RetrieveAccountsAsyncTask.Type.REBLOGGED || type == RetrieveAccountsAsyncTask.Type.FAVOURITED)
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2018-10-22 17:09:25 +02:00
else if (type == RetrieveAccountsAsyncTask.Type.CHANNELS)
asyncTask = new RetrieveAccountsAsyncTask(context, instance, name, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-05-05 16:36:04 +02:00
return rootView;
}
@Override
public void onCreate(Bundle saveInstance)
{
super.onCreate(saveInstance);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
public void onDestroy() {
super.onDestroy();
2017-05-05 16:36:04 +02:00
if(asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
asyncTask.cancel(true);
}
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);
2017-06-03 18:18:27 +02:00
if( apiResponse.getError() != null){
2018-11-25 10:45:16 +01:00
Toasty.error(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
swipeRefreshLayout.setRefreshing(false);
swiped = false;
2017-08-19 19:10:43 +02:00
flag_loading = false;
return;
}
2017-08-19 19:10:43 +02:00
flag_loading = (apiResponse.getMax_id() == null );
2019-03-31 14:25:40 +02:00
List<Account> accounts;
if( apiResponse.getResults() != null && apiResponse.getResults().getAccounts() != null)
accounts = apiResponse.getResults().getAccounts();
else
accounts = apiResponse.getAccounts();
2017-06-11 17:14:54 +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
if( type == RetrieveAccountsAsyncTask.Type.SEARCH) {
if(max_id == null)
max_id = "0";
max_id = String.valueOf(Integer.valueOf(max_id) + 20);
}else {
max_id = apiResponse.getMax_id();
}
2017-06-16 18:54:46 +02:00
if( swiped ){
accountsListAdapter = new AccountsListAdapter(context, type,targetedId, this.accounts);
2017-06-16 18:54:46 +02:00
lv_accounts.setAdapter(accountsListAdapter);
swiped = false;
}
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-02-12 17:29:18 +01:00
accountsListAdapter.notifyItemRangeChanged(currentPosition,accounts.size());
2017-05-05 16:36:04 +02:00
}
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
2019-01-27 10:40:09 +01:00
if( type != RetrieveAccountsAsyncTask.Type.BLOCKED && (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA))
2017-09-17 12:25:21 +02:00
new RetrieveManyRelationshipsAsyncTask(context, accounts,DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public void onRetrieveRelationship(APIResponse apiResponse) {
if( apiResponse.getError() != null){
2018-11-25 10:45:16 +01:00
Toasty.error(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
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();
if( relationships != null && relationships.size() > 0 ){
for(Relationship relationship: relationships){
for(Account account: accounts){
if( account.getId().equals(userId)){
account.setFollowType(Account.followAction.NOTHING);
continue;
}
if( account.getId().equals(relationship.getId())){
account.setMuting_notifications(relationship.isMuting_notifications());
2017-09-17 12:25:21 +02:00
if( relationship.isFollowing())
account.setFollowType(Account.followAction.FOLLOW);
else
account.setFollowType(Account.followAction.NOT_FOLLOW);
if(relationship.isBlocking())
account.setFollowType(Account.followAction.BLOCK);
else if(relationship.isMuting()) {
2017-09-17 12:25:21 +02:00
account.setFollowType(Account.followAction.MUTE);
}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
}
}