Skip to content

Commit 6c0ffdf

Browse files
authored
Update Weather Overview beta.js
1 parent 431a40e commit 6c0ffdf

File tree

1 file changed

+96
-48
lines changed

1 file changed

+96
-48
lines changed

Weather Overview/Weather Overview beta.js

Lines changed: 96 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Start of Setup
2828
2929
><><><><><><><><><><><*/
3030

31-
let settings = {
31+
/*let settings = {
3232
"apiKey":"3b70d09bec54f8b555452513fd4be001",
3333
"units":"imperial",
3434
"showWindspeed": true,
@@ -38,7 +38,26 @@ let settings = {
3838
"showHumidity": true,
3939
"showLegend": true,
4040
"showAlerts": true
41-
}
41+
}*/
42+
const localFm = FileManager.local();
43+
const settingsPath = localFm.documentsDirectory() + '/weatherOverviewSettings.json';
44+
let settings = {};
45+
loadSettings();
46+
await promptSettings();
47+
48+
if(config.runsInApp)await promptResetSettings();
49+
50+
/*let settings = {
51+
apiKey: "",
52+
units: "",
53+
showWindspeed: true,
54+
showWindArrow: true,
55+
showPrecipitation: true,
56+
showCloudCover: true,
57+
showHumidity: true,
58+
showLegend: true,
59+
showAlerts: true
60+
};*/
4261

4362
//settings variables initialization
4463

@@ -579,60 +598,88 @@ async function updateCheck(version){
579598
/*
580599
#####
581600
End Update Check
582-
#####g
601+
#####
583602
*/
584603
}
585604

586-
async function setup(full){
587-
588-
//log(settings)
589-
if (!('apiKey' in settings) || settings.apiKey == ""){
590-
let q = new Alert()
591-
q.title='API Key'
592-
q.message='Please paste in your OpenWeatherMap API Key'
593-
q.addTextField('API Key',Pasteboard.paste())
594-
q.addAction("Done")
595-
await q.present()
596-
if(q.textFieldValue(0).length < 1)throw new Error("You must enter an API Key, please copy to clipboard and try again")
597-
settings.apiKey = q.textFieldValue(0)
598-
//write the settings to iCloud Drive
599-
fm.writeString(settingsPath, JSON.stringify(settings))
605+
function loadSettings() {
606+
try {
607+
if (localFm.fileExists(settingsPath)) {
608+
settings = JSON.parse(localFm.readString(settingsPath));
609+
}
610+
} catch (e) {
611+
if (config.runsInApp) log(`Failed to load settings: ${e}`);
600612
}
613+
}
601614

602-
if (!('units' in settings)){
603-
let q = new Alert()
604-
q.title="Units"
605-
q.addAction("Imperial")
606-
q.addAction("Metric")
607-
a=await q.presentSheet()
608-
settings['units'] = (a==0)?'imperial':'metric'
615+
function saveSettings() {
616+
try {
617+
localFm.writeString(settingsPath, JSON.stringify(settings));
618+
} catch (e) {
619+
if (config.runsInApp) log(`Failed to save settings: ${e}`);
609620
}
621+
}
610622

611-
let quests = [{'key':'showWindspeed',
612-
'q':'Do you want display the windspeed on the widget?'},
613-
{'key':'showWindArrow','q':'If using windspeed, do you want to display the wind direction as an arrow?'},
614-
{'key':'showPrecipitation','q':'Do you want to display precipitation information?'},
615-
{'key':'showCloudCover','q':'Do you want to show the line display of the cloud cover?'},
616-
{'key':'showHumidity','q':'Do you want to show the line display of the humidity level?'},
617-
{'key':'showLegend','q':'Do you want to display the legend at the top of the widget?'},
618-
{'key':'showAlerts','q':'Do you want to show alerts in your area? A yellow warning triangle for each weather alert in your area will be displayed. Tapping the widget will take you to the OpenWeather page in Safari.'}]
619-
620-
await quests.reduce(async (memo,i)=>{
621-
await memo
622-
623-
if(!(i.key in settings)){
624-
let q = new Alert()
625-
q.message=String(i.q)
626-
q.title='Setup'
627-
q.addAction('Yes')
628-
q.addAction('No')
629-
a=await q.presentSheet()
630-
settings[i.key]=(a==0)?true:false
623+
async function promptSettings() {
624+
const settingsPrompts = [
625+
{ key: 'apiKey', question: 'Please paste in your OpenWeatherMap API Key', type: 'text' },
626+
{ key: 'units', question: 'Choose units:', type: 'selection', options: ['Imperial', 'Metric'] },
627+
{ key: 'showWindspeed', question: 'Display the windspeed on the widget?', type: 'boolean' },
628+
{ key: 'showWindArrow', question: 'Display the wind direction as an arrow?', type: 'boolean' },
629+
{ key: 'showPrecipitation', question: 'Display precipitation information?', type: 'boolean' },
630+
{ key: 'showCloudCover', question: 'Show the line display of the cloud cover?', type: 'boolean' },
631+
{ key: 'showHumidity', question: 'Show the line display of the humidity level?', type: 'boolean' },
632+
{ key: 'showLegend', question: 'Display the legend at the top of the widget?', type: 'boolean' },
633+
{ key: 'showAlerts', question: 'Show alerts in your area?', type: 'boolean' }
634+
];
635+
636+
for (const prompt of settingsPrompts) {
637+
if (!(prompt.key in settings) || !settings[prompt.key]) {
638+
const q = new Alert();
639+
q.title = 'Setup';
640+
q.message = prompt.question;
641+
642+
if (prompt.type === 'text') {
643+
q.addTextField('', Pasteboard.paste());
644+
q.addAction('Done');
645+
await q.present();
646+
settings[prompt.key] = q.textFieldValue(0);
647+
} else if (prompt.type === 'selection') {
648+
for (const option of prompt.options) {
649+
q.addAction(option);
650+
}
651+
const choice = await q.presentSheet();
652+
settings[prompt.key] = prompt.options[choice].toLowerCase();
653+
} else if (prompt.type === 'boolean') {
654+
q.addAction('Yes');
655+
q.addAction('No');
656+
const choice = await q.presentSheet();
657+
settings[prompt.key] = (choice === 0);
658+
}
631659
}
632-
},undefined)
633-
if(config.runsInApp)log(JSON.stringify(settings))
634-
fm.writeString(settingsPath, JSON.stringify(settings))
635-
return true
660+
}
661+
662+
saveSettings();
663+
}
664+
665+
async function promptResetSettings() {
666+
const resetAlert = new Alert();
667+
resetAlert.title = 'Reset Settings';
668+
resetAlert.message = 'Do you want to reset the settings?';
669+
resetAlert.addAction('Yes');
670+
resetAlert.addAction('No');
671+
const choice = await resetAlert.presentAlert();
672+
673+
if (choice === 0) {
674+
resetSettings();
675+
}
676+
}
677+
678+
async function resetSettings() {
679+
settings = {};
680+
saveSettings();
681+
if (config.runsInApp) log('Settings have been reset.');
682+
await promptSettings(); // Re-run the settings questions
636683
}
637684

638685
async function tintSFSymbol(image, color) {
@@ -673,3 +720,4 @@ function logTime(label, startTime) {
673720
const duration = (Date.now() - startTime) / 1000;
674721
console.log(`${label} took: ${duration}s`);
675722
}
723+

0 commit comments

Comments
 (0)