Sfoglia il codice sorgente

enhance(android): allow choice open & share asset file

Andelf 1 anno fa
parent
commit
dd4c5637ac

+ 1 - 0
android/app/capacitor.build.gradle

@@ -9,6 +9,7 @@ android {
 
 apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
 dependencies {
+    implementation project(':capacitor-action-sheet')
     implementation project(':capacitor-app')
     implementation project(':capacitor-camera')
     implementation project(':capacitor-clipboard')

+ 4 - 0
android/app/src/main/assets/capacitor.plugins.json

@@ -1,4 +1,8 @@
 [
+	{
+		"pkg": "@capacitor/action-sheet",
+		"classpath": "com.capacitorjs.plugins.actionsheet.ActionSheetPlugin"
+	},
 	{
 		"pkg": "@capacitor/app",
 		"classpath": "com.capacitorjs.plugins.app.AppPlugin"

+ 3 - 0
android/capacitor.settings.gradle

@@ -2,6 +2,9 @@
 include ':capacitor-android'
 project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor')
 
+include ':capacitor-action-sheet'
+project(':capacitor-action-sheet').projectDir = new File('../node_modules/@capacitor/action-sheet/android')
+
 include ':capacitor-app'
 project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android')
 

+ 1 - 0
package.json

@@ -79,6 +79,7 @@
         "postinstall": "yarn tldraw:build && yarn amplify:build "
     },
     "dependencies": {
+        "@capacitor/action-sheet": "^5.0.7",
         "@capacitor/android": "^5.0.0",
         "@capacitor/app": "^5.0.0",
         "@capacitor/camera": "^5.0.0",

+ 1 - 1
src/main/frontend/components/block.cljs

@@ -395,7 +395,7 @@
                          (let [[rel-dir basename] (util/get-dir-and-basename href)
                                rel-dir (string/replace rel-dir #"^/+" "")
                                asset-url (path/path-join repo-dir rel-dir basename)]
-                           (mobile-intent/share-file asset-url))))]
+                           (mobile-intent/open-or-share-file asset-url))))]
 
         (cond
           (contains? config/audio-formats ext)

+ 18 - 13
src/main/frontend/mobile/intent.cljs

@@ -1,6 +1,7 @@
 (ns frontend.mobile.intent
   (:require ["@capacitor/filesystem" :refer [Filesystem]]
             ["@capacitor/share" :refer [^js Share]]
+            ["@capacitor/action-sheet" :refer [ActionSheet]]
             ["path" :as node-path]
             ["send-intent" :refer [^js SendIntent]]
             [clojure.pprint :as pprint]
@@ -23,21 +24,25 @@
             [logseq.graph-parser.util.page-ref :as page-ref]
             [promesa.core :as p]))
 
-(defn share-file
+(defn open-or-share-file
   "Share file to mobile platform"
   [uri]
-  (cond
-    ;; using ACTION_VIEW to open file with system default app
-    (mobile-util/native-android?)
-    (.openFile mobile-util/folder-picker (clj->js {:uri uri}))
-
-    (mobile-util/native-ios?)
-    (.share Share (clj->js {:url uri
-                            :dialogTitle "Open file with your favorite app"
-                            :title "Open file with your favorite app"}))
-
-    :else
-    (js/window.open uri "_blank")))
+  (p/let [options [{:title "Open"
+                    :style "DEFAULT"}
+                   {:title "Share"}
+                   {:title "Cancel"
+                    :style "CANCEL"}]
+          result (.showActions ActionSheet (clj->js {:title "File Options"
+                                                     :message "Select an option to perform"
+                                                     :options options}))
+          index (.-index result)]
+
+    (when (not= index 2)
+      (if (and (= index 0) (mobile-util/native-android?))
+        (.openFile mobile-util/folder-picker (clj->js {:uri uri}))
+        (.share Share (clj->js {:url uri
+                                :dialogTitle "Open file with your favorite app"
+                                :title "Open file with your favorite app"}))))))
 
 (defn- is-link
   [url]

+ 5 - 0
yarn.lock

@@ -240,6 +240,11 @@
     "@babel/helper-validator-identifier" "^7.22.20"
     to-fast-properties "^2.0.0"
 
+"@capacitor/action-sheet@^5.0.7":
+  version "5.0.7"
+  resolved "https://registry.yarnpkg.com/@capacitor/action-sheet/-/action-sheet-5.0.7.tgz#4808af8c483a85f58c7fffdea76f8d374883e3a0"
+  integrity sha512-8q8fYYy9dwLi4eiDiYq+ys/uOm9C2YpjfAnlG7fYdha8QNpaz2314jR+dxsJO423zYB2Wag9NXlupHpXeGdGOA==
+
 "@capacitor/android@^5.0.0":
   version "5.4.1"
   resolved "https://registry.yarnpkg.com/@capacitor/android/-/android-5.4.1.tgz#5b0445202ca5e48fcb79d0c88e4403acc32504bc"