From 50e365c45869ae57fe8523bce0873560d48386f8 Mon Sep 17 00:00:00 2001 From: "Jan C. Borchardt" <925062+jancborchardt@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:58:34 +0100 Subject: [PATCH 1/2] fix(main): restore deleted note only once on undo Assisted-by: ClaudeCode:claude-opus-5-5 AI-assistant: Claude Code 2.1.325 (Claude Opus 5.5) Signed-off-by: Jan C. Borchardt <925062+jancborchardt@users.noreply.github.com> --- .../owncloud/notes/main/MainViewModel.java | 78 ++++++++----------- .../notes/main/MainViewModelTest.java | 30 +++++++ 2 files changed, 62 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java b/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java index 771cb42a4..50562a4ae 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java @@ -194,15 +194,12 @@ public LiveData> getCategorySort } public LiveData modifyCategoryOrder(@NonNull NavigationCategory selectedCategory, @NonNull CategorySortingMethod sortingMethod) { - return switchMap(getCurrentAccount(), currentAccount -> { - if (currentAccount == null) { - return new MutableLiveData<>(null); - } else { - Log.v(TAG, "[modifyCategoryOrder] - currentAccount: " + currentAccount.getAccountName()); - repo.modifyCategoryOrder(currentAccount.getId(), selectedCategory, sortingMethod); - return new MutableLiveData<>(null); - } - }); + final var currentAccount = getCurrentAccount().getValue(); + if (currentAccount != null) { + Log.v(TAG, "[modifyCategoryOrder] - currentAccount: " + currentAccount.getAccountName()); + repo.modifyCategoryOrder(currentAccount.getId(), selectedCategory, sortingMethod); + } + return new MutableLiveData<>(null); } public void postExpandedCategory(@Nullable String expandedCategory) { @@ -497,17 +494,14 @@ public List getAccounts() { } public LiveData setCategory(Iterable noteIds, @NonNull String category) { - return switchMap(getCurrentAccount(), currentAccount -> { - if (currentAccount == null) { - return new MutableLiveData<>(null); - } else { - Log.v(TAG, "[setCategory] - currentAccount: " + currentAccount.getAccountName()); - for (Long noteId : noteIds) { - repo.setCategory(currentAccount, noteId, category); - } - return new MutableLiveData<>(null); + final var currentAccount = getCurrentAccount().getValue(); + if (currentAccount != null) { + Log.v(TAG, "[setCategory] - currentAccount: " + currentAccount.getAccountName()); + for (Long noteId : noteIds) { + repo.setCategory(currentAccount, noteId, category); } - }); + } + return new MutableLiveData<>(null); } public LiveData moveNoteToAnotherAccount(Account account, long noteId) { @@ -529,29 +523,23 @@ public LiveData toggleFavoriteAndSync(Note note) { } public LiveData deleteNoteAndSync(long id) { - return switchMap(getCurrentAccount(), currentAccount -> { - if (currentAccount == null) { - return new MutableLiveData<>(null); - } else { - Log.v(TAG, "[deleteNoteAndSync] - currentAccount: " + currentAccount.getAccountName()); - repo.deleteNoteAndSync(currentAccount, id); - return new MutableLiveData<>(null); - } - }); + final var currentAccount = getCurrentAccount().getValue(); + if (currentAccount != null) { + Log.v(TAG, "[deleteNoteAndSync] - currentAccount: " + currentAccount.getAccountName()); + repo.deleteNoteAndSync(currentAccount, id); + } + return new MutableLiveData<>(null); } public LiveData deleteNotesAndSync(@NonNull Collection ids) { - return switchMap(getCurrentAccount(), currentAccount -> { - if (currentAccount == null) { - return new MutableLiveData<>(null); - } else { - Log.v(TAG, "[deleteNotesAndSync] - currentAccount: " + currentAccount.getAccountName()); - for (final var id : ids) { - repo.deleteNoteAndSync(currentAccount, id); - } - return new MutableLiveData<>(null); + final var currentAccount = getCurrentAccount().getValue(); + if (currentAccount != null) { + Log.v(TAG, "[deleteNotesAndSync] - currentAccount: " + currentAccount.getAccountName()); + for (final var id : ids) { + repo.deleteNoteAndSync(currentAccount, id); } - }); + } + return new MutableLiveData<>(null); } public LiveData addAccount(@NonNull String url, @NonNull String username, @NonNull String accountName, @NonNull Capabilities capabilities, @Nullable String displayName, @NonNull IResponseCallback callback) { @@ -586,14 +574,12 @@ public LiveData> getFullNotesWithCategory(@NonNull Collection i } public LiveData addNoteAndSync(Note note) { - return switchMap(getCurrentAccount(), currentAccount -> { - if (currentAccount == null) { - return new MutableLiveData<>(); - } else { - Log.v(TAG, "[addNoteAndSync] - currentAccount: " + currentAccount.getAccountName()); - return repo.addNoteAndSync(currentAccount, note); - } - }); + final var currentAccount = getCurrentAccount().getValue(); + if (currentAccount == null) { + return new MutableLiveData<>(); + } + Log.v(TAG, "[addNoteAndSync] - currentAccount: " + currentAccount.getAccountName()); + return repo.addNoteAndSync(currentAccount, note); } public LiveData updateNoteAndSync(@NonNull Note oldNote, @Nullable String newContent, @Nullable String newTitle) { diff --git a/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java b/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java index f8f3cdea9..73a32afde 100644 --- a/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java +++ b/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java @@ -10,10 +10,16 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import android.content.Context; import androidx.arch.core.executor.testing.InstantTaskExecutorRule; +import androidx.lifecycle.MutableLiveData; import androidx.test.core.app.ApplicationProvider; import com.nextcloud.android.sso.exceptions.UnknownErrorException; @@ -31,7 +37,10 @@ import java.util.List; import it.niedermann.owncloud.notes.main.navigation.NavigationItem; +import it.niedermann.owncloud.notes.persistence.NotesRepository; +import it.niedermann.owncloud.notes.persistence.entity.Account; import it.niedermann.owncloud.notes.persistence.entity.CategoryWithNotesCount; +import it.niedermann.owncloud.notes.persistence.entity.Note; import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType; @RunWith(RobolectricTestRunner.class) @@ -163,6 +172,27 @@ public void containsNonInfrastructureRelatedItems() { assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException("Foo", new Exception("Software caused connection abort"))))); } + @Test + public void addNoteAndSync_adds_note_only_once() throws NoSuchFieldException, IllegalAccessException { + final var vm = new MainViewModel(ApplicationProvider.getApplicationContext(), null); + final var repo = mock(NotesRepository.class); + final var repoField = MainViewModel.class.getDeclaredField("repo"); + repoField.setAccessible(true); + repoField.set(vm, repo); + final var currentAccountField = MainViewModel.class.getDeclaredField("currentAccount"); + currentAccountField.setAccessible(true); + final var account = new Account(); + //noinspection unchecked + ((MutableLiveData) currentAccountField.get(vm)).setValue(account); + final var note = new Note(); + when(repo.addNoteAndSync(any(), any())).thenReturn(new MutableLiveData<>(note)); + + vm.addNoteAndSync(note).observeForever(next -> { + }); + + verify(repo, times(1)).addNoteAndSync(account, note); + } + private static List getSaneCategoriesWithNotesCount() { return List.of( new CategoryWithNotesCount(1, "Foo", 13), From e1622dd00eabbd7cc0ec7d528d74bc05c86856c7 Mon Sep 17 00:00:00 2001 From: "Jan C. Borchardt" <925062+jancborchardt@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:24:52 +0100 Subject: [PATCH 2/2] test(main): inject repository instead of using reflection in MainViewModel test Assisted-by: ClaudeCode:claude-opus-5-5 AI-assistant: Claude Code 2.1.325 (Claude Opus 5.5) Signed-off-by: Jan C. Borchardt <925062+jancborchardt@users.noreply.github.com> --- .../owncloud/notes/main/MainViewModel.java | 8 +++++++- .../owncloud/notes/main/MainViewModelTest.java | 13 ++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java b/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java index 50562a4ae..10d317926 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java @@ -33,6 +33,7 @@ import androidx.annotation.MainThread; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; import androidx.annotation.WorkerThread; import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; @@ -101,8 +102,13 @@ public class MainViewModel extends AndroidViewModel { private final MutableLiveData expandedCategory = new MutableLiveData<>(null); public MainViewModel(@NonNull Application application, @NonNull SavedStateHandle savedStateHandle) { + this(application, savedStateHandle, NotesRepository.getInstance(application)); + } + + @VisibleForTesting + MainViewModel(@NonNull Application application, @NonNull SavedStateHandle savedStateHandle, @NonNull NotesRepository repo) { super(application); - this.repo = NotesRepository.getInstance(application); + this.repo = repo; this.state = savedStateHandle; } diff --git a/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java b/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java index 73a32afde..5afd9b930 100644 --- a/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java +++ b/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java @@ -20,6 +20,7 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule; import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.SavedStateHandle; import androidx.test.core.app.ApplicationProvider; import com.nextcloud.android.sso.exceptions.UnknownErrorException; @@ -173,17 +174,11 @@ public void containsNonInfrastructureRelatedItems() { } @Test - public void addNoteAndSync_adds_note_only_once() throws NoSuchFieldException, IllegalAccessException { - final var vm = new MainViewModel(ApplicationProvider.getApplicationContext(), null); + public void addNoteAndSyncAddsNoteOnlyOnce() { final var repo = mock(NotesRepository.class); - final var repoField = MainViewModel.class.getDeclaredField("repo"); - repoField.setAccessible(true); - repoField.set(vm, repo); - final var currentAccountField = MainViewModel.class.getDeclaredField("currentAccount"); - currentAccountField.setAccessible(true); + final var vm = new MainViewModel(ApplicationProvider.getApplicationContext(), new SavedStateHandle(), repo); final var account = new Account(); - //noinspection unchecked - ((MutableLiveData) currentAccountField.get(vm)).setValue(account); + vm.postCurrentAccount(account); final var note = new Note(); when(repo.addNoteAndSync(any(), any())).thenReturn(new MutableLiveData<>(note));