com.google.android.agera.Function Java Examples

The following examples show how to use com.google.android.agera.Function. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComplexRecycleViewActivity.java    From AndroidAgeraTutorial with Apache License 2.0 6 votes vote down vote up
private void setUpRepository() {
    networkExecutor = Executors.newSingleThreadExecutor();

    mMutableRepository = Repositories.mutableRepository(mPagination);

    mLoadDataRepository = Repositories.repositoryWithInitialValue(Result.<ApiResult<GirlInfo>>absent())
            .observe(mMutableRepository)
            .onUpdatesPerLoop()
            .goTo(networkExecutor)
            .attemptGetFrom(new GirlsSupplier(mMutableRepository)).orSkip()
            .thenTransform(new Function<ApiResult<GirlInfo>, Result<ApiResult<GirlInfo>>>() {
                @NonNull
                @Override
                public Result<ApiResult<GirlInfo>> apply(@NonNull ApiResult<GirlInfo> input) {
                    return absentIfNull(input);
                }
            })
            .onDeactivation(RepositoryConfig.SEND_INTERRUPT)
            .compile();

}
 
Example #2
Source File: RepositoryPresentersTest.java    From agera with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBindRepositoryPresenterCollectionOfCollection() {
  final RepositoryPresenter<String> repositoryPresenter =
      repositoryPresenterOf(String.class)
          .layout(LAYOUT_ID)
          .bindWith(binder)
          .bindCollectionWith(collectionBinder)
          .forCollection(new Function<String, List<String>>() {
            @NonNull
            @Override
            public List<String> apply(@NonNull final String input) {
              return singletonList(valueOf(input.charAt(0)));
            }
          });
  repositoryPresenter.bind(STRING, 0, viewHolder);
  verify(binder).bind(FIRST_STRING_CHARACTER, view);
  verify(collectionBinder).bind(STRING, view);
}
 
Example #3
Source File: RepositoryPresentersTest.java    From agera with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBindRepositoryPresenterOfCollection() {
  final RepositoryPresenter<String> repositoryPresenter =
      repositoryPresenterOf(String.class)
          .layout(LAYOUT_ID)
          .bindWith(binder)
          .forCollection(new Function<String, List<String>>() {
            @NonNull
            @Override
            public List<String> apply(@NonNull final String input) {
              return singletonList(valueOf(input.charAt(0)));
            }
          });
  repositoryPresenter.bind(STRING, 0, viewHolder);
  verify(binder).bind(FIRST_STRING_CHARACTER, view);
}
 
Example #4
Source File: RepositoryPresenterCompiler.java    From agera with Apache License 2.0 6 votes vote down vote up
CompiledRepositoryPresenter(
    @NonNull final Function<Object, Integer> layoutId,
    @NonNull final Binder<Object, View> binder,
    @NonNull final Function<Object, Long> stableIdForItem,
    @NonNull final Receiver<View> recycler,
    @NonNull final Function<Object, Object> keyForItem,
    final boolean detectMoves,
    @NonNull final Function<Object, List<Object>> converter,
    @NonNull final Binder<Object, View> collectionBinder) {
  this.collectionBinder = collectionBinder;
  this.converter = converter;
  this.layoutId = layoutId;
  this.binder = binder;
  this.stableIdForItem = stableIdForItem;
  this.recycler = recycler;
  this.enableDiff = keyForItem != NO_KEY_FOR_ITEM;
  this.keyForItem = keyForItem;
  this.detectMoves = detectMoves;
}
 
Example #5
Source File: DataBindingRepositoryPresentersTest.java    From agera with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldBindRepositoryPresenterOfCollection() {
  final RepositoryPresenter<String> repositoryPresenter =
      dataBindingRepositoryPresenterOf(String.class)
          .layout(LAYOUT_ID)
          .itemId(ITEM_ID)
          .forCollection(new Function<String, List<String>>() {
            @NonNull
            @Override
            public List<String> apply(@NonNull final String input) {
              return singletonList(valueOf(input.charAt(0)));
            }
          });
  repositoryPresenter.bind(STRING, 0, viewHolder);

  verify(viewDataBinding).setVariable(ITEM_ID, FIRST_STRING_CHARACTER);
  verify(viewDataBinding).executePendingBindings();
  verifyNoMoreInteractions(viewDataBinding);
}
 
