Saturday, December 30, 2017

Agera Repository


In our previous blog post we have learnt about Agera, about Reactive programming  and Sample code of Hello World Example using Agera - Functional Reactive Programming for Android recommended by Google. In Hello World Example learn about Observable, Updatable, Supplier and Receiver

In this blog I am going to introduce Repository.


What is the Repository ??

The most important concept in Agera is the Repository. Repositories receive, supply, and store data and emit updates. The interfaces Observable, Supplier and Receiver are combined into two types of repositories:


InterfaceObservableSupplierReceiver
Repository
MutableRepository


In our hello world code, you could replace
implements Observable, Supplier<String>, Receiver<String>
with
 implements MutableRepository<String>
but Agera provides a Repository factory, so remove the MyDataSupplier class altogether and replace
  MyDataSupplier myDataSupplier = new MyDataSupplier();
with
 MutableRepository<String> mStringRepo = Repositories.mutableRepository("Initial value by Agera with Repository");
mutableRepository() creates a repository similar to our previous implementation, but that is thread-safe and has a more sophisticated update dispatcher, so let's use that from now on.
Also, remove the updatables (with removeUpdatable()) when you know you're done with them. In our example this is not needed but it is a good practice: it avoids potential leaks and prevents updating destroyed views. The class is now much shorter:
 public class JavaMainActivity extends AppCompatActivity {
     //    MyDataSupplier myDataSupplier = new MyDataSupplier();
     //    Updatable updatable;
     private MutableRepository<String> mStringRepo;
     private Updatable mLoggerUpdatable;


     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

        mStringRepo = Repositories.mutableRepository("Initial value by Agera with Repository");

       final TextView textView = findViewById(R.id.textView);

          // Create an Updatable
          //        updatable = new Updatable() {
          //            @Override
          //            public void update() {
          //                Log.d("AGERA", myDataSupplier.get());
          //                textView.setText(myDataSupplier.get());
          //            }
          //        };
          //        myDataSupplier.addUpdatable(updatable);

          //        myDataSupplier.accept("Hello Agera!");


          // Create an Updatable by Respository
      mLoggerUpdatable = () -> {
          Log.d("AGERA", mStringRepo.get());
          textView.setText(mStringRepo.get());
      };
     }

      @Override
      protected void onStart() {
          super.onStart();
          mStringRepo.addUpdatable(mLoggerUpdatable);

          // Change the repository's value
          mStringRepo.accept("Hello World by Agera with Repository");
      }

    @Override
     protected void onStop() {
        mStringRepo.removeUpdatable(mLoggerUpdatable);
       super.onStop();
     }
 }

In summary:

Agera uses very simple interfaces you should be aware of:
 Observable, Updatable, Supplier and Receiver. These are combined in the Repository and MutableRepository interfaces.
  • Agera provides a factory of implementations of these simple Repositories: Repositories.mutableRepository(T)and Repositories.repository(T) for immutable repositories.
Source code for this can be found here Enjoy !!!!


Contact - Let's connect to each other


Thursday, December 14, 2017

Agera - Functional Reactive Programming for Android

What is Agera?

Agera 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 whenwhere 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.

second question comes to any mind is 

What do we mean by reactive?


Reactive programming is a paradigm related to how changes are propagated in a program. A good example is a spreadsheet: cells contain formulas that depend on other cells' values. When one of these values are changed, there's no need to manually update the resulting values; the changes are propagated for you. Agera brings a reactive flavor to Android using an observer pattern.

Hello World Example

Lets start with simplest example of programming world, that is Hello World Example.
But to learn how to code for "Hello World", we need to learn basic components of Agera.

The foundation of Agera is a set of very simple interfaces including: Observable, Updatable, Supplier and Receiver:

Observable.java
public interface Observable {

  void addUpdatable(Updatable updatable);

  void removeUpdatable(Updatable updatable);
}


Updatable.java

public interface Updatable {

  void update();
}


Supplier.java

public interface Supplier<T> {

  T get();
}

Receiver.java

public interface Receiver<T> {

  void accept(T value);
}


As you can see, these four interfaces are very simple. This is what they're responsible for:

  • Observable => Something that can be observed for changes and stores a list of updatable objects. When a change occurs, the updatables are poked
  • Updatable => Called when an event has occurred. Its update() method will be called when the class it observes changes, but can also be called manually to force an update.
  • Supplier => Something that supplies data when the get() method is called. In this case, "Data" can be anything.
  • Receiver => Something that can receive (and normally store) a value send to it via accept().
lets see how to use them in our code.
I have already written Hello World Example on Github

All the best !!! Happy Coding !!!

Next Step is to learn Repository..

Repository => The most important concept in Agera is the Repository. Repositories receive, supply, and store data and emit updates. The interfaces Observable, Supplier and Receiver are combined into two types of repositories:
  Repository can replace Observable and Supplier
  MutableRepository can replace Observable, Supplier and Receiver
A complete blogpost is coming soon on Repository...!!

let's connect to learn together