Loading app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java +1 −1 Original line number Diff line number Diff line Loading @@ -248,7 +248,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego return true; } else if (itemId == R.id.menu_favorite) { note.setFavorite(!note.getFavorite()); repo.toggleFavoriteAndSync(localAccount, note.getId()); repo.toggleFavoriteAndSync(localAccount, note); listener.onNoteUpdated(note); prepareFavoriteOption(item); return true; Loading app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java +1 −1 Original line number Diff line number Diff line Loading @@ -786,7 +786,7 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A @Override public void onNoteFavoriteClick(int position, View view) { final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(((Note) adapter.getItem(position)).getId()); final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(((Note) adapter.getItem(position))); toggleLiveData.observe(this, (next) -> toggleLiveData.removeObservers(this)); } Loading app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java +5 −6 Original line number Diff line number Diff line Loading @@ -513,15 +513,14 @@ public class MainViewModel extends AndroidViewModel { }); } public LiveData<Void> toggleFavoriteAndSync(long noteId) { public LiveData<Void> toggleFavoriteAndSync(Note note) { return switchMap(getCurrentAccount(), currentAccount -> { if (currentAccount == null) { return new MutableLiveData<>(null); } else { if (currentAccount != null) { Log.v(TAG, "[toggleFavoriteAndSync] - currentAccount: " + currentAccount.getAccountName()); repo.toggleFavoriteAndSync(currentAccount, noteId); return new MutableLiveData<>(null); repo.toggleFavoriteAndSync(currentAccount, note); } return new MutableLiveData<>(null); }); } Loading app/src/main/java/it/niedermann/owncloud/notes/main/items/list/NotesListViewItemTouchHelper.java +1 −1 Original line number Diff line number Diff line Loading @@ -101,7 +101,7 @@ public class NotesListViewItemTouchHelper extends ItemTouchHelper { case ItemTouchHelper.RIGHT -> { viewHolder.setIsRecyclable(false); final var adapterNote = (Note) adapter.getItem(viewHolder.getLayoutPosition()); final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(adapterNote.getId()); final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(adapterNote); toggleLiveData.observe(lifecycleOwner, (next) -> toggleLiveData.removeObservers(lifecycleOwner)); } default -> { Loading app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java +20 −3 Original line number Diff line number Diff line Loading @@ -12,6 +12,7 @@ import static androidx.lifecycle.Transformations.distinctUntilChanged; import static androidx.lifecycle.Transformations.map; import static java.util.stream.Collectors.toMap; import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT; import static it.niedermann.owncloud.notes.shared.util.ApiVersionUtil.getPreferredApiVersion; import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt; import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets; import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.updateSingleNoteWidgets; Loading Loading @@ -47,6 +48,7 @@ import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundExce import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException; import com.nextcloud.android.sso.helper.SingleAccountHelper; import com.nextcloud.android.sso.model.SingleSignOnAccount; import com.owncloud.android.lib.common.utils.Log_OC; import java.util.ArrayList; import java.util.Calendar; Loading Loading @@ -538,11 +540,26 @@ public class NotesRepository { .collect(toMap(Note::getRemoteId, Note::getId)); } // FIXME: RACE CONDITION @AnyThread public void toggleFavoriteAndSync(Account account, long noteId) { public void toggleFavoriteAndSync(Account account, Note note) { executor.submit(() -> { db.getNoteDao().toggleFavorite(noteId); try { final var ssoAccount = AccountImporter.getSingleSignOnAccount(context, account.getAccountName()); final var notesAPI = apiProvider.getNotesAPI(context, ssoAccount, getPreferredApiVersion(account.getApiVersion())); note.setFavorite(!note.getFavorite()); final var result = notesAPI.updateNote(note); final var response = result.execute(); if (response.isSuccessful()) { final var updatedNote = response.body(); if (updatedNote != null) { db.getNoteDao().updateNote(updatedNote); scheduleSync(account, true); } } } catch (Exception e) { Log_OC.e(TAG, "toggleFavoriteAndSync: " + e); } }); } Loading Loading
app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java +1 −1 Original line number Diff line number Diff line Loading @@ -248,7 +248,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego return true; } else if (itemId == R.id.menu_favorite) { note.setFavorite(!note.getFavorite()); repo.toggleFavoriteAndSync(localAccount, note.getId()); repo.toggleFavoriteAndSync(localAccount, note); listener.onNoteUpdated(note); prepareFavoriteOption(item); return true; Loading
app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java +1 −1 Original line number Diff line number Diff line Loading @@ -786,7 +786,7 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A @Override public void onNoteFavoriteClick(int position, View view) { final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(((Note) adapter.getItem(position)).getId()); final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(((Note) adapter.getItem(position))); toggleLiveData.observe(this, (next) -> toggleLiveData.removeObservers(this)); } Loading
app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java +5 −6 Original line number Diff line number Diff line Loading @@ -513,15 +513,14 @@ public class MainViewModel extends AndroidViewModel { }); } public LiveData<Void> toggleFavoriteAndSync(long noteId) { public LiveData<Void> toggleFavoriteAndSync(Note note) { return switchMap(getCurrentAccount(), currentAccount -> { if (currentAccount == null) { return new MutableLiveData<>(null); } else { if (currentAccount != null) { Log.v(TAG, "[toggleFavoriteAndSync] - currentAccount: " + currentAccount.getAccountName()); repo.toggleFavoriteAndSync(currentAccount, noteId); return new MutableLiveData<>(null); repo.toggleFavoriteAndSync(currentAccount, note); } return new MutableLiveData<>(null); }); } Loading
app/src/main/java/it/niedermann/owncloud/notes/main/items/list/NotesListViewItemTouchHelper.java +1 −1 Original line number Diff line number Diff line Loading @@ -101,7 +101,7 @@ public class NotesListViewItemTouchHelper extends ItemTouchHelper { case ItemTouchHelper.RIGHT -> { viewHolder.setIsRecyclable(false); final var adapterNote = (Note) adapter.getItem(viewHolder.getLayoutPosition()); final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(adapterNote.getId()); final var toggleLiveData = mainViewModel.toggleFavoriteAndSync(adapterNote); toggleLiveData.observe(lifecycleOwner, (next) -> toggleLiveData.removeObservers(lifecycleOwner)); } default -> { Loading
app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java +20 −3 Original line number Diff line number Diff line Loading @@ -12,6 +12,7 @@ import static androidx.lifecycle.Transformations.distinctUntilChanged; import static androidx.lifecycle.Transformations.map; import static java.util.stream.Collectors.toMap; import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT; import static it.niedermann.owncloud.notes.shared.util.ApiVersionUtil.getPreferredApiVersion; import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt; import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets; import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.updateSingleNoteWidgets; Loading Loading @@ -47,6 +48,7 @@ import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundExce import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException; import com.nextcloud.android.sso.helper.SingleAccountHelper; import com.nextcloud.android.sso.model.SingleSignOnAccount; import com.owncloud.android.lib.common.utils.Log_OC; import java.util.ArrayList; import java.util.Calendar; Loading Loading @@ -538,11 +540,26 @@ public class NotesRepository { .collect(toMap(Note::getRemoteId, Note::getId)); } // FIXME: RACE CONDITION @AnyThread public void toggleFavoriteAndSync(Account account, long noteId) { public void toggleFavoriteAndSync(Account account, Note note) { executor.submit(() -> { db.getNoteDao().toggleFavorite(noteId); try { final var ssoAccount = AccountImporter.getSingleSignOnAccount(context, account.getAccountName()); final var notesAPI = apiProvider.getNotesAPI(context, ssoAccount, getPreferredApiVersion(account.getApiVersion())); note.setFavorite(!note.getFavorite()); final var result = notesAPI.updateNote(note); final var response = result.execute(); if (response.isSuccessful()) { final var updatedNote = response.body(); if (updatedNote != null) { db.getNoteDao().updateNote(updatedNote); scheduleSync(account, true); } } } catch (Exception e) { Log_OC.e(TAG, "toggleFavoriteAndSync: " + e); } }); } Loading