Skip to content

Commit

Permalink
🔨 Code Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-chavda authored and aditya-css committed Sep 13, 2023
1 parent 2146540 commit b81424f
Show file tree
Hide file tree
Showing 26 changed files with 932 additions and 874 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/**/gradle.wrapper/
**/android/.gradle
**/android/captures/
**/android/gradlew
Expand Down Expand Up @@ -76,4 +76,4 @@ ios/.generated/

.idea/instapk.xml
instapk.log*
pubspec.lock
pubspec.lock
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ import 'package:flutter_credit_card/flutter_credit_card.dart';
),
```

*Floating Card*

```dart
CreditCardWidget(
shouldFloat: true,
shouldAddShadow: true,
shouldAddGlare: true,
);
```
> NOTE: Currently the floating card animation is not supported on mobile platform browsers.
4. Adding CreditCardForm

```dart
Expand Down
16 changes: 2 additions & 14 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.3.0'
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -25,7 +25,7 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 31
compileSdk 31

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -49,16 +49,4 @@ android {
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.mockito:mockito-core:5.0.0'
}

testOptions {
unitTests.all {
useJUnitPlatform()

testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
outputs.upToDateWhen {false}
showStandardStreams = true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,68 +1,116 @@
package com.simform.flutter_credit_card

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.hardware.Sensor
import android.hardware.SensorManager
import android.os.Build
import android.view.Display
import android.view.WindowManager
import androidx.annotation.ChecksSdkIntAtLeast

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler

private const val GYROSCOPE_CHANNEL_NAME = "com.simform.flutter_credit_card/gyroscope"
private const val METHOD_CHANNEL_NAME = "com.simform.flutter_credit_card"

@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.R)
private val isAtLeastOsR: Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R

/** FlutterCreditCardPlugin */
class FlutterCreditCardPlugin: FlutterPlugin {
private lateinit var gyroscopeChannel: EventChannel

private lateinit var methodChannel: MethodChannel

private lateinit var gyroScopeStreamHandler: StreamHandlerImpl

override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
setupEventChannels(binding.applicationContext, binding.binaryMessenger)
setupMethodChannel(binding.applicationContext, binding.binaryMessenger)
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
teardownEventChannels()
teardownMethodChannel()
}

private fun setupEventChannels(context: Context, messenger: BinaryMessenger) {
val sensorsManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager

gyroscopeChannel = EventChannel(messenger, GYROSCOPE_CHANNEL_NAME)
gyroScopeStreamHandler = com.simform.flutter_credit_card.StreamHandlerImpl(
sensorsManager,
Sensor.TYPE_GYROSCOPE,
)
gyroscopeChannel.setStreamHandler(gyroScopeStreamHandler)
}

private fun teardownEventChannels() {
gyroscopeChannel.setStreamHandler(null)
gyroScopeStreamHandler.onCancel(null)
}

private fun setupMethodChannel(context: Context, messenger: BinaryMessenger) {
methodChannel = MethodChannel(messenger, METHOD_CHANNEL_NAME)
methodChannel.setMethodCallHandler { call, result ->
if (call.method == "isGyroscopeAvailable") {
val packageManager: PackageManager = context.packageManager
val gyroExists =
packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE)
result.success(gyroExists)
}
class FlutterCreditCardPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
private var activity: Activity? = null
private lateinit var context: Context

private lateinit var gyroscopeChannel: EventChannel
private lateinit var methodChannel: MethodChannel
private lateinit var gyroscopeStreamHandler: GyroscopeStreamHandler

override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
context = binding.applicationContext
initializeChannels(binding.binaryMessenger)
}
}

private fun teardownMethodChannel() {
methodChannel.setMethodCallHandler(null)
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) =
disposeChannels()

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = if (isAtLeastOsR) binding.activity else null
}

override fun onDetachedFromActivity() {
activity = null
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activity = if (isAtLeastOsR) binding.activity else null
}

override fun onDetachedFromActivityForConfigChanges() {
activity = null
}

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
"initiateEvents" -> {
gyroEventChannelSetup()
result.success(null)
}

"isGyroscopeAvailable" -> result.success(hasGyroAvailability())

"cancelEvents" -> {
gyroEventChannelDisposal()
result.success(null)
}

else -> result.notImplemented()
}
}

private fun initializeChannels(messenger: BinaryMessenger) {
gyroscopeChannel = EventChannel(messenger, GYROSCOPE_CHANNEL_NAME)
gyroEventChannelSetup()
methodChannel = MethodChannel(messenger, METHOD_CHANNEL_NAME)
methodChannel.setMethodCallHandler(this)
}

private fun gyroEventChannelSetup() {
if (!hasGyroAvailability()) return

val sensorsManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager

gyroscopeStreamHandler = GyroscopeStreamHandler(getViewDisplay(), sensorsManager)
gyroscopeChannel.setStreamHandler(gyroscopeStreamHandler)
}

private fun gyroEventChannelDisposal() {
gyroscopeStreamHandler.onCancel(null)
gyroscopeChannel.setStreamHandler(null)
}

private fun disposeChannels() {
gyroEventChannelDisposal()
methodChannel.setMethodCallHandler(null)
activity = null
}

private fun getViewDisplay(): Display? {
if (isAtLeastOsR) {
return activity?.display
} else {
@Suppress("DEPRECATION")
return (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
}
}

companion object {
private const val GYROSCOPE_CHANNEL_NAME = "com.simform.flutter_credit_card/gyroscope"
private const val METHOD_CHANNEL_NAME = "com.simform.flutter_credit_card"
private const val DEFAULT_UPDATE_INTERVAL = SensorManager.SENSOR_DELAY_NORMAL
}
private fun hasGyroAvailability(): Boolean =
context.packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.simform.flutter_credit_card

import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.view.Display
import android.view.Surface
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.EventChannel.EventSink

internal class GyroscopeStreamHandler(
private val display: Display?,
private val sensorManager: SensorManager,
) : EventChannel.StreamHandler {
private var sensorEventListener: SensorEventListener? = null

private val sensor: Sensor by lazy {
sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE)
}

override fun onListen(arguments: Any?, events: EventSink) {
sensorEventListener = createSensorEventListener(events)
// Gyroscope Event sample period set at 60 fps, specified in microseconds.
sensorManager.registerListener(sensorEventListener, sensor, 16666)
}

override fun onCancel(arguments: Any?) = sensorManager.unregisterListener(sensorEventListener)

private fun createSensorEventListener(events: EventSink): SensorEventListener {
return object : SensorEventListener {
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}

override fun onSensorChanged(event: SensorEvent) =
events.success(processForOrientation(event.values))
}
}

private fun processForOrientation(values: FloatArray): DoubleArray {
if (display == null) {
return values.map { it.toDouble() }.toDoubleArray()
} else {
val arr = DoubleArray(3)

val x = values[0].toDouble()
val y = values[1].toDouble()
val z = values[2].toDouble()

when (display.rotation) {
Surface.ROTATION_0,
Surface.ROTATION_180 -> {
arr[0] = x
arr[1] = y
arr[2] = z
}

Surface.ROTATION_270 -> {
arr[0] = y
arr[1] = -x
arr[2] = z
}

Surface.ROTATION_90 -> {
arr[0] = -y
arr[1] = x
arr[2] = z
}
}

return arr
}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit b81424f

Please sign in to comment.