Skip to content

Commit

Permalink
#944 - obs editor - remove date/time/location
Browse files Browse the repository at this point in the history
  • Loading branch information
budowski committed Dec 11, 2020
1 parent c39f979 commit 7a6962c
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 10 deletions.
Expand Up @@ -157,7 +157,7 @@ public Timestamp getTimestamp(String name) {

public void put(String name, Object value) {
try {
mJSONObject.put(name, value);
mJSONObject.put(name, value != null ? value : JSONObject.NULL);
} catch (JSONException e1) {
try {
mJSONObject.put(name, value.toString());
Expand Down
31 changes: 25 additions & 6 deletions iNaturalist/src/main/java/org/inaturalist/android/Observation.java
Expand Up @@ -777,10 +777,13 @@ public boolean merge(Observation observation) {
return isModified;
}
}

public ContentValues getContentValues() {
return getContentValues(false);
}

public ContentValues getContentValues(boolean forceNullValues) {
ContentValues cv = new ContentValues();
if (created_at != null) { cv.put(CREATED_AT, created_at.getTime()); }
if (created_at != null) cv.put(CREATED_AT, created_at.getTime());
cv.put(DESCRIPTION, description);
cv.put(GEOPRIVACY, geoprivacy);
cv.put(TAXON_GEOPRIVACY, taxon_geoprivacy);
Expand All @@ -790,7 +793,13 @@ public ContentValues getContentValues() {
cv.put(ID_PLEASE, id_please);
cv.put(LATITUDE, latitude);
cv.put(LONGITUDE, longitude);
if (observed_on != null) { cv.put(OBSERVED_ON, observed_on.getTime()); }
if (observed_on != null || forceNullValues) {
if (observed_on == null) {
cv.putNull(OBSERVED_ON);
} else {
cv.put(OBSERVED_ON, observed_on.getTime());
}
}
cv.put(OBSERVED_ON_STRING, observed_on_string);
cv.put(OUT_OF_RANGE, out_of_range);
cv.put(CAPTIVE, captive);
Expand All @@ -811,8 +820,14 @@ public ContentValues getContentValues() {
cv.put(SPECIES_GUESS, species_guess);
cv.put(PREFERRED_COMMON_NAME, preferred_common_name);
cv.put(TAXON_ID, taxon_id);
if (time_observed_at != null) { cv.put(TIME_OBSERVED_AT, time_observed_at.getTime()); }
if (updated_at != null) { cv.put(UPDATED_AT, updated_at.getTime()); }
if (time_observed_at != null || forceNullValues) {
if (time_observed_at == null) {
cv.putNull(TIME_OBSERVED_AT);
} else {
cv.put(TIME_OBSERVED_AT, time_observed_at.getTime());
}
}
if (updated_at != null) cv.put(UPDATED_AT, updated_at.getTime());
cv.put(USER_AGENT, user_agent);
cv.put(USER_ID, user_id);
cv.put(USER_LOGIN, user_login);
Expand All @@ -821,7 +836,11 @@ public ContentValues getContentValues() {
cv.put(LAST_COMMENTS_COUNT, last_comments_count);
cv.put(LAST_IDENTIFICATIONS_COUNT, last_identifications_count);
cv.put(IS_DELETED, is_deleted);
cv.put(PREFERS_COMMUNITY_TAXON, (String)(prefers_community_taxon == null ? null : prefers_community_taxon.toString()));
if (prefers_community_taxon == null) {
cv.putNull(PREFERS_COMMUNITY_TAXON);
} else {
cv.put(PREFERS_COMMUNITY_TAXON, prefers_community_taxon.toString());
}

return cv;
}
Expand Down
Expand Up @@ -286,12 +286,22 @@ public boolean onCreateOptionsMenu(Menu menu) {
}

mMenu.getItem(0).setEnabled(mApp.isNetworkAvailable());
}

refreshMenuItems();
}

return true;
}

private void refreshMenuItems() {
mMenu.getItem(1).setVisible(mDateSetByUser != null);
mMenu.getItem(2).setVisible(mTimeSetByUser != null);
mMenu.getItem(3).setVisible(
(mLatitudeView.getText().length() > 0) ||
(mLongitudeView.getText().length() > 0) ||
(mAccuracyView.getText().length() > 0));
}

/**
* LIFECYCLE CALLBACKS
*/
Expand Down Expand Up @@ -1389,6 +1399,86 @@ public boolean onOptionsItemSelected(MenuItem item) {
case android.R.id.home:
return onBack();

case R.id.remove_location:
mHelper.confirm(getString(R.string.remove_location), getString(R.string.are_you_sure_you_want_to_remove_location),
(DialogInterface.OnClickListener) (dialogInterface, i) -> {
mLatitudeView.setText("");
mLongitudeView.setText("");
mAccuracyView.setText("");
findViewById(R.id.coordinates).setVisibility(View.GONE);
mObservation.latitude = null;
mObservation.longitude = null;
mObservation.positional_accuracy = null;
setPlaceGuess(null);
refreshMenuItems();
},
(dialogInterface, i) -> {
},
R.string.yes,
R.string.no
);

return true;

case R.id.remove_date:
mHelper.confirm(getString(R.string.remove_date_observed), getString(R.string.are_you_sure_you_want_to_remove_date),
(DialogInterface.OnClickListener) (dialogInterface, i) -> {
mDateSetByUser = null;
mObservation.observed_on = null;
mObservedOnButton.setText(R.string.set_date);
mObservedOnButton.setTextColor(Color.parseColor("#757575"));

if (mTimeSetByUser != null) {
// Just time set now
Timestamp refDate = new Timestamp(System.currentTimeMillis());
Timestamp datetime = new Timestamp(refDate.getYear(), refDate.getMonth(), refDate.getDate(), mTimeSetByUser.getHours(), mTimeSetByUser.getMinutes(), 0, 0);
if (datetime.getTime() > System.currentTimeMillis()) {
datetime = new Timestamp(System.currentTimeMillis());
}
mObservedOnStringTextView.setText(mApp.formatDatetime(datetime));
} else {
// No time nor date
mObservedOnStringTextView.setText("");
}

refreshMenuItems();
},
(dialogInterface, i) -> {
},
R.string.yes,
R.string.no
);

return true;

case R.id.remove_time:
mHelper.confirm(getString(R.string.remove_time_observed), getString(R.string.are_you_sure_you_want_to_remove_time),
(DialogInterface.OnClickListener) (dialogInterface, i) -> {
mTimeSetByUser = null;
mObservation.time_observed_at = null;
mTimeObservedAtButton.setText(R.string.set_time);
mTimeObservedAtButton.setTextColor(Color.parseColor("#757575"));

if (mDateSetByUser != null) {
// Just date set now
Timestamp refDate = mDateSetByUser;
Timestamp datetime = new Timestamp(refDate.getYear(), refDate.getMonth(), refDate.getDate(), 0, 0, 0, 0);
mObservedOnStringTextView.setText(mApp.formatDatetime(datetime));
} else {
// No time nor date
mObservedOnStringTextView.setText("");
}

refreshMenuItems();
},
(dialogInterface, i) -> {
},
R.string.yes,
R.string.no
);

return true;

case R.id.prefers_community_taxon:
if ((mObservation.prefers_community_taxon == null) || (mObservation.prefers_community_taxon == true)) {
confirm(ObservationEditor.this, R.string.opt_out_of_community_taxon, R.string.opt_out_message,
Expand Down Expand Up @@ -1587,7 +1677,9 @@ private void uiToObservation() {
}
if (((mObservation.description == null) && (mDescriptionTextView.getText().length() > 0)) || (mObservation.description != null)) mObservation.description = mDescriptionTextView.getText().toString();
if (mObservedOnStringTextView.getText() == null || mObservedOnStringTextView.getText().length() == 0) {
mObservation.observed_on_string = null;
mObservation.observed_on_string = null;
mObservation.observed_on = null;
mObservation.time_observed_at = null;
} else {
mObservation.observed_on_string = mObservedOnStringTextView.getText().toString();
mObservation.observed_on = mDateSetByUser;
Expand Down Expand Up @@ -1808,7 +1900,7 @@ private final boolean save(boolean noValidation) {

try {
mObservation.owners_identification_from_vision = mFromSuggestion;
ContentValues cv = mObservation.getContentValues();
ContentValues cv = mObservation.getContentValues(true);
if (mObservation.latitude_changed()) {
cv.put(Observation.POSITIONING_METHOD, "gps");
cv.put(Observation.POSITIONING_DEVICE, "gps");
Expand Down Expand Up @@ -1901,6 +1993,7 @@ public void onDateSet(DatePicker view, int year, int month, int day) {
mDateSetByUser = date;
mObservedOnButton.setTextColor(Color.parseColor("#000000"));

refreshMenuItems();
AnalyticsClient.getInstance().logEvent(AnalyticsClient.EVENT_NAME_OBS_DATE_CHANGED);
}
};
Expand Down Expand Up @@ -1939,6 +2032,7 @@ public void onTimeSet(TimePicker view, int hour, int minute) {
mTimeObservedAtButton.setTextColor(Color.parseColor("#000000"));
mTimeSetByUser = datetime;

refreshMenuItems();
AnalyticsClient.getInstance().logEvent(AnalyticsClient.EVENT_NAME_OBS_DATE_CHANGED);
}
};
Expand Down Expand Up @@ -2188,6 +2282,7 @@ private void setCurrentLocation(Location location) {

mLatitudeView.setText(Double.toString(location.getLatitude()));
mLongitudeView.setText(Double.toString(location.getLongitude()));
refreshMenuItems();

findViewById(R.id.coordinates).setVisibility(View.VISIBLE);

Expand Down Expand Up @@ -2462,6 +2557,7 @@ public void run() {
findViewById(R.id.coordinates).setVisibility(View.VISIBLE);
findViewById(R.id.accuracy_prefix).setVisibility(View.VISIBLE);
findViewById(R.id.accuracy).setVisibility(View.VISIBLE);
refreshMenuItems();
}
} else if (requestCode == TAXON_SEARCH_REQUEST_CODE) {
mTaxonSearchStarted = false;
Expand Down
12 changes: 12 additions & 0 deletions iNaturalist/src/main/res/menu/observation_editor_menu.xml
Expand Up @@ -5,4 +5,16 @@
android:id="@+id/prefers_community_taxon"
android:title="@string/opt_out_of_community_taxon"
app:showAsAction="never" />
<item
android:id="@+id/remove_date"
android:title="@string/remove_date_observed"
app:showAsAction="never" />
<item
android:id="@+id/remove_time"
android:title="@string/remove_time_observed"
app:showAsAction="never" />
<item
android:id="@+id/remove_location"
android:title="@string/remove_location"
app:showAsAction="never" />
</menu>
6 changes: 6 additions & 0 deletions iNaturalist/src/main/res/values/strings.xml
Expand Up @@ -998,5 +998,11 @@
<string name="filter_observations_by_months">Filter observations by months</string>
<!-- Used to demonstrate annotation filters, e.g. Life Stage = Egg -->
<string name="equals_sign">=</string>
<string name="remove_date_observed">Remove date observed</string>
<string name="remove_time_observed">Remove time observed</string>
<string name="remove_location">Remove location</string>
<string name="are_you_sure_you_want_to_remove_date">Are you sure you want to remove the date observed?</string>
<string name="are_you_sure_you_want_to_remove_time">Are you sure you want to remove the time observed?</string>
<string name="are_you_sure_you_want_to_remove_location">Are you sure you want to remove the location?</string>
</resources>

0 comments on commit 7a6962c

Please sign in to comment.