@umituz/react-native-location 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +127 -0
- package/lib/domain/entities/Location.d.ts +144 -0
- package/lib/domain/entities/Location.d.ts.map +1 -0
- package/lib/domain/entities/Location.js +52 -0
- package/lib/domain/entities/Location.js.map +1 -0
- package/lib/index.d.ts +95 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +95 -0
- package/lib/index.js.map +1 -0
- package/lib/infrastructure/services/LocationService.d.ts +93 -0
- package/lib/infrastructure/services/LocationService.d.ts.map +1 -0
- package/lib/infrastructure/services/LocationService.js +250 -0
- package/lib/infrastructure/services/LocationService.js.map +1 -0
- package/lib/presentation/hooks/useLocation.d.ts +95 -0
- package/lib/presentation/hooks/useLocation.d.ts.map +1 -0
- package/lib/presentation/hooks/useLocation.js +156 -0
- package/lib/presentation/hooks/useLocation.js.map +1 -0
- package/package.json +57 -0
- package/src/domain/entities/Location.ts +175 -0
- package/src/index.ts +107 -0
- package/src/infrastructure/services/LocationService.ts +298 -0
- package/src/presentation/hooks/useLocation.ts +225 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useLocation Hook
|
|
3
|
+
*
|
|
4
|
+
* React hook for device location services
|
|
5
|
+
* Wraps locationService with React-friendly state management
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Get current location with loading state
|
|
9
|
+
* - Permission management
|
|
10
|
+
* - Cached location access
|
|
11
|
+
* - Error handling
|
|
12
|
+
* - Format locations for display
|
|
13
|
+
*
|
|
14
|
+
* USAGE:
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { useLocation } from '@umituz/react-native-location';
|
|
17
|
+
*
|
|
18
|
+
* const MyComponent = () => {
|
|
19
|
+
* const {
|
|
20
|
+
* location,
|
|
21
|
+
* loading,
|
|
22
|
+
* error,
|
|
23
|
+
* hasPermission,
|
|
24
|
+
* requestPermission,
|
|
25
|
+
* getCurrentLocation,
|
|
26
|
+
* getCached,
|
|
27
|
+
* formatLocation,
|
|
28
|
+
* } = useLocation();
|
|
29
|
+
*
|
|
30
|
+
* useEffect(() => {
|
|
31
|
+
* // Auto-fetch location on mount
|
|
32
|
+
* getCurrentLocation();
|
|
33
|
+
* }, []);
|
|
34
|
+
*
|
|
35
|
+
* if (loading) return <LoadingIndicator />;
|
|
36
|
+
* if (error) return <ErrorMessage message={error} />;
|
|
37
|
+
*
|
|
38
|
+
* return (
|
|
39
|
+
* <View>
|
|
40
|
+
* {location && (
|
|
41
|
+
* <Text>Location: {formatLocation(location)}</Text>
|
|
42
|
+
* )}
|
|
43
|
+
* <AtomicButton onPress={getCurrentLocation}>
|
|
44
|
+
* Refresh Location
|
|
45
|
+
* </AtomicButton>
|
|
46
|
+
* </View>
|
|
47
|
+
* );
|
|
48
|
+
* };
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
import { useState, useCallback, useEffect } from 'react';
|
|
53
|
+
import { locationService } from '../../infrastructure/services/LocationService';
|
|
54
|
+
import type {
|
|
55
|
+
LocationData,
|
|
56
|
+
LocationPermissionStatus,
|
|
57
|
+
} from '../../domain/entities/Location';
|
|
58
|
+
|
|
59
|
+
export interface UseLocationReturn {
|
|
60
|
+
/** Current location data (null if not retrieved) */
|
|
61
|
+
location: LocationData | null;
|
|
62
|
+
|
|
63
|
+
/** Loading state during location retrieval */
|
|
64
|
+
loading: boolean;
|
|
65
|
+
|
|
66
|
+
/** Error message if location retrieval failed */
|
|
67
|
+
error: string | null;
|
|
68
|
+
|
|
69
|
+
/** Current permission status */
|
|
70
|
+
permissionStatus: LocationPermissionStatus;
|
|
71
|
+
|
|
72
|
+
/** Whether location permissions are granted */
|
|
73
|
+
hasPermission: boolean;
|
|
74
|
+
|
|
75
|
+
/** Whether location services are available on this platform */
|
|
76
|
+
isAvailable: boolean;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Request location permissions from user
|
|
80
|
+
* @returns Promise resolving to true if granted
|
|
81
|
+
*/
|
|
82
|
+
requestPermission: () => Promise<boolean>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get current location with fallback strategies
|
|
86
|
+
* Updates location state and handles errors
|
|
87
|
+
*/
|
|
88
|
+
getCurrentLocation: () => Promise<void>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Get cached location if available
|
|
92
|
+
* @returns Cached location or null
|
|
93
|
+
*/
|
|
94
|
+
getCached: () => LocationData | null;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Format location for display
|
|
98
|
+
* @param loc - LocationData to format
|
|
99
|
+
* @returns Formatted string (address or coordinates)
|
|
100
|
+
*/
|
|
101
|
+
formatLocation: (loc: LocationData) => string;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Clear current location state
|
|
105
|
+
*/
|
|
106
|
+
clearLocation: () => void;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Hook for device location services
|
|
111
|
+
*/
|
|
112
|
+
export const useLocation = (): UseLocationReturn => {
|
|
113
|
+
const [location, setLocation] = useState<LocationData | null>(null);
|
|
114
|
+
const [loading, setLoading] = useState<boolean>(false);
|
|
115
|
+
const [error, setError] = useState<string | null>(null);
|
|
116
|
+
const [permissionStatus, setPermissionStatus] =
|
|
117
|
+
useState<LocationPermissionStatus>('unknown');
|
|
118
|
+
|
|
119
|
+
// Check if location services are available
|
|
120
|
+
const isAvailable = locationService.isLocationAvailable();
|
|
121
|
+
|
|
122
|
+
// Check initial permission status
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
const checkPermission = async () => {
|
|
125
|
+
const hasPermission = await locationService.hasPermissions();
|
|
126
|
+
setPermissionStatus(
|
|
127
|
+
hasPermission ? 'granted' : locationService.getPermissionStatus()
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
if (isAvailable) {
|
|
132
|
+
checkPermission();
|
|
133
|
+
}
|
|
134
|
+
}, [isAvailable]);
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Request location permissions
|
|
138
|
+
*/
|
|
139
|
+
const requestPermission = useCallback(async (): Promise<boolean> => {
|
|
140
|
+
if (!isAvailable) {
|
|
141
|
+
setError('Location services not available on this platform');
|
|
142
|
+
setPermissionStatus('denied');
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
const granted = await locationService.requestPermissions();
|
|
148
|
+
setPermissionStatus(granted ? 'granted' : 'denied');
|
|
149
|
+
|
|
150
|
+
if (!granted) {
|
|
151
|
+
setError('Location permission denied');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return granted;
|
|
155
|
+
} catch (err) {
|
|
156
|
+
setError('Failed to request location permission');
|
|
157
|
+
setPermissionStatus('denied');
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
}, [isAvailable]);
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Get current location with fallback strategies
|
|
164
|
+
*/
|
|
165
|
+
const getCurrentLocation = useCallback(async (): Promise<void> => {
|
|
166
|
+
if (!isAvailable) {
|
|
167
|
+
setError('Location services not available on this platform');
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
setLoading(true);
|
|
172
|
+
setError(null);
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
const currentLocation = await locationService.getCurrentLocation();
|
|
176
|
+
|
|
177
|
+
if (currentLocation) {
|
|
178
|
+
setLocation(currentLocation);
|
|
179
|
+
setError(null);
|
|
180
|
+
} else {
|
|
181
|
+
setError('Unable to retrieve location');
|
|
182
|
+
}
|
|
183
|
+
} catch (err) {
|
|
184
|
+
setError('Failed to get location');
|
|
185
|
+
} finally {
|
|
186
|
+
setLoading(false);
|
|
187
|
+
}
|
|
188
|
+
}, [isAvailable]);
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Get cached location
|
|
192
|
+
*/
|
|
193
|
+
const getCached = useCallback((): LocationData | null => {
|
|
194
|
+
return locationService.getCachedLocation();
|
|
195
|
+
}, []);
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Format location for display
|
|
199
|
+
*/
|
|
200
|
+
const formatLocation = useCallback((loc: LocationData): string => {
|
|
201
|
+
return locationService.formatLocation(loc);
|
|
202
|
+
}, []);
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Clear location state
|
|
206
|
+
*/
|
|
207
|
+
const clearLocation = useCallback((): void => {
|
|
208
|
+
setLocation(null);
|
|
209
|
+
setError(null);
|
|
210
|
+
}, []);
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
location,
|
|
214
|
+
loading,
|
|
215
|
+
error,
|
|
216
|
+
permissionStatus,
|
|
217
|
+
hasPermission: permissionStatus === 'granted',
|
|
218
|
+
isAvailable,
|
|
219
|
+
requestPermission,
|
|
220
|
+
getCurrentLocation,
|
|
221
|
+
getCached,
|
|
222
|
+
formatLocation,
|
|
223
|
+
clearLocation,
|
|
224
|
+
};
|
|
225
|
+
};
|