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..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; } @@ -194,15 +200,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 +500,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 +529,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 +580,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..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 @@ -10,10 +10,17 @@ 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.lifecycle.SavedStateHandle; import androidx.test.core.app.ApplicationProvider; import com.nextcloud.android.sso.exceptions.UnknownErrorException; @@ -31,7 +38,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 +173,21 @@ public void containsNonInfrastructureRelatedItems() { assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException("Foo", new Exception("Software caused connection abort"))))); } + @Test + public void addNoteAndSyncAddsNoteOnlyOnce() { + final var repo = mock(NotesRepository.class); + final var vm = new MainViewModel(ApplicationProvider.getApplicationContext(), new SavedStateHandle(), repo); + final var account = new Account(); + vm.postCurrentAccount(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),