Skip to content

Commit

Permalink
#1321 - unexpected popup menu for IDs/comments in observation viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
budowski committed Jan 29, 2024
1 parent 208828b commit 5a15932
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 98 deletions.
4 changes: 2 additions & 2 deletions iNaturalist/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="auto"
android:versionCode="600"
android:versionName="1.30.7">
android:versionCode="601"
android:versionName="1.30.8">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,54 +120,9 @@ public View getView(final int position, View convertView, ViewGroup parent) {
final String username = item.getJSONObject("user").getString("login");
Timestamp postDate = item.getTimestamp("updated_at");
if (postDate == null) postDate = item.getTimestamp("created_at");

postedOn.setText(String.format(res.getString(item.getString("type").equals("comment") ? R.string.comment_title : R.string.id_title),
username, formatIdDate(mContext, 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 && userIconUrl.length() > 0;

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 {
userPic.setAlpha(100);
}

final View moreMenu = view.findViewById(R.id.more_menu);
final boolean isComment = item.getString("type").equals("comment");
final View loading = view.findViewById(R.id.loading);
final boolean isComment = item.getString("type").equals("comment");
final View moreMenu = view.findViewById(R.id.more_menu);

final DialogInterface.OnClickListener onClick = new DialogInterface.OnClickListener() {
@Override
Expand Down Expand Up @@ -211,59 +166,107 @@ public void onClick(DialogInterface dialogInterface, int i) {
}
};

if (moreMenu != null) {
moreMenu.setVisibility(View.GONE);
moreMenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (loading.getVisibility() == View.VISIBLE) {
return;
}
OnClickListener popupMenuListener = 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;
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();
boolean restoreId = false;
if (menuResource == R.menu.id_menu) {
Boolean isCurrent = item.getBoolean("current");
if (((isCurrent == null) || (!isCurrent)) && (username.equalsIgnoreCase(mLogin))) {
restoreId = true;
}
}

if (!isComment) {
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);
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 (!isComment) {
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);
}
}

}
};


postedOn.setText(String.format(res.getString(item.getString("type").equals("comment") ? R.string.comment_title : R.string.id_title),
username, formatIdDate(mContext, 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 && userIconUrl.length() > 0;

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 {
userPic.setAlpha(100);
}


if (moreMenu != null) {
moreMenu.setVisibility(View.GONE);
moreMenu.setOnClickListener(null);
}

showHiddenContent.setOnClickListener(new OnClickListener() {
Expand Down Expand Up @@ -320,7 +323,10 @@ public void onClick(View view) {
comment.setVisibility(View.VISIBLE);
idLayout.setVisibility(View.GONE);
loading.setVisibility(View.GONE);
if (moreMenu != null) moreMenu.setVisibility(View.VISIBLE);
if (moreMenu != null) {
moreMenu.setVisibility(View.VISIBLE);
moreMenu.setOnClickListener(popupMenuListener);
}
idAgreeLayout.setVisibility(View.GONE);

comment.setTypeface(null, Typeface.NORMAL);
Expand Down Expand Up @@ -434,11 +440,17 @@ public void onClick(View v) {
idAgreeLayout.setVisibility(View.GONE);
}

if (moreMenu != null) moreMenu.setVisibility(View.GONE);
if (moreMenu != null) {
moreMenu.setVisibility(View.GONE);
moreMenu.setOnClickListener(null);
}

if ((mLogin != null) && (username.equalsIgnoreCase(mLogin))) {
idAgreeLayout.setVisibility(View.GONE);
if (moreMenu != null) moreMenu.setVisibility(View.VISIBLE);
if (moreMenu != null) {
moreMenu.setVisibility(View.VISIBLE);
moreMenu.setOnClickListener(popupMenuListener);
}

if ((isCurrent == null) || (!isCurrent)) {
// Faded IDs should not have a "Remove" button
Expand Down Expand Up @@ -501,6 +513,7 @@ public void onClick(View v) {
if (moreMenu != null) {
if ((mLogin == null) || ((mLogin != null) && (!username.equalsIgnoreCase(mLogin)))) {
moreMenu.setVisibility(View.GONE);
moreMenu.setOnClickListener(null);
}
}
} catch (JSONException e) {
Expand Down

0 comments on commit 5a15932

Please sign in to comment.