-
Notifications
You must be signed in to change notification settings - Fork 10
[Feature] Add multi-platform safety #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,47 @@ | ||
| import 'dart:async'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:flutter/foundation.dart' show kIsWeb; | ||
| import 'package:flutter/services.dart'; | ||
| import 'package:gaimon/ahap_to_waveform_converter.dart'; | ||
|
|
||
| class Gaimon { | ||
| static const MethodChannel _channel = MethodChannel('gaimon'); | ||
|
|
||
| static bool get _isAndroid => !kIsWeb && Platform.isAndroid; | ||
| static bool get _isIOS => !kIsWeb && Platform.isIOS; | ||
| static bool get _supportsMethodChannel => _isAndroid || _isIOS; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe adding the kIsWeb in this condition is better?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @istornz This package only supports Android and iOS, so I'm not sure why adding a |
||
|
|
||
| /// check if the device can vibrate or not | ||
| static Future<bool> get canSupportsHaptic async { | ||
| return await _channel.invokeMethod('canSupportsHaptic'); | ||
| if (!_supportsMethodChannel) return false; | ||
| try { | ||
| return await _channel.invokeMethod<bool>('canSupportsHaptic') ?? false; | ||
| } catch (_) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /// generate a selection impact vibration | ||
| static void selection() => HapticFeedback.selectionClick(); | ||
|
|
||
| /// generate an error impact vibration | ||
| static void error() => _channel.invokeMethod('error'); | ||
| static void error() { | ||
| if (!_supportsMethodChannel) return; | ||
| _channel.invokeMethod('error'); | ||
| } | ||
|
|
||
| /// generate a success impact vibration | ||
| static void success() => _channel.invokeMethod('success'); | ||
| static void success() { | ||
| if (!_supportsMethodChannel) return; | ||
| _channel.invokeMethod('success'); | ||
| } | ||
|
|
||
| /// generate a warning impact vibration | ||
| static void warning() => _channel.invokeMethod('warning'); | ||
| static void warning() { | ||
| if (!_supportsMethodChannel) return; | ||
| _channel.invokeMethod('warning'); | ||
| } | ||
|
|
||
| /// generate a heavy impact vibration | ||
| static void heavy() => HapticFeedback.heavyImpact(); | ||
|
|
@@ -34,32 +53,50 @@ class Gaimon { | |
| static void light() => HapticFeedback.lightImpact(); | ||
|
|
||
| /// generate a rigid impact vibration | ||
| static void rigid() => _channel.invokeMethod('rigid'); | ||
| static void rigid() { | ||
| if (!_supportsMethodChannel) return; | ||
| _channel.invokeMethod('rigid'); | ||
| } | ||
|
|
||
| /// generate a soft impact vibration | ||
| static void soft() => _channel.invokeMethod('soft'); | ||
| static void soft() { | ||
| if (!_supportsMethodChannel) return; | ||
| _channel.invokeMethod('soft'); | ||
| } | ||
|
|
||
| /// generate a custom pattern impact vibration | ||
| static void patternFromData(String data) => Platform.isAndroid | ||
| ? _patternFromAhapToWaveform(data) | ||
| : _channel.invokeMethod('pattern', {'data': data}); | ||
| static void patternFromData(String data) { | ||
| if (!_supportsMethodChannel) return; | ||
| if (_isAndroid) { | ||
| _patternFromAhapToWaveform(data); | ||
| } else { | ||
| _channel.invokeMethod('pattern', {'data': data}); | ||
| } | ||
| } | ||
|
|
||
| static void _patternFromAhapToWaveform(String data) { | ||
| final waveform = ahapToWaveform(data); | ||
| patternFromWaveForm(waveform.timings, waveform.amplitudes, waveform.repeat); | ||
| } | ||
|
|
||
| /// stop any ongoing vibration | ||
| static void stop() => _channel.invokeMethod('stop'); | ||
| static void stop() { | ||
| if (!_supportsMethodChannel) return; | ||
| _channel.invokeMethod('stop'); | ||
| } | ||
|
|
||
| /// generate a custom pattern impact vibration from waveform (android only) | ||
| static void patternFromWaveForm( | ||
| List<int> timings, | ||
| List<int> amplitudes, | ||
| bool repeat, | ||
| ) => _channel.invokeMethod('pattern', { | ||
| 'timings': timings, | ||
| 'amplitudes': amplitudes, | ||
| 'repeat': repeat, | ||
| }); | ||
| ) { | ||
| if (!_supportsMethodChannel) return; | ||
| _channel.invokeMethod('pattern', { | ||
| 'timings': timings, | ||
| 'amplitudes': amplitudes, | ||
| 'repeat': repeat, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:gaimon/gaimon.dart'; | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| group('Gaimon Multi-platform Safety Tests', () { | ||
| test('canSupportsHaptic returns false on unsupported platform (macOS/test environment)', () async { | ||
| final supportsHaptic = await Gaimon.canSupportsHaptic; | ||
| expect(supportsHaptic, isFalse); | ||
| }); | ||
|
|
||
| test('calling haptic actions does not throw on unsupported platforms', () { | ||
| expect(() => Gaimon.selection(), returnsNormally); | ||
| expect(() => Gaimon.error(), returnsNormally); | ||
| expect(() => Gaimon.success(), returnsNormally); | ||
| expect(() => Gaimon.warning(), returnsNormally); | ||
| expect(() => Gaimon.heavy(), returnsNormally); | ||
| expect(() => Gaimon.medium(), returnsNormally); | ||
| expect(() => Gaimon.light(), returnsNormally); | ||
| expect(() => Gaimon.rigid(), returnsNormally); | ||
| expect(() => Gaimon.soft(), returnsNormally); | ||
| expect(() => Gaimon.patternFromData('{}'), returnsNormally); | ||
| expect(() => Gaimon.patternFromWaveForm([100], [128], false), returnsNormally); | ||
| expect(() => Gaimon.stop(), returnsNormally); | ||
| }); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is enough, same for iOS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @istornz I'm not sure I'm following - web doesn't support the
PlatformAPI - so why wouldn't we add this before a native platform check?