From 35b140ff91f03ea02c7b2128cd551f1142f0008a Mon Sep 17 00:00:00 2001
From: BreX900 <brexmaster@gmail.com>
Date: Sun, 19 Nov 2023 21:58:40 +0100
Subject: [PATCH] chore: android code integrity

---
 .../workflows/stripe_terminal_integration.yml |  2 +
 stripe_terminal/android/gradle.properties     |  1 -
 .../stripeterminal/mappings/ChargeMappings.kt |  1 -
 .../mappings/PaymentIntentMappings.kt         |  1 -
 .../stripeterminal/mappings/PaymentMethod.kt  |  1 -
 stripe_terminal/example/lib/main.dart         | 89 ++++++++++++-------
 .../example/lib/utils/permission_utils.dart   |  5 ++
 7 files changed, 65 insertions(+), 35 deletions(-)
 create mode 100644 stripe_terminal/example/lib/utils/permission_utils.dart

diff --git a/.github/workflows/stripe_terminal_integration.yml b/.github/workflows/stripe_terminal_integration.yml
index a96f5c0..d3e215f 100644
--- a/.github/workflows/stripe_terminal_integration.yml
+++ b/.github/workflows/stripe_terminal_integration.yml
@@ -42,6 +42,8 @@ jobs:
           distribution: 'zulu'
       - run: ./gradlew spotlessCheck
         working-directory: stripe_terminal/android
+      - run: ./gradlew :mek_stripe_terminal:assemble
+        working-directory: stripe_terminal/example/android
       
   integration-example:
     runs-on: macos-latest
diff --git a/stripe_terminal/android/gradle.properties b/stripe_terminal/android/gradle.properties
index e39c45c..b3e729d 100644
--- a/stripe_terminal/android/gradle.properties
+++ b/stripe_terminal/android/gradle.properties
@@ -12,6 +12,5 @@
 # org.gradle.parallel=true
 #Fri Jul 21 11:00:59 CEST 2023
 android.enableJetifier=true
-android.enableR8=true
 android.useAndroidX=true
 org.gradle.jvmargs=-Xmx1536M
diff --git a/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/ChargeMappings.kt b/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/ChargeMappings.kt
index 8289312..002dffe 100644
--- a/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/ChargeMappings.kt
+++ b/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/ChargeMappings.kt
@@ -5,7 +5,6 @@ import com.stripe.stripeterminal.external.models.PaymentMethodDetails
 import mek.stripeterminal.api.ChargeApi
 import mek.stripeterminal.api.ChargeStatusApi
 import mek.stripeterminal.api.PaymentMethodDetailsApi