Example #6
Source File: DataBindingRepositoryPresenterCompiler.java    From agera with Apache License 2.0 6 votes vote down vote up
CompiledRepositoryPresenter(
    @NonNull final Function<Object, Integer> itemId,
    @NonNull final Function<Object, Integer> layoutId,
    @NonNull final Function<Object, Long> stableIdForItem,
    @NonNull final SparseArray<Object> handlers,
    final int recycleConfig,
    @NonNull final Function<Object, List<Object>> converter,
    final int collectionId,
    @NonNull final Function<Object, Object> keyForItem,
    final boolean detectMoves) {
  this.itemId = itemId;
  this.collectionId = collectionId;
  this.converter = converter;
  this.layoutId = layoutId;
  this.stableIdForItem = stableIdForItem;
  this.recycleConfig = recycleConfig;
  this.handlers = handlers;
  this.enableDiff = keyForItem != NO_KEY_FOR_ITEM;
  this.keyForItem = keyForItem;
  this.detectMoves = detectMoves;
}
 
Example #7
Source File: RepositoryAdapterRecycleViewActivity.java    From AndroidAgeraTutorial with Apache License 2.0 5 votes vote down vote up
private void setUpRepository() {
    networkExecutor = Executors.newSingleThreadExecutor();
    mObservable = new SimpleObservable() { };


    mRepository = Repositories.repositoryWithInitialValue(Result.<List<GirlInfo>>absent())
            .observe(mObservable)
            .onUpdatesPerLoop()
            .goTo(networkExecutor)
            .getFrom(new GirlsSupplier(new Supplier<Integer>() {
                @NonNull
                @Override
                public Integer get() {
                    return mPagination;
                }
            }))
            .thenTransform(new Function<Result<ApiResult<GirlInfo>>, Result<List<GirlInfo>>>() {
                @NonNull
                @Override
                public Result<List<GirlInfo>> apply(@NonNull Result<ApiResult<GirlInfo>> input) {
                    if (input.succeeded() && !input.get().error) {
                        return Result.success(input.get().results);
                    } else {
                        return Result.absent();
                    }
                }
            })
            .onDeactivation(RepositoryConfig.SEND_INTERRUPT)
            .compile();
}
 
Example #8
Source File: SqlDatabaseFunctions.java    From agera with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a sql query {@link Function}.
 */
@NonNull
public static <T> Function<SqlRequest, Result<List<T>>> databaseQueryFunction(
    @NonNull final Supplier<Result<SQLiteDatabase>> database,
    @NonNull Function<Cursor, T> rowMap) {
  return new DatabaseFunction<>(database, new DatabaseQueryMerger<>(rowMap));
}
 
Example #9
Source File: RepositoryPresenterCompiler.java    From agera with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public RPMain diffWith(@NonNull final Function keyForItem, final boolean detectMoves) {
  this.keyForItem = keyForItem;
  this.detectMoves = detectMoves;
  return this;
}
 
Example #10
Source File: RepositoryPresenterCompiler.java    From agera with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public RepositoryPresenter<Result<List>> forResultList() {
  return new CompiledRepositoryPresenter(layoutForItem, binder, stableIdForItem, recycler,
      keyForItem, detectMoves, (Function) resultListAsList(),
      collectionBinder);
}
 
Example #11
Source File: RepositoryPresenterCompiler.java    From agera with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public RepositoryPresenter<Result> forResult() {
  return new CompiledRepositoryPresenter(layoutForItem, binder, stableIdForItem, recycler,
      keyForItem, detectMoves, (Function) resultAsList(),
      collectionBinder);
}
 
Example #12
Source File: RepositoryPresenterCompiler.java    From agera with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public RepositoryPresenter<List> forList() {
  return new CompiledRepositoryPresenter(layoutForItem, binder, stableIdForItem, recycler,
      keyForItem, detectMoves, (Function) identityFunction(),
      collectionBinder);
}
 
Example #13
Source File: DataBindingRepositoryPresenterCompiler.java    From agera with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public RepositoryPresenter<Result<List<Object>>> forResultList() {
  return new CompiledRepositoryPresenter(itemId, layoutFactory,
      stableIdForItem, handlers, recycleConfig, (Function) resultListAsList(), collectionId,
      keyForItem, detectMoves);
}
 
