Skip to content

Commit a9c9f16

Browse files
jung hyun kojung hyun ko
jung hyun ko
authored and
jung hyun ko
committed
ios test done
1 parent 15f688e commit a9c9f16

File tree

9 files changed

+196
-32
lines changed

9 files changed

+196
-32
lines changed

example/ios/Runner.xcodeproj/project.pbxproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@
339339
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
340340
GCC_WARN_UNUSED_FUNCTION = YES;
341341
GCC_WARN_UNUSED_VARIABLE = YES;
342-
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
342+
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
343343
MTL_ENABLE_DEBUG_INFO = NO;
344344
SDKROOT = iphoneos;
345345
SUPPORTED_PLATFORMS = iphoneos;
@@ -417,7 +417,7 @@
417417
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
418418
GCC_WARN_UNUSED_FUNCTION = YES;
419419
GCC_WARN_UNUSED_VARIABLE = YES;
420-
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
420+
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
421421
MTL_ENABLE_DEBUG_INFO = YES;
422422
ONLY_ACTIVE_ARCH = YES;
423423
SDKROOT = iphoneos;
@@ -466,7 +466,7 @@
466466
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
467467
GCC_WARN_UNUSED_FUNCTION = YES;
468468
GCC_WARN_UNUSED_VARIABLE = YES;
469-
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
469+
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
470470
MTL_ENABLE_DEBUG_INFO = NO;
471471
SDKROOT = iphoneos;
472472
SUPPORTED_PLATFORMS = iphoneos;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina6_1" orientation="portrait" appearance="light"/>
34
<dependencies>
45
<deployment identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
7+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
68
</dependencies>
79
<scenes>
810
<!--Flutter View Controller-->
@@ -14,13 +16,14 @@
1416
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
1517
</layoutGuides>
1618
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
17-
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
19+
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
1820
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
19-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
21+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
2022
</view>
2123
</viewController>
2224
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
2325
</objects>
26+
<point key="canvasLocation" x="18" y="-20"/>
2427
</scene>
2528
</scenes>
2629
</document>

example/lib/main.dart

+26-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import 'package:flutter/material.dart';
33
import 'dart:async';
44

5-
import 'package:flutter/services.dart';
5+
//import 'package:flutter/services.dart';
66
import 'package:proximity_sensor/proximity_sensor.dart';
77

