Skip to content

Commit

Permalink
OnyxSdkPenArea: restrict to Onyx devices
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Dec 31, 2022
1 parent febeccb commit 836f4e8
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class OnyxsdkPenPlugin: FlutterPlugin, MethodCallHandler {
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
if (call.method == "isOnyxDevice") {
result.success(android.os.Build.BRAND == "onyx") // todo: check if this is the right value
} else {
result.notImplemented()
}
Expand Down
40 changes: 8 additions & 32 deletions packages/onyxsdk_pen/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:onyxsdk_pen/onyxsdk_pen.dart';
import 'package:onyxsdk_pen/onyxsdk_pen_area.dart';

const String explanation = '''
This text is a child of the OnyxSdkPenArea widget.
If you are running this app on an Onyx device,
you should be able to draw on the screen.
''';

void main() {
runApp(const MyApp());
}
Expand All @@ -17,35 +19,9 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _onyxsdkPenPlugin = OnyxsdkPen();

@override
void initState() {
super.initState();
initPlatformState();
}

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> 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 _onyxsdkPenPlugin.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;
});
}

@override
Expand All @@ -55,9 +31,9 @@ class _MyAppState extends State<MyApp> {
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: OnyxSdkPenArea(
body: const OnyxSdkPenArea(
child: Center(
child: Text('Running on: $_platformVersion\n'),
child: SelectableText(explanation),
),
),
),
Expand Down
27 changes: 0 additions & 27 deletions packages/onyxsdk_pen/example/test/widget_test.dart

This file was deleted.

4 changes: 2 additions & 2 deletions packages/onyxsdk_pen/lib/onyxsdk_pen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import 'onyxsdk_pen_platform_interface.dart';

class OnyxsdkPen {
Future<String?> getPlatformVersion() {
return OnyxsdkPenPlatform.instance.getPlatformVersion();
Future<bool> isOnyxDevice() {
return OnyxsdkPenPlatform.instance.isOnyxDevice();
}
}
29 changes: 26 additions & 3 deletions packages/onyxsdk_pen/lib/onyxsdk_pen_area.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

import 'onyxsdk_pen_platform_interface.dart';

/// Renders a native Android view which uses the Onyx SDK to draw on the screen.
class OnyxSdkPenArea extends StatelessWidget {
class OnyxSdkPenArea extends StatefulWidget {
const OnyxSdkPenArea({
Key? key,
required this.child,
Expand All @@ -16,9 +18,30 @@ class OnyxSdkPenArea extends StatelessWidget {
final Widget child;
// todo: add event handlers

@override
State<OnyxSdkPenArea> createState() => _OnyxSdkPenAreaState();
}

class _OnyxSdkPenAreaState extends State<OnyxSdkPenArea> {
static bool? _isOnyxDevice;
bool get isOnyxDevice {
if (_isOnyxDevice != null) return _isOnyxDevice!;

if (kIsWeb || !Platform.isAndroid) return _isOnyxDevice = false;

// use the platform interface to check if the device is an Onyx device
OnyxsdkPenPlatform.instance.isOnyxDevice().then((isOnyxDevice) {
setState(() {
_isOnyxDevice = isOnyxDevice;
});
});

return _isOnyxDevice = false;
}

@override
Widget build(BuildContext context) {
if (kIsWeb || !Platform.isAndroid) return child;
if (!isOnyxDevice) return widget.child;

// This is used in the platform side to register the view.
const String viewType = 'onyxsdk_pen_area';
Expand All @@ -28,7 +51,7 @@ class OnyxSdkPenArea extends StatelessWidget {
return Stack(
children: [
Positioned.fill(
child: child
child: widget.child
),
Positioned.fill(
child: AndroidView(
Expand Down
6 changes: 3 additions & 3 deletions packages/onyxsdk_pen/lib/onyxsdk_pen_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class MethodChannelOnyxsdkPen extends OnyxsdkPenPlatform {
final methodChannel = const MethodChannel('onyxsdk_pen');

@override
Future<String?> getPlatformVersion() async {
final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
return version;
Future<bool> isOnyxDevice() async {
final isOnyxDevice = await methodChannel.invokeMethod<bool>('isOnyxDevice');
return isOnyxDevice ?? false;
}
}
4 changes: 1 addition & 3 deletions packages/onyxsdk_pen/lib/onyxsdk_pen_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@ abstract class OnyxsdkPenPlatform extends PlatformInterface {
_instance = instance;
}

Future<String?> getPlatformVersion() {
throw UnimplementedError('platformVersion() has not been implemented.');
}
Future<bool> isOnyxDevice() => Future<bool>.value(false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ void main() {

setUp(() {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
return '42';
return false;
});
});

tearDown(() {
channel.setMockMethodCallHandler(null);
});

test('getPlatformVersion', () async {
expect(await platform.getPlatformVersion(), '42');
test('isOnyxDevice', () async {
expect(await platform.isOnyxDevice(), false);
});
}
6 changes: 3 additions & 3 deletions packages/onyxsdk_pen/test/onyxsdk_pen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MockOnyxsdkPenPlatform
implements OnyxsdkPenPlatform {

@override
Future<String?> getPlatformVersion() => Future.value('42');
Future<bool> isOnyxDevice() => Future.value(false);
}

void main() {
Expand All @@ -19,11 +19,11 @@ void main() {
expect(initialPlatform, isInstanceOf<MethodChannelOnyxsdkPen>());
});

test('getPlatformVersion', () async {
test('isOnyxDevice', () async {
OnyxsdkPen onyxsdkPenPlugin = OnyxsdkPen();
MockOnyxsdkPenPlatform fakePlatform = MockOnyxsdkPenPlatform();
OnyxsdkPenPlatform.instance = fakePlatform;

expect(await onyxsdkPenPlugin.getPlatformVersion(), '42');
expect(await onyxsdkPenPlugin.isOnyxDevice(), false);
});
}

0 comments on commit 836f4e8

Please sign in to comment.