Skip to content

Commit

Permalink
feat: embedded ios view
Browse files Browse the repository at this point in the history
  • Loading branch information
Mastersam07 committed Oct 5, 2024
1 parent 7174b9a commit ba26dee
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 49 deletions.
68 changes: 19 additions & 49 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
Expand All @@ -14,7 +15,7 @@ class MyApp extends StatelessWidget {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'LiveChat',
home: Support(),
home: EmbeddedChatWidget(),
);
}
}
Expand Down Expand Up @@ -248,59 +249,28 @@ class _EmbeddedChatWidgetState extends State<EmbeddedChatWidget> {
@override
void initState() {
super.initState();
Livechat.chatEvents.listen((message) {
print('New message: $message');
});
// Livechat.visibilityChanges.listen((isVisible) {
// print('Chat window is visible: $isVisible');
// });
// Livechat.errors.listen((error) {
// print('Error: ${error['errorDescription']}');
// });
// Livechat.uriHandlers.listen((uri) {
// print('Custom URI clicked: $uri');
// });
}

@override
Widget build(BuildContext context) {
Widget embeddedView = UiKitView(
viewType: 'embedded_chat_view',
creationParams: <String, dynamic>{
'licenseNo': '18650673',
},
creationParamsCodec: const StandardMessageCodec(),
);
if (defaultTargetPlatform == TargetPlatform.android) {
embeddedView = AndroidView(
viewType: 'embedded_chat_view',
creationParams: <String, dynamic>{
'licenseNo': '18650673',
},
creationParamsCodec: const StandardMessageCodec(),
);
}
return Scaffold(
body: Expanded(
// child: PlatformViewLink(
// surfaceFactory: (context, controller) {
// return AndroidViewSurface(
// controller: controller as AndroidViewController,
// gestureRecognizers: const <Factory<
// OneSequenceGestureRecognizer>>{},
// hitTestBehavior: PlatformViewHitTestBehavior.opaque,
// );
// },
// onCreatePlatformView: (params) {
// return PlatformViewsService.initSurfaceAndroidView(
// id: params.id,
// viewType: 'embedded_chat_view',
// layoutDirection: TextDirection.ltr,
// creationParams: <String, dynamic>{
// 'licenseNo': '18650673',
// },
// creationParamsCodec: const StandardMessageCodec(),
// onFocus: () {
// params.onFocusChanged(true);
// },
// )
// ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
// ..create();
// },
// viewType: 'embedded_chat_view',
// ),
child: AndroidView(
viewType: 'embedded_chat_view',
creationParams: <String, dynamic>{
'licenseNo': '18650673',
},
creationParamsCodec: const StandardMessageCodec(),
),
),
body: embeddedView,
);
}
}
33 changes: 33 additions & 0 deletions ios/Classes/EmbeddedChatView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Flutter
import LiveChat

class EmbeddedChatView: NSObject, FlutterPlatformView {
private var chatWindow: UIView

init(frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?, binaryMessenger messenger: FlutterBinaryMessenger?) {
self.chatWindow = UIView(frame: frame)
super.init()

let params = args as! [String: Any]
let licenseNo = params["licenseNo"] as? String ?? ""
let groupId = params["groupId"] as? String
let visitorName = params["visitorName"] as? String ?? ""
let visitorEmail = params["visitorEmail"] as? String ?? ""
let customParams = params["customParams"] as? [String: String] ?? [:]

LiveChat.licenseId = licenseNo
LiveChat.groupId = groupId
LiveChat.name = visitorName
LiveChat.email = visitorEmail

for (key, value) in customParams {
LiveChat.setVariable(withKey: key, value: value)
}

LiveChat.presentChat()
}

func view() -> UIView {
return chatWindow
}
}
19 changes: 19 additions & 0 deletions ios/Classes/EmbeddedChatViewFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Flutter
import UIKit

class EmbeddedChatViewFactory: NSObject, FlutterPlatformViewFactory {
private var messenger: FlutterBinaryMessenger

init(messenger: FlutterBinaryMessenger) {
self.messenger = messenger
super.init()
}

func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
return EmbeddedChatView(frame: frame, viewIdentifier: viewId, arguments: args, binaryMessenger: messenger)
}

public func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
return FlutterStandardMessageCodec.sharedInstance()
}
}
7 changes: 7 additions & 0 deletions ios/Classes/SwiftLivechatPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public class SwiftLivechatPlugin: NSObject, FlutterPlugin {
let channel = FlutterMethodChannel(name: "livechatt", binaryMessenger: registrar.messenger())
let instance = SwiftLivechatPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)

let factory = EmbeddedChatViewFactory(messenger: registrar.messenger())
registrar.register(factory, withId: "embedded_chat_view")
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
Expand Down Expand Up @@ -48,6 +51,10 @@ public class SwiftLivechatPlugin: NSObject, FlutterPlugin {

LiveChat.presentChat()
result(nil)

case "clearSession":
LiveChat.clearSession()
result(nil)

default:
result(FlutterMethodNotImplemented)
Expand Down

0 comments on commit ba26dee

Please sign in to comment.