MessagingViewModel

public final class MessagingViewModel : ObservableObject, Identifiable

MessagingViewModel is the key object that sits between the UI and the Model It is a mandatory EnvironmentObject consumed by AssistantView

It implements BindableObject protocol which allows you to directly bind public properties from your SwiftUI view

Public properties

  • Holds information about menu preferences

    Declaration

    Swift

    @Published
    public var menu: PreferencesMenuData? { get set }
  • messages over time (including non-acknowledged client initiated messages which will be removed (and quasi replaced) once bot acknowledges them)

    Declaration

    Swift

    @Published
    public private(set) var model: [MessageData] { get set }
  • messages returned by bot over time

    Declaration

    Swift

    @Published
    public private(set) var acknowledgedMessages: [MessageData] { get set }
  • A closure to provide context-specific information before any message is sent to the bot. The arguments are supplied based on the type of message which gets posted (either a text message or a postback message).

    Example

    You want to pass user information for every message which will be send to the bot. Here, ‘User’ is a custom struct (i.e. not part of this package) conforming to Encodable and has two stored properties named firstName and lastName

     let viewModel: MessagingViewModel(publisher: aPublisher)
    
     viewModel.memoryOptionsProvider = { _, _, _ in
        return MemoryOptions(merge: true, memory: User(firstName: "John", lastName: "Doe"))
     }
    

    Declaration

    Swift

    public var memoryOptionsProvider: ((String?, PostbackType?, PostbackData?) -> MemoryOptions)?

Lifecycle

Public functions

  • Update the current messaging publisher with a new one. The existing model will be cleared.

    Declaration

    Swift

    public func updatePublisher<P>(_ publisher: P) where P : MessagingPublisher

    Parameters

    publisher

    New publisher to bind to this view model.

  • Undocumented

    Declaration

    Swift

    public func postMessage(text: String)
  • Undocumented

    Declaration

    Swift

    public func postMessage(type: PostbackType, postbackData: PostbackData)
  • If you have your own View, you have to call this function when your view appear. It will be called when AssistantView appears.

    Declaration

    Swift

    public func load()
  • If you want to make sure this class gets deallocated correctly, you must cancel the subscriptions from the publisher. Call this function to do so.

    Declaration

    Swift

    public func cancelSubscriptions()