From 1a2a594e1d84df7acf81932663c7658c866ac605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=AA=E8=84=96=E5=AD=90?= Date: Fri, 20 Oct 2023 17:45:24 +0800 Subject: [PATCH] init --- example/lib/main.dart | 31 ++------------------------ ios/Classes/JcPrinterPlugin.m | 9 ++++++++ lib/jc_printer.dart | 4 ++++ lib/jc_printer_method_channel.dart | 6 +++++ lib/jc_printer_platform_interface.dart | 4 ++++ test/jc_printer_test.dart | 10 ++++----- 6 files changed, 30 insertions(+), 34 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index ca45c21..f1fcb56 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'dart:async'; -import 'package:flutter/services.dart'; import 'package:jc_printer/jc_printer.dart'; void main() { @@ -16,35 +15,12 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - String _platformVersion = 'Unknown'; - final _jcPrinterPlugin = JcPrinter(); + final _jcPrinter = JcPrinter(); @override void initState() { super.initState(); - initPlatformState(); - } - - // Platform messages are asynchronous, so we initialize in an async method. - Future initPlatformState() async { - String platformVersion; - // Platform messages may fail, so we use a try/catch PlatformException. - // We also handle the message potentially returning null. - try { - platformVersion = - await _jcPrinterPlugin.getPlatformVersion() ?? 'Unknown platform version'; - } on PlatformException { - platformVersion = 'Failed to get platform version.'; - } - - // If the widget was removed from the tree while the asynchronous platform - // message was in flight, we want to discard the reply rather than calling - // setState to update our non-existent appearance. - if (!mounted) return; - - setState(() { - _platformVersion = platformVersion; - }); + _jcPrinter.connect(); } @override @@ -54,9 +30,6 @@ class _MyAppState extends State { appBar: AppBar( title: const Text('Plugin example app'), ), - body: Center( - child: Text('Running on: $_platformVersion\n'), - ), ), ); } diff --git a/ios/Classes/JcPrinterPlugin.m b/ios/Classes/JcPrinterPlugin.m index 43c6fd6..7713dd1 100644 --- a/ios/Classes/JcPrinterPlugin.m +++ b/ios/Classes/JcPrinterPlugin.m @@ -1,4 +1,5 @@ #import "JcPrinterPlugin.h" +#import "JCAPI.h" @implementation JcPrinterPlugin + (void)registerWithRegistrar:(NSObject*)registrar { @@ -12,6 +13,14 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { if ([@"getPlatformVersion" isEqualToString:call.method]) { result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]); + } else if ([@"connect" isEqualToString:call.method]) { + [JCAPI openPrinter:@"123" completion:^(BOOL isSuccess) { + if (isSuccess) { + result(@"啊叫啊"); + } else { + result(@"版本"); + } + }]; } else { result(FlutterMethodNotImplemented); } diff --git a/lib/jc_printer.dart b/lib/jc_printer.dart index cd30d2f..d323539 100644 --- a/lib/jc_printer.dart +++ b/lib/jc_printer.dart @@ -5,4 +5,8 @@ class JcPrinter { Future getPlatformVersion() { return JcPrinterPlatform.instance.getPlatformVersion(); } + + void connect() { + JcPrinterPlatform.instance.connect(); + } } diff --git a/lib/jc_printer_method_channel.dart b/lib/jc_printer_method_channel.dart index a3091c6..3548e47 100644 --- a/lib/jc_printer_method_channel.dart +++ b/lib/jc_printer_method_channel.dart @@ -14,4 +14,10 @@ class MethodChannelJcPrinter extends JcPrinterPlatform { final version = await methodChannel.invokeMethod('getPlatformVersion'); return version; } + + @override + void connect() async { + final result = await methodChannel.invokeMethod('connect'); + print(result); + } } diff --git a/lib/jc_printer_platform_interface.dart b/lib/jc_printer_platform_interface.dart index e020742..d460658 100644 --- a/lib/jc_printer_platform_interface.dart +++ b/lib/jc_printer_platform_interface.dart @@ -26,4 +26,8 @@ abstract class JcPrinterPlatform extends PlatformInterface { Future getPlatformVersion() { throw UnimplementedError('platformVersion() has not been implemented.'); } + + void connect() { + throw UnimplementedError('connect() has not been implemented.'); + } } diff --git a/test/jc_printer_test.dart b/test/jc_printer_test.dart index d71e24b..d5284be 100644 --- a/test/jc_printer_test.dart +++ b/test/jc_printer_test.dart @@ -2,15 +2,15 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:jc_printer/jc_printer.dart'; import 'package:jc_printer/jc_printer_platform_interface.dart'; import 'package:jc_printer/jc_printer_method_channel.dart'; -import 'package:plugin_platform_interface/plugin_platform_interface.dart'; +// import 'package:plugin_platform_interface/plugin_platform_interface.dart'; -class MockJcPrinterPlatform +/*class MockJcPrinterPlatform with MockPlatformInterfaceMixin implements JcPrinterPlatform { @override Future getPlatformVersion() => Future.value('42'); -} +}*/ void main() { final JcPrinterPlatform initialPlatform = JcPrinterPlatform.instance; @@ -21,8 +21,8 @@ void main() { test('getPlatformVersion', () async { JcPrinter jcPrinterPlugin = JcPrinter(); - MockJcPrinterPlatform fakePlatform = MockJcPrinterPlatform(); - JcPrinterPlatform.instance = fakePlatform; + // MockJcPrinterPlatform fakePlatform = MockJcPrinterPlatform(); + // JcPrinterPlatform.instance = fakePlatform; expect(await jcPrinterPlugin.getPlatformVersion(), '42'); });