-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Added floating animation based on gyroscope's data in IOS
- Loading branch information
1 parent
44104c7
commit e8090ba
Showing
11 changed files
with
224 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.idea/ | ||
.vagrant/ | ||
.sconsign.dblite | ||
.svn/ | ||
|
||
.DS_Store | ||
*.swp | ||
profile | ||
|
||
DerivedData/ | ||
build/ | ||
GeneratedPluginRegistrant.h | ||
GeneratedPluginRegistrant.m | ||
|
||
.generated/ | ||
|
||
*.pbxuser | ||
*.mode1v3 | ||
*.mode2v3 | ||
*.perspectivev3 | ||
|
||
!default.pbxuser | ||
!default.mode1v3 | ||
!default.mode2v3 | ||
!default.perspectivev3 | ||
|
||
xcuserdata | ||
|
||
*.moved-aside | ||
|
||
*.pyc | ||
*sync/ | ||
Icon? | ||
.tags* | ||
|
||
/Flutter/Generated.xcconfig | ||
/Flutter/ephemeral/ | ||
/Flutter/flutter_export_environment.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Flutter | ||
import UIKit | ||
import CoreMotion | ||
|
||
private var motionManager: CMMotionManager? | ||
private var eventChannels = [String: FlutterEventChannel]() | ||
private var streamHandlers = [String: FlutterStreamHandler]() | ||
private func initMotionManager() { | ||
if motionManager == nil { | ||
motionManager = CMMotionManager() | ||
} | ||
} | ||
|
||
private func isGyroscopeAvailable() -> Bool { | ||
initMotionManager() | ||
let gyroAvailable = motionManager?.isGyroAvailable ?? false | ||
return gyroAvailable | ||
} | ||
|
||
|
||
public class FlutterCreditCardPlugin: NSObject, FlutterPlugin { | ||
public static func register(with registrar: FlutterPluginRegistrar) { | ||
|
||
let gyroscopeStreamHandlerName = "com.simform.flutter_credit_card/gyroscope" | ||
let gyroscopeStreamHandler = MTGyroscopeStreamHandler() | ||
streamHandlers[gyroscopeStreamHandlerName] = gyroscopeStreamHandler | ||
|
||
let gyroscopeChannel = FlutterEventChannel(name: gyroscopeStreamHandlerName, binaryMessenger: registrar.messenger()) | ||
gyroscopeChannel.setStreamHandler(gyroscopeStreamHandler) | ||
eventChannels[gyroscopeStreamHandlerName] = gyroscopeChannel | ||
|
||
|
||
let channel = FlutterMethodChannel(name: "com.simform.flutter_credit_card", binaryMessenger: registrar.messenger()) | ||
let instance = FlutterCreditCardPlugin() | ||
registrar.addMethodCallDelegate(instance, channel: channel) | ||
} | ||
|
||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { | ||
switch call.method { | ||
case "isGyroscopeAvailable": | ||
let avaialble = isGyroscopeAvailable() | ||
result(avaialble) | ||
default: | ||
result(FlutterMethodNotImplemented) | ||
} | ||
} | ||
} | ||
|
||
public class MTGyroscopeStreamHandler: NSObject, FlutterStreamHandler { | ||
|
||
public func onListen(withArguments arguments: Any?, eventSink sink: @escaping FlutterEventSink) -> FlutterError? { | ||
initMotionManager() | ||
motionManager?.startGyroUpdates(to: OperationQueue()){ (gyroData, error) in | ||
if let rotationRate = gyroData?.rotationRate { | ||
sink([rotationRate.x,rotationRate.y,rotationRate.z]) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
// Add the timer to the current run loop. | ||
public func onCancel(withArguments arguments: Any?) -> FlutterError? { | ||
motionManager?.stopGyroUpdates() | ||
return FlutterError() | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# | ||
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html. | ||
# Run `pod lib lint flutter_credit_card.podspec` to validate before publishing. | ||
# | ||
Pod::Spec.new do |s| | ||
s.name = 'flutter_credit_card' | ||
s.version = '0.0.1' | ||
s.summary = 'A new Flutter plugin project.' | ||
s.description = <<-DESC | ||
A new Flutter plugin project. | ||
DESC | ||
s.homepage = 'http://example.com' | ||
s.license = { :file => '../LICENSE' } | ||
s.author = { 'Your Company' => '[email protected]' } | ||
s.source = { :path => '.' } | ||
s.source_files = 'Classes/**/*' | ||
s.dependency 'Flutter' | ||
s.platform = :ios, '11.0' | ||
|
||
# Flutter.framework does not contain a i386 slice. | ||
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' } | ||
s.swift_version = '5.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
lib/floating_card_setup/floating_interface/floating_interface_base.dart
This file was deleted.
Oops, something went wrong.
73 changes: 0 additions & 73 deletions
73
lib/floating_card_setup/floating_interface/floating_native_interface.dart
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...p/helper_widgets/glare_effect_widget.dart → ...ating_card_setup/glare_effect_widget.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,72 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/services.dart'; | ||
|
||
import 'floating_card_setup/floating_event.dart'; | ||
import 'flutter_credit_card_platform_interface.dart'; | ||
|
||
/// An implementation of [FlutterCreditCardPlatform] that uses method channels. | ||
class MethodChannelFlutterCreditCard extends FlutterCreditCardPlatform { | ||
/// The method channel used to interact with the native platform. | ||
@visibleForTesting | ||
final methodChannel = const MethodChannel('flutter_credit_card'); | ||
static EventChannel? _gyroscopeEventChannel; | ||
|
||
static MethodChannel? _methodChannel; | ||
|
||
static Stream<FloatingEvent>? _gyroscopeStream; | ||
|
||
@override | ||
bool get isSafariMobile => false; | ||
|
||
static bool _isGyroscopeAvailable = true; | ||
|
||
@override | ||
bool get isGyroscopeAvailable => _isGyroscopeAvailable; | ||
|
||
@override | ||
bool get isPermissionGranted => false; | ||
|
||
@override | ||
bool get isPermissionRequired => false; | ||
|
||
@override | ||
Future<String?> getPlatformVersion() async { | ||
final version = await methodChannel.invokeMethod<String>('getPlatformVersion'); | ||
return version; | ||
Stream<FloatingEvent>? get floatingStream { | ||
try { | ||
_gyroscopeStream ??= _gyroscopeEventChannel | ||
?.receiveBroadcastStream() | ||
.map<FloatingEvent>((dynamic event) { | ||
final List<double> list = event.cast<double>(); | ||
return FloatingEvent( | ||
type: FloatingType.gyroscope, x: list[0], y: list[1], z: list[2]); | ||
}); | ||
_gyroscopeStream?.listen((FloatingEvent event) {}); | ||
return _gyroscopeStream as Stream<FloatingEvent>; | ||
} catch (e) { | ||
// If a PlatformException is thrown, the plugin is not available on the device. | ||
_isGyroscopeAvailable = false; | ||
return null; | ||
} | ||
} | ||
|
||
@override | ||
Future<void> initialize() async { | ||
if (Platform.isIOS || Platform.isAndroid) { | ||
_methodChannel ??= const MethodChannel('com.simform.flutter_credit_card'); | ||
|
||
_isGyroscopeAvailable = | ||
await _methodChannel!.invokeMethod<dynamic>('isGyroscopeAvailable') ?? | ||
false; | ||
|
||
_gyroscopeEventChannel ??= | ||
const EventChannel('com.simform.flutter_credit_card/gyroscope'); | ||
|
||
} else if (Platform.isMacOS || Platform.isLinux || Platform.isWindows) { | ||
// Desktop platforms should not use the gyroscope events. | ||
_isGyroscopeAvailable = false; | ||
} | ||
|
||
return; | ||
} | ||
|
||
|
||
@override | ||
Future<bool> requestPermission() async => true; | ||
} |
Oops, something went wrong.