fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/drawers/AccountsFollowRequestAdapte...

182 lines
7.2 KiB
Java
Raw Normal View History

package fr.gouv.etalab.mastodon.drawers;
/* Copyright 2017 Thomas Schneider
*
2017-07-10 10:33:24 +02:00
* This file is a part of Mastalab
*
* 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
* 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,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
2017-06-07 13:58:21 +02:00
import android.content.Intent;
import android.content.SharedPreferences;
2017-06-07 13:58:21 +02:00
import android.graphics.PorterDuff;
import android.os.AsyncTask;
2017-06-07 13:58:21 +02:00
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import java.util.ArrayList;
import java.util.List;
2017-06-07 13:58:21 +02:00
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import mastodon.etalab.gouv.fr.mastodon.R;
/**
* Created by Thomas on 07/05/2017.
* Adapter for accounts asking a follow request
*/
public class AccountsFollowRequestAdapter extends BaseAdapter implements OnPostActionInterface {
private List<Account> accounts;
private LayoutInflater layoutInflater;
private ImageLoader imageLoader;
private DisplayImageOptions options;
private Context context;
private AccountsFollowRequestAdapter accountsFollowRequestAdapter;
public AccountsFollowRequestAdapter(Context context, List<Account> accounts){
this.accounts = accounts;
layoutInflater = LayoutInflater.from(context);
imageLoader = ImageLoader.getInstance();
this.context = context;
options = new DisplayImageOptions.Builder().displayer(new SimpleBitmapDisplayer()).cacheInMemory(false)
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
accountsFollowRequestAdapter = this;
}
@Override
public int getCount() {
return accounts.size();
}
@Override
public Object getItem(int position) {
return accounts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Account account = accounts.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.drawer_account_follow_request, parent, false);
holder = new ViewHolder();
holder.account_pp = (ImageView) convertView.findViewById(R.id.account_pp);
holder.account_un = (TextView) convertView.findViewById(R.id.account_un);
holder.btn_authorize = (Button) convertView.findViewById(R.id.btn_authorize);
holder.btn_reject = (Button) convertView.findViewById(R.id.btn_reject);
holder.account_container = (LinearLayout) convertView.findViewById(R.id.account_container);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
2017-06-07 13:58:21 +02:00
holder.btn_authorize.getBackground().setColorFilter(ContextCompat.getColor(context, R.color.green_1), PorterDuff.Mode.MULTIPLY);
holder.btn_reject.getBackground().setColorFilter(ContextCompat.getColor(context, R.color.red_1), PorterDuff.Mode.MULTIPLY);
holder.account_un.setText(String.format("@%s", account.getUsername()));
//Profile picture
imageLoader.displayImage(account.getAvatar(), holder.account_pp, options);
2017-06-07 13:58:21 +02:00
holder.account_pp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openAccountDetails(account.getId());
}
});
holder.account_un.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openAccountDetails(account.getId());
}
});
holder.btn_authorize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PostActionAsyncTask(context, API.StatusAction.AUTHORIZE, account.getId(), AccountsFollowRequestAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
holder.btn_reject.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PostActionAsyncTask(context, API.StatusAction.REJECT, account.getId(), AccountsFollowRequestAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
return convertView;
}
2017-06-07 13:58:21 +02:00
private void openAccountDetails(String userId){
Intent intent = new Intent(context, ShowAccountActivity.class);
Bundle b = new Bundle();
b.putString("accountId", userId);
intent.putExtras(b);
context.startActivity(intent);
}
@Override
public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) {
if( error != 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, error.getError(),Toast.LENGTH_LONG).show();
return;
}
Helper.manageMessageStatusCode(context, statusCode, statusAction);
//When authorizing or rejecting an account, this account is removed from the list
List<Account> accountToRemove = new ArrayList<>();
if( statusAction == API.StatusAction.AUTHORIZE || statusAction == API.StatusAction.REJECT){
for(Account account: accounts){
if( account.getId().equals(userId))
accountToRemove.add(account);
}
accounts.removeAll(accountToRemove);
accountsFollowRequestAdapter.notifyDataSetChanged();
}
}
private class ViewHolder {
ImageView account_pp;
Button btn_authorize;
Button btn_reject;
TextView account_un;
LinearLayout account_container;
}
}