Search in sources :

Example 6 with Note

use of io.jawg.osmcontributor.model.entities.Note in project osm-contributor by jawg.

the class NoteManager method onPleaseApplyNewComment.

@Subscribe(threadMode = ThreadMode.ASYNC)
public void onPleaseApplyNewComment(PleaseApplyNewComment event) {
    Timber.d("please apply new comment");
    if (loginManager.checkCredentials()) {
        Note note = syncNoteManager.remoteAddComment(createComment(event.getNote(), event.getAction(), event.getText()));
        if (note != null) {
            mergeBackendNote(note);
            bus.post(new NewNoteCreatedEvent(note.getId()));
            bus.post(new SyncFinishUploadNote(note));
        }
    } else {
        bus.post(new ApplyNewCommentFailedEvent());
    }
}
Also used : NewNoteCreatedEvent(io.jawg.osmcontributor.ui.events.map.NewNoteCreatedEvent) SyncFinishUploadNote(io.jawg.osmcontributor.rest.events.SyncFinishUploadNote) SyncFinishUploadNote(io.jawg.osmcontributor.rest.events.SyncFinishUploadNote) Note(io.jawg.osmcontributor.model.entities.Note) ApplyNewCommentFailedEvent(io.jawg.osmcontributor.ui.events.note.ApplyNewCommentFailedEvent) Subscribe(org.greenrobot.eventbus.Subscribe)

Example 7 with Note

use of io.jawg.osmcontributor.model.entities.Note in project osm-contributor by jawg.

the class NoteManager method mergeBackendNote.

/**
 * Merge Note in parameters to the already in the database.
 *
 * @param remoteNote The Note to merge.
 */
public void mergeBackendNote(Note remoteNote) {
    Note localNote = noteDao.queryByBackendId(remoteNote.getBackendId());
    if (localNote != null) {
        remoteNote.setId(localNote.getId());
    }
    saveNote(remoteNote);
}
Also used : SyncFinishUploadNote(io.jawg.osmcontributor.rest.events.SyncFinishUploadNote) Note(io.jawg.osmcontributor.model.entities.Note)

Example 8 with Note

use of io.jawg.osmcontributor.model.entities.Note in project osm-contributor by jawg.

the class NoteManager method mergeBackendNotes.

/**
 * Merge Notes in parameters to those already in the database.
 *
 * @param remoteNotes The Notes to merge.
 */
public void mergeBackendNotes(List<Note> remoteNotes) {
    List<Note> toMergeNotes = new ArrayList<>();
    Map<String, Note> remoteNotesMap = new HashMap<>();
    // Map remote Note backend Ids
    for (Note note : remoteNotes) {
        remoteNotesMap.put(note.getBackendId(), note);
    }
    // List matching Notes
    List<Note> localNotes = noteDao.queryByBackendIds(remoteNotesMap.keySet());
    Map<String, Note> localNotesMap = new HashMap<>();
    // Map matching local Notes
    for (Note localNote : localNotes) {
        localNotesMap.put(localNote.getBackendId(), localNote);
    }
    // Browse remote notes
    for (Note remoteNote : remoteNotes) {
        Note localNote = localNotesMap.get(remoteNote.getBackendId());
        if (localNote != null) {
            remoteNote.setId(localNote.getId());
        }
        // This Note should be updated
        toMergeNotes.add(remoteNote);
    }
    // saveNotes of either new or existing Notes
    saveNotes(toMergeNotes);
}
Also used : HashMap(java.util.HashMap) SyncFinishUploadNote(io.jawg.osmcontributor.rest.events.SyncFinishUploadNote) Note(io.jawg.osmcontributor.model.entities.Note) ArrayList(java.util.ArrayList)

Example 9 with Note

use of io.jawg.osmcontributor.model.entities.Note in project osm-contributor by jawg.

the class OSMSyncNoteManager method remoteAddComment.

/**
 * {@inheritDoc}
 */
