From cb1fffa421c46c8e5829a930ea48d6b0a4c85043 Mon Sep 17 00:00:00 2001
From: Muhesh7 <vmspride7@gmail.com>
Date: Wed, 8 Feb 2023 22:30:52 +0530
Subject: [PATCH] refactor: implement singelton instance of intent-action

---
 android/.project                               | 11 +++++++++++
 android/app/.project                           | 11 +++++++++++
 android/app/build.gradle                       |  5 +----
 android/app/src/main/AndroidManifest.xml       | 10 +---------
 android/build.gradle                           |  1 -
 lib/app.dart                                   | 13 +++++++------
 lib/services/app_lifecycle_service.dart        |  6 ++++++
 lib/ui/home/home_gallery_widget.dart           |  4 ----
 lib/ui/home_widget.dart                        |  5 -----
 lib/ui/huge_listview/lazy_loading_gallery.dart | 14 ++++++--------
 lib/ui/viewer/gallery/gallery.dart             |  4 ----
 11 files changed, 43 insertions(+), 41 deletions(-)

diff --git a/android/.project b/android/.project
index 3964dd3f5..3b28f0b1f 100644
--- a/android/.project
+++ b/android/.project
@@ -14,4 +14,15 @@
 	<natures>
 		<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
 	</natures>
+	<filteredResources>
+		<filter>
+			<id>1675831639778</id>
+			<name></name>
+			<type>30</type>
+			<matcher>
+				<id>org.eclipse.core.resources.regexFilterMatcher</id>
+				<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
+			</matcher>
+		</filter>
+	</filteredResources>
 </projectDescription>
diff --git a/android/app/.project b/android/app/.project
index ac485d7c3..8f1d5f8a0 100644
--- a/android/app/.project
+++ b/android/app/.project
@@ -20,4 +20,15 @@
 		<nature>org.eclipse.jdt.core.javanature</nature>
 		<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
 	</natures>
+	<filteredResources>
+		<filter>
+			<id>1675831639783</id>
+			<name></name>
+			<type>30</type>
+			<matcher>
+				<id>org.eclipse.core.resources.regexFilterMatcher</id>
+				<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
+			</matcher>
+		</filter>
+	</filteredResources>
 </projectDescription>
diff --git a/android/app/build.gradle b/android/app/build.gradle
index 69b475196..57bbcb58c 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -79,10 +79,7 @@ android {
         fdroid {
             dimension "default"
             applicationIdSuffix ".fdroid"
-<<<<<<< HEAD
-=======
-            // signingConfig null
->>>>>>> 4615956c (feat[gallery_extention]: integrate handler for gallery pick intent)
+            signingConfig null
         }
     }
 
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 3c042a5a2..ef7502570 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -68,15 +68,7 @@
             </intent-filter>
 
         </activity>
-        <provider
-        android:name="androidx.core.content.FileProvider"
-        android:authorities="${applicationId}.file_provider"
-        android:exported="false"
-        android:grantUriPermissions="true">
-        <meta-data
-            android:name="android.support.FILE_PROVIDER_PATHS"
-            android:resource="@xml/provider_paths" />
-        </provider>
+
         <!-- Don't delete the meta-data below.
              This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
         <meta-data android:name="flutterEmbedding" android:value="2"/>
diff --git a/android/build.gradle b/android/build.gradle
index d2f33daf0..04b667263 100644
--- a/android/build.gradle
+++ b/android/build.gradle
@@ -2,7 +2,6 @@ buildscript {
     ext.kotlin_version = '1.6.21'
     repositories {
         google()
-        mavenCentral()  //add this line
         jcenter()
     }
 
diff --git a/lib/app.dart b/lib/app.dart
index 5b8b0477d..f80ed85df 100644
--- a/lib/app.dart
+++ b/lib/app.dart
@@ -40,7 +40,7 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
     WidgetsBinding.instance.addObserver(this);
   }
 
