Skip to content

Commit

Permalink
Moved some code to event driven architecture. This also fixes #940
Browse files Browse the repository at this point in the history
  • Loading branch information
federicoiosue committed Dec 9, 2023
1 parent 9bb2a96 commit c44a611
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@
import it.feio.android.omninotes.models.adapters.PlacesAutoCompleteAdapter;
import it.feio.android.omninotes.models.listeners.OnAttachingFileListener;
import it.feio.android.omninotes.models.listeners.OnGeoUtilResultListener;
import it.feio.android.omninotes.models.listeners.OnNoteSaved;
import it.feio.android.omninotes.models.listeners.OnReminderPickedListener;
import it.feio.android.omninotes.models.listeners.RecyclerViewItemClickSupport;
import it.feio.android.omninotes.models.views.ExpandableHeightGridView;
Expand Down Expand Up @@ -183,7 +182,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
Expand All @@ -192,7 +190,7 @@

public class DetailFragment extends BaseFragment implements OnReminderPickedListener,
OnTouchListener,
OnAttachingFileListener, TextWatcher, CheckListChangedListener, OnNoteSaved,
OnAttachingFileListener, TextWatcher, CheckListChangedListener,
OnGeoUtilResultListener {

private static final int TAKE_PHOTO = 1;
Expand Down Expand Up @@ -383,7 +381,7 @@ public void onPause() {

// Checks "goBack" value to avoid performing a double saving
if (!goBack) {
saveNote(this);
saveNote();
}

if (toggleChecklistView != null) {
Expand Down Expand Up @@ -1136,7 +1134,7 @@ private void showNoteInfo() {

private void navigateUp() {
afterSavedReturnsToList = true;
saveAndExit(this);
saveAndExit();
}

private void toggleChecklist() {
Expand Down Expand Up @@ -1480,8 +1478,7 @@ private void discard() {
}

if (noteOriginal.get_id() != null) {
new SaveNoteTask(mFragment, false)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, noteOriginal);
new SaveNoteTask(false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, noteOriginal);
BaseActivity.notifyAppWidgets(mainActivity);
} else {
goHome();
Expand All @@ -1501,7 +1498,7 @@ private void archiveNote(boolean archive) {
goBack = true;
exitMessage = archive ? getString(R.string.note_archived) : getString(R.string.note_unarchived);
exitCroutonStyle = archive ? ONStyle.WARN : ONStyle.INFO;
saveNote(this);
saveNote();
}

@SuppressLint("NewApi")
Expand All @@ -1522,7 +1519,7 @@ private void trashNote(boolean trash) {
} else {
ReminderHelper.addReminder(getAppContext(), note);
}
saveNote(this);
saveNote();
}

private void deleteNote() {
Expand All @@ -1537,21 +1534,19 @@ private void deleteNote() {
}).build().show();
}

public void saveAndExit(OnNoteSaved mOnNoteSaved) {
public void saveAndExit() {
if (isAdded()) {
exitMessage = getString(R.string.note_updated);
exitCroutonStyle = ONStyle.CONFIRM;
goBack = true;
saveNote(mOnNoteSaved);
saveNote();
}
}

/**
* Save new notes, modify them or archive
*/
void saveNote(OnNoteSaved mOnNoteSaved) {

// Changed fields
void saveNote() {
noteTmp.setTitle(getNoteTitle());
noteTmp.setContent(getNoteContent());

Expand All @@ -1575,7 +1570,7 @@ void saveNote(OnNoteSaved mOnNoteSaved) {

noteTmp.setAttachmentsListOld(note.getAttachmentsList());

new SaveNoteTask(mOnNoteSaved, lastModificationUpdatedNeeded()).executeOnExecutor(AsyncTask
new SaveNoteTask(lastModificationUpdatedNeeded()).executeOnExecutor(AsyncTask
.THREAD_POOL_EXECUTOR, noteTmp);
}

Expand All @@ -1602,21 +1597,6 @@ private boolean lastModificationUpdatedNeeded() {
return noteTmp.isChanged(note);
}

@Override
public void onNoteSaved(Note noteSaved) {
if (!activityPausing) {
EventBus.getDefault().post(new NotesUpdatedEvent(Collections.singletonList(noteSaved)));
deleteMergedNotes(mergedNotesIds);
if (noteTmp.getAlarm() != null && !noteTmp.getAlarm().equals(note.getAlarm())) {
ReminderHelper.showReminderMessage(String.valueOf(noteTmp.getAlarm()));
}
}
note = new Note(noteSaved);
if (goBack) {
goHome();
}
}

private void deleteMergedNotes(List<String> mergedNotesIds) {
ArrayList<Note> notesToDelete = new ArrayList<>();
if (mergedNotesIds != null) {
Expand Down Expand Up @@ -2190,6 +2170,19 @@ public void onEventMainThread(PushbulletReplyEvent pushbulletReplyEvent) {
binding.fragmentDetailContent.detailContent.setText(text);
}

public void onEvent(NotesUpdatedEvent event) {
if (!activityPausing) {
deleteMergedNotes(mergedNotesIds);
if (noteTmp.getAlarm() != null && !noteTmp.getAlarm().equals(note.getAlarm())) {
ReminderHelper.showReminderMessage(String.valueOf(noteTmp.getAlarm()));
}
}
note = new Note(event.getNotes().get(0));
if (goBack) {
goHome();
}
}

private static class OnGeoUtilResultListenerImpl implements OnGeoUtilResultListener {

private final WeakReference<MainActivity> mainActivityWeakReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import it.feio.android.omninotes.async.bus.NavigationUpdatedNavDrawerClosedEvent;
import it.feio.android.omninotes.async.bus.NotesLoadedEvent;
import it.feio.android.omninotes.async.bus.NotesMergeEvent;
import it.feio.android.omninotes.async.bus.NotesUpdatedEvent;
import it.feio.android.omninotes.async.bus.PasswordRemovedEvent;
import it.feio.android.omninotes.async.notes.NoteLoaderTask;
import it.feio.android.omninotes.async.notes.NoteProcessorArchive;
Expand Down Expand Up @@ -126,10 +127,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import rx.Observable;
import rx.functions.Action1;


public class ListFragment extends BaseFragment implements OnViewTouchedListener,
Expand Down Expand Up @@ -1189,6 +1193,16 @@ public void onEvent(NotesLoadedEvent notesLoadedEvent) {
closeFab();
}

public void onEvent(NotesUpdatedEvent notesUpdatedEvent) {
Observable.from(notesUpdatedEvent.getNotes()).forEach(updatedNote ->
Observable.range(0, listAdapter.getNotes().size() - 1)
.filter(i -> listAdapter.getItem(i).get_id().equals(updatedNote.get_id()))
.forEach(i -> {
listAdapter.getNotes().set(i, updatedNote);
listAdapter.notifyItemChanged(i);
}));
}

private void initSwipeGesture() {
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ private Fragment checkFragmentInstance(int id, Object instanceClass) {

@Override
public void onBackPressed() {

// SketchFragment
Fragment f = checkFragmentInstance(R.id.fragment_container, SketchFragment.class);
if (f != null) {
Expand All @@ -312,8 +311,9 @@ public void onBackPressed() {
// DetailFragment
f = checkFragmentInstance(R.id.fragment_container, DetailFragment.class);
if (f != null) {
((DetailFragment) f).goBack = true;
((DetailFragment) f).saveAndExit((DetailFragment) f);
var detailDragment = (DetailFragment) f;
detailDragment.goBack = true;
detailDragment.saveAndExit();
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

import android.content.Context;
import android.os.AsyncTask;
import de.greenrobot.event.EventBus;
import it.feio.android.omninotes.OmniNotes;
import it.feio.android.omninotes.async.bus.NotesUpdatedEvent;
import it.feio.android.omninotes.db.DbHelper;
import it.feio.android.omninotes.helpers.LogDelegate;
import it.feio.android.omninotes.models.Attachment;
import it.feio.android.omninotes.models.Note;
import it.feio.android.omninotes.models.listeners.OnNoteSaved;
import it.feio.android.omninotes.utils.ReminderHelper;
import it.feio.android.omninotes.utils.StorageHelper;
import it.feio.android.omninotes.utils.date.DateUtils;
Expand All @@ -35,22 +36,13 @@ public class SaveNoteTask extends AsyncTask<Note, Void, Note> {

private Context context;
private boolean updateLastModification = true;
private OnNoteSaved mOnNoteSaved;


public SaveNoteTask(boolean updateLastModification) {
this(null, updateLastModification);
}


public SaveNoteTask(OnNoteSaved mOnNoteSaved, boolean updateLastModification) {
super();
this.context = OmniNotes.getAppContext();
this.mOnNoteSaved = mOnNoteSaved;
this.updateLastModification = updateLastModification;
}


@Override
protected Note doInBackground(Note... params) {
Note note = params[0];
Expand All @@ -66,13 +58,12 @@ protected Note doInBackground(Note... params) {
return note;
}


private void purgeRemovedAttachments(Note note) {
List<Attachment> deletedAttachments = note.getAttachmentsListOld();
for (Attachment attachment : note.getAttachmentsList()) {
if (attachment.getId() != null) {
// Workaround to prevent deleting attachments if instance is changed (app restart)
if (deletedAttachments.indexOf(attachment) == -1) {
if (!deletedAttachments.contains(attachment)) {
attachment = getFixedAttachmentInstance(deletedAttachments, attachment);
}
deletedAttachments.remove(attachment);
Expand All @@ -85,7 +76,6 @@ private void purgeRemovedAttachments(Note note) {
}
}


private Attachment getFixedAttachmentInstance(List<Attachment> deletedAttachments,
Attachment attachment) {
for (Attachment deletedAttachment : deletedAttachments) {
Expand All @@ -96,12 +86,10 @@ private Attachment getFixedAttachmentInstance(List<Attachment> deletedAttachment
return attachment;
}


@Override
protected void onPostExecute(Note note) {
super.onPostExecute(note);
if (this.mOnNoteSaved != null) {
mOnNoteSaved.onNoteSaved(note);
}
EventBus.getDefault().post(new NotesUpdatedEvent(List.of(note)));
}

}

This file was deleted.

0 comments on commit c44a611

Please sign in to comment.