Page Contents
Overview: Android LocalInstallation class
The LocalInstallation
class is not a client mirror of the server Installation
Model.
Instead, it wraps the Installation
model into an API that makes it easy to implement the correct client semantics.
- When
LocalInstallation.save()
is called by the application for the first time, a newInstallation
record is created on the server. The data are cached in application’sSharedPreferences
, thus all subsequent calls ofsave()
update this existing record. That way there is exactly oneInstallation
record for each instance of the application (each phone running the app). - It is ok to create a new
LocalInstallation
instance whenever you need to update any of theInstallation
properties. Since the data is cached inSharedPreferences
, allLocalInstallation
objects are initialized from the same data at the creation time. Just don’t forget to callsave
to persist your changes both on the server and in the local storage.
Associating installations with authenticated users
- By default,
userId
isnull
, which means the user has not logged in yet (has not created an account yet). -
After a successful login, call
LocalInstallation.setUserId()
to update the user relation and save the changes.userRepository.loginUser(email, password, new UserRepository<User>.LoginCallback() { @Override public void onSuccess(AccessToken token, User currentUser) { final LocalInstallation installation = new LocalInstallation(context, adapter); installation.setUserId(currentUser.getId()); installation.save(/* callback */); } @Override public void onError(Throwable t) { // handle the error } );
-
After a logout, set
userId
back tonull
in order to flag the installation as anonymous.userRepository.logout(new UserRepository<User>.VoidCallback() { @Override public void onSuccess() { final LocalInstallation installation = new LocalInstallation(context, adapter); installation.setUserId(null); installation.save(/* callback */); } @Override public void onError(Throwable t) { // handle the error } );
Alternatively, you can use the status
property to flag the installations where the user was logged out.
This way it is possible to tell which user was the last one logged on the device:
- A new installation has
userId: null
andstatus: "Active"
. - Login updates the
userId
with a non-empty id value and setsstatus: "Active"
. - Logout keeps the
userId
value but changes thestatus
to a differed value, e.g."LoggedOut"
. - Subsequent login updates the
userId
to the new id value and setsstatus
back to"Active"
.