Skip to content

Commit

Permalink
#1349 - for obscured/private observations (that are not yours) - only…
Browse files Browse the repository at this point in the history
… show month+year
  • Loading branch information
budowski committed Jul 21, 2024
1 parent 9d2e119 commit 7f9894d
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 13 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="610"
android:versionName="1.31.1">
android:versionCode="611"
android:versionName="1.31.2">

<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 @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ForkJoinPool;

import org.apache.commons.lang3.time.FastDateFormat;
import org.joda.time.Duration;
Expand Down Expand Up @@ -220,7 +221,7 @@ public boolean onMenuItemClick(android.view.MenuItem menuItem) {


postedOn.setText(String.format(res.getString(item.getString("type").equals("comment") ? R.string.comment_title : R.string.id_title),
username, formatIdDate(mContext, postDate)));
username, formatIdDate(mContext, postDate, mObservation)));

OnClickListener showUser = new OnClickListener() {
@Override
Expand Down Expand Up @@ -537,11 +538,37 @@ private boolean allowToShowHiddenContent(BetterJSONObject item) {
return false;
}

public static String formatIdDate(Context context, Timestamp postDate) {
public static String formatIdDate(Context context, Timestamp postDate, BetterJSONObject observation) {
// Only show month/year for observations that you don't own + obscured/private
INaturalistApp app = (INaturalistApp) context.getApplicationContext();
String currentUser = app.currentUserLogin();
boolean obsByUser = observation != null &&
currentUser != null &&
observation.getJSONObject("user") != null &&
observation.getJSONObject("user").optString("login", "").equals(currentUser);
boolean isPrivateOrObscured = false;

if (observation != null) {
if (observation.getString("geoprivacy") != null) {
if (observation.getString("geoprivacy").equals("private") ||
observation.getString("geoprivacy").equals("obscured")) {
isPrivateOrObscured = true;
}
}
if (observation.getString("taxon_geoprivacy") != null) {
if (observation.getString("taxon_geoprivacy").equals("private") ||
observation.getString("taxon_geoprivacy").equals("obscured")) {
isPrivateOrObscured = true;
}
}
}

boolean isObscured = !obsByUser && isPrivateOrObscured;

Duration difference = new Duration(postDate.getTime(), (new Date()).getTime());
long days = difference.getStandardDays();

if (days <= 30) {
if (days <= 30 && !isObscured) {
// Less than 30 days ago - display as 3m (mins), 3h (hours), 3d (days) or 3w (weeks)
if (days < 1) {
long hours = difference.getStandardHours();
Expand All @@ -563,7 +590,10 @@ public static String formatIdDate(Context context, Timestamp postDate) {
calDate.setTimeInMillis(postDate.getTime());

String dateFormatString;
if (today.get(Calendar.YEAR) != calDate.get(Calendar.YEAR)) {
if (isObscured) {
// Only show month+year for obscured observations
dateFormatString = context.getString(R.string.date_obscured);
} else if (today.get(Calendar.YEAR) != calDate.get(Calendar.YEAR)) {
// Previous year(s)
dateFormatString = context.getString(R.string.date_short);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ public class FavoritesAdapter extends ArrayAdapter<BetterJSONObject> {
private List<BetterJSONObject> mItems;
private Context mContext;
private String mLogin;
private BetterJSONObject mObservation;

public boolean isEnabled(int position) {
return false;
}

public FavoritesAdapter(Context context, List<BetterJSONObject> objects) {
public FavoritesAdapter(Context context, List<BetterJSONObject> objects, BetterJSONObject observation) {
super(context, R.layout.favorite_item, objects);

mItems = objects;
mContext = context;
mHelper = new ActivityHelper(mContext);
mObservation = observation;

SharedPreferences prefs = mContext.getSharedPreferences("iNaturalistPreferences", Activity.MODE_PRIVATE);
mLogin = prefs.getString("username", null);
Expand All @@ -83,7 +85,7 @@ public View getView(final int position, View convertView, ViewGroup parent) {
userNameText.setText(username);
Timestamp postDate = item.getTimestamp("created_at");

favDate.setText(CommentsIdsAdapter.formatIdDate(mContext, postDate));
favDate.setText(CommentsIdsAdapter.formatIdDate(mContext, postDate, mObservation));

final ImageView userPic = (ImageView) view.findViewById(R.id.user_pic);
boolean hasUserIcon = item.getJSONObject("user").has("user_icon_url") && !item.getJSONObject("user").isNull("user_icon_url");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ public void onGlobalLayout() {
} else {
dateObserved.setVisibility(View.VISIBLE);
Timestamp observationDate = new Timestamp(observationTimestamp);
dateObserved.setText(CommentsIdsAdapter.formatIdDate(mContext, observationDate));
dateObserved.setText(CommentsIdsAdapter.formatIdDate(mContext, observationDate, null));
}
}
Trace.endSection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public static JSONObject getMinimalObservation(JSONObject observation) {
if (observation.has("identifications_count") && !observation.isNull("identifications_count")) minimaldObs.put("identifications_count", observation.optInt("identifications_count"));
if (observation.has("private_latitude") && !observation.isNull("private_latitude")) minimaldObs.put("private_latitude", observation.optString("private_latitude"));
if (observation.has("private_longitude") && !observation.isNull("private_longitude")) minimaldObs.put("private_longitude", observation.optString("private_longitude"));
if (observation.has("geoprivacy") && !observation.isNull("geoprivacy")) minimaldObs.put("geoprivacy", observation.optString("geoprivacy"));
if (observation.has("taxon_geoprivacy") && !observation.isNull("taxon_geoprivacy")) minimaldObs.put("taxon_geoprivacy", observation.optString("taxon_geoprivacy"));

if (observation.has("location") && !minimaldObs.has("latitude") && !minimaldObs.has("longitude")) {
String location = observation.getString("location");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import com.squareup.picasso.Picasso;
import com.viewpagerindicator.CirclePageIndicator;

import org.apache.commons.lang3.time.FastDateFormat;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -1211,7 +1212,7 @@ private void refreshFavorites() {
mRemoveFavorite.setVisibility(View.GONE);
}

mFavoritesAdapter = new FavoritesAdapter(getActivity(), mFavorites);
mFavoritesAdapter = new FavoritesAdapter(getActivity(), mFavorites, new BetterJSONObject(mObsJson));
mFavoritesList.setAdapter(mFavoritesAdapter);

mRemoveFavorite.setOnClickListener(new OnClickListener() {
Expand Down Expand Up @@ -2392,6 +2393,38 @@ private JSONObject downloadJson(String uri) {
}

private String formatObservedOn(Timestamp date, Timestamp time) {
BetterJSONObject observation = mObsJson != null ? new BetterJSONObject(mObsJson) : null;
// Only show month/year for observations that you don't own + obscured/private
String currentUser = mApp.currentUserLogin();
boolean obsByUser = observation != null &&
currentUser != null &&
observation.getJSONObject("user") != null &&
observation.getJSONObject("user").optString("login", "").equals(currentUser);
boolean isPrivateOrObscured = false;

if (observation != null) {
if (observation.getString("geoprivacy") != null) {
if (observation.getString("geoprivacy").equals("private") ||
observation.getString("geoprivacy").equals("obscured")) {
isPrivateOrObscured = true;
}
}
if (observation.getString("taxon_geoprivacy") != null) {
if (observation.getString("taxon_geoprivacy").equals("private") ||
observation.getString("taxon_geoprivacy").equals("obscured")) {
isPrivateOrObscured = true;
}
}
}

boolean isObscured = !obsByUser && isPrivateOrObscured;

if (isObscured && date != null) {
// Only show month+year for obscured observations
FastDateFormat dateFormat = FastDateFormat.getInstance(getString(R.string.date_obscured), getResources().getConfiguration().locale);
return dateFormat.format(new Date(date.getTime()));
}

StringBuilder format = new StringBuilder();

if (date != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
String noHTMLDescription = newsContent.getText().toString();
newsContent.setText(noHTMLDescription.replaceAll("[\\xa0]+", "").trim()); // Strip all HTML/Markdown formatting in the preview text
BetterJSONObject newsItem = new BetterJSONObject(item);
newsDate.setText(CommentsIdsAdapter.formatIdDate(mContext, newsItem.getTimestamp("published_at")));
newsDate.setText(CommentsIdsAdapter.formatIdDate(mContext, newsItem.getTimestamp("published_at"), null));

if (firstPhotoUrl != null) {
// Set the article photo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public View getView(int position, View convertView, ViewGroup parent) {

obsPic.setVisibility(View.VISIBLE);

final String dateFormatted = CommentsIdsAdapter.formatIdDate(mContext, item.getTimestamp("created_at"));
final String dateFormatted = CommentsIdsAdapter.formatIdDate(mContext, item.getTimestamp("created_at"), null);
String userName = null;
String userIconUrl = null;
JSONObject user = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void onError() {
if (dateTimestamp == null) {
date.setVisibility(View.INVISIBLE);
} else {
date.setText(CommentsIdsAdapter.formatIdDate(mContext, dateTimestamp));
date.setText(CommentsIdsAdapter.formatIdDate(mContext, dateTimestamp, new BetterJSONObject(item)));
date.setVisibility(View.VISIBLE);
}

Expand Down
2 changes: 2 additions & 0 deletions iNaturalist/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,8 @@
<string name="time_short_12_hour">hh:mma</string>
<!-- Long date formatting template. See https://developer.android.com/reference/java/text/SimpleDateFormat#date-and-time-patterns for formatting options -->
<string name="date_long">MMM d, yyyy</string>
<!-- Obscured observations date formatting template (only month+year). See https://developer.android.com/reference/java/text/SimpleDateFormat#date-and-time-patterns for formatting options -->
<string name="date_obscured">MMM \'\'yy</string>
<!-- Long date with time formatting template. See https://developer.android.com/reference/java/text/SimpleDateFormat#date-and-time-patterns for formatting options -->
<string name="date_long_with_time">MMMM d, yyyy hh:mma</string>
<string name="show_common_names">Show common names</string>
Expand Down

0 comments on commit 7f9894d

Please sign in to comment.