-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget-task-handler.js
More file actions
202 lines (172 loc) · 6.55 KB
/
Copy pathwidget-task-handler.js
File metadata and controls
202 lines (172 loc) · 6.55 KB
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
import React from 'react';
import { requestWidgetUpdate } from 'react-native-android-widget';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { calculatePrayerTimes } from './src/utils/prayerEngine';
import { PrayerWidget } from './src/widgets/PrayerWidget';
const ASR_METHOD_KEY = '@muslimatlas_asr_method';
const PRAYER_OFFSETS_KEY = '@muslimatlas_prayer_offsets';
const CALCULATION_METHOD_KEY = '@muslimatlas_calc_method';
const HIGH_LATITUDE_RULE_KEY = '@muslimatlas_high_lat_rule';
export async function widgetTaskHandler(props) {
const { widgetAction } = props;
if (widgetAction === 'WIDGET_ADDED' || widgetAction === 'WIDGET_UPDATE' || widgetAction === 'WIDGET_RESIZED') {
// 1. Load preferences and location from AsyncStorage
let lat = 51.5074; // London fallback
let lng = -0.1278;
let asrMethod = 'standard';
let prayerOffsets = { Fajr: 0, Sunrise: 0, Dhuhr: 0, Asr: 0, Maghrib: 0, Isha: 0 };
let calculationMethod = 'MuslimWorldLeague';
let highLatitudeRule = 'Auto';
let locationName = 'Muslim Atlas';
try {
const cachedLoc = await AsyncStorage.getItem('@cached_location');
if (cachedLoc) {
const parsedLoc = JSON.parse(cachedLoc);
if (parsedLoc?.latitude && parsedLoc?.longitude) {
lat = parsedLoc.latitude;
lng = parsedLoc.longitude;
}
}
const storedLocName = await AsyncStorage.getItem('@cached_location_name');
if (storedLocName) {
locationName = storedLocName;
}
const storedAsr = await AsyncStorage.getItem(ASR_METHOD_KEY);
if (storedAsr) {
asrMethod = storedAsr;
}
const storedOffsets = await AsyncStorage.getItem(PRAYER_OFFSETS_KEY);
if (storedOffsets) {
prayerOffsets = JSON.parse(storedOffsets);
}
const storedCalc = await AsyncStorage.getItem(CALCULATION_METHOD_KEY);
if (storedCalc) {
calculationMethod = storedCalc;
}
const storedHighLat = await AsyncStorage.getItem(HIGH_LATITUDE_RULE_KEY);
if (storedHighLat) {
highLatitudeRule = storedHighLat;
}
} catch (e) {
console.warn('Widget task handler failed to load config:', e);
}
// 2. Calculate prayer times
const now = new Date();
const times = calculatePrayerTimes(lat, lng, now, {
asrMethod,
prayerOffsets,
calculationMethod,
highLatitudeRule,
});
// 3. Find next upcoming prayer
const currentMs = now.getHours() * 60 + now.getMinutes();
const getMs = (timeString) => {
if (!timeString) return 0;
const timeStr = timeString.split(' ')[0];
if (!timeStr || !timeStr.includes(':')) return 0;
const [h, m] = timeStr.split(':').map(Number);
return (isNaN(h) || isNaN(m)) ? 0 : h * 60 + m;
};
const prayers = [
{ name: 'Fajr', ms: getMs(times.Fajr) },
{ name: 'Sunrise', ms: getMs(times.Sunrise) },
{ name: 'Dhuhr', ms: getMs(times.Dhuhr) },
{ name: 'Asr', ms: getMs(times.Asr) },
{ name: 'Maghrib', ms: getMs(times.Maghrib) },
{ name: 'Isha', ms: getMs(times.Isha) },
];
let nextPrayer = prayers.find(p => p.ms > currentMs);
const nextPrayerName = nextPrayer ? nextPrayer.name : 'Fajr';
let currentPrayer = [...prayers].reverse().find(p => currentMs >= p.ms);
if (!currentPrayer) {
currentPrayer = prayers[prayers.length - 1]; // Isha if before Fajr
}
const currentPrayerName = currentPrayer.name;
// 4. Render the widget UI
props.renderWidget(
<PrayerWidget
prayerTimes={times}
nextPrayerName={nextPrayerName}
currentPrayerName={currentPrayerName}
locationName={locationName}
/>
);
}
}
/**
* Triggers a manual update of all active widget instances.
*/
export async function updateAppWidgets() {
try {
let lat = 51.5074;
let lng = -0.1278;
let asrMethod = 'standard';
let prayerOffsets = { Fajr: 0, Sunrise: 0, Dhuhr: 0, Asr: 0, Maghrib: 0, Isha: 0 };
let calculationMethod = 'MuslimWorldLeague';
let highLatitudeRule = 'Auto';
let locationName = 'Muslim Atlas';
const cachedLoc = await AsyncStorage.getItem('@cached_location');
if (cachedLoc) {
const parsedLoc = JSON.parse(cachedLoc);
if (parsedLoc?.latitude && parsedLoc?.longitude) {
lat = parsedLoc.latitude;
lng = parsedLoc.longitude;
}
}
const storedLocName = await AsyncStorage.getItem('@cached_location_name');
if (storedLocName) {
locationName = storedLocName;
}
const storedAsr = await AsyncStorage.getItem(ASR_METHOD_KEY);
if (storedAsr) asrMethod = storedAsr;
const storedOffsets = await AsyncStorage.getItem(PRAYER_OFFSETS_KEY);
if (storedOffsets) prayerOffsets = JSON.parse(storedOffsets);
const storedCalc = await AsyncStorage.getItem(CALCULATION_METHOD_KEY);
if (storedCalc) calculationMethod = storedCalc;
const storedHighLat = await AsyncStorage.getItem(HIGH_LATITUDE_RULE_KEY);
if (storedHighLat) highLatitudeRule = storedHighLat;
const now = new Date();
const times = calculatePrayerTimes(lat, lng, now, {
asrMethod,
prayerOffsets,
calculationMethod,
highLatitudeRule,
});
const currentMs = now.getHours() * 60 + now.getMinutes();
const getMs = (timeString) => {
if (!timeString) return 0;
const timeStr = timeString.split(' ')[0];
if (!timeStr || !timeStr.includes(':')) return 0;
const [h, m] = timeStr.split(':').map(Number);
return (isNaN(h) || isNaN(m)) ? 0 : h * 60 + m;
};
const prayers = [
{ name: 'Fajr', ms: getMs(times.Fajr) },
{ name: 'Sunrise', ms: getMs(times.Sunrise) },
{ name: 'Dhuhr', ms: getMs(times.Dhuhr) },
{ name: 'Asr', ms: getMs(times.Asr) },
{ name: 'Maghrib', ms: getMs(times.Maghrib) },
{ name: 'Isha', ms: getMs(times.Isha) },
];
let nextPrayer = prayers.find(p => p.ms > currentMs);
const nextPrayerName = nextPrayer ? nextPrayer.name : 'Fajr';
let currentPrayer = [...prayers].reverse().find(p => currentMs >= p.ms);
if (!currentPrayer) {
currentPrayer = prayers[prayers.length - 1]; // Isha if before Fajr
}
const currentPrayerName = currentPrayer.name;
requestWidgetUpdate({
widgetName: 'PrayerWidget',
renderWidget: () => (
<PrayerWidget
prayerTimes={times}
nextPrayerName={nextPrayerName}
currentPrayerName={currentPrayerName}
locationName={locationName}
/>
),
});
} catch (e) {
console.warn('Failed to update app widgets:', e);
}
}