Skip to content

Commit

Permalink
✨ Added floating animation based on gyroscope's data in IOS
Browse files Browse the repository at this point in the history
  • Loading branch information
kavantrivedi authored and aditya-css committed Sep 18, 2023
1 parent 44104c7 commit e8090ba
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 142 deletions.
38 changes: 38 additions & 0 deletions ios/.gitignore
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
68 changes: 68 additions & 0 deletions ios/Classes/FlutterCreditCardPlugin.swift
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()
}

}

23 changes: 23 additions & 0 deletions ios/flutter_credit_card.podspec
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
2 changes: 1 addition & 1 deletion lib/credit_card_background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter_credit_card/floating_card_setup/floating_controller.dart';
import 'package:flutter_credit_card/floating_card_setup/helper_widgets/glare_effect_widget.dart';
import 'package:flutter_credit_card/floating_card_setup/glare_effect_widget.dart';

import 'constants.dart';
import 'floating_card_setup/constants.dart';
Expand Down
7 changes: 4 additions & 3 deletions lib/credit_card_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_credit_card/constants.dart';
import 'package:flutter_credit_card/extension.dart';
import 'package:flutter_credit_card/flutter_credit_card_platform_interface.dart';

import 'credit_card_animation.dart';
import 'credit_card_background.dart';
import 'credit_card_brand.dart';
import 'custom_card_type_icon.dart';
import 'floating_card_setup/floating_controller.dart';
import 'floating_card_setup/floating_event.dart';
import 'floating_card_setup/floating_interface/floating_interface_base.dart';
import 'glassmorphism_config.dart';

const Map<CardType, String> CardTypeIconAsset = <CardType, String>{
Expand Down Expand Up @@ -167,7 +167,8 @@ class CreditCardWidget extends StatefulWidget {

final bool isFloatingAnimationEnabled;

static final FloatingPlatform instance = FloatingPlatform.instance;
static final FlutterCreditCardPlatform instance =
FlutterCreditCardPlatform.instance;

/// floating animation enabled/disabled
@override
Expand Down Expand Up @@ -298,7 +299,7 @@ class _CreditCardWidgetState extends State<CreditCardWidget>
// Apply the damping factor — which may equal 1 and have no effect, if damping is null.
frontFloatingController.x *= frontFloatingController.floatingBackFactor;
frontFloatingController.y *= frontFloatingController.floatingBackFactor;

// Rotate the matrix by the resulting x and y values.
matrix.rotateX(frontFloatingController.x);
matrix.rotateY(frontFloatingController.y);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_credit_card/floating_card_setup/floating_controller.dart';

import '../constants.dart';
import 'constants.dart';

class GlareEffectWidget extends StatelessWidget {
const GlareEffectWidget({
Expand Down
69 changes: 62 additions & 7 deletions lib/flutter_credit_card_method_channel.dart
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;
}
Loading

0 comments on commit e8090ba

Please sign in to comment.