diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java b/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java index 87a49d323..3bc06b2cb 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java @@ -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; @@ -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; @@ -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; @@ -383,7 +381,7 @@ public void onPause() { // Checks "goBack" value to avoid performing a double saving if (!goBack) { - saveNote(this); + saveNote(); } if (toggleChecklistView != null) { @@ -1136,7 +1134,7 @@ private void showNoteInfo() { private void navigateUp() { afterSavedReturnsToList = true; - saveAndExit(this); + saveAndExit(); } private void toggleChecklist() { @@ -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(); @@ -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") @@ -1522,7 +1519,7 @@ private void trashNote(boolean trash) { } else { ReminderHelper.addReminder(getAppContext(), note); } - saveNote(this); + saveNote(); } private void deleteNote() { @@ -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()); @@ -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); } @@ -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 mergedNotesIds) { ArrayList notesToDelete = new ArrayList<>(); if (mergedNotesIds != null) { @@ -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 mainActivityWeakReference; diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/ListFragment.java b/omniNotes/src/main/java/it/feio/android/omninotes/ListFragment.java index 7f62f3d6a..9ce5b45d4 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/ListFragment.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/ListFragment.java @@ -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; @@ -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, @@ -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) { diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/MainActivity.java b/omniNotes/src/main/java/it/feio/android/omninotes/MainActivity.java index 5e940b7bd..032406de4 100755 --- a/omniNotes/src/main/java/it/feio/android/omninotes/MainActivity.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/MainActivity.java @@ -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) { @@ -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; } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/async/notes/SaveNoteTask.java b/omniNotes/src/main/java/it/feio/android/omninotes/async/notes/SaveNoteTask.java index 7bb766d47..4723dc84d 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/async/notes/SaveNoteTask.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/async/notes/SaveNoteTask.java @@ -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; @@ -35,22 +36,13 @@ public class SaveNoteTask extends AsyncTask { 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]; @@ -66,13 +58,12 @@ protected Note doInBackground(Note... params) { return note; } - private void purgeRemovedAttachments(Note note) { List 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); @@ -85,7 +76,6 @@ private void purgeRemovedAttachments(Note note) { } } - private Attachment getFixedAttachmentInstance(List deletedAttachments, Attachment attachment) { for (Attachment deletedAttachment : deletedAttachments) { @@ -96,12 +86,10 @@ private Attachment getFixedAttachmentInstance(List 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))); } + } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/models/listeners/OnNoteSaved.java b/omniNotes/src/main/java/it/feio/android/omninotes/models/listeners/OnNoteSaved.java deleted file mode 100644 index adaaac35c..000000000 --- a/omniNotes/src/main/java/it/feio/android/omninotes/models/listeners/OnNoteSaved.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (C) 2013-2023 Federico Iosue (federico@iosue.it) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package it.feio.android.omninotes.models.listeners; - -import it.feio.android.omninotes.models.Note; - - -public interface OnNoteSaved { - - void onNoteSaved(Note noteSaved); -}