package org.inaturalist.android;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewCallback;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;

class ProjectUserAdapter extends ArrayAdapter<String> {
    private ArrayList<JSONObject> mResultList;
    private Context mContext;

    public ProjectUserAdapter(Context context, ArrayList<JSONObject> results) {
        super(context, android.R.layout.simple_list_item_1);

        mContext = context;
        mResultList = results;
    }

    @Override
    public int getCount() {
        return (mResultList != null ? mResultList.size() : 0);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.project_user_item, parent, false);
        JSONObject item = null;
        int count;
        try {
            JSONObject jsonObject = mResultList.get(position);
            if (jsonObject.has("observation_count")) {
                count = jsonObject.getInt("observation_count");
            } else {
                count = jsonObject.getInt("count");
            }
            item = jsonObject.getJSONObject("user");
        } catch (JSONException e) {
            e.printStackTrace();
            return view;
        }

        // Get the taxon display name according to device locale
        try {
            ImageView userPic = (ImageView) view.findViewById(R.id.user_pic);
            TextView username = (TextView) view.findViewById(R.id.username);
            TextView rank = (TextView) view.findViewById(R.id.rank);
            TextView countText = (TextView) view.findViewById(R.id.count);

            DecimalFormat formatter = new DecimalFormat("#,###,###");
            rank.setText(formatter.format(position + 1));
            username.setText(item.getString("login"));
            countText.setText(formatter.format(count));

            if (item.has("icon_url") && !item.isNull("icon_url")) {
                UrlImageViewHelper.setUrlDrawable(userPic, item.getString("icon_url"), R.drawable.ic_account_circle_black_24dp, new UrlImageViewCallback() {
                    @Override
                    public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) { }

                    @Override
                    public Bitmap onPreSetBitmap(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {
                        // Return a circular version of the profile picture
                        Bitmap centerCrop = ImageUtils.centerCropBitmap(loadedBitmap);
                        return ImageUtils.getCircleBitmap(centerCrop);
                    }
                });
            } else {
                userPic.setImageResource(R.drawable.ic_account_circle_black_24dp);
            }

            view.setTag(item);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return view;
    }

}


@@ -448,16 +448,53 @@
 wHelper;
+%0Aimport com.squareup.picasso.Picasso;
 %0A%0Aimport
@@ -2529,539 +2529,198 @@
     
-UrlImageViewHelper.setUrlDrawable(userPic, item.getString(%22icon_url%22), R.drawable.ic_account_circle_black_24dp, new UrlImageViewCallback() %7B%0A                    @Override%0A                    public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) %7B %7D%0A%0A                    @Override%0A                    public Bitmap onPreSetBitmap(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) %7B%0A                        // Return a circular version of the profile picture
+Picasso.with(mContext).%0A                        load(item.getString(%22icon_url%22)).%0A                        placeholder(R.drawable.ic_account_circle_black_24dp).%0A                        fit().
 %0A   
@@ -2744,15 +2744,8 @@
     
-Bitmap 
 cent
@@ -2754,124 +2754,91 @@
 Crop
- = ImageUtils.centerCropBitmap(loadedBitmap);%0A                        return ImageUtils.getCircleBitmap(centerCrop);
+().%0A                        transform(new UserActivitiesAdapter.CircleTransform()).
 %0A   
@@ -2858,27 +2858,24 @@
     
-%7D%0A
     
-            %7D
+into(userPic
 );%0A 

Fix crashlytics crash (RejectedExecutionException)