Search in sources :

Example 1 with SqlUpdateRequest

use of com.google.android.agera.database.SqlUpdateRequest in project agera by google.

the class NotesStore method notesStore.

@NonNull
static synchronized NotesStore notesStore(@NonNull final Context applicationContext) {
    // Create a database supplier that initializes the database. This is also used to supply the
    // database in all database operations.
    final NotesSqlDatabaseSupplier databaseSupplier = databaseSupplier(applicationContext);
    // Create a function that processes database write operations.
    final Function<SqlInsertRequest, Result<Long>> insertNoteFunction = databaseInsertFunction(databaseSupplier);
    final Function<SqlUpdateRequest, Result<Integer>> updateNoteFunction = databaseUpdateFunction(databaseSupplier);
    final Function<SqlDeleteRequest, Result<Integer>> deleteNoteFunction = databaseDeleteFunction(databaseSupplier);
    final UpdateDispatcher updateDispatcher = updateDispatcher();
    final Receiver<SqlDeleteRequest> delete = value -> STORE_EXECUTOR.execute(() -> {
        deleteNoteFunction.apply(value);
        updateDispatcher.update();
    });
    final Receiver<SqlUpdateRequest> update = value -> STORE_EXECUTOR.execute(() -> {
        updateNoteFunction.apply(value);
        updateDispatcher.update();
    });
    final Receiver<SqlInsertRequest> insert = value -> STORE_EXECUTOR.execute(() -> {
        insertNoteFunction.apply(value);
        updateDispatcher.update();
    });
    // Create the wired up notes store
    return new NotesStore(repositoryWithInitialValue(INITIAL_VALUE).observe(updateDispatcher).onUpdatesPerLoop().goTo(STORE_EXECUTOR).getFrom(() -> sqlRequest().sql(GET_NOTES_FROM_TABLE).compile()).attemptTransform(databaseQueryFunction(databaseSupplier, cursor -> note(cursor.getInt(ID_COLUMN_INDEX), cursor.getString(NOTE_COLUMN_INDEX)))).orEnd(staticFunction(INITIAL_VALUE)).thenTransform(notes -> {
        final Map<Character, List<Note>> notesGroupsData = new TreeMap<>();
        for (final Note note : notes) {
            final String noteText = note.getNote();
            final char groupId = isEmpty(noteText) ? 0 : noteText.charAt(0);
            List<Note> notesGroupData = notesGroupsData.get(groupId);
            if (notesGroupData == null) {
                notesGroupData = new ArrayList<>();
                notesGroupsData.put(groupId, notesGroupData);
            }
            notesGroupData.add(note);
        }
        final List<NoteGroup> notesGroups = new ArrayList<>();
        for (final Map.Entry<Character, List<Note>> groupData : notesGroupsData.entrySet()) {
            notesGroups.add(noteGroup(groupData.getKey(), groupData.getValue()));
        }
        return notesGroups;
    }).onConcurrentUpdate(SEND_INTERRUPT).onDeactivation(SEND_INTERRUPT).compile(), insert, update, delete, databaseSupplier);
}
Also used : Context(android.content.Context) TextUtils.isEmpty(android.text.TextUtils.isEmpty) SqlDeleteRequest(com.google.android.agera.database.SqlDeleteRequest) NOTES_NOTE_COLUMN(com.google.android.agera.testapp.NotesSqlDatabaseSupplier.NOTES_NOTE_COLUMN) NOTES_NOTE_ID_COLUMN(com.google.android.agera.testapp.NotesSqlDatabaseSupplier.NOTES_NOTE_ID_COLUMN) NOTES_TABLE(com.google.android.agera.testapp.NotesSqlDatabaseSupplier.NOTES_TABLE) Result(com.google.android.agera.Result) SqlDatabaseFunctions.databaseDeleteFunction(com.google.android.agera.database.SqlDatabaseFunctions.databaseDeleteFunction) NotesSqlDatabaseSupplier.databaseSupplier(com.google.android.agera.testapp.NotesSqlDatabaseSupplier.databaseSupplier) NonNull(android.support.annotation.NonNull) Repository(com.google.android.agera.Repository) SqlInsertRequest(com.google.android.agera.database.SqlInsertRequest) SqlRequests.sqlInsertRequest(com.google.android.agera.database.SqlRequests.sqlInsertRequest) ArrayList(java.util.ArrayList) SqlDatabaseSupplier(com.google.android.agera.database.SqlDatabaseSupplier) SqlDatabaseFunctions.databaseQueryFunction(com.google.android.agera.database.SqlDatabaseFunctions.databaseQueryFunction) Map(java.util.Map) NoteGroup.noteGroup(com.google.android.agera.testapp.NoteGroup.noteGroup) Note.note(com.google.android.agera.testapp.Note.note) Receiver(com.google.android.agera.Receiver) SqlDatabaseFunctions.databaseInsertFunction(com.google.android.agera.database.SqlDatabaseFunctions.databaseInsertFunction) Executor(java.util.concurrent.Executor) Collections.emptyList(java.util.Collections.emptyList) Executors.newSingleThreadExecutor(java.util.concurrent.Executors.newSingleThreadExecutor) Repositories.repositoryWithInitialValue(com.google.android.agera.Repositories.repositoryWithInitialValue) SqlDatabaseFunctions.databaseUpdateFunction(com.google.android.agera.database.SqlDatabaseFunctions.databaseUpdateFunction) SqlRequests.sqlRequest(com.google.android.agera.database.SqlRequests.sqlRequest) SqlUpdateRequest(com.google.android.agera.database.SqlUpdateRequest) Functions.staticFunction(com.google.android.agera.Functions.staticFunction) Observables.updateDispatcher(com.google.android.agera.Observables.updateDispatcher) SqlRequests.sqlUpdateRequest(com.google.android.agera.database.SqlRequests.sqlUpdateRequest) List(java.util.List) String.valueOf(java.lang.String.valueOf) Function(com.google.android.agera.Function) TreeMap(java.util.TreeMap) SqlRequests.sqlDeleteRequest(com.google.android.agera.database.SqlRequests.sqlDeleteRequest) SEND_INTERRUPT(com.google.android.agera.RepositoryConfig.SEND_INTERRUPT) UpdateDispatcher(com.google.android.agera.UpdateDispatcher) SqlUpdateRequest(com.google.android.agera.database.SqlUpdateRequest) ArrayList(java.util.ArrayList) UpdateDispatcher(com.google.android.agera.UpdateDispatcher) Result(com.google.android.agera.Result) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) TreeMap(java.util.TreeMap) SqlInsertRequest(com.google.android.agera.database.SqlInsertRequest) SqlDeleteRequest(com.google.android.agera.database.SqlDeleteRequest) Map(java.util.Map) TreeMap(java.util.TreeMap) NonNull(android.support.annotation.NonNull)