-  Future<IntentAction> initIntentAction() async {
+  Future<bool> initIntentAction() async {
     IntentAction intentAction = IntentAction.main;
     try {
       final actionResult = await _mediaExtensionPlugin.getIntentAction();
@@ -51,7 +51,8 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
     if (intentAction == IntentAction.main) {
       _configureBackgroundFetch();
     }
-    return intentAction;
+    AppLifecycleService.instance.setIntentAction(intentAction);
+    return true;
   }
 
   @override
@@ -59,8 +60,8 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
     if (Platform.isAndroid || kDebugMode) {
       return FutureBuilder(
         future: initIntentAction(),
-        builder: (BuildContext context, AsyncSnapshot<IntentAction> snapshot) {
-          return snapshot.data != null
+        builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
+          return snapshot.data != null && snapshot.data == true
               ? AdaptiveTheme(
                   light: lightThemeData,
                   dark: darkThemeData,
@@ -70,7 +71,7 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
                     themeMode: ThemeMode.system,
                     theme: lightTheme,
                     darkTheme: dartTheme,
-                    home: HomeWidget(intentAction: snapshot.data!),
+                    home: const HomeWidget(),
                     debugShowCheckedModeBanner: false,
                     builder: EasyLoading.init(),
                     supportedLocales: AppLocalizations.supportedLocales,
@@ -87,7 +88,7 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
         themeMode: ThemeMode.system,
         theme: lightThemeData,
         darkTheme: darkThemeData,
-        home: const HomeWidget(intentAction: IntentAction.main),
+        home: const HomeWidget(),
         debugShowCheckedModeBanner: false,
         builder: EasyLoading.init(),
         supportedLocales: AppLocalizations.supportedLocales,
diff --git a/lib/services/app_lifecycle_service.dart b/lib/services/app_lifecycle_service.dart
index 55c6bf959..58c761993 100644
--- a/lib/services/app_lifecycle_service.dart
+++ b/lib/services/app_lifecycle_service.dart
@@ -1,15 +1,21 @@
 import 'package:logging/logging.dart';
+import 'package:media_extension/media_extension_action_types.dart';
 
 class AppLifecycleService {
   final _logger = Logger("AppLifecycleService");
 
   bool isForeground = false;
+  IntentAction intentAction = IntentAction.main;
 
   static final AppLifecycleService instance =
       AppLifecycleService._privateConstructor();
 
   AppLifecycleService._privateConstructor();
 
+  void setIntentAction(IntentAction intentAction) {
+    this.intentAction = intentAction;
+  }
+
   void onAppInForeground(String reason) {
     _logger.info("App in foreground via $reason");
     isForeground = true;
diff --git a/lib/ui/home/home_gallery_widget.dart b/lib/ui/home/home_gallery_widget.dart
index 343c78d01..bde394a10 100644
--- a/lib/ui/home/home_gallery_widget.dart
+++ b/lib/ui/home/home_gallery_widget.dart
@@ -1,5 +1,4 @@
 import 'package:flutter/material.dart';
-import 'package:media_extension/media_extension_action_types.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/db/files_db.dart';
@@ -19,13 +18,11 @@ class HomeGalleryWidget extends StatelessWidget {
   final Widget? header;
   final Widget? footer;
   final SelectedFiles selectedFiles;
-  final IntentAction intentAction;
 
   const HomeGalleryWidget({
     Key? key,
     this.header,
     this.footer,
-    required this.intentAction,
     required this.selectedFiles,
   }) : super(key: key);
 
@@ -33,7 +30,6 @@ class HomeGalleryWidget extends StatelessWidget {
   Widget build(BuildContext context) {
     final double bottomSafeArea = MediaQuery.of(context).padding.bottom;
     final gallery = Gallery(
-      intentAction: intentAction,
       asyncLoader: (creationStartTime, creationEndTime, {limit, asc}) async {
         final ownerID = Configuration.instance.getUserID();
         final hasSelectedAllForBackup =
diff --git a/lib/ui/home_widget.dart b/lib/ui/home_widget.dart
index bbea9be7e..b35d05f95 100644
--- a/lib/ui/home_widget.dart
+++ b/lib/ui/home_widget.dart
@@ -5,7 +5,6 @@ import 'package:flutter/material.dart';
 import 'package:flutter/scheduler.dart';
 import 'package:flutter/services.dart';
 import 'package:logging/logging.dart';
-import 'package:media_extension/media_extension_action_types.dart';
 import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
 import 'package:move_to_background/move_to_background.dart';
 import 'package:photos/core/configuration.dart';
@@ -49,12 +48,9 @@ import 'package:uni_links/uni_links.dart';
 
 class HomeWidget extends StatefulWidget {
   const HomeWidget({
-    this.intentAction = IntentAction.main,
     Key? key,
   }) : super(key: key);
 
-  final IntentAction intentAction;
-
   @override
   State<StatefulWidget> createState() => _HomeWidgetState();
 }
@@ -368,7 +364,6 @@ class _HomeWidgetState extends State<HomeWidget> {
                 showBackupFolderHook
                     ? const StartBackupHookWidget(headerWidget: _headerWidget)
                     : HomeGalleryWidget(
-                        intentAction: widget.intentAction,
                         header: _headerWidget,
                         footer: const PreserveFooterWidget(),
                         selectedFiles: _selectedFiles,
diff --git a/lib/ui/huge_listview/lazy_loading_gallery.dart b/lib/ui/huge_listview/lazy_loading_gallery.dart
index ebebccebf..9baa94d67 100644
--- a/lib/ui/huge_listview/lazy_loading_gallery.dart
+++ b/lib/ui/huge_listview/lazy_loading_gallery.dart
@@ -15,6 +15,7 @@ import 'package:photos/events/files_updated_event.dart';
 import 'package:photos/extensions/string_ext.dart';
 import 'package:photos/models/file.dart';
 import 'package:photos/models/selected_files.dart';
+import 'package:photos/services/app_lifecycle_service.dart';
 import 'package:photos/theme/ente_theme.dart';
 import 'package:photos/ui/huge_listview/place_holder_widget.dart';
 import 'package:photos/ui/viewer/file/detail_page.dart';
@@ -36,7 +37,6 @@ class LazyLoadingGallery extends StatefulWidget {
   final String? logTag;
   final Stream<int> currentIndexStream;
   final int photoGirdSize;
-  final IntentAction intentAction;
   LazyLoadingGallery(
     this.files,
     this.index,
@@ -48,7 +48,6 @@ class LazyLoadingGallery extends StatefulWidget {
     this.currentIndexStream, {
     this.logTag = "",
     this.photoGirdSize = photoGridSizeDefault,
-    this.intentAction = IntentAction.main,
     Key? key,
   }) : super(key: key ?? UniqueKey());
 
@@ -262,7 +261,6 @@ class _LazyLoadingGalleryState extends State<LazyLoadingGallery> {
           _toggleSelectAllFromDay,
           _areAllFromDaySelected,
           widget.photoGirdSize,
-          intentAction: widget.intentAction,
         ),
       );
     }
@@ -291,7 +289,6 @@ class LazyLoadingGridView extends StatefulWidget {
   final ValueNotifier toggleSelectAllFromDay;
   final ValueNotifier areAllFilesSelected;
   final int? photoGridSize;
-  final IntentAction intentAction;
 
   LazyLoadingGridView(
     this.tag,
@@ -303,7 +300,6 @@ class LazyLoadingGridView extends StatefulWidget {
     this.toggleSelectAllFromDay,
     this.areAllFilesSelected,
     this.photoGridSize, {
-    this.intentAction = IntentAction.main,
     Key? key,
   }) : super(key: key ?? UniqueKey());
 
@@ -430,7 +426,7 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
         if (widget.selectedFiles.files.isNotEmpty) {
           _selectFile(file);
         } else {
-          if (widget.intentAction == IntentAction.pick) {
+          if (AppLifecycleService.instance.intentAction == IntentAction.pick) {
             final ioFile = await getFile(file);
             _mediaExtensionPlugin.setResult("file://${ioFile!.path}");
           } else {
@@ -439,8 +435,10 @@ class _LazyLoadingGridViewState extends State<LazyLoadingGridView> {
         }
       },
       onLongPress: () {
-        HapticFeedback.lightImpact();
-        _selectFile(file);
+        if (AppLifecycleService.instance.intentAction == IntentAction.main) {
+          HapticFeedback.lightImpact();
+          _selectFile(file);
+        }
       },
       child: ClipRRect(
         borderRadius: BorderRadius.circular(1),
diff --git a/lib/ui/viewer/gallery/gallery.dart b/lib/ui/viewer/gallery/gallery.dart
index cfafe48cb..9fe53a590 100644
--- a/lib/ui/viewer/gallery/gallery.dart
+++ b/lib/ui/viewer/gallery/gallery.dart
@@ -3,7 +3,6 @@ import 'dart:async';
 import 'package:flutter/foundation.dart';
 import 'package:flutter/material.dart';
 import 'package:logging/logging.dart';
-import 'package:media_extension/media_extension_action_types.dart';
 import 'package:photos/core/constants.dart';
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/ente_theme_data.dart';
@@ -29,7 +28,6 @@ typedef GalleryLoader = Future<FileLoadResult> Function(
 });
 
 class Gallery extends StatefulWidget {
-  final IntentAction intentAction;
   final GalleryLoader asyncLoader;
   final List<File>? initialFiles;
   final Stream<FilesUpdatedEvent>? reloadEvent;
@@ -44,7 +42,6 @@ class Gallery extends StatefulWidget {
   final double scrollBottomSafeArea;
 
   const Gallery({
-    this.intentAction = IntentAction.main,
     required this.asyncLoader,
     required this.selectedFiles,
     required this.tagPrefix,
@@ -251,7 +248,6 @@ class _GalleryState extends State<Gallery> {
               .map((event) => event.index),
           logTag: _logTag,
           photoGirdSize: _photoGridSize,
-          intentAction: widget.intentAction,
         );
         if (widget.header != null && index == 0) {
           gallery = Column(children: [widget.header!, gallery]);