Skip to content

The Module Class

Aditya Vaidyam edited this page Feb 25, 2016 · 2 revisions

The Module Class.

The Module Class is the controller in Health365's MVC paradigm. In a nutshell, the work of the Module comes in as follows.

// The user made a change to a Dog Module via the UI. 
Dispatcher.UTILITY.run(() -> { 
    this.dataContext().ifExists((data) -> {
        Dog puppy = new Dog();
        puppy.setName("Alfred Hitchcock");
        data.add(puppy);
    });
    
    // Now update the UI and inform the user.
    Dispatcher.UI.run(() -> {
        this.viewContext().ifExists((view) -> {
            view.makeToast("Your new puppy was saved successfully!").show();
        });
    });
});

More to come on this later on.

Definition

The Module class is an abstract class which all of Health365 application modules must extend. It uses a dataContext() and the viewContext() which binds to the a certain module allows the module to work with both the Realm back-end services and the UI front-end services without worrying about specific details. With help from a Dispatcher object, the module can do these transactions on a background thread or the UI thread for free!

Using the dataContext()

The data context is set up as follows.

// class declarations above
private RealmContext<Dog> dogRealm;

public DogModule(){
     this.dogRealm = new RealmContext<>();
     this.dogRealm.init(BaseApplication.application(), Dog.class, "dog.realm")
}

And that's it! Your module will now have access to it's own realm. From here it can query, add, remove, etc. so you can do the application logic needed with your data. Shown below is an example of how to interact with the dataContext.

// The user made a change to a Dog Module via the UI. 
Dispatcher.UTILITY.run(() -> { 
    this.dataContext().ifExists((data) -> {
        Dog puppy = new Dog();
        puppy.setName("Alfred Hitchcock");
        data.add(puppy); // <-- This is the model being updated. 
    });
  // UI update stuff below. 

Updating the View

    // data context above...
    // Now update the UI and inform the user.
    Dispatcher.UI.run(() -> {
        this.viewContext().ifExists((view) -> {
            // updates the View
            view.makeToast("Your new puppy was saved successfully!").show(); 
        });
    });
});

The viewContext() will allow us to display changes in the model prompted by the User, to the User. This part of the code only if the view associated with this module is exists. However, you can do something like the example below to post a Notification if the view is not in the right context for the module.

this.viewContext.ifExists((view)-> {
     // do something
   }).OrElse() -> {
       Notification notification = new Notification.Builder(this)
          .setSmallIcon(R.mipmap.ic_launcher)
          .setContentTitle(getString(R.string.app_name))
          .setAutoCancel(true)
          .setPriority(Notification.PRIORITY_MAX)
          .setDefaults(Notification.DEFAULT_VIBRATE)
          .setContentIntent(pendingIntent)
          .setContentText(text)
          .build();
       getNotificationManager().notify(NOTIFICATION_ID, notification);
   });
Clone this wiki locally