2017-05-05 16:36:04 +02:00
|
|
|
package fr.gouv.etalab.mastodon.fragments;
|
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2017-07-10 10:33:24 +02:00
|
|
|
* This file is a part of Mastalab
|
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.
|
|
|
|
*
|
2017-07-10 10:33:24 +02:00
|
|
|
* Mastalab 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.
|
|
|
|
*
|
2017-08-04 11:11:27 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
2017-05-05 16:36:04 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.AsyncTask;
|
2017-08-26 19:39:11 +02:00
|
|
|
import android.os.Build;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.os.Bundle;
|
2017-05-26 17:20:36 +02:00
|
|
|
import android.os.Parcelable;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.support.v4.app.Fragment;
|
2017-08-23 09:56:42 +02:00
|
|
|
import android.support.v4.view.ViewCompat;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2017-10-24 11:44:07 +02:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.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;
|
2017-05-30 19:31:44 +02:00
|
|
|
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
|
|
|
|
|
|
|
import fr.gouv.etalab.mastodon.asynctasks.RetrieveManyRelationshipsAsyncTask;
|
2017-06-03 18:18:27 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.APIResponse;
|
2017-05-05 16:36:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
2017-09-17 12:25:21 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
|
2017-05-05 16:36:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
2017-09-17 12:25:21 +02:00
|
|
|
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveManyRelationshipsInterface;
|
2017-05-05 16:36:04 +02:00
|
|
|
import mastodon.etalab.gouv.fr.mastodon.R;
|
|
|
|
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
|
|
|
|
import fr.gouv.etalab.mastodon.drawers.AccountsListAdapter;
|
|
|
|
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountsInterface;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
private String targetedId;
|
2017-06-11 17:14:54 +02:00
|
|
|
private boolean swiped;
|
2017-10-24 11:44:07 +02:00
|
|
|
private RecyclerView lv_accounts;
|
2017-08-26 19:39:11 +02:00
|
|
|
boolean hideHeader;
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
View rootView = inflater.inflate(R.layout.fragment_accounts, container, false);
|
|
|
|
|
|
|
|
context = getContext();
|
2017-06-11 17:14:54 +02:00
|
|
|
boolean comesFromSearch = false;
|
2017-08-26 19:39:11 +02:00
|
|
|
hideHeader = false;
|
2017-05-05 16:36:04 +02:00
|
|
|
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");
|
|
|
|
targetedId = bundle.getString("targetedId", null);
|
2017-05-20 19:40:46 +02:00
|
|
|
hideHeader = bundle.getBoolean("hideHeader", false);
|
2017-05-26 17:20:36 +02:00
|
|
|
if( bundle.containsKey("accounts")){
|
|
|
|
ArrayList<Parcelable> accountsReceived = bundle.getParcelableArrayList("accounts");
|
|
|
|
assert accountsReceived != null;
|
|
|
|
for(Parcelable account: accountsReceived){
|
|
|
|
accounts.add((Account)account);
|
|
|
|
}
|
|
|
|
comesFromSearch = true;
|
|
|
|
}
|
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-05-05 16:36:04 +02:00
|
|
|
|
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);
|
2017-07-20 15:00:43 +02:00
|
|
|
accountsListAdapter = new AccountsListAdapter(context, type, targetedId, this.accounts);
|
2017-05-05 16:36:04 +02:00
|
|
|
lv_accounts.setAdapter(accountsListAdapter);
|
|
|
|
|
2017-05-26 17:20:36 +02:00
|
|
|
if( !comesFromSearch) {
|
|
|
|
//Hide account header when scrolling for ShowAccountActivity
|
2017-08-26 19:39:11 +02:00
|
|
|
if (hideHeader && Build.VERSION.SDK_INT >= 21)
|
|
|
|
ViewCompat.setNestedScrollingEnabled(lv_accounts, true);
|
2017-10-24 11:44:07 +02:00
|
|
|
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;
|
|
|
|
if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING)
|
|
|
|
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
else
|
|
|
|
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
nextElementLoader.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2017-08-26 19:39:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-05-26 17:20:36 +02:00
|
|
|
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
|
|
|
max_id = null;
|
|
|
|
accounts = new ArrayList<>();
|
|
|
|
firstLoad = true;
|
|
|
|
flag_loading = true;
|
2017-06-11 17:14:54 +02:00
|
|
|
swiped = true;
|
2017-05-26 17:20:36 +02:00
|
|
|
if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING)
|
|
|
|
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
else
|
|
|
|
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
|
|
|
});
|
2017-08-13 11:30:28 +02:00
|
|
|
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
|
|
|
|
R.color.mastodonC2,
|
|
|
|
R.color.mastodonC3);
|
2017-05-26 17:20:36 +02:00
|
|
|
|
|
|
|
if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING)
|
|
|
|
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
else
|
|
|
|
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}else {
|
|
|
|
accountsListAdapter.notifyDataSetChanged();
|
|
|
|
mainLoader.setVisibility(View.GONE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
|
|
|
if( accounts == null || accounts.size() == 0 )
|
|
|
|
textviewNoAction.setVisibility(View.VISIBLE);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:12:50 +02:00
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2017-05-05 16:36:04 +02:00
|
|
|
if(asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
|
|
|
|
asyncTask.cancel(true);
|
|
|
|
}
|
|
|
|
|
2017-08-20 16:53:41 +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) {
|
2017-05-30 19:31:44 +02:00
|
|
|
mainLoader.setVisibility(View.GONE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2017-06-03 18:18:27 +02:00
|
|
|
if( apiResponse.getError() != null){
|
2017-05-30 19:31:44 +02:00
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true);
|
|
|
|
if( show_error_messages)
|
2017-06-14 17:19:16 +02:00
|
|
|
Toast.makeText(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
|
2017-06-17 07:49:47 +02:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
swiped = false;
|
2017-08-19 19:10:43 +02:00
|
|
|
flag_loading = false;
|
2017-05-30 19:31:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-08-19 19:10:43 +02:00
|
|
|
flag_loading = (apiResponse.getMax_id() == null );
|
2017-06-03 18:18:27 +02:00
|
|
|
List<Account> 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);
|
2017-06-03 18:18:27 +02:00
|
|
|
max_id = apiResponse.getMax_id();
|
2017-06-16 18:54:46 +02:00
|
|
|
if( swiped ){
|
2017-07-20 15:00:43 +02:00
|
|
|
accountsListAdapter = new AccountsListAdapter(context, type,targetedId, this.accounts);
|
2017-06-16 18:54:46 +02:00
|
|
|
lv_accounts.setAdapter(accountsListAdapter);
|
|
|
|
swiped = false;
|
|
|
|
}
|
2017-06-19 15:59:34 +02:00
|
|
|
if( accounts != null && accounts.size() > 0) {
|
2017-05-05 16:36:04 +02:00
|
|
|
for(Account tmpAccount: accounts){
|
|
|
|
this.accounts.add(tmpAccount);
|
|
|
|
}
|
|
|
|
accountsListAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
firstLoad = false;
|
2017-09-17 12:25:21 +02:00
|
|
|
|
|
|
|
if( type != RetrieveAccountsAsyncTask.Type.BLOCKED && type != RetrieveAccountsAsyncTask.Type.MUTED)
|
|
|
|
new RetrieveManyRelationshipsAsyncTask(context, accounts,DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRetrieveRelationship(APIResponse apiResponse) {
|
|
|
|
if( apiResponse.getError() != null){
|
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true);
|
|
|
|
if( show_error_messages)
|
|
|
|
Toast.makeText(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
|
|
|
|
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())){
|
|
|
|
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())
|
|
|
|
account.setFollowType(Account.followAction.MUTE);
|
|
|
|
else if(relationship.isRequested())
|
|
|
|
account.setFollowType(Account.followAction.REQUEST_SENT);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
accountsListAdapter.notifyDataSetChanged();
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
}
|