Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Mar 1, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 625
Home
Magnus Ernstsson edited this page Apr 24, 2016
·
19 revisions

Agera (Swedish for “to act”) is a super lightweight Android library that helps prepare data for consumption by the Android application components (such as Activities), or objects therein (such as Views), that have life-cycles in one form or another. It introduces a flavor of functional reactive programming, facilitates clear separation of the when, where and what factors of a data processing flow, and enables describing such a complex and asynchronous flow with a single expression, in near natural language.
Below is an sample demonstrating some of the features of Agera. This wiki together with the javadoc, explains how each part of Agera works.
publicclassAgeraActivityextendsActivityimplementsReceiver<Bitmap>, Updatable {
privatestaticfinalExecutorServiceNETWORK_EXECUTOR =
newSingleThreadExecutor();
privatestaticfinalExecutorServiceDECODE_EXECUTOR =
newSingleThreadExecutor();
privatestaticfinalStringBACKGROUND_BASE_URL =
"http://www.gravatar.com/avatar/4df6f4fe5976df17deeea19443d4429d?s=";
privateRepository<Result<Bitmap>> background;
privateImageViewbackgroundView;
@OverrideprotectedvoidonCreate(finalBundlesavedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content viewsetContentView(R.layout.activity_main);
// Find the background viewbackgroundView = (ImageView) findViewById(R.id.background);
// Create a repository containing the result of a bitmap request. Initially// absent, but configured to fetch the bitmap over the network based on// display size.background = repositoryWithInitialValue(Result.<Bitmap>absent())
.observe() // Optionally refresh the bitmap on events. In this case never
.onUpdatesPerLoop() // Refresh per Looper thread loop. In this case never
.getFrom(newSupplier<HttpRequest>() {
@NonNull@OverridepublicHttpRequestget() {
DisplayMetricsdisplayMetrics = getResources().getDisplayMetrics();
intsize = Math.max(displayMetrics.heightPixels,
displayMetrics.widthPixels);
returnhttpGetRequest(BACKGROUND_BASE_URL + size)
.compile();
}
}) // Supply an HttpRequest based on the display size
.goTo(NETWORK_EXECUTOR) // Change execution to the network executor
.attemptTransform(httpFunction())
.orSkip() // Make the actual http request, skip on failure
.goTo(DECODE_EXECUTOR) // Change execution to the decode executor
.thenTransform(newFunction<HttpResponse, Result<Bitmap>>() {
@NonNull@OverridepublicResult<Bitmap> apply(@NonNullHttpResponseresponse) {
byte[] body = response.getBody();
returnabsentIfNull(decodeByteArray(body, 0, body.length));
}
}) // Decode the response to the result of a bitmap, absent on failure
.onDeactivation(SEND_INTERRUPT) // Interrupt thread on deactivation
.compile(); // Create the repository
}
@OverrideprotectedvoidonResume() {
super.onResume();
// Start listening to the repository, triggering the flowbackground.addUpdatable(this);
}
@OverrideprotectedvoidonPause() {
super.onPause();
// Stop listening to the repository, deactivating itbackground.removeUpdatable(this);
}
@Overridepublicvoidupdate() {
// Called as the repository is updated// If containing a valid bitmap, send to accept belowbackground.get().ifSucceededSendTo(this);
}
@Overridepublicvoidaccept(@NonNullBitmapbackground) {
// Set the background bitmap to the background viewbackgroundView.setImageBitmap(background);
}
}