Recall that list Screenlets require an Interactor to instantiate the Connector and receive the server call’s results. In this article, you’ll create Entry List Screenlet’s Interactor.
Because this Interactor is so similar to that of Guestbook List Screenlet, the steps to create it aren’t explained in detail. Focus is instead placed on the few places in the code where the Interactors diverge. For a full explanation of the code, see the article on creating Guestbook List Screenlet’s Interactor.
Creating Your Interactor’s Folder
Follow these steps to create your Interactor’s folder:
-
In the Finder, create the
Interactor
folder inside your project’sEntryListScreenlet
folder. -
Drag and drop the
Interactor
folder from the Finder into your Xcode project, under theEntryListScreenlet
folder. In the dialog that appears, select Copy items if needed, Create groups, and the Liferay Guestbook target. Then click Finish. TheInteractor
folder now appears in your project.
Now you’re ready to create the Interactor.
Creating the Interactor
Recall that the Interactor class of a list Screenlet that implements fluent
pagination must extend
the BaseListPageLoadInteractor
class.
Your Interactor class must also contain any properties the Screenlet needs, and
an initializer that sets them. This initializer also needs arguments for the
following properties, which it passes to the superclass initializer:
screenlet
: ABaseListScreenlet
reference. This ensures the Interactor always has a Screenlet reference.page
: The page number to retrieve.computeRowCount
: Whether to call the Connector’sdoAddRowCountServiceCall
method.
Follow these steps to create Entry List Screenlet’s Interactor:
-
In the Project navigator, right-click the
Interactor
folder you added above and select New File. In the dialog that appears, fill out each screen as follows:- Select iOS → Source → Cocoa Touch Class, and click Next.
- Name the class
EntryListPageLoadInteractor
, set it to extendBaseListPageLoadInteractor
, select Swift for the language, and click Next. - Make sure the
Interactor
folder and group is selected, as well as the Liferay Guestbook target (these should be selected by default). Click Create.
-
Replace the class file’s contents with this code:
import UIKit import LiferayScreens class EntryListPageLoadInteractor: BaseListPageLoadInteractor { private let groupId: Int64 private let guestbookId: Int64 init(screenlet: BaseListScreenlet, page: Int, computeRowCount: Bool, groupId: Int64, guestbookId: Int64) { self.groupId = (groupId != 0) ? groupId : LiferayServerContext.groupId self.guestbookId = guestbookId super.init(screenlet: screenlet, page: page, computeRowCount: computeRowCount) } public override func createListPageConnector() -> PaginationLiferayConnector { let screenlet = self.screenlet as! BaseListScreenlet return EntryListPageLiferayConnector( startRow: screenlet.firstRowForPage(self.page), endRow: screenlet.firstRowForPage(self.page + 1), computeRowCount: self.computeRowCount, groupId: groupId, guestbookId: guestbookId) } override public func convertResult(_ serverResult: [String:AnyObject]) -> AnyObject { return EntryModel(attributes: serverResult) } override public func cacheKey(_ op: PaginationLiferayConnector) -> String { return "\(groupId)-\(guestbookId)" } }
This class is almost identical to Guestbook List Screenlet’s Interactor,
GuestbookListPageLoadInteractor
. The only real difference is thatEntryListPageLoadInteractor
handles entries. It therefore needs aguestbookId
variable to define the guestbook to retrieve entries from. This variable is set in the initializer and then used in thecreateListPageConnector
method to create aEntryListPageLiferayConnector
instance. TheconvertResult
method receives each[String:AnyObject]
entry from the server and transforms it into anEntryModel
object. Also recall that thecacheKey
method must return a key that can be used with online mode. For entries, a combination of thegroupId
andguestbookId
is a sufficient key.
Great! Your Interactor is finished. Next, you’ll create the delegate.