Search in sources :

Example 16 with Note

use of it.niedermann.owncloud.notes.persistence.entity.Note in project nextcloud-notes by stefan-niedermann.

the class NotesRepository method updateNoteAndSync.

/**
 * Updates a single Note with a new content.
 * The title is derived from the new content automatically, and modified date as well as DBStatus are updated, too -- if the content differs to the state in the database.
 *
 * @param oldNote    Note to be changed
 * @param newContent New content. If this is <code>null</code>, then <code>oldNote</code> is saved again (useful for undoing changes).
 * @param newTitle   New title. If this is <code>null</code>, then either the old title is reused (in case the note has been synced before) or a title is generated (in case it is a new note)
 * @param callback   When the synchronization is finished, this callback will be invoked (optional).
 * @return changed {@link Note} if differs from database, otherwise the old {@link Note}.
 */
@WorkerThread
public Note updateNoteAndSync(@NonNull Account localAccount, @NonNull Note oldNote, @Nullable String newContent, @Nullable String newTitle, @Nullable ISyncCallback callback) {
    final Note newNote;
    // Re-read the up to date remoteId from the database because the UI might not have the state after synchronization yet
    // https://github.com/stefan-niedermann/nextcloud-notes/issues/1198
    @Nullable final Long remoteId = db.getNoteDao().getRemoteId(oldNote.getId());
    if (newContent == null) {
        newNote = new Note(oldNote.getId(), remoteId, oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), oldNote.getExcerpt(), oldNote.getScrollY());
    } else {
        final String title;
        if (newTitle != null) {
            title = newTitle;
        } else {
            final ApiVersion preferredApiVersion = ApiVersionUtil.getPreferredApiVersion(localAccount.getApiVersion());
            if ((remoteId == null || preferredApiVersion == null || preferredApiVersion.compareTo(ApiVersion.API_VERSION_1_0) < 0) && (defaultNonEmptyTitle.equals(oldNote.getTitle()))) {
                title = NoteUtil.generateNonEmptyNoteTitle(newContent, context);
            } else {
                title = oldNote.getTitle();
            }
        }
        newNote = new Note(oldNote.getId(), remoteId, Calendar.getInstance(), title, newContent, oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag(), DBStatus.LOCAL_EDITED, localAccount.getId(), generateNoteExcerpt(newContent, title), oldNote.getScrollY());
    }
    int rows = db.getNoteDao().updateNote(newNote);
    // if data was changed, set new status and schedule sync (with callback); otherwise invoke callback directly.
    if (rows > 0) {
        notifyWidgets();
        if (callback != null) {
            addCallbackPush(localAccount, callback);
        }
        scheduleSync(localAccount, true);
        return newNote;
    } else {
        if (callback != null) {
            callback.onFinish();
        }
        return oldNote;
    }
}
Also used : ApiVersion(it.niedermann.owncloud.notes.shared.model.ApiVersion) Note(it.niedermann.owncloud.notes.persistence.entity.Note) Nullable(androidx.annotation.Nullable) WorkerThread(androidx.annotation.WorkerThread)

Example 17 with Note

use of it.niedermann.owncloud.notes.persistence.entity.Note in project nextcloud-notes by stefan-niedermann.

the class SingleNoteWidgetConfigurationActivity method onNoteClick.

@Override
public void onNoteClick(int position, View v) {
    final var note = (Note) adapter.getItem(position);
    final var args = getIntent().getExtras();
    if (args == null) {
        finish();
        return;
    }
    final int appWidgetId = args.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    executor.submit(() -> {
        try {
            mainViewModel.createOrUpdateSingleNoteWidgetData(new SingleNoteWidgetData(appWidgetId, note.getAccountId(), note.getId(), NotesApplication.getAppTheme(this).getModeId()));
            final var updateIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, getApplicationContext(), SingleNoteWidget.class).putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            setResult(RESULT_OK, updateIntent);
            getApplicationContext().sendBroadcast(updateIntent);
            finish();
        } catch (SQLException e) {
            Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    });
}
Also used : SQLException(android.database.SQLException) Note(it.niedermann.owncloud.notes.persistence.entity.Note) Intent(android.content.Intent) SingleNoteWidgetData(it.niedermann.owncloud.notes.persistence.entity.SingleNoteWidgetData)

Example 18 with Note

use of it.niedermann.owncloud.notes.persistence.entity.Note in project nextcloud-notes by stefan-niedermann.

the class SlotterUtilTest method fillListByInitials_shouldAddSectionItems.

