Search in sources :

Example 6 with DBNote

use of it.niedermann.owncloud.notes.model.DBNote in project nextcloud-notes by stefan-niedermann.

the class NoteSQLiteOpenHelper method getNotesCustom.

/**
 * Query the database with a custom raw query.
 * @param selection      A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself).
 * @param selectionArgs  You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.
 * @param orderBy        How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
 * @return List of Notes
 */
@NonNull
@WorkerThread
private List<DBNote> getNotesCustom(@NonNull String selection, @NonNull String[] selectionArgs, @Nullable String orderBy) {
    SQLiteDatabase db = getReadableDatabase();
    if (selectionArgs.length > 2) {
        Log.v("Note", selection + "   ----   " + selectionArgs[0] + " " + selectionArgs[1] + " " + selectionArgs[2]);
    }
    Cursor cursor = db.query(table_notes, columns, selection, selectionArgs, null, null, orderBy);
    List<DBNote> notes = new ArrayList<>();
    while (cursor.moveToNext()) {
        notes.add(getNoteFromCursor(cursor));
    }
    cursor.close();
    return notes;
}
Also used : DBNote(it.niedermann.owncloud.notes.model.DBNote) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 7 with DBNote

use of it.niedermann.owncloud.notes.model.DBNote in project nextcloud-notes by stefan-niedermann.

the class LoadNotesListTask method fillListByTime.

@NonNull
@WorkerThread
private List<Item> fillListByTime(@NonNull List<DBNote> noteList) {
    List<Item> itemList = new ArrayList<>();
    // #12 Create Sections depending on Time
    boolean todaySet, yesterdaySet, weekSet, monthSet, earlierSet;
    todaySet = yesterdaySet = weekSet = monthSet = earlierSet = false;
    Calendar today = Calendar.getInstance();
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);
    Calendar yesterday = Calendar.getInstance();
    yesterday.set(Calendar.DAY_OF_YEAR, yesterday.get(Calendar.DAY_OF_YEAR) - 1);
    yesterday.set(Calendar.HOUR_OF_DAY, 0);
    yesterday.set(Calendar.MINUTE, 0);
    yesterday.set(Calendar.SECOND, 0);
    yesterday.set(Calendar.MILLISECOND, 0);
    Calendar week = Calendar.getInstance();
    week.set(Calendar.DAY_OF_WEEK, week.getFirstDayOfWeek());
    week.set(Calendar.HOUR_OF_DAY, 0);
    week.set(Calendar.MINUTE, 0);
    week.set(Calendar.SECOND, 0);
    week.set(Calendar.MILLISECOND, 0);
    Calendar month = Calendar.getInstance();
    month.set(Calendar.DAY_OF_MONTH, 0);
    month.set(Calendar.HOUR_OF_DAY, 0);
    month.set(Calendar.MINUTE, 0);
    month.set(Calendar.SECOND, 0);
    month.set(Calendar.MILLISECOND, 0);
    for (int i = 0; i < noteList.size(); i++) {
        DBNote currentNote = noteList.get(i);
        if (currentNote.isFavorite()) {
        // don't show as new section
        } else if (!todaySet && currentNote.getModified().getTimeInMillis() >= today.getTimeInMillis()) {
            // after 00:00 today
            if (i > 0) {
                itemList.add(new SectionItem(context.getResources().getString(R.string.listview_updated_today)));
            }
            todaySet = true;
        } else if (!yesterdaySet && currentNote.getModified().getTimeInMillis() < today.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= yesterday.getTimeInMillis()) {
            // between today 00:00 and yesterday 00:00
            if (i > 0) {
                itemList.add(new SectionItem(context.getResources().getString(R.string.listview_updated_yesterday)));
            }
            yesterdaySet = true;
        } else if (!weekSet && currentNote.getModified().getTimeInMillis() < yesterday.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= week.getTimeInMillis()) {
            // between yesterday 00:00 and start of the week 00:00
            if (i > 0) {
                itemList.add(new SectionItem(context.getResources().getString(R.string.listview_updated_this_week)));
            }
            weekSet = true;
        } else if (!monthSet && currentNote.getModified().getTimeInMillis() < week.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= month.getTimeInMillis()) {
            // between start of the week 00:00 and start of the month 00:00
            if (i > 0) {
                itemList.add(new SectionItem(context.getResources().getString(R.string.listview_updated_this_month)));
            }
            monthSet = true;
        } else if (!earlierSet && currentNote.getModified().getTimeInMillis() < month.getTimeInMillis()) {
            // before start of the month 00:00
            if (i > 0) {
                itemList.add(new SectionItem(context.getResources().getString(R.string.listview_updated_earlier)));
            }
            earlierSet = true;
        }
        itemList.add(currentNote);
    }
    return itemList;
}
Also used : DBNote(it.niedermann.owncloud.notes.model.DBNote) SectionItem(it.niedermann.owncloud.notes.model.SectionItem) SectionItem(it.niedermann.owncloud.notes.model.SectionItem) Item(it.niedermann.owncloud.notes.model.Item) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 8 with DBNote