-import mek.stripeterminal.api.toApi
 import mek.stripeterminal.toHashMap
 
 fun Charge.toApi(): ChargeApi {
diff --git a/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/PaymentIntentMappings.kt b/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/PaymentIntentMappings.kt
index 8656b59..d00c040 100644
--- a/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/PaymentIntentMappings.kt
+++ b/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/PaymentIntentMappings.kt
@@ -18,7 +18,6 @@ import mek.stripeterminal.api.PaymentIntentUsageApi
 import mek.stripeterminal.api.PaymentMethodOptionsParametersApi
 import mek.stripeterminal.api.PaymentMethodTypeApi
 import mek.stripeterminal.api.PaymentStatusApi
-import mek.stripeterminal.api.toHost
 import mek.stripeterminal.toHashMap
 
 fun PaymentIntent.toApi(): PaymentIntentApi {
diff --git a/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/PaymentMethod.kt b/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/PaymentMethod.kt
index d8c0609..48a30d2 100644
--- a/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/PaymentMethod.kt
+++ b/stripe_terminal/android/src/main/kotlin/mek/stripeterminal/mappings/PaymentMethod.kt
@@ -2,7 +2,6 @@ package mek.stripeterminal.mappings
 
 import com.stripe.stripeterminal.external.models.PaymentMethod
 import mek.stripeterminal.api.PaymentMethodApi
-import mek.stripeterminal.api.toApi
 import mek.stripeterminal.toHashMap
 
 fun PaymentMethod.toApi(): PaymentMethodApi {
diff --git a/stripe_terminal/example/lib/main.dart b/stripe_terminal/example/lib/main.dart
index 464e4b1..ca168c6 100644
--- a/stripe_terminal/example/lib/main.dart
+++ b/stripe_terminal/example/lib/main.dart
@@ -6,6 +6,7 @@ import 'dart:io';
 import 'package:example/models/discovery_method.dart';
 import 'package:example/models/k.dart';
 import 'package:example/stripe_api.dart';
+import 'package:example/utils/permission_utils.dart';
 import 'package:flutter/material.dart';
 import 'package:mek_stripe_terminal/mek_stripe_terminal.dart';
 import 'package:permission_handler/permission_handler.dart';
@@ -15,19 +16,35 @@ void main() async {
 
   print('Stripe Secret Key: ${StripeApi.secretKey.isNotEmpty}');
 
-  runApp(const MaterialApp(
-    home: MyApp(),
-  ));
+  runApp(const App());
 }
 
-class MyApp extends StatefulWidget {
-  const MyApp({super.key});
+class App extends StatelessWidget {
+  const App({super.key});
 
   @override
-  State<MyApp> createState() => _MyAppState();
+  Widget build(BuildContext context) {
+    return MaterialApp(
+      theme: ThemeData.from(
+        useMaterial3: true,
+        colorScheme: ColorScheme.fromSeed(
+          brightness: Brightness.dark,
+          seedColor: Colors.amber,
+        ),
+      ),
+      home: const HomeScreen(),
+    );
+  }
 }
 
-class _MyAppState extends State<MyApp> {
+class HomeScreen extends StatefulWidget {
+  const HomeScreen({super.key});
+
+  @override
+  State<HomeScreen> createState() => _HomeScreenState();
+}
+
+class _HomeScreenState extends State<HomeScreen> {
   final _api = StripeApi();
   Terminal? _terminal;
 
@@ -71,10 +88,23 @@ class _MyAppState extends State<MyApp> {
     ];
 
     for (final permission in permissions) {
-      final result = await permission.request();
-      print('$permission: $result');
+      final status = await permission.request();
+      print('$permission: $status');
 
-      if (result == PermissionStatus.denied || result == PermissionStatus.permanentlyDenied) return;
+      if (status == PermissionStatus.denied || status == PermissionStatus.permanentlyDenied) {
+        _showSnackBar('Please grant ${permission.name} permission.');
+        return;
+      }
+    }
+
+    for (final service in permissions.whereType<PermissionWithService>()) {
+      final status = await service.serviceStatus;
+      print('$service: $status');
+
+      if (status != ServiceStatus.enabled) {
+        _showSnackBar('Please enable ${service.name} service.');
+        return;
+      }
     }
 
     final terminal = await Terminal.getInstance(
@@ -286,6 +316,8 @@ class _MyAppState extends State<MyApp> {
 
   @override
   Widget build(BuildContext context) {
+    final viewPadding = MediaQuery.viewPaddingOf(context);
+
     final terminal = _terminal;
     final paymentIntent = _paymentIntent;
     final collectingPaymentMethod = _collectingPaymentMethod;
@@ -452,29 +484,24 @@ class _MyAppState extends State<MyApp> {
     return DefaultTabController(
       length: tabs.length,
       child: Scaffold(
-        body: Column(
-          children: [
-            Material(
-              color: Theme.of(context).colorScheme.primary,
-              child: SafeArea(
-                child: TabBar(
-                  isScrollable: true,
-                  tabs: tabs.keys.toList(),
-                ),
-              ),
+        appBar: PreferredSize(
+          preferredSize: Size.fromHeight(64.0 + viewPadding.top),
+          child: Padding(
+            padding: EdgeInsets.only(top: viewPadding.top),
+            child: TabBar(
+              isScrollable: true,
+              tabs: tabs.keys.toList(),
             ),
-            Expanded(
-              child: TabBarView(
-                children: tabs.values.map((e) {
-                  return SingleChildScrollView(
-                    child: Column(
-                      children: e,
-                    ),
-                  );
-                }).toList(),
+          ),
+        ),
+        body: TabBarView(
+          children: tabs.values.map((e) {
+            return SingleChildScrollView(
+              child: Column(
+                children: e,
               ),
-            ),
-          ],
+            );
+          }).toList(),
         ),
       ),
     );
diff --git a/stripe_terminal/example/lib/utils/permission_utils.dart b/stripe_terminal/example/lib/utils/permission_utils.dart
new file mode 100644
index 0000000..95e1c3c
--- /dev/null
+++ b/stripe_terminal/example/lib/utils/permission_utils.dart
@@ -0,0 +1,5 @@
+import 'package:permission_handler/permission_handler.dart';
+
+extension PermissionName on Permission {
+  String get name => toString().split('.').last;
+}