Search in sources :

Example 1 with BottomSheet

use of com.cocosw.bottomsheet.BottomSheet in project iNaturalistAndroid by inaturalist.

the class CommentsIdsAdapter method getView.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final Resources res = mContext.getResources();
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View view = inflater.inflate(mIsNewLayout ? R.layout.comment_id_item_obs_viewer : R.layout.comment_id_item, parent, false);
    final BetterJSONObject item = mItems.get(position);
    try {
        final TextView comment = (TextView) view.findViewById(R.id.comment);
        RelativeLayout idLayout = (RelativeLayout) view.findViewById(R.id.id_layout);
        final RelativeLayout idAgreeLayout = (RelativeLayout) view.findViewById(R.id.id_agree_container);
        TextView postedOn = (TextView) view.findViewById(R.id.posted_on);
        final String username = item.getJSONObject("user").getString("login");
        Timestamp postDate = item.getTimestamp("updated_at");
        if (postDate == null)
            postDate = item.getTimestamp("created_at");
        if (mIsNewLayout) {
            postedOn.setText(String.format(res.getString(item.getString("type").equals("comment") ? R.string.comment_title : R.string.id_title), username, formatIdDate(mContext, postDate)));
        } else {
            SimpleDateFormat format = new SimpleDateFormat("LLL d, yyyy");
            postedOn.setText(String.format(res.getString(R.string.posted_by), (mLogin != null) && username.equalsIgnoreCase(mLogin) ? res.getString(R.string.you) : username, format.format(postDate)));
        }
        OnClickListener showUser = new OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, UserProfile.class);
                intent.putExtra("user", new BetterJSONObject(item.getJSONObject("user")));
                mContext.startActivity(intent);
            }
        };
        final ImageView userPic = (ImageView) view.findViewById(R.id.user_pic);
        JSONObject user = item.getJSONObject("user");
        String userIconUrl = user.optString("user_icon_url", user.optString("icon"));
        boolean hasUserIcon = userIconUrl != null;
        userPic.setOnClickListener(showUser);
        postedOn.setOnClickListener(showUser);
        if (hasUserIcon) {
            Picasso.with(mContext).load(userIconUrl).fit().centerCrop().placeholder(R.drawable.ic_account_circle_black_24dp).transform(new UserActivitiesAdapter.CircleTransform()).into(userPic, new Callback() {

                @Override
                public void onSuccess() {
                // Nothing to do here
                }

                @Override
                public void onError() {
                }
            });
        } else {
            if (mIsNewLayout) {
                userPic.setAlpha(100);
            }
        }
        final ImageView moreMenu = (ImageView) view.findViewById(R.id.more_menu);
        final boolean isComment = item.getString("type").equals("comment");
        final View loading = view.findViewById(R.id.loading);
        final DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                switch(which) {
                    case R.id.restore:
                        loading.setVisibility(View.VISIBLE);
                        mOnIDAddedCb.onIdentificationRestored(item);
                        break;
                    case R.id.delete:
                        // Display deletion confirmation dialog
                        mHelper.confirm(mContext.getString(isComment ? R.string.delete_comment : R.string.delete_id), isComment ? R.string.delete_comment_message : R.string.delete_id_message, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                loading.setVisibility(View.VISIBLE);
                                if (isComment) {
                                    mOnIDAddedCb.onCommentRemoved(item);
                                } else {
                                    mOnIDAddedCb.onIdentificationRemoved(item);
                                }
                            }
                        }, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                            }
                        }, R.string.yes, R.string.no);
                        break;
                    case R.id.edit:
                        if (isComment) {
                            mOnIDAddedCb.onCommentUpdated(item);
                        } else {
                            mOnIDAddedCb.onIdentificationUpdated(item);
                        }
                }
            }
        };
        if (moreMenu != null) {
            moreMenu.setVisibility(View.GONE);
            moreMenu.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (loading.getVisibility() == View.VISIBLE) {
                        return;
                    }
                    int menuResource = isComment && username.equalsIgnoreCase(mLogin) ? R.menu.comment_menu : R.menu.id_menu;
                    boolean restoreId = false;
                    if (menuResource == R.menu.id_menu) {
                        Boolean isCurrent = item.getBoolean("current");
                        if (((isCurrent == null) || (!isCurrent)) && (username.equalsIgnoreCase(mLogin))) {
                            restoreId = true;
                        }
                    }
                    Menu popupMenu;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                        PopupMenu popup = new PopupMenu(getContext(), moreMenu);
                        popup.getMenuInflater().inflate(menuResource, popup.getMenu());
                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                            @Override
                            public boolean onMenuItemClick(android.view.MenuItem menuItem) {
                                onClick.onClick(null, menuItem.getItemId());
                                return true;
                            }
                        });
                        popupMenu = popup.getMenu();
                        popup.show();
                    } else {
                        BottomSheet sheet = new BottomSheet.Builder((Activity) mContext).sheet(menuResource).listener(onClick).show();
                        popupMenu = sheet.getMenu();
                    }
                    if (restoreId) {
                        // Show restore ID menu option
                        popupMenu.getItem(0).setVisible(false);
                        popupMenu.getItem(1).setVisible(true);
                    } else {
                        // Show withdraw ID menu option
                        popupMenu.getItem(0).setVisible(true);
                        popupMenu.getItem(1).setVisible(false);
                    }
                }
            });
        }
        if (item.getString("type").equals("comment")) {
            // Comment
            comment.setVisibility(View.VISIBLE);
            idLayout.setVisibility(View.GONE);
            loading.setVisibility(View.GONE);
            if (moreMenu != null)
                moreMenu.setVisibility(View.VISIBLE);
            if (mIsNewLayout)
                idAgreeLayout.setVisibility(View.GONE);
            comment.setText(Html.fromHtml(item.getString("body")));
            Linkify.addLinks(comment, Linkify.ALL);
            comment.setMovementMethod(LinkMovementMethod.getInstance());
            if (mIsNewLayout) {
                postedOn.setTextColor(postedOn.getTextColors().withAlpha(255));
                if (hasUserIcon)
                    userPic.setAlpha(255);
            }
        } else {
            // Identification
            idLayout.setVisibility(View.VISIBLE);
            String body = item.getString("body");
            if (body != null && body.length() > 0) {
                comment.setText(Html.fromHtml(body));
                Linkify.addLinks(comment, Linkify.ALL);
                comment.setMovementMethod(LinkMovementMethod.getInstance());
                comment.setVisibility(View.VISIBLE);
                if (!mIsNewLayout) {
                    ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) comment.getLayoutParams();
                    layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + 25, layoutParams.rightMargin, layoutParams.bottomMargin);
                    comment.setLayoutParams(layoutParams);
                }
            } else {
                comment.setVisibility(View.GONE);
            }
            ImageView idPic = (ImageView) view.findViewById(R.id.id_pic);
            JSONObject taxonObject = item.getJSONObject("taxon");
            JSONObject defaultPhoto = taxonObject.optJSONObject("default_photo");
            if (defaultPhoto != null) {
                UrlImageViewHelper.setUrlDrawable(idPic, taxonObject.optString("image_url", defaultPhoto.optString("square_url")), R.drawable.iconic_taxon_unknown);
            } else {
                idPic.setImageResource(R.drawable.iconic_taxon_unknown);
            }
            TextView idName = (TextView) view.findViewById(R.id.id_name);
            idName.setText(TaxonUtils.getTaxonName(mContext, item.getJSONObject("taxon")));
            TextView idTaxonName = (TextView) view.findViewById(R.id.id_taxon_name);
            idTaxonName.setText(TaxonUtils.getTaxonScientificName(item.getJSONObject("taxon")));
            int rankLevel = item.getJSONObject("taxon").optInt("rank_level");
            idTaxonName.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
            if (rankLevel <= 20) {
                idTaxonName.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.ITALIC));
            }
            Boolean isCurrent = item.getBoolean("current");
            if ((isCurrent == null) || (!isCurrent)) {
                // An outdated identification - show as faded-out
                idName.setTextColor(idName.getTextColors().withAlpha(100));
                idTaxonName.setTextColor(idTaxonName.getTextColors().withAlpha(100));
                if (!mIsNewLayout)
                    postedOn.setTextColor(postedOn.getTextColors().withAlpha(100));
                idPic.setAlpha(100);
                userPic.setAlpha(100);
            } else {
                idName.setTextColor(idName.getTextColors().withAlpha(255));
                idTaxonName.setTextColor(idTaxonName.getTextColors().withAlpha(255));
                if (!mIsNewLayout)
                    postedOn.setTextColor(postedOn.getTextColors().withAlpha(255));
                idPic.setAlpha(255);
                if (hasUserIcon)
                    userPic.setAlpha(255);
            }
            final View agree = view.findViewById(R.id.id_agree);
            agree.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if ((mLogin != null) && (username.equalsIgnoreCase(mLogin))) {
                        mOnIDAddedCb.onIdentificationRemoved(item);
                    } else {
                        mOnIDAddedCb.onIdentificationAdded(item);
                        mTaxonId = item.getInt("taxon_id");
                    }
                    loading.setVisibility(View.VISIBLE);
                    if (!mIsNewLayout) {
                        agree.setVisibility(View.GONE);
                    }
                    mAgreeing.set(position, true);
                }
            });
            loading.setVisibility(View.GONE);
            // See if there's ID of the same taxon before this one
            int currentTaxonId = item.getInt("taxon_id");
            boolean foundPreviousSameTaxon = false;
            for (int i = 0; i < position; i++) {
                BetterJSONObject taxon = mItems.get(i);
                Integer taxonId = taxon.getInt("taxon_id");
                if ((taxonId != null) && (taxonId == currentTaxonId)) {
                    foundPreviousSameTaxon = true;
                    break;
                }
            }
            boolean didNotIdThisBefore = true;
            for (int i = 0; i < mItems.size(); i++) {
                if (mLogin == null)
                    break;
                if (i == position)
                    continue;
                BetterJSONObject taxon = mItems.get(i);
                if ((taxon.getJSONObject("user").getString("login").equalsIgnoreCase(mLogin))) {
                    Integer taxonId = taxon.getInt("taxon_id");
                    if ((taxonId != null) && (taxonId == currentTaxonId) && (taxon.getBoolean("current"))) {
                        // Agreed on the current taxon type before
                        didNotIdThisBefore = false;
                        break;
                    }
                }
            }
            if (!foundPreviousSameTaxon && didNotIdThisBefore) {
                // show agree button
                if (mIsNewLayout) {
                    idAgreeLayout.setVisibility(View.VISIBLE);
                } else {
                    agree.setVisibility(View.VISIBLE);
                }
            } else {
                // Second (or more) taxon id of its kind - don't show agree button
                if (mIsNewLayout) {
                    idAgreeLayout.setVisibility(View.GONE);
                } else {
                    agree.setVisibility(View.GONE);
                }
            }
            if (moreMenu != null)
                moreMenu.setVisibility(View.GONE);
            if ((mLogin != null) && (username.equalsIgnoreCase(mLogin))) {
                if (!mIsNewLayout) {
                    ((Button) agree).setText(R.string.remove);
                    agree.setVisibility(View.VISIBLE);
                } else {
                    idAgreeLayout.setVisibility(View.GONE);
                    if (moreMenu != null)
                        moreMenu.setVisibility(View.VISIBLE);
                }
                if ((isCurrent == null) || (!isCurrent)) {
                    // Faded IDs should not have a "Remove" button
                    if (mIsNewLayout) {
                        idAgreeLayout.setVisibility(View.GONE);
                    } else {
                        agree.setVisibility(View.GONE);
                    }
                }
            } else {
                if (!mIsNewLayout)
                    ((Button) agree).setText(R.string.agree);
            }
            if ((mAgreeing.get(position) != null) && (mAgreeing.get(position) == true)) {
                loading.setVisibility(View.VISIBLE);
                if (!mIsNewLayout) {
                    agree.setVisibility(View.GONE);
                } else {
                    idAgreeLayout.setVisibility(View.GONE);
                }
            }
            if (mLogin == null) {
                // Can't agree if not logged in
                if (!mIsNewLayout) {
                    agree.setVisibility(View.GONE);
                } else {
                    idAgreeLayout.setVisibility(View.GONE);
                }
            }
            if (mIsNewLayout) {
                OnClickListener listener = new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(mContext, TaxonActivity.class);
                        JSONObject taxon = mItems.get(position).getJSONObject("taxon");
                        intent.putExtra(TaxonActivity.TAXON, new BetterJSONObject(taxon));
                        intent.putExtra(TaxonActivity.OBSERVATION, mObservation);
                        intent.putExtra(TaxonActivity.DOWNLOAD_TAXON, true);
                        mContext.startActivity(intent);
                    }
                };
                idLayout.setOnClickListener(listener);
                idName.setOnClickListener(listener);
                idTaxonName.setOnClickListener(listener);
            }
            boolean isActive = item.getJSONObject("taxon").optBoolean("is_active", true);
            ViewGroup taxonInactive = (ViewGroup) view.findViewById(R.id.taxon_inactive);
            taxonInactive.setVisibility(isActive ? View.GONE : View.VISIBLE);
            if (!isActive) {
                idTaxonName.setPaintFlags(idTaxonName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                idName.setPaintFlags(idTaxonName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            }
            taxonInactive.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    INaturalistApp app = (INaturalistApp) mContext.getApplicationContext();
                    String inatNetwork = app.getInaturalistNetworkMember();
                    String inatHost = app.getStringResourceByName("inat_host_" + inatNetwork);
                    Locale deviceLocale = mContext.getResources().getConfiguration().locale;
                    String deviceLanguage = deviceLocale.getLanguage();
                    String taxonUrl = String.format("%s/taxon_changes?taxon_id=%d&locale=%s", inatHost, item.getInt("taxon_id"), deviceLanguage);
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(taxonUrl));
                    mContext.startActivity(i);
                }
            });
        }
        if (moreMenu != null) {
            if ((mLogin == null) || ((mLogin != null) && (!username.equalsIgnoreCase(mLogin)))) {
                moreMenu.setVisibility(View.GONE);
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    view.setTag(item);
    if (!mIsNewLayout)
        view.setOnClickListener(this);
    return view;
}
Also used : Locale(java.util.Locale) DialogInterface(android.content.DialogInterface) Timestamp(java.sql.Timestamp) Button(android.widget.Button) TextView(android.widget.TextView) ImageView(android.widget.ImageView) PopupMenu(android.widget.PopupMenu) Menu(android.view.Menu) ViewGroup(android.view.ViewGroup) JSONException(org.json.JSONException) Intent(android.content.Intent) OnClickListener(android.view.View.OnClickListener) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) Paint(android.graphics.Paint) UrlImageViewCallback(com.koushikdutta.urlimageviewhelper.UrlImageViewCallback) Callback(com.squareup.picasso.Callback) JSONObject(org.json.JSONObject) LayoutInflater(android.view.LayoutInflater) RelativeLayout(android.widget.RelativeLayout) OnClickListener(android.view.View.OnClickListener) Resources(android.content.res.Resources) BottomSheet(com.cocosw.bottomsheet.BottomSheet) SimpleDateFormat(java.text.SimpleDateFormat) PopupMenu(android.widget.PopupMenu)

Aggregations

DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Resources (android.content.res.Resources)1 Paint (android.graphics.Paint)1 LayoutInflater (android.view.LayoutInflater)1 Menu (android.view.Menu)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 ViewGroup (android.view.ViewGroup)1 Button (android.widget.Button)1 ImageView (android.widget.ImageView)1 PopupMenu (android.widget.PopupMenu)1 RelativeLayout (android.widget.RelativeLayout)1 TextView (android.widget.TextView)1 BottomSheet (com.cocosw.bottomsheet.BottomSheet)1 UrlImageViewCallback (com.koushikdutta.urlimageviewhelper.UrlImageViewCallback)1 Callback (com.squareup.picasso.Callback)1 Timestamp (java.sql.Timestamp)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Locale (java.util.Locale)1