Example #14
Source File: DataBindingRepositoryPresenterCompiler.java    From agera with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public RepositoryPresenter<List<Object>> forList() {
  return new CompiledRepositoryPresenter(itemId, layoutFactory, stableIdForItem,
      handlers, recycleConfig, (Function) identityFunction(), collectionId, keyForItem,
      detectMoves);
}
 
Example #15
Source File: DataBindingRepositoryPresenterCompiler.java    From agera with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public DBRPMain diffWith(@NonNull final Function keyForItem, final boolean detectMoves) {
  this.keyForItem = keyForItem;
  this.detectMoves = detectMoves;
  return this;
}
 
Example #16
Source File: RowHandler.java    From agera with Apache License 2.0 5 votes vote down vote up
@NonNull
static <TRow> RowHandler<TRow> rowBinder(
    @NonNull final RecycledViewPool pool,
    @NonNull final Function<TRow, LayoutManager> layoutManager,
    @NonNull final Function<TRow, Long> stableIdFunction,
    @NonNull final Function<TRow, RepositoryPresenter<TRow>> presenterFromView) {
  return new RowHandler<>(pool, stableIdFunction, presenterFromView, layoutManager);
}
 
Example #17
Source File: RowHandler.java    From agera with Apache License 2.0 5 votes vote down vote up
private RowHandler(@NonNull final RecycledViewPool pool,
    @NonNull final Function<TRow, Long> stableId,
    @NonNull final Function<TRow, RepositoryPresenter<TRow>> presenter,
    @NonNull final Function<TRow, LayoutManager> layoutManager) {
  this.stableId = checkNotNull(stableId);
  this.presenter = checkNotNull(presenter);
  this.layoutManager = layoutManager;
  this.itemRowStates = new IdentityHashMap<>();
  this.previousStableIds = new IdentityHashMap<>();
  this.adapterRepositories = new IdentityHashMap<>();
  this.startedAdapters = new HashSet<>();
  this.pool = pool;
}
 
Example #18
Source File: Okhttp3AgeraFastjsonActivity.java    From SaveVolley with Apache License 2.0 5 votes vote down vote up
@Override protected void initData() {
    SaveVolley saveVolley = SaveVolleys
        .<GankData>request(TEST_URL)
        .method(Method.GET)
        .parseStyle(FASTJSON)
        .classOf(GankData.class)
        .createRequest()
        .context(this)
        .compile();

    final Repository<GankResultData> repository = Repositories
        .repositoryWithInitialValue(INITIAL_VALUE)
        .observe(saveVolley.getReservoir())
        .onUpdatesPerLoop()
        .goTo(executor)
        .attemptGetFrom(saveVolley.getReservoir())
        .orSkip()
        .thenAttemptTransform(new Function<Object, Result<GankResultData>>() {
            /**
             * Returns the result of applying this function to {@code input}.
             */
            @NonNull @Override public Result<GankResultData> apply(@NonNull Object input) {
                if (input instanceof GankData) {
                    return Result.success(((GankData) input).results.get(0));
                } else if (input instanceof VolleyError) {
                    return Result.failure((VolleyError) input);
                }
                return Result.failure();
            }
        })
        .orSkip()
        .compile();

    repository.addUpdatable(new Updatable() {
        @Override public void update() {
            getContentText.setText(repository.get().toString());
        }
    });
}
 
