A Screenlet’s Interactor makes the service call to retrieve the data you need from a Liferay instance. An Interactor is made up of several components:
-
The event class. This class lets you handle communication between the Screenlet’s components via event objects that contain the server call’s results. Screens uses the EventBus library for this. Screens supplies the
BasicEvent
class andBaseListEvent
class for communicatingJSONObject
andJSONArray
results within Screenlets, respectively. You can create your own event classes by extendingBasicEvent
. You should create your own event classes when you must communicate objects other thanJSONObject
orJSONArray
. The example Add Bookmark Screenlet only needs to communicateJSONObject
instances, so it usesBasicEvent
. -
The listener interface. This defines the methods the app developer needs to respond to the Screenlet’s behavior. For example, Login Screenlet’s listener defines the
onLoginSuccess
andonLoginFailure
methods. Screens calls these methods when login succeeds or fails, respectively. By implementing these methods in the activity or fragment class that contains the Screenlet, the app developer can respond to login success and failure. Similarly, the example Add Bookmark Screenlet’s listener interface defines two methods: one for responding to the Screenlet’s failure to add a bookmark and one for responding to its success to add a bookmark:public interface AddBookmarkListener { void onAddBookmarkFailure(Exception exception); void onAddBookmarkSuccess(); }
-
The Interactor class. This class must extend Screens’s
BaseRemoteInteractor
with your listener and event as type arguments. The listener lets the Interactor class send the server call’s results to any classes that implement the listener. In the implementation of the method that makes the server call, theexecute
method, you must use the Mobile SDK to make an asynchronous service call. This means you must get a session and then make the server call. You make the server call by creating an instance of the Mobile SDK service (e.g.,BookmarksEntryService
) that can call the Liferay service you need and then making the call. The Interactor class must also process the event object that contains the call’s results and then notify the listener of those results. You do this by implementing theonSuccess
andonFailure
methods to invoke the correspondinggetListener()
methods.For example, the
AddBookmarkInteractor
class is Add Bookmark Screenlet’s Interactor class. This class implements theexecute
method, which adds a bookmark to a folder in a Liferay instance’s Bookmarks portlet. This method first validates the bookmark’s URL and folder. It then calls thegetJSONObject
method to add the bookmark, and concludes by returning a newBasicEvent
object created from theJSONObject
. Theif
statement in thegetJSONObject
method checks the Liferay version so it can create the appropriateBookmarksEntryService
instance needed to make the server call. Regardless of the Liferay version, thegetSession()
method retrieves the existing session created by Login Screenlet upon successful login. The session’saddEntry
method makes the server call. The Screenlet calls theonSuccess
oronFailure
method to notify the listener of the server call’s success or failure, respectively. In either case, theBasicEvent
object contains the server call’s results. Since this Screenlet doesn’t retrieve anything from the server, however, there’s no need to process theBasicEvent
object in theonSuccess
method; calling the listener’sonAddBookmarkSuccess
method is sufficient. Here’s the complete code forAddBookmarkInteractor
:public class AddBookmarkInteractor extends BaseRemoteInteractor<AddBookmarkListener, BasicEvent> { @Override public BasicEvent execute(Object[] args) throws Exception { String url = (String) args[0]; String title = (String) args[1]; long folderId = (long) args[2]; validate(url, folderId); JSONObject jsonObject = getJSONObject(url, title, folderId); return new BasicEvent(jsonObject); } @Override public void onSuccess(BasicEvent event) throws Exception { getListener().onAddBookmarkSuccess(); } @Override public void onFailure(BasicEvent event) { getListener().onAddBookmarkFailure(event.getException()); } private void validate(String url, long folderId) { if (url == null || url.isEmpty() || !URLUtil.isValidUrl(url)) { throw new IllegalArgumentException("Invalid url"); } else if (folderId == 0) { throw new IllegalArgumentException("folderId not set"); } } @NonNull private JSONObject getJSONObject(String url, String title, long folderId) throws Exception { if (LiferayServerContext.isLiferay7()) { return new BookmarksEntryService(getSession()).addEntry(LiferayServerContext.getGroupId(), folderId, title, url, "", null); } else { return new com.liferay.mobile.android.v62.bookmarksentry.BookmarksEntryService( getSession()).addEntry(LiferayServerContext.getGroupId(), folderId, title, url, "", null); } } }
Sweetness! Your Screenlet’s Interactor is done. Next, you’ll create the Screenlet class.