Skip to content

Commit fa28b0d

Browse files
authored
Merge pull request #12 from YsnKsy/use-hooks-simplified
feat(src/index): concat 2 hooks into one useLocationSettings()
2 parents d7c66e6 + d94ea01 commit fa28b0d

File tree

6 files changed

+74
-107
lines changed

6 files changed

+74
-107
lines changed

README.md

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,28 @@ yarn add react-native-location-settings-enabler
3131
```js
3232
import LocationSettingsEnabler from "react-native-location-settings-enabler"
3333

34-
const { PRIORITIES, useCheckSettings, requestResolutionSettings } = LocationSettingsEnabler
35-
const { HIGH_ACCURACY } = PRIORITIES
36-
37-
// Define configuration
38-
const config = {
39-
priority: HIGH_ACCURACY, // default BALANCED_POWER_ACCURACY
40-
alwaysShow: true, // default false
41-
needBle: false, // default false
42-
}
43-
44-
const req = () => requestResolutionSettings(config)
34+
const {
35+
PRIORITIES: { HIGH_ACCURACY },
36+
useLocationSettings,
37+
} = LocationSettingsEnabler
4538

46-
// React Native App Component
39+
// React Component
4740
const App = () => {
48-
// use hook to check location status
49-
const enabled = useCheckSettings(config)
41+
const [enabled, requestResolution] = useLocationSettings(
42+
{
43+
priority: HIGH_ACCURACY, // default BALANCED_POWER_ACCURACY
44+
alwaysShow: true, // default false
45+
needBle: true, // default false
46+
},
47+
false /* optional: default undefined */,
48+
)
5049

5150
return (
52-
<View>{!enabled && <Button onPress={req} title="Request Resolution Location Settings" />}</View>
51+
<View>
52+
{!enabled && (
53+
<Button onPress={requestResolution} title="Request Resolution Location Settings" />
54+
)}
55+
</View>
5356
)
5457
}
5558
```
@@ -61,8 +64,11 @@ const App = () => {
6164
```js
6265
import LocationSettingsEnabler from "react-native-location-settings-enabler"
6366

64-
const { PRIORITIES, checkSettings, requestResolutionSettings } = LocationSettingsEnabler
65-
const { HIGH_ACCURACY } = PRIORITIES
67+
const {
68+
PRIORITIES: { HIGH_ACCURACY },
69+
checkSettings,
70+
requestResolutionSettings
71+
} = LocationSettingsEnabler
6672

6773
// Adds a listener to be invoked when location settings checked using
6874
// [checkSettings] or changed using [requestResolutionSettings]
@@ -141,45 +147,31 @@ Static object contain a list quality of service for location updates. If your ap
141147

142148
### Methods
143149

144-
> ### `useCheckSettings({ priority, alwaysShow, needBle }, initialSatus?)`
150+
> ### `useLocationSettings({ priority, alwaysShow, needBle }, initialStatus?)`
145151
146152
```js
147153
import LocationSettingsEnabler from "react-native-location-settings-enabler"
148154

149-
const { useCheckSettings, PRIORITIES } = LocationSettingsEnabler
150-
const { HIGH_ACCURACY } = PRIORITIES
155+
const {
156+
useLocationSettings,
157+
PRIORITIES: { HIGH_ACCURACY },
158+
} = LocationSettingsEnabler
151159

152-
const enabled = useCheckSettings({
160+
const [enabled, requestResolution] = useLocationSettings({
153161
priority: HIGH_ACCURACY, // optional: default BALANCED_POWER_ACCURACY
154162
alwaysShow: true, // optional: default false
155163
needBle: true, // optional: default false
156164
})
157165

158166
console.log(`Location are ${enabled ? "enabled" : "disabled"}`)
159-
```
160-
161-
Hook let you check if the user's device location is turned off / on.
162-
163-
---
164167

165-
> ### `useRequestResolutionSettings({ priority, alwaysShow, needBle }, initialSatus?)`
166-
167-
```js
168-
import LocationSettingsEnabler from "react-native-location-settings-enabler"
169-
170-
const { useRequestResolutionSettings, PRIORITIES } = LocationSettingsEnabler
171-
const { HIGH_ACCURACY } = PRIORITIES
172-
173-
const enabled = useRequestResolutionSettings({
174-
priority: HIGH_ACCURACY, // optional: default BALANCED_POWER_ACCURACY
175-
alwaysShow: true, // optional: default false
176-
needBle: true, // optional: default false
177-
})
178-
179-
console.log(`Location are ${enabled ? "enabled" : "disabled"}`)
168+
// ...
169+
if (!enabled) {
170+
requestResolution()
171+
}
180172
```
181173

182-
Hooks let display an activity where the user's can turn location 'on', only if location is already turned 'off'.
174+
Hook let you check the user's device location status 'on' / 'off' and method let you display an activity where they can turn location 'on'.
183175

184176
---
185177

@@ -188,8 +180,10 @@ Hooks let display an activity where the user's can turn location 'on', only if l
188180
```js
189181
import LocationSettingsEnabler from "react-native-location-settings-enabler"
190182

191-
const { checkSettings, PRIORITIES } = LocationSettingsEnabler
192-
const { HIGH_ACCURACY } = PRIORITIES
183+
const {
184+
checkSettings,
185+
PRIORITIES: { HIGH_ACCURACY },
186+
} = LocationSettingsEnabler
193187

194188
checkSettings({
195189
priority: HIGH_ACCURACY, // optional: default BALANCED_POWER_ACCURACY
@@ -207,8 +201,10 @@ Checking if the user's device location is turned on / off.
207201
```js
208202
import LocationSettingsEnabler from "react-native-location-settings-enabler"
209203

210-
const { requestResolutionSettings, PRIORITIES } = LocationSettingsEnabler
211-
const { HIGH_ACCURACY } = PRIORITIES
204+
const {
205+
requestResolutionSettings,
206+
PRIORITIES: { HIGH_ACCURACY },
207+
} = LocationSettingsEnabler
212208

213209
requestResolutionSettings({
214210
priority: HIGH_ACCURACY, // optional: default BALANCED_POWER_ACCURACY

example/src/App.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ import * as React from "react"
22
import { StyleSheet, View, Text, Button } from "react-native"
33
import LocationSettingsEnabler from "react-native-location-settings-enabler"
44

5-
const { PRIORITIES, useCheckSettings, requestResolutionSettings } = LocationSettingsEnabler
6-
const { HIGH_ACCURACY } = PRIORITIES
7-
8-
const config = {
9-
priority: HIGH_ACCURACY,
10-
alwaysShow: true,
11-
needBle: true,
12-
}
5+
const {
6+
PRIORITIES: { HIGH_ACCURACY },
7+
useLocationSettings,
8+
} = LocationSettingsEnabler
139

1410
const LocationStatus = (props: { enabled: boolean | undefined }) => (
1511
<Text style={styles.status}>
@@ -29,10 +25,12 @@ const RequestResolutionSettingsBtn = (props: { onPress: any }) => (
2925
<Button color="red" title="Request Resolution Location Settings" onPress={props.onPress} />
3026
)
3127

32-
const requestResolution = () => requestResolutionSettings(config)
33-
3428
const App: React.FunctionComponent<unknown> = () => {
35-
const enabled = useCheckSettings(config)
29+
const [enabled, requestResolution] = useLocationSettings({
30+
priority: HIGH_ACCURACY,
31+
alwaysShow: true,
32+
needBle: true,
33+
})
3634

3735
return (
3836
<View style={styles.container}>

src/__mocks__/react-native-location-settings-enabler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ export default {
77
LOW_POWER: 104,
88
NO_POWER: 105,
99
},
10-
useCheckSettings: jest.fn(() => true || false),
11-
useRequestResolutionSettings: jest.fn(() => true || false),
10+
useLocationSettings: jest.fn(() => [Boolean, jest.fn()]),
1211
checkSettings: jest.fn(),
1312
requestResolutionSettings: jest.fn(),
1413
addListener: jest.fn(() => {

src/__tests__/index.test.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,13 @@ describe("Test native module 'react-native-location-settings-enabler'", () => {
1212
needBle: false,
1313
}
1414

15-
test("[ LocationSettingsEnabler.useCheckSettings ] is a valid hook method", () => {
16-
const useCheckSettings = LocationSettingsEnabler.useCheckSettings
17-
expect(useCheckSettings).toBeTruthy()
18-
expect(useCheckSettings(options)).toBe(true || false)
19-
})
20-
21-
test("[ LocationSettingsEnabler.useRequestResolutionSettings ] is a valid hook method", () => {
22-
const useRequestResolutionSettings = LocationSettingsEnabler.useRequestResolutionSettings
23-
expect(useRequestResolutionSettings).toBeTruthy()
24-
expect(useRequestResolutionSettings(options)).toBe(true || false)
25-
})
26-
27-
test("[ LocationSettingsEnabler.useCheckSettings ] is a valid hook method", () => {
28-
const addListener = LocationSettingsEnabler.addListener
29-
expect(addListener).toBeTruthy()
30-
expect(addListener(listner).remove).toBeTruthy()
15+
test("[ LocationSettingsEnabler.useLocationSettings ] is a valid hook method", () => {
16+
const useLocationSettings = LocationSettingsEnabler.useLocationSettings
17+
expect(useLocationSettings).toBeTruthy()
18+
const [enabled, requestResolution] = useLocationSettings(options)
19+
expect(enabled).toBe(Boolean)
20+
expect(requestResolution).toBeTruthy()
21+
expect(requestResolution()).toBeUndefined()
3122
})
3223

3324
test("[ LocationSettingsEnabler.addListener ] is a valid function subscriber", () => {

src/index.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { useState, useEffect } from "react"
22
import { NativeModules, NativeEventEmitter } from "react-native"
3-
import type { LocationSettingsEnablerType, Listener, Config, LocationStatus } from "./types"
3+
import type {
4+
LocationSettingsEnablerType,
5+
Listener,
6+
Config,
7+
LocationStatus,
8+
LocationSettings,
9+
} from "./types"
410

511
const { LocationSettingsEnabler } = NativeModules
612
const EVENT_NAME = "onChangeLocationSettings"
@@ -16,10 +22,10 @@ LocationSettingsEnabler.once = (listener: Listener, context?: any) =>
1622

1723
LocationSettingsEnabler.PRIORITIES = LocationSettingsEnabler.getConstants()
1824

19-
LocationSettingsEnabler.useCheckSettings = (
25+
LocationSettingsEnabler.useLocationSettings = (
2026
settings: Config,
2127
initial?: LocationStatus,
22-
): LocationStatus => {
28+
): LocationSettings => {
2329
const [enabled, setEnabled] = useState<LocationStatus>(initial || undefined)
2430
useEffect(() => {
2531
const listner = LocationSettingsEnabler.addListener(({ locationEnabled }) =>
@@ -29,23 +35,7 @@ LocationSettingsEnabler.useCheckSettings = (
2935
if (enabled) listner.remove()
3036
return () => listner.remove()
3137
}, [enabled])
32-
return enabled
33-
}
34-
35-
LocationSettingsEnabler.useRequestResolutionSettings = (
36-
settings: Config,
37-
initial?: LocationStatus,
38-
): LocationStatus => {
39-
const [enabled, setEnabled] = useState<LocationStatus>(initial || undefined)
40-
useEffect(() => {
41-
const listner = LocationSettingsEnabler.addListener(({ locationEnabled }) =>
42-
setEnabled(locationEnabled),
43-
)
44-
LocationSettingsEnabler.requestResolutionSettings(settings)
45-
if (enabled) listner.remove()
46-
return () => listner.remove()
47-
}, [enabled])
48-
return enabled
38+
return [enabled, () => LocationSettingsEnabler.requestResolutionSettings(settings)]
4939
}
5040

5141
export default LocationSettingsEnabler as LocationSettingsEnablerType

src/types.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export type Listener = (args: { locationEnabled: boolean }) => void
5252

5353
export type LocationStatus = boolean | undefined
5454

55+
export type LocationSettings = [LocationStatus, () => void]
56+
5557
export interface LocationSettingsEnablerType {
5658
/**
5759
* Static object contain a list quality of service for location updates.
@@ -62,22 +64,13 @@ export interface LocationSettingsEnablerType {
6264
PRIORITIES: Priorities
6365

6466
/**
65-
* Hooks let you check if the user's device location is turned off / on.
66-
*
67-
* @param config -
68-
* @param initial? -
69-
* @returns boolean | false - location status
70-
*/
71-
useCheckSettings(config: Config, initial?: LocationStatus): LocationStatus
72-
73-
/**
74-
* Hooks let display an activity where the user's can turn location 'on'
67+
* Hook let you check the user's device location status 'on' / 'off' and method let you display an activity where they can turn location 'on'.
7568
*
7669
* @param config -
7770
* @param initial? -
78-
* @returns boolean | false - location status
71+
* @returns LocationSettings - location status
7972
*/
80-
useRequestResolutionSettings(config: Config, initial?: LocationStatus): LocationStatus
73+
useLocationSettings(config: Config, initial?: LocationStatus): LocationSettings
8174

8275
/**
8376
* Checking if the user's device location is turned off.

0 commit comments

Comments
 (0)