use of com.google.android.agera.database.SqlDeleteRequest 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(String.valueOf(groupData.getKey()), groupData.getKey(), groupData.getValue()));
}
return notesGroups;
}).onConcurrentUpdate(SEND_INTERRUPT).onDeactivation(SEND_INTERRUPT).compile(), insert, update, delete, databaseSupplier);
}
Aggregations