use of com.google.android.agera.rvadapter.RepositoryAdapter 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, List<Note>> rowHandler = rowBinder(pool, (r) -> new LinearLayoutManager(getContext(), HORIZONTAL, false), NoteGroup::getId, NoteGroup::getNotes, (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).forList());
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.rvadapter.RepositoryAdapter in project agera by google.
the class RowHandler method bind.
@Override
public void bind(@NonNull final TRow row, @NonNull final View view) {
final RecyclerView recyclerView = (RecyclerView) view;
final long id = stableId.apply(row);
final TRowItems rowItems = data.apply(row);
if (!(recyclerView.getAdapter() instanceof RepositoryAdapter)) {
final MutableRepository<TRowItems> newRepository = mutableRepository(rowItems);
final RepositoryPresenter<TRowItems> newPresenter = presenter.apply(row);
final RepositoryAdapter newAdapter = repositoryAdapter().add(newRepository, newPresenter).build();
recyclerView.setRecycledViewPool(pool);
recyclerView.setLayoutManager(layoutManager.apply(row));
adapterRepositories.put(newAdapter, newRepository);
startedAdapters.add(newAdapter);
newAdapter.setHasStableIds(true);
recyclerView.setAdapter(newAdapter);
recyclerView.addOnScrollListener(this);
previousStableIds.put(newAdapter, id);
newAdapter.startObserving();
} else {
final RepositoryAdapter adapter = (RepositoryAdapter) recyclerView.getAdapter();
adapterRepositories.get(adapter).accept(rowItems);
previousStableIds.put(adapter, id);
if (!startedAdapters.contains(adapter)) {
adapter.startObserving();
startedAdapters.add(adapter);
}
}
recyclerView.getLayoutManager().onRestoreInstanceState(itemRowStates.get(id));
}
use of com.google.android.agera.rvadapter.RepositoryAdapter in project agera by google.
the class RowHandler method accept.
@Override
public void accept(@NonNull final View view) {
final RecyclerView recyclerView = (RecyclerView) view;
final RepositoryAdapter adapter = (RepositoryAdapter) recyclerView.getAdapter();
itemRowStates.put(previousStableIds.get(adapter), recyclerView.getLayoutManager().onSaveInstanceState());
if (startedAdapters.remove(adapter)) {
adapter.stopObserving();
}
}
Aggregations