Search in sources :

Example 1 with PleaseCreatePoiEvent

use of io.jawg.osmcontributor.model.events.PleaseCreatePoiEvent in project osm-contributor by jawg.

the class EditPoiFragment method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (tagsAdapter == null) {
        getActivity().finish();
        return true;
    }
    if (id == R.id.action_confirm_edit) {
        // If we are in expert mode, directly create or update the Poi
        if (sharedPreferences.getBoolean(getString(R.string.shared_prefs_expert_mode), false)) {
            if (creation) {
                getActivity().setResult(EditPoiActivity.POI_CREATED, null);
                eventBus.post(new PleaseCreatePoiEvent(poi, tagsAdapter.getPoiChanges()));
                OsmAnswers.localPoiAction(poi.getType().getTechnicalName(), "add");
            } else if (tagsAdapter.isChange()) {
                getActivity().setResult(EditPoiActivity.POI_EDITED, null);
                eventBus.post(new PleaseApplyPoiChanges(tagsAdapter.getPoiChanges()));
                OsmAnswers.localPoiAction(poi.getType().getTechnicalName(), "update");
            } else {
                Toast.makeText(getActivity(), R.string.not_any_changes, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        // else check if all mandatory tags are completed.
        if (creation) {
            if (!poi.getType().hasMandatoryTags() || tagsAdapter.isValidChanges()) {
                getActivity().setResult(EditPoiActivity.POI_CREATED, null);
                eventBus.post(new PleaseCreatePoiEvent(poi, tagsAdapter.getPoiChanges()));
                OsmAnswers.localPoiAction(poi.getType().getTechnicalName(), "add");
            } else {
                tagsAdapter.refreshErrorStatus();
                Toast.makeText(getActivity(), R.string.uncompleted_fields, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        // In edit mode, check if there are changes then check if all the mandatory tags are completed.
        if (tagsAdapter.isChange()) {
            if (tagsAdapter.isValidChanges()) {
                getActivity().setResult(EditPoiActivity.POI_EDITED, null);
                eventBus.post(new PleaseApplyPoiChanges(tagsAdapter.getPoiChanges()));
                OsmAnswers.localPoiAction(poi.getType().getTechnicalName(), "update");
            } else {
                tagsAdapter.refreshErrorStatus();
                Toast.makeText(getActivity(), R.string.uncompleted_fields, Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(getActivity(), R.string.not_any_changes, Toast.LENGTH_SHORT).show();
        }
        return true;
    }
    if (id == android.R.id.home) {
        if (!tagsAdapter.isChange()) {
            getActivity().setResult(Activity.RESULT_CANCELED, null);
            getActivity().finish();
        } else {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
            alertDialog.setTitle(R.string.quit_edition_title);
            alertDialog.setMessage(R.string.quit_edition_message);
            alertDialog.setPositiveButton(R.string.quit_edition_positive_btn, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    getActivity().setResult(Activity.RESULT_CANCELED, null);
                    getActivity().finish();
                }
            });
            alertDialog.setNegativeButton(R.string.quit_edition_negative_btn, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            alertDialog.show();
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Also used : AlertDialog(android.app.AlertDialog) PleaseCreatePoiEvent(io.jawg.osmcontributor.model.events.PleaseCreatePoiEvent) DialogInterface(android.content.DialogInterface) PleaseApplyPoiChanges(io.jawg.osmcontributor.ui.events.edition.PleaseApplyPoiChanges)

Aggregations

AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 PleaseCreatePoiEvent (io.jawg.osmcontributor.model.events.PleaseCreatePoiEvent)1 PleaseApplyPoiChanges (io.jawg.osmcontributor.ui.events.edition.PleaseApplyPoiChanges)1