use of com.google.android.agera.Result in project agera by google.
the class NotesFragment method onCreate.
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
notesStore = notesStore(getContext().getApplicationContext());
pool = new RecycledViewPool();
final RowHandler<NoteGroup> rowHandler = rowBinder(pool, (r) -> new LinearLayoutManager(getContext(), HORIZONTAL, false), NoteGroup::getId, (r) -> dataBindingRepositoryPresenterOf(Note.class).layout(R.layout.text_layout).itemId(BR.note).handler(BR.click, (Receiver<Note>) (note) -> {
final EditText editText = new EditText(getContext());
editText.setId(R.id.edit);
editText.setText(note.getNote());
new AlertDialog.Builder(getContext()).setTitle(R.string.edit_note).setView(editText).setPositiveButton(R.string.edit, (d, i) -> notesStore.updateNote(note, editText.getText().toString())).create().show();
}).handler(BR.longClick, (Receiver<Note>) notesStore::deleteNote).stableIdForItem(Note::getId).collectionId(BR.noteGroup).forCollection(NoteGroup::getNotes));
adapter = repositoryAdapter().addLayout(layout(R.layout.header)).add(notesStore.getNotesRepository(), repositoryPresenterOf(NoteGroup.class).layout(R.layout.note_group_layout).stableIdForItem(NoteGroup::getId).bindWith(rowHandler).recycleWith(rowHandler).forList()).addItem(getInstance().format(new Date()), dataBindingRepositoryPresenterOf(String.class).layout(R.layout.footer).itemId(BR.string).forItem()).build();
adapter.setHasStableIds(true);
final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
backgroundRepository = repositoryWithInitialValue(Result.<Bitmap>absent()).observe().onUpdatesPerLoop().goTo(networkExecutor).getFrom(() -> "http://www.gravatar.com/avatar/4df6f4fe5976df17deeea19443d4429d?s=" + Math.max(displayMetrics.heightPixels, displayMetrics.widthPixels)).transform(url -> httpGetRequest(url).compile()).attemptTransform(httpFunction()).orEnd(Result::failure).goTo(calculationExecutor).thenTransform(input -> {
final byte[] body = input.getBody();
return absentIfNull(decodeByteArray(body, 0, body.length));
}).onDeactivation(SEND_INTERRUPT).compile();
}
use of com.google.android.agera.Result 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