-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
333 lines (293 loc) · 11.6 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// utils.js
import * as Location from 'expo-location';
import { Alert, Linking, Platform } from 'react-native';
import moment from 'moment';
import locationsData from './database/locations_basic.json'; // Ensure this path matches the location of your locations data file
// Haversine formula to calculate distance between two coordinates
export const getDistance = (lat1, lng1, lat2, lng2) => {
const toRad = (x) => (x * Math.PI) / 180;
const R = 6371; // Earth radius in km
const dLat = toRad(lat2 - lat1);
const dLng = toRad(lng2 - lng1);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(toRad(lat1)) * Math.cos(toRad(lat2))
* Math.sin(dLng / 2) * Math.sin(dLng / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
};
// Function to filter locations that are open at the current time
export const filterOpenLocations = (categoryData) => {
const now = moment();
const currentDay = now.day(); // 0-6 where 0 is Sunday and 6 is Saturday
const currentTime = now.format('HH:mm');
const isOpen = (openHours) => openHours.some((hours) => {
if (hours.days.includes(currentDay)) {
const openTime = moment(hours.open, 'HH:mm').subtract(1, 'hour'); // Adjusted filter to include 1 hour before
const closeTime = moment(hours.close, 'HH:mm');
// Handle case where close time is past midnight
if (closeTime.isBefore(openTime)) {
return currentTime >= openTime.format('HH:mm') || currentTime <= closeTime.format('HH:mm');
}
return currentTime >= openTime.format('HH:mm') && currentTime <= closeTime.format('HH:mm');
}
return false;
});
const openLocations = categoryData.filter((location) => isOpen(location.openHours || []));
console.log('Current Day:', currentDay);
console.log('Current Time:', currentTime);
console.log('Filtered Locations:', openLocations);
return openLocations;
};
// Function to find the closest location from a category
export const findClosestLocation = async (category, userLocation) => {
if (!userLocation) {
throw new Error('User location is not provided');
}
const categoryData = locationsData[category];
if (!categoryData) {
Alert.alert('Error', `Category ${category} not found`);
return null;
}
const openLocations = filterOpenLocations(categoryData);
if (!openLocations || openLocations.length === 0) {
console.log('No open locations found in the selected category');
return null;
}
let closestLocation = openLocations[0];
let shortestDistance = getDistance(
userLocation.latitude,
userLocation.longitude,
closestLocation.coordinates.lat,
closestLocation.coordinates.lng,
);
openLocations.forEach((location) => {
const distance = getDistance(
userLocation.latitude,
userLocation.longitude,
location.coordinates.lat,
location.coordinates.lng,
);
if (distance < shortestDistance) {
shortestDistance = distance;
closestLocation = location;
}
});
return { location: closestLocation, distance: shortestDistance };
};
// Function to get the user's current location
export const getUserLocation = async () => {
try {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
Alert.alert('Permission Denied', 'Permission to access location was denied');
return null;
}
const location = await Location.getCurrentPositionAsync({});
return location.coords;
} catch (error) {
console.error('Location Error', "Couldn't get location");
throw error;
}
};
// Function to open Google Maps with given coordinates and mode
// Function to open Google Maps or Moovit with given coordinates and mode
export const openGoogleMaps = (lat, lng, mode = 'w') => {
const modeMappingApp = {
driving: 'd', // Google Maps app uses 'd' for driving
walking: 'w', // Google Maps app uses 'w' for walking
bicycling: 'bicycling',
transit: 'transit',
bus: 'transit', // Specifying 'transit' as the mode for bus in native app
};
const modeMappingWeb = {
driving: 'driving',
walking: 'walking',
bicycling: 'bicycling',
transit: 'transit',
bus: 'transit', // Web also uses 'transit' for bus
};
const modeParamApp = modeMappingApp[mode] || 'd'; // Default to driving if mode is not recognized for native apps
const modeParamWeb = modeMappingWeb[mode] || 'driving'; // Default to driving for web
if (mode === 'transit') {
const moovitUrl = `moovit://directions?dest_lat=${lat}&dest_lon=${lng}&travelMode=publicTransport`;
// Try to open Moovit for transit
Linking.openURL(moovitUrl).catch(() => {
openGoogleMapsViaLinking(lat, lng, modeParamApp, modeParamWeb);
});
} else {
openGoogleMapsViaLinking(lat, lng, modeParamApp, modeParamWeb);
}
};
const openGoogleMapsViaLinking = (lat, lng, modeParamApp, modeParamWeb) => {
const baseUrl = Platform.OS === 'android'
? `google.navigation:q=${lat},${lng}&mode=${modeParamApp}`
: `comgooglemaps://?daddr=${lat},${lng}&directionsmode=${modeParamApp}`;
const webUrl = `https://www.google.com/maps/dir/?api=1&destination=${lat},${lng}&travelmode=${modeParamWeb}`;
// Try to open the native Google Maps
Linking.canOpenURL(baseUrl).then((supported) => {
if (supported) {
Linking.openURL(baseUrl);
} else {
// If the app isn't installed, fall back to opening in a web browser
Linking.openURL(webUrl).catch(() => {
Alert.alert('Error', 'Unable to open Google Maps in a browser.');
});
}
}).catch(() => {
Alert.alert('Error', 'An error occurred while trying to open the map.');
});
};
// Return Colors for Special Requirement Background and Text Colors
export const requirementsColorMapping = (colorName) => {
const mappings = {
Red: {
backgroundColor: '#ffd1d1', // Example light red
textColor: 'darkred', // Darker red for text
},
Yellow: {
backgroundColor: '#ffe8ad', // Example light yellow
textColor: '#543c00', // Darker yellow for text
},
Green: {
backgroundColor: '#c1fcbb', // Example light green
textColor: '#256029', // Darker green for text
},
};
return mappings[colorName] || { backgroundColor: '#FFFFFF', textColor: '#000000' }; // Default case
};
// Return Formatted week schedule based on database
export const formatOpenHours = (openHoursArray) => {
const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
// // Ensure we're working with an array for both old and new structures
// if (!Array.isArray(openHoursArray)) {
// // If openHoursArray is not an array, assume it's the old single object structure
// openHoursArray = [openHoursArray]; // Wrap it in an array for compatibility
// }
// Function to format a single openHours object
const formatSingleOpenHours = (openHours) => {
if (openHours.days.length === 7 && openHours.days.every((val, i) => val === i)) {
return `Everyday \n${formatTime(openHours.open)} - ${formatTime(openHours.close)}`;
}
const daysFormatted = openHours.days.reduce((acc, day, index, arr) => {
if (index > 0 && day - arr[index - 1] === 1) {
acc[acc.length - 1].push(day);
} else {
acc.push([day]);
}
return acc;
}, []).map((group) => {
if (group.length > 1) {
return `${daysOfWeek[group[0]]} - ${daysOfWeek[group[group.length - 1]]}`;
}
return daysOfWeek[group[0]];
}).join(', ');
return `${daysFormatted} \n ${formatTime(openHours.open)} - ${formatTime(openHours.close)}`;
};
// Iterate over each openHours object, format it, and combine the results
return openHoursArray.map(formatSingleOpenHours).join('\n\n');
};
// Convert 24-hour format to 12-hour format
export const formatTime = (time) => {
const [hours, minutes] = time.split(':').map(Number);
const isPM = hours >= 12;
const formattedHours = (((hours + 11) % 12) + 1);
return `${formattedHours}:${minutes < 10 ? '0' : ''}${minutes} ${isPM ? 'pm' : 'am'}`;
};
export const updateLocationStatus = (openHoursArray) => {
// If time info not found, immediately return a status indicating
// that the location operates by schedule only
if (!openHoursArray || openHoursArray.length === 0) {
return { status: 'scheduleOnly', message: 'Hours by Appointment', time: '' };
}
const now = moment();
const dayOfWeek = now.day(); // Sunday as 0 through Saturday as 6
// let currentOpenHours = null;
// Find open hours for today
const currentOpenHours = openHoursArray.find((openHours) => openHours.days.includes(dayOfWeek));
let status = '';
let time = '';
if (currentOpenHours) {
const openTime = moment(currentOpenHours.open, 'HH:mm');
const closeTime = moment(currentOpenHours.close, 'HH:mm');
const closingSoonTime = moment(closeTime).subtract(1, 'hours');
// Determine status based on current time if open today
if (now.isBetween(openTime, closingSoonTime)) {
status = 'open';
time = formatTime(currentOpenHours.close);
} else if (now.isBetween(closingSoonTime, closeTime)) {
status = 'closingSoon';
time = formatTime(currentOpenHours.close);
} else if (now.isAfter(closeTime)) {
status = 'closed';
time = '';
} else if (now.isBefore(openTime) && now.isAfter(moment(openTime).subtract(1, 'hours'))) {
status = 'openingSoon';
time = `Today ${formatTime(currentOpenHours.open)}`;
} else if (now.isBefore(openTime) && now.isSame(moment(), 'day')) {
// Add this condition to check if the location opens later today
status = 'closed';
time = `Today ${formatTime(currentOpenHours.open)}`;
} else {
status = 'closed';
}
}
// If closed or not open today, find the next open day
if (!currentOpenHours || (status === 'closed' && time === '')) {
const sortedDays = openHoursArray.reduce((acc, { days }) => [...acc, ...days], []).filter(
(day) => day > dayOfWeek,
).sort();
const nextDay = sortedDays.length ? sortedDays[0]
: openHoursArray.reduce((acc, oh) => acc.concat(oh.days), []).sort()[0];
const daysUntilNextOpen = nextDay >= dayOfWeek ? nextDay - dayOfWeek : nextDay + 7 - dayOfWeek;
const nextOpenDate = moment().add(daysUntilNextOpen, 'days');
const nextOpenHours = openHoursArray.find((oh) => oh.days.includes(nextOpenDate.day()));
const nextOpenDayFormatted = nextOpenDate.isSame(moment().add(1, 'days'), 'day') ? 'Tomorrow' : nextOpenDate.format('dddd');
time = `${nextOpenDayFormatted} at ${formatTime(nextOpenHours.open)}`;
status = 'closed';
}
// Constructing the message based on status
const message = status === 'open' || status === 'closingSoon' ? 'Will Close' : 'Will Open';
return { status, message, time };
};
export const getStatusStyles = (status) => {
let backgroundColor;
let textColor;
let text;
switch (status) {
case 'closingSoon':
backgroundColor = '#ffe8ad';
textColor = '#543c00';
text = 'Closes Soon';
break;
case 'scheduleOnly':
backgroundColor = '#ffe8ad';
textColor = '#543c00';
text = 'Schedule Only';
break;
case 'openingSoon':
backgroundColor = '#c1fcbb';
textColor = '#075400';
text = 'Opens Soon';
break;
case 'open':
backgroundColor = '#c1fcbb';
textColor = '#075400';
text = 'Opens Now';
break;
case 'closed':
backgroundColor = '#ffd1d1';
textColor = 'darkred';
text = 'Closes Now';
break;
default:
backgroundColor = 'transparent';
textColor = 'black';
text = '';
break;
}
return {
statusBackgroundColor: { backgroundColor },
statusTextStyleColor: textColor,
statusText: text,
};
};