diff --git a/README.md b/README.md index 9ae1fd9..d936a9f 100644 --- a/README.md +++ b/README.md @@ -46,21 +46,23 @@ yarn add @reason-react-native/permissions ```reason open ReactNative; -open ReactNativePermissions; let requestCamera = () => { - Permissions.request( - switch (Platform.os) { - | os when os === "ios" => Permissions.Constants.IOS.camera - | _ => Permissions.Constants.Android.camera - }, - ) - |> Js.Promise.then_(permissionStatus => - switch (permissionStatus) { - | status when status === Results.granted => Js.Promise.resolve() - | _ => Js.Promise.reject(Js.Exn.raiseError("permission error")) - } - ); + ReactNativePermissions.( + request( + switch (Platform.os) { + | os when os === "ios" => Ios.camera + | _ => Android.camera + }, + ) + |> Js.Promise.then_(permissionStatus => + switch (permissionStatus) { + | status when status === granted => Js.Promise.resolve() + | _ => Js.Promise.reject(Js.Exn.raiseError("permission error")) + } + ) + ) + ); }; ``` diff --git a/src/ReactNativePermissions.bs.js b/src/ReactNativePermissions.bs.js index 7517a25..705cbf6 100644 --- a/src/ReactNativePermissions.bs.js +++ b/src/ReactNativePermissions.bs.js @@ -1,21 +1,10 @@ 'use strict'; -var IOS = { }; - var Android = { }; -var Constants = { - IOS: IOS, - Android: Android -}; - -var Results = { }; - -var $$Permissions = { - Constants: Constants, - Results: Results -}; +var Ios = { }; -exports.$$Permissions = $$Permissions; +exports.Android = Android; +exports.Ios = Ios; /* No side effect */ diff --git a/src/ReactNativePermissions.re b/src/ReactNativePermissions.re index 88b73c1..5767daf 100644 --- a/src/ReactNativePermissions.re +++ b/src/ReactNativePermissions.re @@ -1,50 +1,201 @@ -module Permissions = { - module Constants = { - type t; - - module IOS = { - [@bs.module "react-native-permissions"] - [@bs.scope ("default", "PERMISSIONS", "IOS")] - external camera: t = "CAMERA"; - - [@bs.module "react-native-permissions"] - [@bs.scope ("default", "PERMISSIONS", "IOS")] - external locationAlways: t = "LOCATION_ALWAYS"; - }; - - module Android = { - [@bs.module "react-native-permissions"] - [@bs.scope ("default", "PERMISSIONS", "ANDROID")] - external camera: t = "CAMERA"; - - [@bs.module "react-native-permissions"] - [@bs.scope ("default", "PERMISSIONS", "ANDROID")] - external accessCoarseLocation: t = "ACCESS_COARSE_LOCATION"; - - [@bs.module "react-native-permissions"] - [@bs.scope ("default", "PERMISSIONS", "ANDROID")] - external accessFineLocation: t = "ACCESS_FINE_LOCATION"; - }; - }; - - module Results = { - type t; - [@bs.module "react-native-permissions"] [@bs.scope ("default", "RESULTS")] - external unavailable: t = "UNAVAILABLE"; - [@bs.module "react-native-permissions"] [@bs.scope ("default", "RESULTS")] - external denied: t = "DENIED"; - [@bs.module "react-native-permissions"] [@bs.scope ("default", "RESULTS")] - external granted: t = "GRANTED"; - [@bs.module "react-native-permissions"] [@bs.scope ("default", "RESULTS")] - external blocked: t = "BLOCKED"; - }; - - [@bs.module "react-native-permissions"] [@bs.scope "default"] - external openSettings: unit => Js.Promise.t(unit) = "openSettings"; - - [@bs.module "react-native-permissions"] [@bs.scope "default"] - external request: Constants.t => Js.Promise.t(Results.t) = "request"; - - [@bs.module "react-native-permissions"] [@bs.scope "default"] - external check: Constants.t => Js.Promise.t(Results.t) = "check"; +type permission; + +module Android = { + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external accept_handover: permission = "ACCEPT_HANDOVER"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external access_background_location: permission = + "ACCESS_BACKGROUND_LOCATION"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external access_coarse_location: permission = "ACCESS_COARSE_LOCATION"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external access_fine_location: permission = "ACCESS_FINE_LOCATION"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external activity_recognition: permission = "ACTIVITY_RECOGNITION"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external add_voicemail: permission = "ADD_VOICEMAIL"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external answer_phone_calls: permission = "ANSWER_PHONE_CALLS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external body_sensors: permission = "BODY_SENSORS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external call_phone: permission = "CALL_PHONE"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external camera: permission = "CAMERA"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external get_accounts: permission = "GET_ACCOUNTS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external process_outgoing_calls: permission = "PROCESS_OUTGOING_CALLS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external read_calendar: permission = "READ_CALENDAR"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external read_call_log: permission = "READ_CALL_LOG"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external read_contacts: permission = "READ_CONTACTS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external read_external_storage: permission = "READ_EXTERNAL_STORAGE"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external read_phone_numbers: permission = "READ_PHONE_NUMBERS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external read_phone_state: permission = "READ_PHONE_STATE"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external read_sms: permission = "READ_SMS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external receive_mms: permission = "RECEIVE_MMS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external receive_sms: permission = "RECEIVE_SMS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external receive_wap_push: permission = "RECEIVE_WAP_PUSH"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external record_audio: permission = "RECORD_AUDIO"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external send_sms: permission = "SEND_SMS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external use_sip: permission = "USE_SIP"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external write_calendar: permission = "WRITE_CALENDAR"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external write_call_log: permission = "WRITE_CALL_LOG"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external write_contacts: permission = "WRITE_CONTACTS"; + [@bs.module "react-native-permissions"] + [@bs.scope ("PERMISSIONS", "ANDROID")] + external write_external_storage: permission = "WRITE_EXTERNAL_STORAGE"; +}; + +module Ios = { + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external bluetooth_peripheral: permission = "BLUETOOTH_PERIPHERAL"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external calendars: permission = "CALENDARS"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external camera: permission = "CAMERA"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external contacts: permission = "CONTACTS"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external face_id: permission = "FACE_ID"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external location_always: permission = "LOCATION_ALWAYS"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external location_when_in_use: permission = "LOCATION_WHEN_IN_USE"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external media_library: permission = "MEDIA_LIBRARY"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external microphone: permission = "MICROPHONE"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external motion: permission = "MOTION"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external photo_library: permission = "PHOTO_LIBRARY"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external reminders: permission = "REMINDERS"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external siri: permission = "SIRI"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external speech_recognition: permission = "SPEECH_RECOGNITION"; + [@bs.module "react-native-permissions"] [@bs.scope ("PERMISSIONS", "IOS")] + external storekit: permission = "STOREKIT"; }; + +type permissionStatus; + +/* This feature is not available (on this device / in this context) */ +[@bs.module "react-native-permissions"] [@bs.scope "RESULTS"] +external unavailable: permissionStatus = "UNAVAILABLE"; + +/* The permission has not been requested / is denied but requestable */ +[@bs.module "react-native-permissions"] [@bs.scope "RESULTS"] +external denied: permissionStatus = "DENIED"; + +/* The permission is granted */ +[@bs.module "react-native-permissions"] [@bs.scope "RESULTS"] +external granted: permissionStatus = "GRANTED"; + +/* The permission is denied and not requestable anymore */ +[@bs.module "react-native-permissions"] [@bs.scope "RESULTS"] +external blocked: permissionStatus = "BLOCKED"; + +// methods + +[@bs.module "react-native-permissions"] +external check: permission => Js.Promise.t(permissionStatus) = "check"; + +type requestRationale = { + title: string, + message: string, + buttonPositive: option(string), + buttonNegative: option(string), + buttonNeutral: option(string), +}; + +[@bs.module "react-native-permissions"] +external request: permission => Js.Promise.t(permissionStatus) = "request"; + +[@bs.module "react-native-permissions"] +external requestWithRational: + (permission, requestRationale) => Js.Promise.t(permissionStatus) = + "request"; + +type notificationSettings = { + // properties only availables on iOS + // unavailable settings will not be included in the response object + alert: option(bool), + badge: option(bool), + sound: option(bool), + lockScreen: option(bool), + carPlay: option(bool), + notificationCenter: option(bool), + criticalAlert: option(bool), +}; + +type requestNotificationsResult = { + status: permissionStatus, + settings: notificationSettings, +}; + +[@bs.module "react-native-permissions"] +external checkNotifications: unit => Js.Promise.t(requestNotificationsResult) = + "checkNotifications"; + +// only used on iOS +// string can be +// "alert" +// "badge" +// "sound" +// "criticalAlert" +// "carPlay" +// "provisional" +[@bs.module "react-native-permissions"] +external requestNotifications: + array(string) => Js.Promise.t(requestNotificationsResult) = + "requestNotifications"; + +[@bs.module "react-native-permissions"] +external openSettings: unit => Js.Promise.t(unit) = "openSettings";