88
void main() {
@@ -15,7 +15,11 @@ class MyApp extends StatefulWidget {
1515
}
1616

1717
class _MyAppState extends State<MyApp> {
18-
String _platformVersion = 'Unknown';
18+
//String _platformVersion = 'Unknown';
19+
List<StreamSubscription<dynamic>> _streamSubscriptions =
20+
<StreamSubscription<dynamic>>[];
21+
22+
bool _proximityOn = false;
1923

2024
@override
2125
void initState() {
@@ -25,12 +29,24 @@ class _MyAppState extends State<MyApp> {
2529

2630
// Platform messages are asynchronous, so we initialize in an async method.
2731
Future<void> initPlatformState() async {
28-
String platformVersion;
32+
//TODO null check. --> ProximitySensor.proximityEvents
33+
_streamSubscriptions
34+
.add(ProximitySensor.proximityEvents.listen((ProximityEvent event) {
35+
setState(() {
36+
_proximityOn = event.getValue();
37+
print("in main.dart : " + _proximityOn.toString());
38+
});
39+
}));
40+
41+
/*
42+
//String platformVersion;
43+
bool proximityOn = false;
2944
// Platform messages may fail, so we use a try/catch PlatformException.
3045
try {
31-
platformVersion = await ProximitySensor.platformVersion;
46+
//platformVersion = await ProximitySensor.platformVersion;
47+
//proximityOn = await ProximitySensor.proximityOn;
3248
} on PlatformException {
33-
platformVersion = 'Failed to get platform version.';
49+
//TODO
3450
}
3551
3652
// If the widget was removed from the tree while the asynchronous platform
@@ -39,19 +55,21 @@ class _MyAppState extends State<MyApp> {
3955
if (!mounted) return;
4056
4157
setState(() {
42-
_platformVersion = platformVersion;
58+
//_platformVersion = platformVersion;
59+
_proximityOn = proximityOn;
4360
});
61+
*/
4462
}
4563

4664
@override
4765
Widget build(BuildContext context) {
4866
return MaterialApp(
4967
home: Scaffold(
5068
appBar: AppBar(
51-
title: const Text('Plugin example app'),
69+
title: const Text('Proximity Sensor example'),
5270
),
5371
body: Center(
54-
child: Text('Running on: $_platformVersion\n'),
72+
child: Text('proximity : $_proximityOn\n'),
5573
),
5674
),
5775
);

ios/Classes/ProximitySensorPlugin.h

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22

33
@interface ProximitySensorPlugin : NSObject<FlutterPlugin>
44
@end
5+
6+
@interface ProximityStreamHandler : NSObject<FlutterStreamHandler>
7+
@end

ios/Classes/ProximitySensorPlugin.m

+69-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,78 @@
1+
2+
#import <Foundation/Foundation.h>
13
#import "ProximitySensorPlugin.h"
2-
#if __has_include(<proximity_sensor/proximity_sensor-Swift.h>)
3-
#import <proximity_sensor/proximity_sensor-Swift.h>
4-
#else
4+
5+
//#if __has_include(<proximity_sensor/proximity_sensor-Swift.h>)
6+
//#import <proximity_sensor/proximity_sensor-Swift.h>
7+
//#else
58
// Support project import fallback if the generated compatibility header
69
// is not copied when this plugin is created as a library.
710
// https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816
8-
#import "proximity_sensor-Swift.h"
9-
#endif
11+
//#import "proximity_sensor-Swift.h"
12+
//#endif
1013

14+
////////////////////////////////////////////////////////////////////////////////
1115
@implementation ProximitySensorPlugin
1216
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
13-
[SwiftProximitySensorPlugin registerWithRegistrar:registrar];
17+
18+
ProximityStreamHandler* handler = [[ProximityStreamHandler alloc] init];
19+
FlutterEventChannel* channel = [FlutterEventChannel eventChannelWithName:@"proximity_sensor"
20+
binaryMessenger:[registrar messenger]];
21+
[channel setStreamHandler: handler];
22+
//messenger returns a `FlutterBinaryMessenger` for creating Dart/iOS communication
23+
//channels to be used by the plugin.
24+
25+
//---------------------------------------------------------- TODO
26+
//[SwiftProximitySensorPlugin registerWithRegistrar:registrar];
27+
}
28+
@end
29+
30+
////////////////////////////////////////////////////////////////////////////////
31+
NSNotificationCenter* observer;
32+
@implementation ProximityStreamHandler
33+
34+
//Sets up an event stream and begin emitting events.
35+
//Invoked when the first listener is registered with the Stream associated to
36+
//this channel on the Flutter side.
37+
38+
- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events {
39+
40+
UIDevice* device = [UIDevice currentDevice];
41+
//먼저 센서 값을 일단 보내고..
42+
device.proximityMonitoringEnabled = YES;
43+
int onoff = device.proximityState ? 0 : 1 ;
44+
NSMutableData * data = [NSMutableData dataWithCapacity: 1 * sizeof(int)];
45+
[data appendBytes: &onoff length:sizeof(int)];
46+
events([FlutterStandardTypedData typedDataWithInt32:data]); //function pointer..
47+
48+
//queue 에 저장해서 계속 전송하게 한다
49+
NSOperationQueue * sensor_queue = [ NSOperationQueue mainQueue]; //Returns the operation queue associated with the main thread.
50+
observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceProximityStateDidChangeNotification
51+
object:nil
52+
queue:sensor_queue
53+
//queue:nil //TEST XXX
54+
usingBlock: ^(NSNotification* noti){
55+
UIDevice* device = [UIDevice currentDevice];
56+
int onoff = device.proximityState ? 0 : 1 ;
57+
NSLog(@"onoff=%d\n", onoff);
58+
59+
60+
NSMutableData * data = [NSMutableData dataWithCapacity: 1 * sizeof(int)];
61+
[data appendBytes: &onoff length:sizeof(int)];
62+
events([FlutterStandardTypedData typedDataWithInt32:data]); //function pointer..
63+
} ] ;
64+
return nil;
65+
}
66+
67+
//Tears down an event stream.
68+
//Invoked when the last listener is deregistered from the Stream associated to
69+
//this channel on the Flutter side.
70+
- (FlutterError *)onCancelWithArguments:(id)arguments{
71+
UIDevice* device = [UIDevice currentDevice];
72+
device.proximityMonitoringEnabled = NO;
73+
[[NSNotificationCenter defaultCenter] removeObserver:observer
74+
name:UIDeviceProximityStateDidChangeNotification
75+
object:nil];
76+
return nil;
1477
}
1578
@end
+18-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
import Flutter
22
import UIKit
33

4+
//https://flutterdevs.com/blog/creating-a-flutter-plugin-for-android-and-ios-image-gallery/
5+
/*
46
public class SwiftProximitySensorPlugin: NSObject, FlutterPlugin {
5-
public static func register(with registrar: FlutterPluginRegistrar) {
7+
8+
public static func register(with registrar: FlutterPluginRegistrar) {
9+
//UIDevice.current.isProximityMonitoringEnabled = true
10+
611
let channel = FlutterMethodChannel(name: "proximity_sensor", binaryMessenger: registrar.messenger())
712
let instance = SwiftProximitySensorPlugin()
813
registrar.addMethodCallDelegate(instance, channel: channel)
914
}
10-
15+
16+
//////////////////////////////////////////////////////////////////////////////
1117
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
12-
result("iOS " + UIDevice.current.systemVersion)
18+
//result("iOS " + UIDevice.current.systemVersion)
19+
//getProximityOnOff
20+
//result(UIDevice.current.proximityState)
21+
22+
if call.method == "getProximityOnOff" {
23+
result(UIDevice.current.proximityState)
24+
}
25+
26+
//FlutterStreamHandler
1327
}
1428
}
29+
*/

lib/proximity_sensor.dart

+66-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,75 @@
11
// The Dart API for the plugin.
22

33
import 'dart:async';
4-
54
import 'package:flutter/services.dart';
65

6+
////////////////////////////////////////////////////////////////////////////////
7+
//XXX stream 을 받게 추가한것
8+
const EventChannel _stream_channel = EventChannel('proximity_sensor');
9+
10+
class ProximityEvent {
11+
final int onoff;
12+
ProximityEvent(this.onoff);
13+
bool getValue() => onoff == 0 ? true : false;
14+
15+
@override
16+
String toString() => onoff == 0 ? 'true' : 'false';
17+
}
18+
19+
/*
20+
ProximityEvent _listToProximityEvent(int onoff) {
21+
return new ProximityEvent(onoff);
22+
}
23+
*/
24+
25+
/*
26+
Stream<ProximityEvent> get proximityEvents {
27+
_sensor_events = _stream_channel
28+
.receiveBroadcastStream()
29+
//.map((event) => _listToProximityEvent(event.cast<int>()));
30+
.map((event) => new ProximityEvent(event.cast<int>()));
31+
return _sensor_events;
32+
}
33+
*/
34+
35+
////////////////////////////////////////////////////////////////////////////////
736
class ProximitySensor {
8-
static const MethodChannel _channel = const MethodChannel('proximity_sensor');
37+
static Stream<ProximityEvent> _sensor_events;
38+
static Stream<ProximityEvent> get proximityEvents {
39+
_sensor_events = _stream_channel.receiveBroadcastStream()
40+
//.map((event) => _listToProximityEvent(event.cast<int>()));
41+
//.map((event) => new ProximityEvent(event.cast<int>()));
42+
.map((event) {
43+
print("this is dart code --> " + event.toString());
44+
return new ProximityEvent(event.cast<int>()[0]); //XXX XXX XXX
45+
46+
//return new ProximityEvent(event.cast<int>());
47+
//--> list<int,int..>
48+
//XXX XXX XXX error 안됨. list 임.!
49+
//--> Unhandled Exception: type 'CastList<int, int>' is not a subtype of type 'int'
50+
}); //XXX TEST
51+
52+
//print(_sensor_events.toString()); //TEST --> 실행안됨
53+
/*
54+
print("return events...");
55+
print("this is dart code --> " + _sensor_events.toString());
56+
*/
57+
return _sensor_events; //null error 방지 _stream_channel.receiveBroadcastStream() 로 만들어진것을 보내지 않으면 에러 발생
58+
}
59+
/*
60+
//XXX 이게 원래 있던것
61+
//static const MethodChannel _channel = const MethodChannel('proximity_sensor');
62+
63+
// static Future<String> get platformVersion async {
64+
// final String version = await _channel.invokeMethod('getPlatformVersion');
65+
// return version;
66+
// }
967
10-
static Future<String> get platformVersion async {
11-
final String version = await _channel.invokeMethod('getPlatformVersion');
12-
return version;
68+
static Future<bool> get proximityOn async {
69+
// final String version = await _channel.invokeMethod('getPlatformVersion');
70+
// return version;
71+
final bool onoff = await _channel.invokeMethod('getProximityOnOff');
72+
return onoff;
1373
}
74+
*/
1475
}

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: proximity_sensor
2-
description: A new flutter plugin project.
2+
description: sensor. only for proximity.
33
version: 0.0.1
44
author:
55
homepage:

test/proximity_sensor_test.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void main() {
1818
});
1919

2020
test('getPlatformVersion', () async {
21-
expect(await ProximitySensor.platformVersion, '42');
21+
//TODO
22+
//expect(await ProximitySensor.proximityOn .platformVersion, '42');
2223
});
2324
}

0 commit comments

Comments
 (0)