use of it.niedermann.owncloud.notes.model.DBNote in project nextcloud-notes by stefan-niedermann.

the class NoteSQLiteOpenHelper method debugPrintFullDB.

public void debugPrintFullDB() {
    List<DBNote> notes = getNotesCustom("", new String[] {}, default_order);
    Log.v(getClass().getSimpleName(), "Full Database (" + notes.size() + " notes):");
    for (DBNote note : notes) {
        Log.v(getClass().getSimpleName(), "     " + note);
    }
}
Also used : DBNote(it.niedermann.owncloud.notes.model.DBNote)

Example 9 with DBNote

use of it.niedermann.owncloud.notes.model.DBNote in project nextcloud-notes by stefan-niedermann.

the class NoteSQLiteOpenHelper 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 callback When the synchronization is finished, this callback will be invoked (optional).
 * @return changed note if differs from database, otherwise the old note.
 */
public DBNote updateNoteAndSync(@NonNull DBNote oldNote, @Nullable String newContent, @Nullable ICallback callback) {
    // debugPrintFullDB();
    DBNote newNote;
    if (newContent == null) {
        newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED);
    } else {
        newNote = new DBNote(oldNote.getId(), oldNote.getRemoteId(), Calendar.getInstance(), NoteUtil.generateNonEmptyNoteTitle(newContent, getContext()), newContent, oldNote.isFavorite(), oldNote.getCategory(), oldNote.getEtag(), DBStatus.LOCAL_EDITED);
    }
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(key_status, newNote.getStatus().getTitle());
    values.put(key_title, newNote.getTitle());
    values.put(key_category, newNote.getCategory());
    values.put(key_modified, newNote.getModified(DATE_FORMAT));
    values.put(key_content, newNote.getContent());
    int rows = db.update(table_notes, values, key_id + " = ? AND (" + key_content + " != ? OR " + key_category + " != ?)", new String[] { String.valueOf(newNote.getId()), newNote.getContent(), newNote.getCategory() });
    // if data was changed, set new status and schedule sync (with callback); otherwise invoke callback directly.
    if (rows > 0) {
        notifyNotesChanged();
        if (callback != null) {
            serverSyncHelper.addCallbackPush(callback);
        }
        serverSyncHelper.scheduleSync(true);
        return newNote;
    } else {
        if (callback != null) {
            callback.onFinish();
        }
        return oldNote;
    }
}
Also used : DBNote(it.niedermann.owncloud.notes.model.DBNote) ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 10 with DBNote

use of it.niedermann.owncloud.notes.model.DBNote in project nextcloud-notes by stefan-niedermann.

the class NotesListViewActivity method onActivityResult.

/**
 * Handles the Results of started Sub Activities (Created Note, Edited Note)
 *
 * @param requestCode int to distinguish between the different Sub Activities
 * @param resultCode  int Return Code
 * @param data        Intent
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == create_note_cmd) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // not need because of db.synchronisation in createActivity
            DBNote createdNote = (DBNote) data.getExtras().getSerializable(CREATED_NOTE);
            adapter.add(createdNote);
        // TODO scroll to top
        }
    } else if (requestCode == server_settings) {
        // Create new Instance with new URL and credentials
        db = NoteSQLiteOpenHelper.getInstance(this);
        if (db.getNoteServerSyncHelper().isSyncPossible()) {
            adapter.removeAll();
            swipeRefreshLayout.setRefreshing(true);
            synchronize();
        } else {
            Toast.makeText(getApplicationContext(), getString(R.string.error_sync, getString(NotesClientUtil.LoginStatus.NO_NETWORK.str)), Toast.LENGTH_LONG).show();
        }
    }
}
Also used : DBNote(it.niedermann.owncloud.notes.model.DBNote)

Aggregations

DBNote (it.niedermann.owncloud.notes.model.DBNote)13 Intent (android.content.Intent)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 NonNull (android.support.annotation.NonNull)3 WorkerThread (android.support.annotation.WorkerThread)3 Item (it.niedermann.owncloud.notes.model.Item)3 ArrayList (java.util.ArrayList)3 ContentValues (android.content.ContentValues)2 Bundle (android.os.Bundle)2 ItemAdapter (it.niedermann.owncloud.notes.model.ItemAdapter)2 SectionItem (it.niedermann.owncloud.notes.model.SectionItem)2 PendingIntent (android.app.PendingIntent)1 SharedPreferences (android.content.SharedPreferences)1 Cursor (android.database.Cursor)1 Canvas (android.graphics.Canvas)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 SearchView (android.support.v7.widget.SearchView)1 ItemTouchHelper (android.support.v7.widget.helper.ItemTouchHelper)1 SimpleCallback (android.support.v7.widget.helper.ItemTouchHelper.SimpleCallback)1