Search in sources :

Example 1 with TransactionInsertOrReplace

use of com.winsonchiu.reader.data.database.TransactionInsertOrReplace in project Reader by TheKeeperOfPie.

the class TableLink method storeLink.

public Action1<SQLiteDatabase> storeLink(final Link link) {
    return new Action1<SQLiteDatabase>() {

        @Override
        public void call(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.beginTransaction();
            if (link.getComments().getChildren().isEmpty()) {
                TransactionInsertOrReplace transaction = getInsertOrReplace(sqLiteDatabase, COLUMN_TITLE, COLUMN_AUTHOR, COLUMN_JSON);
                transaction.insertOrReplace(link.getId(), System.currentTimeMillis(), link.getCreatedUtc(), link.getTitle(), link.getAuthor(), link.getJson());
            } else {
                TransactionInsertOrReplace transaction = getInsertOrReplace(sqLiteDatabase, COLUMN_TITLE, COLUMN_AUTHOR, COLUMN_JSON, COLUMN_COMMENTS);
                transaction.insertOrReplace(link.getId(), System.currentTimeMillis(), link.getCreatedUtc(), link.getTitle(), link.getAuthor(), link.getJson(), link.getJsonComments());
            }
            sqLiteDatabase.setTransactionSuccessful();
            sqLiteDatabase.endTransaction();
        }
    };
}
Also used : Action1(rx.functions.Action1) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) TransactionInsertOrReplace(com.winsonchiu.reader.data.database.TransactionInsertOrReplace)

Example 2 with TransactionInsertOrReplace

use of com.winsonchiu.reader.data.database.TransactionInsertOrReplace in project Reader by TheKeeperOfPie.

the class TableSubredditPage method appendPage.

public Action1<SQLiteDatabase> appendPage(final Subreddit subreddit, final List<String> ids, Sort sort, Time time) {
    return new Action1<SQLiteDatabase>() {

        @Override
        public void call(SQLiteDatabase sqLiteDatabase) {
            Cursor query = sqLiteDatabase.query(NAME, COLUMNS, _ID + " = ? LIMIT 1", new String[] { subreddit.getUrl() }, null, null, null);
            if (query != null) {
                if (query.moveToFirst()) {
                    String json = query.getString(query.getColumnIndex(COLUMN_LINKS));
                    try {
                        LinkedHashSet<String> idsCurrent = new LinkedHashSet<>(Arrays.asList(ComponentStatic.getObjectMapper().readValue(json, String[].class)));
                        idsCurrent.addAll(ids);
                        ids.clear();
                        ids.addAll(idsCurrent);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                query.close();
            }
            sqLiteDatabase.beginTransaction();
            try {
                String json = ComponentStatic.getObjectMapper().writeValueAsString(ids);
                TransactionInsertOrReplace transaction = getInsertOrReplace(sqLiteDatabase, COLUMNS);
                transaction.insertOrReplace(subreddit.getUrl(), System.currentTimeMillis(), subreddit.getCreatedUtc(), subreddit.getDisplayName(), json);
                sqLiteDatabase.setTransactionSuccessful();
            } catch (IOException e) {
                e.printStackTrace();
            }
            sqLiteDatabase.endTransaction();
        }
    };
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Action1(rx.functions.Action1) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) TransactionInsertOrReplace(com.winsonchiu.reader.data.database.TransactionInsertOrReplace) IOException(java.io.IOException) Cursor(android.database.Cursor)

Example 3 with TransactionInsertOrReplace

use of com.winsonchiu.reader.data.database.TransactionInsertOrReplace in project Reader by TheKeeperOfPie.

the class TableSubredditPage method storePage.

public Action1<SQLiteDatabase> storePage(final Subreddit subreddit, final List<String> ids, Sort sort, Time time) {
    return new Action1<SQLiteDatabase>() {

        @Override
        public void call(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.beginTransaction();
            try {
                String json = ComponentStatic.getObjectMapper().writeValueAsString(ids);
                TransactionInsertOrReplace transaction = getInsertOrReplace(sqLiteDatabase, COLUMNS);
                transaction.insertOrReplace(subreddit.getUrl(), System.currentTimeMillis(), subreddit.getCreatedUtc(), subreddit.getDisplayName(), json);
                sqLiteDatabase.setTransactionSuccessful();
            } catch (IOException e) {
                e.printStackTrace();
            }
            sqLiteDatabase.endTransaction();
        }
    };
}
Also used : Action1(rx.functions.Action1) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) TransactionInsertOrReplace(com.winsonchiu.reader.data.database.TransactionInsertOrReplace) IOException(java.io.IOException)

Example 4 with TransactionInsertOrReplace

use of com.winsonchiu.reader.data.database.TransactionInsertOrReplace in project Reader by TheKeeperOfPie.

the class TableLink method storeLinks.

public Action1<SQLiteDatabase> storeLinks(final List<Link> links) {
    return new Action1<SQLiteDatabase>() {

        @Override
        public void call(SQLiteDatabase sqLiteDatabase) {
            List<Link> linksWithComments = new ArrayList<>();
            for (int index = links.size() - 1; index >= 0; index--) {
                if (!links.get(index).getComments().getChildren().isEmpty()) {
                    linksWithComments.add(links.remove(index));
                }
            }
            TransactionInsertOrReplace transaction = getInsertOrReplace(sqLiteDatabase, COLUMN_TITLE, COLUMN_AUTHOR, COLUMN_JSON);
            sqLiteDatabase.beginTransaction();
            for (Link link : links) {
                transaction.insertOrReplace(link.getId(), System.currentTimeMillis(), link.getCreatedUtc(), link.getTitle(), link.getAuthor(), link.getJson());
            }
            sqLiteDatabase.setTransactionSuccessful();
            sqLiteDatabase.endTransaction();
            TransactionInsertOrReplace transactionWithComments = getInsertOrReplace(sqLiteDatabase, COLUMN_TITLE, COLUMN_AUTHOR, COLUMN_JSON, COLUMN_COMMENTS);
            sqLiteDatabase.beginTransaction();
            for (Link link : linksWithComments) {
                transactionWithComments.insertOrReplace(link.getId(), System.currentTimeMillis(), link.getCreatedUtc(), link.getTitle(), link.getAuthor(), link.getJson(), link.getJsonComments());
            }
            sqLiteDatabase.setTransactionSuccessful();
            sqLiteDatabase.endTransaction();
        }
    };
}
Also used : Action1(rx.functions.Action1) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) TransactionInsertOrReplace(com.winsonchiu.reader.data.database.TransactionInsertOrReplace) ArrayList(java.util.ArrayList) Link(com.winsonchiu.reader.data.reddit.Link)

Aggregations

SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 TransactionInsertOrReplace (com.winsonchiu.reader.data.database.TransactionInsertOrReplace)4 Action1 (rx.functions.Action1)4 IOException (java.io.IOException)2 Cursor (android.database.Cursor)1 Link (com.winsonchiu.reader.data.reddit.Link)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1