Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const locationFile = FileManager.local().joinPath(FileManager.local().temporaryDirectory(), 'location.txt');
- let location = null;
- // Attempt to retrieve current location
- try {
- location = await Location.current();
- } catch (error) {
- console.error('Error retrieving location:', error);
- }
- // Fallback to stored location data if current location retrieval fails
- if (!location) {
- try {
- const storedLocationData = FileManager.local().readString(locationFile);
- if (storedLocationData) {
- location = JSON.parse(storedLocationData);
- console.log('Using stored location data as a fallback:', location);
- } else {
- console.error('No location data available.');
- }
- } catch (error) {
- console.error('Error reading stored location data:', error);
- }
- }
- // Update stored location data with the current location (if retrieved)
- if (location) {
- FileManager.local().writeString(locationFile, JSON.stringify(location));
- }
- if (location) {
- const lat = location.latitude;
- const lon = location.longitude;
- const uvIndexRequest = new Request(`https://api.openweathermap.org/data/3.0/onecall?lat=${lat}&lon=${lon}&exclude=hourly,minutely,alerts&appid=REPLACEWITHAPIKEY`);
- const uvIndexResponse = await uvIndexRequest.loadJSON();
- const currentUVIndex = uvIndexResponse.current.uvi.toFixed(1);
- const todayMaxUVIndex = uvIndexResponse.daily[0].uvi.toFixed(1);
- const tomorrowMaxUVIndex = uvIndexResponse.daily[1].uvi.toFixed(1);
- const todayMaxUVIndexTime = new Date(uvIndexResponse.daily[0].dt * 1000).toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' });
- const tomorrowMaxUVIndexTime = new Date(uvIndexResponse.daily[1].dt * 1000).toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' });
- // Create widget
- let widget = new ListWidget();
- widget.setPadding(8, 16, 16, 0);
- // Add title
- let titleText = widget.addText('UV Index βοΈ');
- titleText.font = Font.boldSystemFont(16);
- titleText.textColor = Color.white();
- widget.addSpacer(0);
- // Add current UV index
- let currentUVIndexText = widget.addText(currentUVIndex);
- currentUVIndexText.font = Font.systemFont(36);
- currentUVIndexText.textColor = Color.white();
- widget.addSpacer(30);
- // Determine the current date and tomorrow's date
- const now = new Date();
- const today = now.toLocaleDateString('en-US', { day: 'numeric', month: 'long' });
- const tomorrow = new Date(now);
- tomorrow.setDate(tomorrow.getDate() + 1);
- const tomorrowFormatted = tomorrow.toLocaleDateString('en-US', { day: 'numeric', month: 'long' });
- // Add maximum UV index for today or tomorrow
- let maxUVIndexText;
- let maxUVIndexTimeText;
- if (now.getHours() >= 20) {
- maxUVIndexText = widget.addText(`Tomorrow's Max: ${tomorrowMaxUVIndex}`);
- maxUVIndexTimeText = widget.addText(`(around ${tomorrowMaxUVIndexTime})`);
- } else {
- maxUVIndexText = widget.addText(`Today's Max: ${todayMaxUVIndex}`);
- maxUVIndexTimeText = widget.addText(`(around ${todayMaxUVIndexTime})`);
- }
- maxUVIndexText.font = Font.systemFont(14);
- maxUVIndexText.textColor = Color.white();
- maxUVIndexTimeText.font = Font.systemFont(12);
- maxUVIndexTimeText.textColor = Color.white();
- // Set widget background color
- widget.backgroundColor = new Color("#B2675E");
- // Present widget
- if (config.runsInWidget) {
- // Display widget in the widget area
- Script.setWidget(widget);
- } else {
- // Display widget in the app
- widget.presentMedium();
- }
- Script.complete();
- } else {
- console.error('Location data not available.');
- }
Advertisement
Add Comment
Please, Sign In to add comment