util.cljs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (ns frontend.mobile.util
  2. (:require ["@capacitor/core" :refer [Capacitor registerPlugin]]
  3. ["@capacitor/splash-screen" :refer [SplashScreen]]
  4. ["@logseq/capacitor-file-sync" :refer [FileSync]]
  5. [clojure.string :as string]
  6. [promesa.core :as p]))
  7. (defn platform []
  8. (.getPlatform Capacitor))
  9. (defn native-platform? []
  10. (.isNativePlatform Capacitor))
  11. (defn native-ios? []
  12. (and (native-platform?)
  13. (= (platform) "ios")))
  14. (defn native-android? []
  15. (and (native-platform?)
  16. (= (platform) "android")))
  17. (defn convert-file-src [path-str]
  18. (.convertFileSrc Capacitor path-str))
  19. (defonce folder-picker (registerPlugin "FolderPicker"))
  20. (when (native-ios?)
  21. (defonce ios-utils (registerPlugin "Utils"))
  22. (defonce ios-file-container (registerPlugin "FileContainer")))
  23. ;; NOTE: both iOS and android share the same API
  24. (when (native-platform?)
  25. (defonce file-sync FileSync)
  26. (defonce fs-watcher (registerPlugin "FsWatcher")))
  27. (defn hide-splash []
  28. (.hide SplashScreen))
  29. (defn get-idevice-model
  30. []
  31. (when (native-ios?)
  32. (let [width (.-width js/screen)
  33. height (.-height js/screen)
  34. landscape? (> width height)
  35. [width height] (if landscape? [height width] [width height])]
  36. [(case [width height]
  37. [320 568] "iPhoneSE4"
  38. [375 667] "iPhone8"
  39. [375 812] "iPhoneX"
  40. [390 844] "iPhone12"
  41. [414 736] "iPhone8Plus"
  42. [414 896] "iPhone11"
  43. [428 926] "iPhone13ProMax"
  44. [476 847] "iPhone7Plus"
  45. [744 1133] "iPadmini8.3"
  46. [768 1024] "iPad9.7"
  47. [810 1080] "iPad10.2"
  48. [820 1180] "iPad10.9"
  49. [834 1112] "iPadAir10.5"
  50. [834 1194] "iPadPro11"
  51. [1024 1366] "iPadPro12.9"
  52. "Not a known Apple device!")
  53. landscape?])))
  54. (defn native-iphone-without-notch?
  55. []
  56. (when-let [model (get-idevice-model)]
  57. (string/starts-with? (first model) "iPhone8")))
  58. (defn native-iphone?
  59. []
  60. (when-let [model (get-idevice-model)]
  61. (and (string/starts-with? (first model) "iPhone")
  62. (not (string/starts-with? (first model) "iPhone8")))))
  63. (defn native-ipad?
  64. []
  65. (when-let [model (get-idevice-model)]
  66. (string/starts-with? (first model) "iPad")))
  67. (defn check-ios-zoomed-display
  68. "Detect whether iOS device is in Zoom Display"
  69. []
  70. (p/let [is-zoomed? (p/chain (.isZoomed ios-utils)
  71. #(js->clj % :keywordize-keys true))]
  72. (when (:isZoomed is-zoomed?)
  73. (let [^js cl (.-classList js/document.documentElement)]
  74. (.add cl "is-zoomed-native-ios")))))
  75. (defn iCloud-container-path?
  76. "Check whether `path' is logseq's iCloud container path on iOS"
  77. [path]
  78. (string/includes? path "iCloud~com~logseq~logseq"))