mirror of
https://github.com/ThisIsBenny/iOS-Widgets.git
synced 2025-05-02 04:37:40 +00:00
better error handling. fallback widget
Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
parent
f4addec5d2
commit
1b24b4e209
1 changed files with 102 additions and 73 deletions
|
@ -2,9 +2,21 @@
|
||||||
// These must be at the very top of the file. Do not edit.
|
// These must be at the very top of the file. Do not edit.
|
||||||
// icon-color: teal; icon-glyph: magic;
|
// icon-color: teal; icon-glyph: magic;
|
||||||
|
|
||||||
// Credits:
|
/**************
|
||||||
// - Sillium@GitHub (https://gist.github.com/Sillium/f904fb89444bc8dde12cfc07b8fa8728)
|
Version 1.0.1
|
||||||
// - Chaeimg@Github (https://github.com/chaeimg/battCircle)
|
|
||||||
|
Changelog:
|
||||||
|
v1.0.1:
|
||||||
|
- Better Error handling
|
||||||
|
- Better logging
|
||||||
|
- Fallback Widget screen in case of an error
|
||||||
|
|
||||||
|
If you have problems or need help, ask for support here: https://github.com/ThisIsBenny/iOS-Widgets/issues
|
||||||
|
|
||||||
|
Credits:
|
||||||
|
- Sillium@GitHub (https://gist.github.com/Sillium/f904fb89444bc8dde12cfc07b8fa8728)
|
||||||
|
- Chaeimg@Github (https://github.com/chaeimg/battCircle)
|
||||||
|
**************/
|
||||||
|
|
||||||
// How many minutes should the cache be valid
|
// How many minutes should the cache be valid
|
||||||
let cacheMinutes = 60;
|
let cacheMinutes = 60;
|
||||||
|
@ -101,12 +113,10 @@ async function getSessionCookies() {
|
||||||
})
|
})
|
||||||
try {
|
try {
|
||||||
let res = await req.loadJSON()
|
let res = await req.loadJSON()
|
||||||
console.log("Login sucessfull")
|
|
||||||
return { cookies: req.response.cookies, msisdn: res.msisdn}
|
return { cookies: req.response.cookies, msisdn: res.msisdn}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Login vailed! Please check if Wifi is disabled.")
|
console.log("Login vailed! Please check if Wifi is disabled.")
|
||||||
console.log(e)
|
throw new Error(`Login failed with HTTP-Status-Code ${req.response.statusCode}`)
|
||||||
throw e
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -130,15 +140,26 @@ async function getUsage() {
|
||||||
let datenContainer = res['serviceUsageVBO']['usageAccounts'][0]['usageGroup'].find(function(v){
|
let datenContainer = res['serviceUsageVBO']['usageAccounts'][0]['usageGroup'].find(function(v){
|
||||||
return v.container == "Daten"
|
return v.container == "Daten"
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (datenContainer === undefined) {
|
||||||
|
throw new Error("Can't find usageGroup 'Daten'")
|
||||||
|
} else {
|
||||||
console.log("usageGroup 'Daten' founded")
|
console.log("usageGroup 'Daten' founded")
|
||||||
|
}
|
||||||
|
|
||||||
let datenvolumen = datenContainer.usage.find(function(v){
|
let datenvolumen = datenContainer.usage.find(function(v){
|
||||||
return v.code == "-1"
|
return v.code == "-1"
|
||||||
})
|
})
|
||||||
console.log(datenvolumen)
|
|
||||||
|
if (datenvolumen === undefined) {
|
||||||
|
console.log("Can't find Usage with Code -1. Please check the following log to find the correct code for your case and addjust the code to 4 lines above: " + JSON.stringify(datenContainer.usage))
|
||||||
|
throw new Error("Can't find Usage with Code -1.")
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
total: datenvolumen.total || 0,
|
total: datenvolumen.total,
|
||||||
used: datenvolumen.used || 0,
|
used: datenvolumen.used,
|
||||||
remaining: datenvolumen.remaining || 0
|
remaining: datenvolumen.remaining
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Loading usage data failed")
|
console.log("Loading usage data failed")
|
||||||
|
@ -181,19 +202,22 @@ try {
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
|
if (cacheExists) {
|
||||||
console.log("Get from Cache")
|
console.log("Get from Cache")
|
||||||
data = JSON.parse(files.readString(cachePath))
|
data = JSON.parse(files.readString(cachePath))
|
||||||
lastUpdate = cacheDate
|
lastUpdate = cacheDate
|
||||||
|
} else {
|
||||||
|
console.log("No fallback to cache possible. Due to missing cache.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(data)
|
|
||||||
|
|
||||||
// Create Widget
|
// Create Widget
|
||||||
let widget = new ListWidget();
|
let widget = new ListWidget();
|
||||||
|
|
||||||
widget.setPadding(10, 10, 10, 10)
|
widget.setPadding(10, 10, 10, 10)
|
||||||
|
|
||||||
if (useGradient) {
|
if (data !== undefined) {
|
||||||
|
if (useGradient) {
|
||||||
const gradient = new LinearGradient()
|
const gradient = new LinearGradient()
|
||||||
gradient.locations = [0, 1]
|
gradient.locations = [0, 1]
|
||||||
gradient.colors = [
|
gradient.colors = [
|
||||||
|
@ -201,57 +225,62 @@ if (useGradient) {
|
||||||
new Color(backColor2)
|
new Color(backColor2)
|
||||||
]
|
]
|
||||||
widget.backgroundGradient = gradient
|
widget.backgroundGradient = gradient
|
||||||
} else {
|
} else {
|
||||||
widget.backgroundColor = new Color(backColor)
|
widget.backgroundColor = new Color(backColor)
|
||||||
}
|
}
|
||||||
|
|
||||||
let provider = widget.addText("Vodafone")
|
let provider = widget.addText("Vodafone")
|
||||||
provider.font = Font.mediumSystemFont(12)
|
provider.font = Font.mediumSystemFont(12)
|
||||||
provider.textColor = new Color(textColor)
|
provider.textColor = new Color(textColor)
|
||||||
|
|
||||||
widget.addSpacer()
|
widget.addSpacer()
|
||||||
|
|
||||||
let remainingPercentage = (100 / data.total * data.remaining).toFixed(0);
|
let remainingPercentage = (100 / data.total * data.remaining).toFixed(0);
|
||||||
|
|
||||||
drawArc(
|
drawArc(
|
||||||
new Point(canvSize / 2, canvSize / 2),
|
new Point(canvSize / 2, canvSize / 2),
|
||||||
canvRadius,
|
canvRadius,
|
||||||
canvWidth,
|
canvWidth,
|
||||||
Math.floor(remainingPercentage * 3.6)
|
Math.floor(remainingPercentage * 3.6)
|
||||||
);
|
);
|
||||||
|
|
||||||
const canvTextRect = new Rect(
|
const canvTextRect = new Rect(
|
||||||
0,
|
0,
|
||||||
100 - canvTextSize / 2,
|
100 - canvTextSize / 2,
|
||||||
canvSize,
|
canvSize,
|
||||||
canvTextSize
|
canvTextSize
|
||||||
);
|
);
|
||||||
canvas.setTextAlignedCenter();
|
canvas.setTextAlignedCenter();
|
||||||
canvas.setTextColor(new Color(textColor));
|
canvas.setTextColor(new Color(textColor));
|
||||||
canvas.setFont(Font.boldSystemFont(canvTextSize));
|
canvas.setFont(Font.boldSystemFont(canvTextSize));
|
||||||
canvas.drawTextInRect(`${remainingPercentage}%`, canvTextRect);
|
canvas.drawTextInRect(`${remainingPercentage}%`, canvTextRect);
|
||||||
|
|
||||||
const canvImage = canvas.getImage();
|
const canvImage = canvas.getImage();
|
||||||
let image = widget.addImage(canvImage);
|
let image = widget.addImage(canvImage);
|
||||||
image.centerAlignImage()
|
image.centerAlignImage()
|
||||||
|
|
||||||
widget.addSpacer()
|
widget.addSpacer()
|
||||||
|
|
||||||
// Total Values
|
// Total Values
|
||||||
let remainingGB = (data.remaining / 1024).toFixed(2)
|
let remainingGB = (data.remaining / 1024).toFixed(2)
|
||||||
let totalGB = (data.total / 1024).toFixed(0)
|
let totalGB = (data.total / 1024).toFixed(0)
|
||||||
let totalValuesText = widget.addText(`${remainingGB} GB von ${totalGB} GB`)
|
let totalValuesText = widget.addText(`${remainingGB} GB von ${totalGB} GB`)
|
||||||
totalValuesText.font = Font.mediumSystemFont(12)
|
totalValuesText.font = Font.mediumSystemFont(12)
|
||||||
totalValuesText.centerAlignText()
|
totalValuesText.centerAlignText()
|
||||||
totalValuesText.textColor = new Color(textColor)
|
totalValuesText.textColor = new Color(textColor)
|
||||||
|
|
||||||
// Last Update
|
// Last Update
|
||||||
widget.addSpacer(5)
|
widget.addSpacer(5)
|
||||||
let lastUpdateText = widget.addDate(lastUpdate)
|
let lastUpdateText = widget.addDate(lastUpdate)
|
||||||
lastUpdateText.font = Font.mediumSystemFont(10)
|
lastUpdateText.font = Font.mediumSystemFont(10)
|
||||||
lastUpdateText.centerAlignText()
|
lastUpdateText.centerAlignText()
|
||||||
lastUpdateText.applyTimeStyle()
|
lastUpdateText.applyTimeStyle()
|
||||||
lastUpdateText.textColor = Color.lightGray()
|
lastUpdateText.textColor = Color.lightGray()
|
||||||
|
} else {
|
||||||
|
let fallbackText = widget.addText("Es ist ein Fehler aufgetreten! Bitte prüfen Sie die Logs direkt in der App.")
|
||||||
|
fallbackText.font = Font.mediumSystemFont(12)
|
||||||
|
fallbackText.textColor = new Color(textColor)
|
||||||
|
}
|
||||||
|
|
||||||
if(!config.runsInWidget) {
|
if(!config.runsInWidget) {
|
||||||
await widget.presentSmall()
|
await widget.presentSmall()
|
||||||
|
|
Loading…
Add table
Reference in a new issue