package fr.gouv.etalab.mastodon.fragments; /* Copyright 2017 Thomas Schneider * * This file is a part of Mastodon Etalab for mastodon.etalab.gouv.fr * * 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. * * Mastodon Etalab 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 Thomas Schneider; if not, * see . */ import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.SwipeRefreshLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import fr.gouv.etalab.mastodon.asynctasks.RetrieveFollowRequestSentAsyncTask; import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.drawers.AccountsFollowRequestAdapter; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountsInterface; import mastodon.etalab.gouv.fr.mastodon.R; /** * 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 AsyncTask asyncTask; private AccountsFollowRequestAdapter accountsFollowRequestAdapter; private String max_id; private List accounts; private RelativeLayout mainLoader, nextElementLoader, textviewNoAction; private boolean firstLoad; private SwipeRefreshLayout swipeRefreshLayout; private int accountPerPage; private TextView no_action_text; private boolean swiped; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //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; swiped = false; swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); accountPerPage = sharedpreferences.getInt(Helper.SET_ACCOUNTS_PER_PAGE, 40); final ListView lv_accounts = (ListView) rootView.findViewById(R.id.lv_accounts); no_action_text = (TextView) rootView.findViewById(R.id.no_action_text); mainLoader = (RelativeLayout) rootView.findViewById(R.id.loader); nextElementLoader = (RelativeLayout) rootView.findViewById(R.id.loading_next_accounts); textviewNoAction = (RelativeLayout) rootView.findViewById(R.id.no_action); mainLoader.setVisibility(View.VISIBLE); nextElementLoader.setVisibility(View.GONE); accountsFollowRequestAdapter = new AccountsFollowRequestAdapter(context, this.accounts); lv_accounts.setAdapter(accountsFollowRequestAdapter); lv_accounts.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem + visibleItemCount == totalItemCount) { if (!flag_loading) { flag_loading = true; asyncTask = new RetrieveFollowRequestSentAsyncTask(context, max_id, DisplayFollowRequestSentFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); nextElementLoader.setVisibility(View.VISIBLE); } } else { nextElementLoader.setVisibility(View.GONE); } } }); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { max_id = null; accounts = new ArrayList<>(); firstLoad = true; flag_loading = true; swiped = true; asyncTask = new RetrieveFollowRequestSentAsyncTask(context, max_id, DisplayFollowRequestSentFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }); swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark); asyncTask = new RetrieveFollowRequestSentAsyncTask(context, max_id, DisplayFollowRequestSentFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 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 onStop() { super.onStop(); if(asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING) asyncTask.cancel(true); } @Override public void onRetrieveAccounts(APIResponse apiResponse) { mainLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE); 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; } List accounts = apiResponse.getAccounts(); if( !swiped && firstLoad && (accounts == null || accounts.size() == 0)) { no_action_text.setText(context.getString(R.string.no_follow_request)); textviewNoAction.setVisibility(View.VISIBLE); }else textviewNoAction.setVisibility(View.GONE); max_id = apiResponse.getMax_id(); if( accounts != null) { for(Account tmpAccount: accounts){ this.accounts.add(tmpAccount); } accountsFollowRequestAdapter.notifyDataSetChanged(); } swipeRefreshLayout.setRefreshing(false); firstLoad = false; flag_loading = accounts != null && accounts.size() < accountPerPage; } }