From 0ffe47919371a1748d2540ddaf1ac10fb15f8578 Mon Sep 17 00:00:00 2001 From: BrtqKr Date: Mon, 4 Mar 2024 16:29:27 +0100 Subject: [PATCH] make single image flow work --- .../chat/ShareActionHandlerModule.kt | 15 ++--- .../com/expensify/chat/image/ImageUtils.kt | 2 + .../chat/intentHandler/ImageIntentHandler.kt | 18 +++++- src/pages/share/ShareRootPage.tsx | 59 ++++++++----------- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/android/app/src/main/java/com/expensify/chat/ShareActionHandlerModule.kt b/android/app/src/main/java/com/expensify/chat/ShareActionHandlerModule.kt index e4ca0852e519..b6e6bf212362 100644 --- a/android/app/src/main/java/com/expensify/chat/ShareActionHandlerModule.kt +++ b/android/app/src/main/java/com/expensify/chat/ShareActionHandlerModule.kt @@ -1,5 +1,8 @@ package com.expensify.chat +import android.content.Context +import android.util.Log +import com.expensify.chat.intentHandler.IntentHandlerConstants import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.Callback import com.facebook.react.bridge.ReactApplicationContext @@ -12,20 +15,18 @@ class ShareActionHandlerModule(reactContext: ReactApplicationContext) : ReactCon @ReactMethod fun processFiles(callback: Callback) { try { - //Perform your operation here. For instance, load your files - - val fileArray: ArrayList = ArrayList() - // Add your files to "fileArray" + val sharedPreferences = reactApplicationContext.getSharedPreferences(IntentHandlerConstants.preferencesFile, Context.MODE_PRIVATE) + val fileSet = sharedPreferences.getStringSet(IntentHandlerConstants.fileArrayProperty, setOf()) + val fileArray: ArrayList = ArrayList(fileSet) val resultArray = Arguments.createArray() for (file in fileArray) { resultArray.pushString(file) } - //invoke the callback with your loaded files - callback.invoke(null, resultArray) - + callback.invoke(resultArray) } catch (exception: Exception) { + Log.e("ImageIntentHandler", exception.toString()) callback.invoke(exception.toString(), null) } } diff --git a/android/app/src/main/java/com/expensify/chat/image/ImageUtils.kt b/android/app/src/main/java/com/expensify/chat/image/ImageUtils.kt index 5a5642457ecd..1fa176f04dac 100644 --- a/android/app/src/main/java/com/expensify/chat/image/ImageUtils.kt +++ b/android/app/src/main/java/com/expensify/chat/image/ImageUtils.kt @@ -104,6 +104,8 @@ object ImageUtils { val imageFile: File = createImageFile(context) saveImageFromMediaProviderUri(uri, imageFile, context) resultingPath = imageFile.absolutePath + Log.i("ImageIntentHandler", "save image$resultingPath") + } catch (ex: IOException) { Log.e(tag, "Couldn't save image from intent", ex) } diff --git a/android/app/src/main/java/com/expensify/chat/intentHandler/ImageIntentHandler.kt b/android/app/src/main/java/com/expensify/chat/intentHandler/ImageIntentHandler.kt index e4532a3c021f..14d8a6f163c4 100644 --- a/android/app/src/main/java/com/expensify/chat/intentHandler/ImageIntentHandler.kt +++ b/android/app/src/main/java/com/expensify/chat/intentHandler/ImageIntentHandler.kt @@ -7,8 +7,14 @@ import android.util.Log import com.expensify.chat.image.ImageUtils.copyUriToStorage +object IntentHandlerConstants { + const val preferencesFile = "shareActionHandler" + const val fileArrayProperty = "filePaths" +} + + class ImageIntentHandler(private val context: Context) : AbstractIntentHandler() { - override fun handle(intent: Intent?): Boolean { + override fun handle(intent: Intent?): Boolean { Log.i("ImageIntentHandler", "Handle intent" + intent.toString()) if (intent == null) { return false @@ -37,15 +43,21 @@ class ImageIntentHandler(private val context: Context) : AbstractIntentHandler() private fun handleSingleImageIntent(intent: Intent, context: Context) { (intent.getParcelableExtra(Intent.EXTRA_STREAM))?.let { imageUri -> + + Log.i("ImageIntentHandler", "handleSingleImageIntent$imageUri") // Update UI to reflect image being shared if (imageUri == null) { return } - val resultingImagePaths = ArrayList() + val fileArrayList: ArrayList = ArrayList() val resultingPath: String? = copyUriToStorage(imageUri, context) if (resultingPath != null) { - resultingImagePaths.add(resultingPath) + fileArrayList.add(resultingPath) + val sharedPreferences = context.getSharedPreferences(IntentHandlerConstants.preferencesFile, Context.MODE_PRIVATE) + val editor = sharedPreferences.edit() + editor.putStringSet(IntentHandlerConstants.fileArrayProperty, fileArrayList.toSet()) + editor.apply() } } } diff --git a/src/pages/share/ShareRootPage.tsx b/src/pages/share/ShareRootPage.tsx index b1ecdb2703cd..028e7439a0df 100644 --- a/src/pages/share/ShareRootPage.tsx +++ b/src/pages/share/ShareRootPage.tsx @@ -15,7 +15,7 @@ import Navigation from '@navigation/Navigation'; import OnyxTabNavigator, {TopTab} from '@navigation/OnyxTabNavigator'; import MoneyRequestParticipantsSelector from '@pages/iou/steps/MoneyRequstParticipantsPage/MoneyRequestParticipantsSelector'; import CONST from '@src/CONST'; -// import ShareActionHandlerModule from '@src/modules/ShareActionHandlerModule'; +import ShareActionHandlerModule from '@src/modules/ShareActionHandlerModule'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type {Report} from '@src/types/onyx'; @@ -37,41 +37,29 @@ function ShareRootPage({selectedTab, iou}: ShareRootPageProps) { const selectedReportID = useRef(optimisticReportID); const appState = useRef(AppState.currentState); - // const handleAppStateChange = (nextAppState: AppStateStatus) => { - // if (appState.current.match(/inactive|background/) && nextAppState === 'active') { - // console.log('PROCESSED FILES ATTEMPT'); + const handleAppStateChange = (nextAppState: AppStateStatus) => { + if (appState.current.match(/inactive|background/) && nextAppState === 'active') { + console.log('PROCESSED FILES ATTEMPT'); - // ShareActionHandlerModule.processFiles((processedFiles) => { - // // eslint-disable-next-line no-console - // console.log('PROCESSED FILES ', processedFiles); - // }); - // } + ShareActionHandlerModule.processFiles((processedFiles) => { + // eslint-disable-next-line no-console + console.log('PROCESSED FILES ', processedFiles); + }); + } + console.log('AppState', nextAppState); - // appState.current = nextAppState; - // // eslint-disable-next-line no-console - // console.log('AppState', appState.current); - // }; - - // const handleAppStateFocus = (nextAppState: AppStateStatus) => { - // console.log('PROCESSED FILES ATTEMPT'); - - // ShareActionHandlerModule.processFiles((processedFiles) => { - // // eslint-disable-next-line no-console - // console.log('PROCESSED FILES ', processedFiles); - // }); - - // appState.current = nextAppState; - // // eslint-disable-next-line no-console - // console.log('AppState', appState.current); - // }; + appState.current = nextAppState; + // eslint-disable-next-line no-console + console.log('AppState', appState.current); + }; - // useEffect(() => { - // const changeSubscription = Platform.OS === 'ios' ? AppState.addEventListener('change', handleAppStateChange) : AppState.addEventListener('focus', handleAppStateFocus); + useEffect(() => { + const changeSubscription = AppState.addEventListener('change', handleAppStateChange); - // return () => { - // changeSubscription.remove(); - // }; - // }, []); + return () => { + changeSubscription.remove(); + }; + }, []); const navigateBack = () => { Navigation.dismissModal(); @@ -80,12 +68,13 @@ function ShareRootPage({selectedTab, iou}: ShareRootPageProps) { const goToNextStep = useCallback(() => { // const nextStepIOUType = numberOfParticipants.current === 1 ? CONST.IOU.TYPE.REQUEST : CONST.IOU.TYPE.SPLIT; const nextStepIOUType = CONST.IOU.TYPE.REQUEST; - IOU.startMoneyRequest_temporaryForRefactor(optimisticReportID, false, CONST.IOU.REQUEST_TYPE.SCAN); + // IOU.startMoneyRequest_temporaryForRefactor(optimisticReportID, false, CONST.IOU.REQUEST_TYPE.SCAN); IOU.setMoneyRequestTag(transactionID, ''); - IOU.resetMoneyRequestCategory_temporaryForRefactor(transactionID); + // IOU.resetMoneyRequestCategory_temporaryForRefactor(transactionID); Navigation.navigate(ROUTES.SHARE_SCAN_CONFIRM.getRoute(nextStepIOUType, transactionID, selectedReportID.current || optimisticReportID)); }, [transactionID, optimisticReportID]); + console.log('share root page'); return (