Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Events and Models Example

Adrián Rivero edited this page Mar 16, 2017 · 12 revisions

Let's say we have an User Model, and a List of Friends, and once that we add a new user to the system, we need to update our friends list. We'll have a button in our activity to update the users via a network operation, and then we'll send an event to update the friends list.

This is our User Model:

@LocalDBModel
@ServerModel(
    baseUrl="http://app.user-friends.com",
    get="/friends/{query}"
)
public class User extends Model {
    int remote_id;  //ID of the user in the server

    String name;
    String email;
    int age;

    boolean friends;  //Indicates that the user is a friend of the logged user.
    boolean current; //Indicates that the user is the logged in user.

    @AfterLoad
    @ServerModel
    void afterLoad(String query) {
        friends = true;
        FriendsDownloaded_.create("User " + query + " friends downloaded successfully!").postEvent();
    }

    @AfterPut
    void sendUpdate() {
        UpdateFriends_.post();
    }
}

And the code for our activity:

@EActivity(R.layout.activity_main)
public class EnhancedActivity extends Activity {
    @Model(query="current=1")
    User_ currentUser;

    @Model(query="friends=1")
    @UpdateOnEvent(UpdateFriends.class)
    List<User_> friends;

    @Model(query="{currentUser.getRemoteId()}", async=true)
    @ServerModel
    @LoadOnEvent(UpdateButtonClicked.class)
    @PutOnEvent(FriendsDownloaded.class)
    List<User_> serverFriends;

    @Click
    void btnUpdate() {
       UpdateButtonClicked_.post();
    }

    @Event
    void onFriendsDownloaded(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

Let's explain the code for a better understanding. We injected the current user in the field "currentUser".

The list "serverFriends" will be used to download the friends from the server, and the list "friends" will show all the friends of the current user that are stored locally in the DataBase.

After the button with ID "btnUpdate" is pressed, the event UpdateButtonClicked will be dispatched; this will trigger the injection of serverFriends that is annotated with @LoadOnEvent, and the friends list is going to be downloaded directly from the server.

When the list is successfully downloaded then "serverModelLoaded" method is triggered, here we set "friends" to true, in order to save it in the DB indicating that this user is the current user friend, and we dispatch the event FriendsDownloaded with a message that will be shown with a toast. This action is going to save also the downloaded list to the Local Database because "serverFriends" is annotated with @PutOnEvent.

Finally after the users are successfully saved locally the method "afterPut" will be triggered, sending a new event named UpdateFriends that will cause the list "friends" to be reloaded from the DB because the field it is annotated with @UpdateOnEvent.

Note that it's not necessary to declarar @LocalDBModel to "friends" injection process, because the @LocalDBModel annotation has precedence over the @ServerDBModel annotation in injection.