Skip to content

Commit

Permalink
Fix method channel setup and get size method
Browse files Browse the repository at this point in the history
  • Loading branch information
crow committed Aug 8, 2024
1 parent c1ff6d0 commit a5f2227
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
14 changes: 7 additions & 7 deletions example/lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class _HomeState extends State<Home> {
child: Container(
alignment: Alignment.center,
child: Wrap(children: <Widget>[
Center(
child:EmbeddedView(embeddedId: "hundredpxhundredpx"),
Center(
child:EmbeddedView(embeddedId: "test"),
),
Image.asset(
'assets/airship.png',
Expand All @@ -48,11 +48,11 @@ class _HomeState extends State<Home> {
bool pushEnabled = snapshot.data ?? false;
enableNotificationsButton =
Center(child: NotificationsEnabledButton(
onPressed: () {
Airship.push.setUserNotificationsEnabled(true);
setState(() {});
},
));
onPressed: () {
Airship.push.setUserNotificationsEnabled(true);
setState(() {});
},
));
return Visibility(
visible: !pushEnabled,
child: enableNotificationsButton);
Expand Down
31 changes: 19 additions & 12 deletions ios/Classes/AirshipEmbeddedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Foundation
import AirshipKit
import SwiftUI

class AirshipEmbeddedViewFactory : NSObject, FlutterPlatformViewFactory {
let registrar : FlutterPluginRegistrar
class AirshipEmbeddedViewFactory: NSObject, FlutterPlatformViewFactory {
let registrar: FlutterPluginRegistrar

init(_ registrar: FlutterPluginRegistrar) {
self.registrar = registrar
Expand All @@ -18,16 +18,15 @@ class AirshipEmbeddedViewFactory : NSObject, FlutterPlatformViewFactory {
}
}

/// The Flutter wrapper for the Airship embedded view
class AirshipEmbeddedViewWrapper : NSObject, FlutterPlatformView {
class AirshipEmbeddedViewWrapper: NSObject, FlutterPlatformView {
private static let embeddedIdKey: String = "embeddedId"

@ObservedObject
var viewModel = FlutterAirshipEmbeddedView.ViewModel()

public var viewController: UIViewController?

let channel : FlutterMethodChannel
let channel: FlutterMethodChannel
private var _view: UIView

init(frame: CGRect, viewId: Int64, registrar: FlutterPluginRegistrar, args: Any?) {
Expand All @@ -53,14 +52,22 @@ class AirshipEmbeddedViewWrapper : NSObject, FlutterPlatformView {

rootView.viewModel.size = frame.size
}

channel.setMethodCallHandler { [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) in
self?.handle(call, result: result)
}
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) async {
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getSize":
let width = _view.bounds.width
let height = _view.bounds.height
result(["width": width, "height": height])
default:
result(FlutterError(code:"UNAVAILABLE",
message:"Unknown method: \(call.method)",
details:nil))
result(FlutterError(code: "UNAVAILABLE",
message: "Unknown method: \(call.method)",
details: nil))
}
}

Expand All @@ -71,7 +78,7 @@ class AirshipEmbeddedViewWrapper : NSObject, FlutterPlatformView {

struct FlutterAirshipEmbeddedView: View {
@ObservedObject
var viewModel:ViewModel
var viewModel: ViewModel

var body: some View {
if let embeddedID = viewModel.embeddedID {
Expand All @@ -95,14 +102,14 @@ struct FlutterAirshipEmbeddedView: View {

var height: CGFloat {
guard let height = self.size?.height, height > 0 else {
return try! AirshipUtils.mainWindow()?.screen.bounds.height ?? 420
return (try? AirshipUtils.mainWindow()?.screen.bounds.height) ?? 420
}
return height
}

var width: CGFloat {
guard let width = self.size?.width, width > 0 else {
return try! AirshipUtils.mainWindow()?.screen.bounds.width ?? 420
return (try? AirshipUtils.mainWindow()?.screen.bounds.width) ?? 420
}
return width
}
Expand Down
18 changes: 16 additions & 2 deletions lib/src/airship_embedded_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,24 @@ class EmbeddedView extends StatefulWidget {
class EmbeddedViewState extends State<EmbeddedView> {
late MethodChannel _channel;

double _nativeViewWidth = 0;
double _nativeViewHeight = 0;

@override
void initState() {
super.initState();
_channel = MethodChannel('com.airship.flutter/EmbeddedView_${widget.embeddedId}');
_channel.setMethodCallHandler(_methodCallHandler);
}

Future<void> _getNativeViewSize() async {
try {
final size = await _channel.invokeMethod<Map>('getSize');
setState(() {
_nativeViewWidth = size?['width'];
_nativeViewHeight = size?['height'];
});
} on PlatformException catch (e) {
print("Failed to get native view size: '${e.message}'.");
}
}

Future<void> _methodCallHandler(MethodCall call) async {
Expand All @@ -40,6 +53,7 @@ class EmbeddedViewState extends State<EmbeddedView> {
Future<void> _onPlatformViewCreated(int id) async {
_channel = MethodChannel('com.airship.flutter/EmbeddedView_$id');
_channel.setMethodCallHandler(_methodCallHandler);
_getNativeViewSize();
}

/// Fall back to screen-sized constraints when constraints can be inferred
Expand Down

0 comments on commit a5f2227

Please sign in to comment.