Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,8 +102,13 @@ public class MainViewModel extends AndroidViewModel {
private final MutableLiveData<String> 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;
}

Expand Down Expand Up @@ -194,15 +200,12 @@ public LiveData<Pair<NavigationCategory, CategorySortingMethod>> getCategorySort
}

public LiveData<Void> 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) {
Expand Down Expand Up @@ -497,17 +500,14 @@ public List<Account> getAccounts() {
}

public LiveData<Void> setCategory(Iterable<Long> 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<Note> moveNoteToAnotherAccount(Account account, long noteId) {
Expand All @@ -529,29 +529,23 @@ public LiveData<Void> toggleFavoriteAndSync(Note note) {
}

public LiveData<Void> 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<Void> deleteNotesAndSync(@NonNull Collection<Long> 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<ImportStatus> addAccount(@NonNull String url, @NonNull String username, @NonNull String accountName, @NonNull Capabilities capabilities, @Nullable String displayName, @NonNull IResponseCallback<Account> callback) {
Expand Down Expand Up @@ -586,14 +580,12 @@ public LiveData<List<Note>> getFullNotesWithCategory(@NonNull Collection<Long> i
}

public LiveData<Note> 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<Void> updateNoteAndSync(@NonNull Note oldNote, @Nullable String newContent, @Nullable String newTitle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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<CategoryWithNotesCount> getSaneCategoriesWithNotesCount() {
return List.of(
new CategoryWithNotesCount(1, "Foo", 13),
Expand Down
Loading