public Note remoteAddComment(final Comment comment) {
    Call<OsmDto> osmDtoCall;
    switch(comment.getAction()) {
        case Comment.ACTION_CLOSE:
            osmDtoCall = osmRestClient.closeNote(comment.getNote().getBackendId(), comment.getText(), "");
            break;
        case Comment.ACTION_REOPEN:
            osmDtoCall = osmRestClient.reopenNote(comment.getNote().getBackendId(), comment.getText(), "");
            break;
        case Comment.ACTION_OPEN:
            osmDtoCall = osmRestClient.addNote(comment.getNote().getLatitude(), comment.getNote().getLongitude(), comment.getText(), "");
            break;
        default:
            osmDtoCall = osmRestClient.addComment(comment.getNote().getBackendId(), comment.getText(), "");
            break;
    }
    try {
        Response<OsmDto> response = osmDtoCall.execute();
        if (response.isSuccessful()) {
            OsmDto osmDto = response.body();
            if (osmDto != null) {
                NoteDto noteDto = osmDto.getNoteDtoList().get(0);
                Note note = noteMapper.convertNoteDtoToNote(noteDto);
                note.setUpdated(false);
                note.setId(comment.getNote().getId());
                return note;
            }
        } else if (response.code() == 409) {
            bus.post(new SyncConflictingNoteErrorEvent(comment.getNote()));
        }
    } catch (IOException e) {
        Timber.e(e, "Retrofit error, couldn't create comment !");
    }
    bus.post(new SyncUploadNoteRetrofitErrorEvent(comment.getNote().getId()));
    return null;
}
Also used : OsmDto(io.jawg.osmcontributor.rest.dtos.osm.OsmDto) SyncConflictingNoteErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncConflictingNoteErrorEvent) Note(io.jawg.osmcontributor.model.entities.Note) SyncUploadNoteRetrofitErrorEvent(io.jawg.osmcontributor.rest.events.error.SyncUploadNoteRetrofitErrorEvent) IOException(java.io.IOException) NoteDto(io.jawg.osmcontributor.rest.dtos.osm.NoteDto)

Example 10 with Note

use of io.jawg.osmcontributor.model.entities.Note in project osm-contributor by jawg.

the class NoteCommentDialogFragment method onCreateDialog.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    ((OsmTemplateApplication) getActivity().getApplication()).getOsmTemplateComponent().inject(this);
    lat = getArguments().getDouble(LAT);
    lng = getArguments().getDouble(LNG);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final EditText input = new EditText(this.getActivity());
    input.setMaxLines(5);
    openKeyboard();
    String title = getResources().getString(R.string.title_creation_note);
    builder.setTitle(title).setView(input).setPositiveButton(R.string.create_note, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            String comment = input.getText().toString();
            if (!comment.isEmpty()) {
                Note note = new Note();
                note.setStatus(Note.STATUS_SYNC);
                note.setLatitude(lat);
                note.setLongitude(lng);
                note.setUpdated(true);
                eventBus.post(new PleaseApplyNewComment(note, Comment.ACTION_OPEN, comment));
                closeKeyboard();
            }
        }
    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    final AlertDialog dialog = builder.create();
    dialog.show();
    // Initially disable the button
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    input.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            // Check if edittext is empty
            if (TextUtils.isEmpty(s)) {
                // Disable ok button
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
            } else {
                // Something into edit text. Enable the button.
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
            }
        }
    });
    return dialog;
}
Also used : AlertDialog(android.app.AlertDialog) EditText(android.widget.EditText) DialogInterface(android.content.DialogInterface) PleaseApplyNewComment(io.jawg.osmcontributor.ui.events.map.PleaseApplyNewComment) Note(io.jawg.osmcontributor.model.entities.Note) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable)

Aggregations

Note (io.jawg.osmcontributor.model.entities.Note)15 ArrayList (java.util.ArrayList)5 Comment (io.jawg.osmcontributor.model.entities.Comment)4 SyncFinishUploadNote (io.jawg.osmcontributor.rest.events.SyncFinishUploadNote)4 DateTime (org.joda.time.DateTime)3 Poi (io.jawg.osmcontributor.model.entities.Poi)2 NoteDto (io.jawg.osmcontributor.rest.dtos.osm.NoteDto)2 PleaseChangeValuesDetailNoteFragmentEvent (io.jawg.osmcontributor.ui.events.map.PleaseChangeValuesDetailNoteFragmentEvent)2 NoteManager (io.jawg.osmcontributor.ui.managers.NoteManager)2 MapElement (io.jawg.osmcontributor.utils.core.MapElement)2 Subscribe (org.greenrobot.eventbus.Subscribe)2 Test (org.junit.Test)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Bitmap (android.graphics.Bitmap)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 Animation (android.view.animation.Animation)1 EditText (android.widget.EditText)1 PoisAndNotesDownloadedEvent (io.jawg.osmcontributor.model.events.PoisAndNotesDownloadedEvent)1