# Mini-Program Custom Inline Components

# 1. Creating Custom Inline Components

Supported App Types

Mini-Program✅ Mini-Game🚫 H5 Application🚫

When the SDK receives a create event for a custom inline component, it triggers the FATClient's embedComponentDelegate delegate method - (void)onCreateEmbedComponent:(FATEmbedComponentInfo *)componentInfo completion:(void (^)(UIView<FATCustomEmbedViewProtocol> * _Nullable componentView))completion. The Host-App implements this method and can return a custom native component in the completion callback. The custom native component must implement the FATCustomEmbedViewProtocol protocol. The SDK will then render this component inline on the page.

- (void)onCreateEmbedComponent:(FATEmbedComponentInfo *)componentInfo completion:(void (^)(UIView * _Nullable componentView))completion {
    CGFloat top =  0;
    CGFloat left =  0;
    CGFloat width = componentInfo.width;
    CGFloat height = componentInfo.height;
    FATComponentView *componentView = [[FATComponentView alloc]initWithFrame:CGRectMake(left , top, width, height)];
    [componentView setComponentInfo:componentInfo];
    completion(componentView);
}

# 2. Events for Custom Inline Components

Custom inline components have update events, delete events, and events sent from the Mini-Program to the custom inline component. The custom inline component can implement the FATCustomEmbedViewProtocol protocol methods to receive and handle these events.

Supported App Types

Mini-Program✅ Mini-Game🚫 H5 Application🚫

/// Custom component update
/// - Parameters:
///   - componentInfo: Parameters for the component update
///   - completion: Update completion callback, must be triggered
- (void)onUpdate:(FATEmbedComponentInfo *)componentInfo withCompletion:(void (^)(void))completion {
    // Display data information
    NSString *dataText = @"Data Information:\n";
    if (componentInfo.dataDic && [componentInfo.dataDic isKindOfClass:[NSDictionary class]]) {
        for (NSString *key in componentInfo.dataDic) {
            id value = componentInfo.dataDic[key];
            dataText = [dataText stringByAppendingFormat:@"%@: %@\n", key, value];
        }
    } else {
        dataText = [dataText stringByAppendingString:@"No data information available"];
    }
    self.dataTextView.text = dataText;
    
    // Call the completion callback
    completion();
}



/// Custom component is deleted. You can perform cleanup logic in this method.
/// - Parameter completion: Completion callback, must be triggered
- (void)onRemoveWithComplete:(void (^)(void))completion {
    completion();
}


/// Receives an event sent from the Mini-Program to the custom component
/// - Parameters:
///   - event: The event
///   - completion: Processing completion callback, must be triggered, callback data can be nil
- (void)receiveComponentEvent:(FATEmbedComponentEvent *)event completion:(void (^)(NSDictionary * _Nullable))completion {
    completion(@{@"test":@"abc"});
}

# 3. Host-App Sending Events to Mini-Program Custom Inline Components

Supported App Types

Mini-Program✅ Mini-Game🚫 H5 Application🚫

The Host-App can send events to the Mini-Program's custom inline component objects via the embedComponentManager object of FATClient.

/// Sends a custom component event to the Mini-Program
/// - Parameters:
///   - eventName: Event name
///   - appletId: Mini-Program ID; if not provided, the event will be sent to the currently running Mini-Program
///   - paramString: Event parameters
///   - pageId: Page ID; if not provided, the current page will be used
///   - componentId: The component's domId
///   - handler: Event result callback
- (void)sendEvent:(NSString *)eventName applet:(NSString *)appletId paramString:(NSString *)paramString pageId:(NSNumber *)pageId componentDomId:(NSString *) componentDomId handler:(void (^)(id result, NSError *error))handler;

# 4. Host-App Retrieving and Deleting Custom Inline Components

Supported App Types

Mini-Program✅ Mini-Game🚫 H5 Application🚫

The Host-App can retrieve or delete custom inline components via the embedComponentManager object of FATClient. Note: It is recommended that the Host-App does not hold references to custom inline components. The SDK holds the custom inline components on the Mini-Program page object. When the page is destroyed, the custom inline components will also be destroyed.

/// Retrieves a custom inline component
/// - Parameters:
///   - appletId: Mini-Program ID; if not provided, inline components will be retrieved from the current Mini-Program
///   - pageId: Page ID; if not provided, inline components will be retrieved from the topmost page of the Mini-Program
///   - componentId: Inline component ID
- (UIView *)getEmbedComponent:(NSString * _Nullable)appletId pageId:(NSString * _Nullable)pageId componentId:(NSString *)componentId;

/// Host-App deletes a custom inline component
/// - Parameters:
///   - appletId: Mini-Program ID; if not provided, the inline component in the current Mini-Program will be deleted
///   - pageId: Page ID; if not provided, the inline component in the topmost page of the Mini-Program will be deleted
///   - componentId: Inline component ID
- (BOOL)removeEmbedComponent:(NSString * _Nullable)appletId pageId:(NSString * _Nullable)pageId componentId:(NSString *)componentId;
© FinClip with ❤ , Since 2017