@Test
public void fillListByInitials_shouldAddSectionItems() {
    final var notes = List.of(new Note(1L, Calendar.getInstance(), "Aaa", "", "", false, ""), new Note(1L, Calendar.getInstance(), "Abc", "", "", false, ""), new Note(1L, Calendar.getInstance(), "Bbb", "", "", false, ""), new Note(1L, Calendar.getInstance(), "Bcd", "", "", false, ""), new Note(1L, Calendar.getInstance(), "Def", "", "", false, ""));
    final var items = SlotterUtil.fillListByInitials(ApplicationProvider.getApplicationContext(), notes);
    assertEquals(2, items.stream().filter(item -> item instanceof SectionItem).count());
    assertEquals(SectionItem.class, items.get(2).getClass());
    assertEquals(SectionItem.class, items.get(5).getClass());
}
Also used : SectionItem(it.niedermann.owncloud.notes.main.items.section.SectionItem) Note(it.niedermann.owncloud.notes.persistence.entity.Note) Test(org.junit.Test)

Example 19 with Note

use of it.niedermann.owncloud.notes.persistence.entity.Note in project nextcloud-notes by stefan-niedermann.

the class SlotterUtilTest method fillListByInitials_shouldAcceptEmptyTitles.

@Test
public void fillListByInitials_shouldAcceptEmptyTitles() {
    final var notes = List.of(new Note(1L, Calendar.getInstance(), "", "", "", false, ""), new Note(2L, Calendar.getInstance(), "Foo", "", "", false, ""), new Note(3L, Calendar.getInstance(), "Bar", "", "", false, ""));
    final var items = SlotterUtil.fillListByInitials(ApplicationProvider.getApplicationContext(), notes);
}
Also used : Note(it.niedermann.owncloud.notes.persistence.entity.Note) Test(org.junit.Test)

Example 20 with Note

use of it.niedermann.owncloud.notes.persistence.entity.Note in project nextcloud-notes by stefan-niedermann.

the class NotesDaoTest method deleteNoteById.

@Test
public void deleteNoteById() throws InterruptedException {
    db.getNoteDao().addNote(new Note(1, 1L, Calendar.getInstance(), "T", "C", "", false, "1", LOCAL_DELETED, account.getId(), "", 0));
    db.getNoteDao().deleteByNoteId(1, LOCAL_DELETED);
    assertNull(db.getNoteDao().getNoteById(1));
    assertNull(NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getNoteById$(1)));
    db.getNoteDao().addNote(new Note(1, 1L, Calendar.getInstance(), "T", "C", "", false, "1", LOCAL_DELETED, account.getId(), "", 0));
    db.getNoteDao().deleteByNoteId(1, VOID);
    assertEquals(1, db.getNoteDao().getNoteById(1).getId());
    assertEquals(1, NotesTestingUtil.getOrAwaitValue(db.getNoteDao().getNoteById$(1)).getId());
}
Also used : Note(it.niedermann.owncloud.notes.persistence.entity.Note) Test(org.junit.Test)

Aggregations

Note (it.niedermann.owncloud.notes.persistence.entity.Note)21 Test (org.junit.Test)13 Account (it.niedermann.owncloud.notes.persistence.entity.Account)5 Intent (android.content.Intent)3 NonNull (androidx.annotation.NonNull)3 NextcloudFilesAppAccountNotFoundException (com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)3 Capabilities (it.niedermann.owncloud.notes.shared.model.Capabilities)3 NavigationCategory (it.niedermann.owncloud.notes.shared.model.NavigationCategory)3 Before (org.junit.Before)3 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)2 Bundle (android.os.Bundle)2 InstantTaskExecutorRule (androidx.arch.core.executor.testing.InstantTaskExecutorRule)2 Room (androidx.room.Room)2 ApplicationProvider (androidx.test.core.app.ApplicationProvider)2 NextcloudHttpRequestFailedException (com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException)2 TokenMismatchException (com.nextcloud.android.sso.exceptions.TokenMismatchException)2 CategoryWithNotesCount (it.niedermann.owncloud.notes.persistence.entity.CategoryWithNotesCount)2 DBStatus (it.niedermann.owncloud.notes.shared.model.DBStatus)2 LOCAL_DELETED (it.niedermann.owncloud.notes.shared.model.DBStatus.LOCAL_DELETED)2 LOCAL_EDITED (it.niedermann.owncloud.notes.shared.model.DBStatus.LOCAL_EDITED)2