Search in sources :

Example 1 with Receiver

use of com.google.android.agera.Receiver 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();
}
Also used : Bundle(android.os.Bundle) RepositoryAdapter.repositoryAdapter(com.google.android.agera.rvadapter.RepositoryAdapter.repositoryAdapter) Date(java.util.Date) HttpFunctions.httpFunction(com.google.android.agera.net.HttpFunctions.httpFunction) LayoutPresenters.layout(com.google.android.agera.rvadapter.LayoutPresenters.layout) ImageView(android.widget.ImageView) Result(com.google.android.agera.Result) Repository(com.google.android.agera.Repository) RecycledViewPool(android.support.v7.widget.RecyclerView.RecycledViewPool) NotesStore.notesStore(com.google.android.agera.testapp.NotesStore.notesStore) RowHandler.rowBinder(com.google.android.agera.testapp.RowHandler.rowBinder) Result.absentIfNull(com.google.android.agera.Result.absentIfNull) DateFormat.getInstance(java.text.DateFormat.getInstance) View(android.view.View) Updatable(com.google.android.agera.Updatable) RepositoryAdapter(com.google.android.agera.rvadapter.RepositoryAdapter) Receiver(com.google.android.agera.Receiver) Executor(java.util.concurrent.Executor) DataBindingRepositoryPresenters.dataBindingRepositoryPresenterOf(com.google.android.agera.rvdatabinding.DataBindingRepositoryPresenters.dataBindingRepositoryPresenterOf) LayoutInflater(android.view.LayoutInflater) Fragment(android.support.v4.app.Fragment) Executors.newSingleThreadExecutor(java.util.concurrent.Executors.newSingleThreadExecutor) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Repositories.repositoryWithInitialValue(com.google.android.agera.Repositories.repositoryWithInitialValue) DisplayMetrics(android.util.DisplayMetrics) ViewGroup(android.view.ViewGroup) RepositoryPresenters.repositoryPresenterOf(com.google.android.agera.rvadapter.RepositoryPresenters.repositoryPresenterOf) AlertDialog(android.app.AlertDialog) RecyclerView(android.support.v7.widget.RecyclerView) List(java.util.List) BitmapFactory.decodeByteArray(android.graphics.BitmapFactory.decodeByteArray) Bitmap(android.graphics.Bitmap) SEND_INTERRUPT(com.google.android.agera.RepositoryConfig.SEND_INTERRUPT) HttpRequests.httpGetRequest(com.google.android.agera.net.HttpRequests.httpGetRequest) Nullable(android.support.annotation.Nullable) HORIZONTAL(android.support.v7.widget.LinearLayoutManager.HORIZONTAL) EditText(android.widget.EditText) EditText(android.widget.EditText) Receiver(com.google.android.agera.Receiver) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DisplayMetrics(android.util.DisplayMetrics) Date(java.util.Date) RecycledViewPool(android.support.v7.widget.RecyclerView.RecycledViewPool) List(java.util.List) NotesStore.notesStore(com.google.android.agera.testapp.NotesStore.notesStore)

Example 2 with Receiver

use of com.google.android.agera.Receiver 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

Receiver (com.google.android.agera.Receiver)2 Repositories.repositoryWithInitialValue (com.google.android.agera.Repositories.repositoryWithInitialValue)2 Repository (com.google.android.agera.Repository)2 SEND_INTERRUPT (com.google.android.agera.RepositoryConfig.SEND_INTERRUPT)2 Result (com.google.android.agera.Result)2 List (java.util.List)2 Executor (java.util.concurrent.Executor)2 Executors.newSingleThreadExecutor (java.util.concurrent.Executors.newSingleThreadExecutor)2 AlertDialog (android.app.AlertDialog)1 Context (android.content.Context)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory.decodeByteArray (android.graphics.BitmapFactory.decodeByteArray)1 Bundle (android.os.Bundle)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 Fragment (android.support.v4.app.Fragment)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 HORIZONTAL (android.support.v7.widget.LinearLayoutManager.HORIZONTAL)1 RecyclerView (android.support.v7.widget.RecyclerView)1 RecycledViewPool (android.support.v7.widget.RecyclerView.RecycledViewPool)1