2022-12-23 17:23:48 +00:00
|
|
|
(ns react-native.permissions
|
2023-10-16 22:03:18 +00:00
|
|
|
(:require
|
|
|
|
["react-native-permissions" :refer (check openSettings requestMultiple PERMISSIONS RESULTS)]
|
|
|
|
[react-native.platform :as platform]))
|
2022-12-23 17:23:48 +00:00
|
|
|
|
|
|
|
(def permissions-map
|
|
|
|
{:read-external-storage (cond
|
|
|
|
platform/android? (.-READ_EXTERNAL_STORAGE (.-ANDROID PERMISSIONS)))
|
|
|
|
:write-external-storage (cond
|
|
|
|
platform/low-device? (.-WRITE_EXTERNAL_STORAGE (.-ANDROID PERMISSIONS)))
|
2023-12-11 15:52:23 +00:00
|
|
|
:read-media-images (cond
|
|
|
|
platform/android? (.-READ_MEDIA_IMAGES (.-ANDROID PERMISSIONS)))
|
|
|
|
:post-notifications (cond
|
|
|
|
platform/android? (.-POST_NOTIFICATIONS (.-ANDROID PERMISSIONS)))
|
2022-12-23 17:23:48 +00:00
|
|
|
:camera (cond
|
|
|
|
platform/android? (.-CAMERA (.-ANDROID PERMISSIONS))
|
|
|
|
platform/ios? (.-CAMERA (.-IOS PERMISSIONS)))
|
|
|
|
:record-audio (cond
|
|
|
|
platform/android? (.-RECORD_AUDIO (.-ANDROID PERMISSIONS))
|
|
|
|
platform/ios? (.-MICROPHONE (.-IOS PERMISSIONS)))})
|
|
|
|
|
|
|
|
(defn all-granted?
|
|
|
|
[permissions]
|
|
|
|
(let [permission-vals (distinct (vals permissions))]
|
|
|
|
(and (= (count permission-vals) 1)
|
|
|
|
(not (#{(.-BLOCKED RESULTS) (.-DENIED RESULTS)} (first permission-vals))))))
|
|
|
|
|
|
|
|
(defn request-permissions
|
|
|
|
[{:keys [permissions on-allowed on-denied]
|
|
|
|
:or {on-allowed #()
|
|
|
|
on-denied #()}}]
|
|
|
|
(let [permissions (remove nil? (mapv #(get permissions-map %) permissions))]
|
|
|
|
(if (empty? permissions)
|
|
|
|
(on-allowed)
|
|
|
|
(-> (requestMultiple (clj->js permissions))
|
|
|
|
(.then #(if (all-granted? (js->clj %))
|
|
|
|
(on-allowed)
|
|
|
|
(on-denied)))
|
|
|
|
(.catch on-denied)))))
|
|
|
|
|
|
|
|
(defn permission-granted?
|
|
|
|
[permission on-result on-error]
|
|
|
|
(-> (check (get permissions-map permission))
|
|
|
|
(.then #(on-result (not (#{(.-BLOCKED RESULTS) (.-DENIED RESULTS)} %))))
|
|
|
|
(.catch #(on-error %))))
|
2023-06-26 13:35:13 +00:00
|
|
|
|
|
|
|
(def open-settings openSettings)
|