Example #19
Source File: RecycleViewActivity.java    From AndroidAgeraTutorial with Apache License 2.0 5 votes vote down vote up
private void setUpRepository() {
    networkExecutor = Executors.newSingleThreadExecutor();
    uiExecutor = UiThreadExecutor.newUiThreadExecutor();
    mObservable = new SimpleObservable() { };


    mRepository = Repositories.repositoryWithInitialValue(Result.<ApiResult<GirlInfo>>absent())
            .observe(mObservable)
            .onUpdatesPerLoop()
            //.goTo(uiExecutor)
            .getFrom(new Supplier<Object>() {
                @NonNull
                @Override
                public Object get() {
                    Toast.makeText(RecycleViewActivity.this, "load data begin", Toast.LENGTH_LONG).show();
                    return new Object();
                }
            })
            .goTo(networkExecutor)
            .getFrom(new GirlsSupplier(new Supplier<Integer>() {
                @NonNull
                @Override
                public Integer get() {
                    return mPagination;
                }
            }))

            .goTo(uiExecutor)
            .thenTransform(new Function<Result<ApiResult<GirlInfo>>, Result<ApiResult<GirlInfo>>>() {
                @NonNull
                @Override
                public Result<ApiResult<GirlInfo>> apply(@NonNull Result<ApiResult<GirlInfo>> input) {
                    Toast.makeText(RecycleViewActivity.this, "load data end", Toast.LENGTH_LONG).show();
                    return input;
                }
            })
            .onDeactivation(RepositoryConfig.SEND_INTERRUPT)
            .compile();
}
 
Example #20
Source File: HurlAgeraFastjsonActivity.java    From SaveVolley with Apache License 2.0 5 votes vote down vote up
@Override protected void initData() {
    SaveVolley saveVolley = SaveVolleys
        .<GankData>request(TEST_URL)
        .method(Method.GET)
        .parseStyle(FASTJSON)
        .classOf(GankData.class)
        .createRequest()
        .context(this)
        .compile();

    final Repository<GankResultData> repository = Repositories
        .repositoryWithInitialValue(INITIAL_VALUE)
        .observe(saveVolley.getReservoir())
        .onUpdatesPerLoop()
        .goTo(executor)
        .attemptGetFrom(saveVolley.getReservoir())
        .orSkip()
        .thenAttemptTransform(new Function<Object, Result<GankResultData>>() {
            /**
             * Returns the result of applying this function to {@code input}.
             */
            @NonNull @Override public Result<GankResultData> apply(@NonNull Object input) {
                if (input instanceof GankData) {
                    return Result.success(((GankData) input).results.get(0));
                } else if (input instanceof VolleyError) {
                    return Result.failure((VolleyError) input);
                }
                return Result.failure();
            }
        })
        .orSkip()
        .compile();

    repository.addUpdatable(new Updatable() {
        @Override public void update() {
            getContentText.setText(repository.get().toString());
        }
    });
}
 
Example #21
Source File: HurlAgeraGsonActivity.java    From SaveVolley with Apache License 2.0 5 votes vote down vote up
@Override protected void initData() {
    SaveVolley saveVolley = SaveVolleys
        .<GankData>request(TEST_URL)
        .method(Method.GET)
        .parseStyle(GSON)
        .classOf(GankData.class)
        .createRequest()
        .context(this)
        .compile();

    final Repository<GankResultData> repository = Repositories
        .repositoryWithInitialValue(INITIAL_VALUE)
        .observe(saveVolley.getReservoir())
        .onUpdatesPerLoop()
        .goTo(executor)
        .attemptGetFrom(saveVolley.getReservoir())
        .orSkip()
        .thenAttemptTransform(new Function<Object, Result<GankResultData>>() {
            /**
             * Returns the result of applying this function to {@code input}.
             */
            @NonNull @Override public Result<GankResultData> apply(@NonNull Object input) {
                if (input instanceof GankData) {
                    return Result.success(((GankData) input).results.get(0));
                } else if (input instanceof VolleyError) {
                    return Result.failure((VolleyError) input);
                }
                return Result.failure();
            }
        })
        .orSkip()
        .compile();

    repository.addUpdatable(new Updatable() {
        @Override public void update() {
            getContentText.setText(repository.get().toString());
        }
    });
}
 
Example #22
Source File: Okhttp3AgeraGsonActivity.java    From SaveVolley with Apache License 2.0 5 votes vote down vote up
@Override protected void initData() {
    SaveVolley saveVolley = SaveVolleys
        .<GankData>request(TEST_URL)
        .method(Method.GET)
        .parseStyle(GSON)
        .classOf(GankData.class)
        .createRequest()
        .context(this)
        .compile();

    final Repository<GankResultData> repository = Repositories.repositoryWithInitialValue(
        INITIAL_VALUE)
        .observe(saveVolley.getReservoir())
        .onUpdatesPerLoop()
        .goTo(executor)
        .attemptGetFrom(saveVolley.getReservoir())
        .orSkip()
        .thenAttemptTransform(new Function<Object, Result<GankResultData>>() {
            /**
             * Returns the result of applying this function to {@code input}.
             */
            @NonNull @Override public Result<GankResultData> apply(@NonNull Object input) {
                if (input instanceof GankData) {
                    return Result.success(((GankData) input).results.get(0));
                } else if (input instanceof VolleyError) {
                    return Result.failure((VolleyError) input);
                }
                return Result.failure();
            }
        })
        .orSkip()
        .compile();

    repository.addUpdatable(new Updatable() {
        @Override public void update() {
            getContentText.setText(repository.get().toString());
        }
    });
}
 
