Widgets

Widgets will let you present custom content in the messaging channel. Whenever a widget is added to your app build, the app will be able to parse and present widget messages, and will let users compose and send widget messages.

Creating A Widget

Setting Up The Project

To create custom widget, you will need to create a framework project in xCode and follow the requirenemts.

  1. Create a Framework project in xCode.
  2. Add "ChatKit" dependency to your framework using cocoapods Podfile

     source 'https://github.com/CocoaPods/Specs.git'
     source 'https://github.com/magnetsystems/Specs.git'
    
     platform :ios, '9.0'
    
     inhibit_all_warnings!
    
     use_frameworks!
    
    pod 'ChatKit', 
    # Add other dependencies here
    
  3. Generate Workspace using pod install shell command.

Adding Widget Presentaion Logic [Required]

  1. Create a main Controller class, that implements ChatKit.WidgetControlling protocol.
  2. Override widget name and model type

     public override init() {
         super.init()
         name = "TextWidget"
         modelType = Model.self
    }
    

    this name will be used to identify the widget type on the clinet and should be unique. Widget type will be used as a model.

  3. Adding support for presentation types

     public override init() {
         super.init()
         name = "TextWidget"
         modelType = Model.self
    
         registerPresenter(presenter: self, 
                           forSurface: WidgetKit.WidgetSurface.Channel.rawValue)
         registerPresenter(presenter: self,                          forSurface: WidgetKit.WidgetSurface.Creator.rawValue)
    }
    

    For Channel presentation, the presenter should conform to ChannelWidgetPresenting protocol.
    For Creator presentation, the presenter should conform to ChannelWidgetCreating protocol.

  4. Adding Channel support

    Override following methods from ChannelWidgetPresenting protocol, to addsupport for Channel surface.

     func widgetViewPresentation() -> WidgetChannelPresentation
     func widgetView() -> UIView
     func configureWidgetView(widgetView:UIView, withMessage message:WidgetMessage, presentingNavigationController navigationController:UINavigationController?)
     func widgetViewSizeForMessage( message:WidgetMessage ) -> CGSize
     @objc optional func containerCellTypeForMessage( message:WidgetMessage ) -> WidgetContainerCell.Type
    
  5. Adding Creator support

    Override following methods from ChannelWidgetCreating protocol, to add support for Creator surface.

    func composerViewControllerWith(send: @escaping (WidgetModel) -> (), cancel: @escaping ()->()) -> UIViewController
    var composerTitle:String {get}
    

Adding Widget Support To Your App

To add your CustomWidget widget to your app, you need to add the CustomWidget framework and register your WidgetControllerBase subclasss.

WidgetsController.sharedInstance.registerWidgetController(widgetController: CustomWidget.Controller())

Channel UI

Channel can be presented using ChannelViewController.

Customizing Channel UI

Channel view controller can be customized at some level.

Customizing message composer

Customize using a xib file

channelViewController.composerViewPresenter.setNib(nib:UINib)

Customize using a Class

channelViewController.composerViewPresenter.setClass(viewType:ChannelViewComposer)

Customizing message cell

channelViewController.channelTableViewController.channelCellProvider.cellType = CustomCellType.self

Creating Custom Channel UI

Create a view controller with a table view. Create instance if ChannelTableViewController ans set tableView property.

ChannelTableViewController.tableView = tableView

Creating custom TableView datasource and delegate

When creating a view controller with a table view. use instance of ChannelCellProvider to create cells, and to get their height.

    func tableView(_ tableView: UITableView, 
    cellForRowAt indexPath: IndexPath, 
    forMessage message:WidgetMessage, 
    inNavigationController 
    navigationController:UINavigationController?) -> UITableViewCell

    func tableView(_ tableView: UITableView, 
    heightForRowAt indexPath: IndexPath, 
    forMessage message:WidgetMessage) -> CGFloat