Skip to content

Commit

Permalink
🔨 Code Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-css committed Sep 19, 2023
1 parent 22c5403 commit 4f77665
Show file tree
Hide file tree
Showing 32 changed files with 1,072 additions and 895 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
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ import 'package:flutter_credit_card/flutter_credit_card.dart';
showBackView: isCvvFocused,
cardbgColor: Colors.black,
glassmorphismConfig: Glassmorphism.defaultConfig(),
enableFloatingCard: true,
floatConfig: FloatConfig(
isGlareEnabled: true,
isShadowEnabled: true,
shadowConfig: FloatShadowConfig.preset(),
),
backgroundImage: 'assets/card_bg.png',
labelValidThru: 'VALID\nTHRU',
obscureCardNumber: true,
Expand Down Expand Up @@ -109,6 +115,41 @@ import 'package:flutter_credit_card/flutter_credit_card.dart';
),
```

*Floating Card*
> NOTE: Currently the floating card animation is not supported on mobile platform browsers.
To enable floating card animation, pass `FloatConfig` to `floatConfig` parameter of
`CreditCardWidget`. Passing `null` would disable the floating card animation (default).
You can do so by following ways:

> Passing no value to `shadowConfig` would disable the shadow beneath the credit card widget.
+ Default Shadow Configuration
```dart
CreditCardWidget(
floatConfig: FloatConfig(
isGlareEnabled: true,
isShadowEnabled: true,
shadowConfig: FloatShadowConfig.preset(),
),
);
```

+ Custom Shadow Configuration
```dart
CreditCardWidget(
floatConfig: FloatConfig(
isGlareEnabled: true,
isShadowEnabled: true,
shadowConfig: FloatShadowConfig(
offset: Offset(10, 10),
color: Colors.black84,
blurRadius: 15,
),
),
);
```

4. Adding CreditCardForm

```dart
Expand Down Expand Up @@ -161,7 +202,8 @@ Check out the **example** app in the [example](example) directory or the 'Exampl

## Credit

This package's animation is inspired from from this [Dribbble](https://dribbble.com/shots/2187649-Credit-card-Checkout-flow-AMEX) art.
- This package's flip animation is inspired from this [Dribbble](https://dribbble.com/shots/2187649-Credit-card-Checkout-flow-AMEX) art.
- This package's float animation is inspired from the [Motion](https://pub.dev/packages/motion) flutter package.

## Main Contributors

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
@@ -0,0 +1,7 @@
object Constants {
const val GYROSCOPE_CHANNEL_NAME: String = "com.simform.flutter_credit_card/gyroscope"
const val METHOD_CHANNEL_NAME: String = "com.simform.flutter_credit_card"
const val INITIATE_EVENTS: String = "initiateEvents"
const val IS_GYRO_AVAILABLE: String = "isGyroscopeAvailable"
const val CANCEL_EVENTS: String = "cancelEvents"
}
Original file line number Diff line number Diff line change
@@ -1,68 +1,63 @@
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 com.simform.flutter_credit_card.gyroscope.GyroscopeChannelImpl
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.MethodChannel

/** FlutterCreditCardPlugin */
class FlutterCreditCardPlugin: FlutterPlugin {
private lateinit var gyroscopeChannel: EventChannel
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.R)
private val isAtLeastOsR: Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R

private lateinit var methodChannel: MethodChannel
/** FlutterCreditCardPlugin */
class FlutterCreditCardPlugin : FlutterPlugin, ActivityAware {
private var gyroscopeChannel: GyroscopeChannelImpl? = null
private var activity: Activity? = null

private lateinit var gyroScopeStreamHandler: StreamHandlerImpl
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) =
initializeChannels(binding.applicationContext, binding.binaryMessenger)

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

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
teardownEventChannels()
teardownMethodChannel()
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = if (isAtLeastOsR) binding.activity else null
}

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

gyroscopeChannel = EventChannel(messenger, GYROSCOPE_CHANNEL_NAME)
gyroScopeStreamHandler = com.simform.flutter_credit_card.StreamHandlerImpl(
sensorsManager,
Sensor.TYPE_GYROSCOPE,
)
gyroscopeChannel.setStreamHandler(gyroScopeStreamHandler)
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activity = if (isAtLeastOsR) binding.activity else null
}

private fun teardownEventChannels() {
gyroscopeChannel.setStreamHandler(null)
gyroScopeStreamHandler.onCancel(null)
}
override fun onDetachedFromActivityForConfigChanges() {
activity = 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)
}
private fun initializeChannels(context: Context, messenger: BinaryMessenger) {
gyroscopeChannel = GyroscopeChannelImpl(context, messenger) { getViewDisplay(context) }
}
}

private fun teardownMethodChannel() {
methodChannel.setMethodCallHandler(null)
}
private fun getViewDisplay(context: Context): 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 disposeChannels() {
gyroscopeChannel?.dispose()
gyroscopeChannel = null
activity = null
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.simform.flutter_credit_card.gyroscope

import Constants
import android.content.Context
import android.content.pm.PackageManager
import android.hardware.SensorManager
import android.view.Display
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

internal class GyroscopeChannelImpl(
private val context: Context,
private val messenger: BinaryMessenger,
private val getDisplay: () -> Display?,
) : MethodCallHandler {
private val methodChannel: MethodChannel
private var eventChannel: EventChannel? = null
private var streamHandler: GyroscopeStreamHandler? = null

init {
eventStreamSetup()
methodChannel = MethodChannel(messenger, Constants.METHOD_CHANNEL_NAME)
methodChannel.setMethodCallHandler(this)
}

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
Constants.INITIATE_EVENTS -> {
eventStreamSetup()
result.success(null)
}

Constants.IS_GYRO_AVAILABLE -> result.success(hasGyroAvailability())

Constants.CANCEL_EVENTS -> {
eventStreamDisposal()
result.success(null)
}

else -> result.notImplemented()
}
}

fun dispose() {
eventStreamDisposal()
methodChannel.setMethodCallHandler(null)
}

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

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

eventChannel = if (eventChannel == null) EventChannel(
messenger,
Constants.GYROSCOPE_CHANNEL_NAME,
) else eventChannel

streamHandler = GyroscopeStreamHandler(getDisplay(), sensorsManager)
eventChannel!!.setStreamHandler(streamHandler)
}

private fun eventStreamDisposal() {
streamHandler?.onCancel(null)
eventChannel?.setStreamHandler(null)
eventChannel = null
}

private fun hasGyroAvailability(): Boolean =
context.packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE)
}
Loading

0 comments on commit 4f77665

Please sign in to comment.