Example #23
Source File: SimpleActivityB.java    From AndroidAgeraTutorial with Apache License 2.0 5 votes vote down vote up
private void setUpRepository() {
    networkExecutor = Executors.newSingleThreadExecutor();
    mObservable = new OnClickObservable() {
        @Override
        public void onClick( ) {
            dispatchUpdate();
        }
    };

    Supplier<String> imageUriSupplier = new Supplier<String>() {
        @NonNull
        @Override
        public String get() {
            return MockRandomData.getRandomImage();
        }
    };

    mRepository = Repositories.repositoryWithInitialValue(Result.<Bitmap>absent())
            .observe(mObservable)
            .onUpdatesPerLoop()
            .getFrom(imageUriSupplier)
            .goTo(networkExecutor)
            .thenTransform(new Function<String, Result<Bitmap>>() {
                @NonNull
                @Override
                public Result<Bitmap> apply(@NonNull String input) {
                    return new ImageSupplier(input).get();
                }
            })
            .compile();
}
 
Example #24
Source File: HttpFunctions.java    From agera with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a default http {@link Function} that returns a {@link Result} with a
 * {@link HttpResponse} from a {@link HttpRequest}.
 */
@NonNull
public static Function<HttpRequest, Result<HttpResponse>> httpFunction() {
  return HTTP_FUNCTION;
}
 
Example #25
Source File: DataBindingRepositoryPresenterCompiler.java    From agera with Apache License 2.0 4 votes vote down vote up
@NonNull
@Override
public RepositoryPresenter<Result<Object>> forResult() {
  return new CompiledRepositoryPresenter(itemId, layoutFactory, stableIdForItem,
      handlers, recycleConfig, (Function) resultAsList(), collectionId, keyForItem, detectMoves);
}
 
Example #26
Source File: SqlDatabaseFunctions.java    From agera with Apache License 2.0 4 votes vote down vote up
private DatabaseQueryMerger(@NonNull final Function<Cursor, T> cursorToItem) {
  this.cursorToItem = checkNotNull(cursorToItem);
}
 
Example #27
Source File: SqlDatabaseFunctions.java    From agera with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a sql delete {@link Function}.
 */
@NonNull
public static Function<SqlDeleteRequest, Result<Integer>> databaseDeleteFunction(
    @NonNull final Supplier<Result<SQLiteDatabase>> database) {
  return new DatabaseFunction<>(database, new DatabaseDeleteMerger());
}
 
Example #28
Source File: SqlDatabaseFunctions.java    From agera with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a sql update {@link Function}.
 */
@NonNull
public static Function<SqlUpdateRequest, Result<Integer>> databaseUpdateFunction(
    @NonNull final Supplier<Result<SQLiteDatabase>> database) {
  return new DatabaseFunction<>(database, new DatabaseUpdateMerger());
}
 
Example #29
Source File: SqlDatabaseFunctions.java    From agera with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a sql insert {@link Function}.
 */
@NonNull
public static Function<SqlInsertRequest, Result<Long>> databaseInsertFunction(
    @NonNull final Supplier<Result<SQLiteDatabase>> database) {
  return new DatabaseFunction<>(database, new DatabaseInsertMerger());
}
 
Example #30
Source File: RepositoryPresenterCompiler.java    From agera with Apache License 2.0 4 votes vote down vote up
@NonNull
@Override
public RepositoryPresenter forCollection(@NonNull final Function converter) {
  return new CompiledRepositoryPresenter(layoutForItem, binder, stableIdForItem, recycler,
      keyForItem, detectMoves, converter, collectionBinder);
}