Aggregations

Context (android.content.Context)1 NonNull (android.support.annotation.NonNull)1 TextUtils.isEmpty (android.text.TextUtils.isEmpty)1 Function (com.google.android.agera.Function)1 Functions.staticFunction (com.google.android.agera.Functions.staticFunction)1 Observables.updateDispatcher (com.google.android.agera.Observables.updateDispatcher)1 Receiver (com.google.android.agera.Receiver)1 Repositories.repositoryWithInitialValue (com.google.android.agera.Repositories.repositoryWithInitialValue)1 Repository (com.google.android.agera.Repository)1 SEND_INTERRUPT (com.google.android.agera.RepositoryConfig.SEND_INTERRUPT)1 Result (com.google.android.agera.Result)1 UpdateDispatcher (com.google.android.agera.UpdateDispatcher)1 SqlDatabaseFunctions.databaseDeleteFunction (com.google.android.agera.database.SqlDatabaseFunctions.databaseDeleteFunction)1 SqlDatabaseFunctions.databaseInsertFunction (com.google.android.agera.database.SqlDatabaseFunctions.databaseInsertFunction)1 SqlDatabaseFunctions.databaseQueryFunction (com.google.android.agera.database.SqlDatabaseFunctions.databaseQueryFunction)1 SqlDatabaseFunctions.databaseUpdateFunction (com.google.android.agera.database.SqlDatabaseFunctions.databaseUpdateFunction)1 SqlDatabaseSupplier (com.google.android.agera.database.SqlDatabaseSupplier)1 SqlDeleteRequest (com.google.android.agera.database.SqlDeleteRequest)1 SqlInsertRequest (com.google.android.agera.database.SqlInsertRequest)1 SqlRequests.sqlDeleteRequest (com.google.android.agera.database.SqlRequests.sqlDeleteRequest)1