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