From 9e3a696a3dfaed85ad38ea9a32a1f341ad03c87f Mon Sep 17 00:00:00 2001
From: Stefan Seiz <seiz@imd.net>
Date: Sat, 2 Jan 2021 13:21:36 +0100
Subject: [PATCH 1/5] Update number-of-covild-19-vaccinations.js

- prevent error if user deletes Widget-"Parameter"
- format numbers with more readable units and their correct abbreviation
---
 .../number-of-covild-19-vaccinations.js       | 52 +++++++++++++++----
 1 file changed, 42 insertions(+), 10 deletions(-)

diff --git a/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js b/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
index 285477d..9a73591 100644
--- a/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
+++ b/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
@@ -1,11 +1,19 @@
+// Home: https://github.com/ThisIsBenny/iOS-Widgets/tree/main/number-of-covild-19-vaccinations
+// Variables used by Scriptable.
+// These must be at the very top of the file. Do not edit.
+// icon-color: red; icon-glyph: syringe;
 // Variables used by Scriptable.
 // These must be at the very top of the file. Do not edit.
 // icon-color: red; icon-glyph: syringe;
 
 /**************
-Version 1.0.1
+Version 1.0.2
 
 Changelog:
+  v1.0.2
+          - prevent error if user deletes Widget-"Parameter"
+          - format numbers with more readable units and their correct abbreviation
+          
   v1.0.1:
           - fix sorting issue
 
@@ -32,7 +40,7 @@ config.widgetFamily = config.widgetFamily || 'large'
 let widgetInputRAW = args.widgetParameter;
 let selectedState
 
-if (widgetInputRAW !== null) {
+if (widgetInputRAW !== null && widgetInputRAW !== "" ) {
   if (/^Baden-Württemberg|Bayern|Berlin|Brandenburg|Bremen|Hamburg|Hessen|Mecklenburg-Vorpommern|Niedersachsen|Nordrhein-Westfalen|Rheinland-Pfalz|Saarland|Sachsen|Sachsen-Anhalt|Schleswig-Holstein|Thüringen$/.test(widgetInputRAW.toString().trim()) === false) {
      throw new Error('Kein gültiges Bundesland. Bitte prüfen Sie die Eingabe.') 
   }
@@ -279,19 +287,32 @@ if (config.widgetFamily === 'large') {
     imageStack1.addSpacer()
     column.addSpacer(2)
     
-    const total1 = parseInt((result.states[selectedState].total / 1000).toFixed(0)).toLocaleString('de')
+    let total1 = (result.states[selectedState].total / 1000).toFixed(0)
+    let total1unit = " Tsd."
+    // if total is a million or more, format as millions and not thousands
+    if ( result.states[selectedState].total > 999999 ){
+      total1 =  (result.states[selectedState].total / 1000000).toFixed(0)
+      total1unit = " Mio."
+    }
     let vaccinated1
-    if (result.states[selectedState].vaccinated > 999) {
-      vaccinated1 = parseInt((result.states[selectedState].vaccinated / 1000).toFixed(0)).toLocaleString('de') + 't'
+    let vaccinated1unit = ""
+    if ( result.states[selectedState].vaccinated > 999999){
+      vaccinated1 =  (result.states[selectedState].vaccinated / 1000000).toFixed(0)
+      vaccinated1unit = " Mio."
+    }
+    else if ( result.states[selectedState].vaccinated > 999 ) {
+      vaccinated1 =  (result.states[selectedState].vaccinated / 1000).toFixed(0)
+      vaccinated1unit = " Tsd."
     } else {
       vaccinated1 = result.states[selectedState].vaccinated
+      
     }
     
     const numbersText1Stack = column.addStack()
     numbersText1Stack.layoutHorizontally()
     numbersText1Stack.addSpacer()
     
-    const numbersText1 = numbersText1Stack.addText(`${vaccinated1} von ${total1}t`)  
+    const numbersText1 = numbersText1Stack.addText(`${vaccinated1}${vaccinated1unit} von ${total1}${total1unit}`)  
     numbersText1.font = Font.systemFont(fontSize2)
     numbersText1Stack.addSpacer()
     
@@ -316,14 +337,25 @@ if (config.widgetFamily === 'large') {
     imageStack2.addImage(getDiagram(result.quote));
     imageStack2.addSpacer()
     
-    const total2 = parseInt((result.total / 1000).toFixed(0)).toLocaleString('de')
-    let vaccinated2 = parseInt((result.vaccinated / 1000).toFixed(0)).toLocaleString('de')
+    let total2 = (result.total / 1000).toFixed(0)
+    let total2unit = " Tsd."
+    // if total is a million or more, format as millions and not thousands
+    if ( result.total > 999999 ){
+    	total2 = (result.total / 1000000).toFixed(0)
+    	total2unit = " Mio."
+    }
+    let vaccinated2 = (result.vaccinated / 1000).toFixed(0)
+    let vaccinated2unit = " Tsd."
+    if ( result.vaccinated > 999999 ){
+    	vaccinated2 = (result.vaccinated / 1000000).toFixed(0)
+    	vaccinated2unit = " Mio."
+    }
     
     const numbersText2Stack = column2.addStack()
     numbersText2Stack.layoutHorizontally()
     numbersText2Stack.addSpacer()
     
-    const numbersText2 = numbersText2Stack.addText(`${vaccinated2}t von ${total2}t`)  
+    const numbersText2 = numbersText2Stack.addText(`${vaccinated2}${vaccinated2unit} von ${total2}${total2unit}`)  
     numbersText2.font = Font.systemFont(fontSize2)
     numbersText2Stack.addSpacer()
     
@@ -356,4 +388,4 @@ if (!config.runsInWidget) {
 } else {
   Script.setWidget(widget)
 }
-Script.complete()
\ No newline at end of file
+Script.complete()

From da9005bb5afde7dfec8fa44de3ac5f92d45b0546 Mon Sep 17 00:00:00 2001
From: Stefan Seiz <seiz@imd.net>
Date: Sat, 2 Jan 2021 14:11:42 +0100
Subject: [PATCH 2/5] Amend number formating

---
 .../number-of-covild-19-vaccinations.js                   | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js b/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
index 9a73591..12aa26d 100644
--- a/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
+++ b/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
@@ -291,13 +291,13 @@ if (config.widgetFamily === 'large') {
     let total1unit = " Tsd."
     // if total is a million or more, format as millions and not thousands
     if ( result.states[selectedState].total > 999999 ){
-      total1 =  (result.states[selectedState].total / 1000000).toFixed(0)
+      total1 =  (result.states[selectedState].total / 1000000).toLocaleString('de', {maximumFractionDigits: 1})
       total1unit = " Mio."
     }
     let vaccinated1
     let vaccinated1unit = ""
     if ( result.states[selectedState].vaccinated > 999999){
-      vaccinated1 =  (result.states[selectedState].vaccinated / 1000000).toFixed(0)
+      vaccinated1 =  (result.states[selectedState].vaccinated / 1000000).toLocaleString('de', {maximumFractionDigits: 2})
       vaccinated1unit = " Mio."
     }
     else if ( result.states[selectedState].vaccinated > 999 ) {
@@ -341,13 +341,13 @@ if (config.widgetFamily === 'large') {
     let total2unit = " Tsd."
     // if total is a million or more, format as millions and not thousands
     if ( result.total > 999999 ){
-    	total2 = (result.total / 1000000).toFixed(0)
+    	total2 = (result.total / 1000000).toLocaleString('de', {maximumFractionDigits: 1})
     	total2unit = " Mio."
     }
     let vaccinated2 = (result.vaccinated / 1000).toFixed(0)
     let vaccinated2unit = " Tsd."
     if ( result.vaccinated > 999999 ){
-    	vaccinated2 = (result.vaccinated / 1000000).toFixed(0)
+    	vaccinated2 = (result.vaccinated / 1000000).toLocaleString('de', {maximumFractionDigits: 2})
     	vaccinated2unit = " Mio."
     }
     

From cfbcbe677d1f0a9bb43057930a753de729e43604 Mon Sep 17 00:00:00 2001
From: Stefan Seiz <seiz@imd.net>
Date: Sat, 2 Jan 2021 18:10:29 +0100
Subject: [PATCH 3/5] Always use gray textColor because dynamic colors are not
 supported when used with DrawContext

Percent-Values were invisible in the videt and only visible in the scriptable.app.
---
 .../number-of-covild-19-vaccinations.js                         | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js b/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
index 12aa26d..4f61b1d 100644
--- a/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
+++ b/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
@@ -156,7 +156,7 @@ function getDiagram(percentage) {
     canvTextSize
   )
   canvas.setTextAlignedCenter()
-  canvas.setTextColor(Color.dynamic(Color.black(), Color.white()))
+  canvas.setTextColor(Color.gray())
   canvas.setFont(Font.boldSystemFont(canvTextSize))
   canvas.drawTextInRect(`${percentage}%`, canvTextRect)
 

From 8636270994c45792f8db1175febad3a3f4d8e264 Mon Sep 17 00:00:00 2001
From: Stefan Seiz <seiz@imd.net>
Date: Sat, 2 Jan 2021 18:46:38 +0100
Subject: [PATCH 4/5] Localize percent-value number format and adjust
 text-rect-height, so commas aren't cut off

---
 .../number-of-covild-19-vaccinations.js              | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js b/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
index 4f61b1d..49f3fb2 100644
--- a/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
+++ b/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
@@ -153,12 +153,12 @@ function getDiagram(percentage) {
     0,
     100 - canvTextSize / 2,
     canvSize,
-    canvTextSize
+    canvTextSize * 1.4 // X-height "* 1.4" so e.g. commas aren't cut off
   )
   canvas.setTextAlignedCenter()
   canvas.setTextColor(Color.gray())
   canvas.setFont(Font.boldSystemFont(canvTextSize))
-  canvas.drawTextInRect(`${percentage}%`, canvTextRect)
+  canvas.drawTextInRect(`${percentage.toLocaleString(Device.language())}%`, canvTextRect)
 
   return canvas.getImage()
 }
@@ -291,13 +291,13 @@ if (config.widgetFamily === 'large') {
     let total1unit = " Tsd."
     // if total is a million or more, format as millions and not thousands
     if ( result.states[selectedState].total > 999999 ){
-      total1 =  (result.states[selectedState].total / 1000000).toLocaleString('de', {maximumFractionDigits: 1})
+      total1 =  (result.states[selectedState].total / 1000000).toLocaleString(Device.language(), {maximumFractionDigits: 1})
       total1unit = " Mio."
     }
     let vaccinated1
     let vaccinated1unit = ""
     if ( result.states[selectedState].vaccinated > 999999){
-      vaccinated1 =  (result.states[selectedState].vaccinated / 1000000).toLocaleString('de', {maximumFractionDigits: 2})
+      vaccinated1 =  (result.states[selectedState].vaccinated / 1000000).toLocaleString(Device.language(), {maximumFractionDigits: 2})
       vaccinated1unit = " Mio."
     }
     else if ( result.states[selectedState].vaccinated > 999 ) {
@@ -341,13 +341,13 @@ if (config.widgetFamily === 'large') {
     let total2unit = " Tsd."
     // if total is a million or more, format as millions and not thousands
     if ( result.total > 999999 ){
-    	total2 = (result.total / 1000000).toLocaleString('de', {maximumFractionDigits: 1})
+    	total2 = (result.total / 1000000).toLocaleString(Device.language(), {maximumFractionDigits: 1})
     	total2unit = " Mio."
     }
     let vaccinated2 = (result.vaccinated / 1000).toFixed(0)
     let vaccinated2unit = " Tsd."
     if ( result.vaccinated > 999999 ){
-    	vaccinated2 = (result.vaccinated / 1000000).toLocaleString('de', {maximumFractionDigits: 2})
+    	vaccinated2 = (result.vaccinated / 1000000).toLocaleString(Device.language(), {maximumFractionDigits: 2})
     	vaccinated2unit = " Mio."
     }
     

From b0b9e7543d82b3683dc4f064136aafa8fb2ebc4e Mon Sep 17 00:00:00 2001
From: Stefan Seiz <seiz@imd.net>
Date: Sat, 2 Jan 2021 23:34:31 +0100
Subject: [PATCH 5/5] remove double Scriptable-Variables and make alternate
 units optional via an additional parameter

---
 .../number-of-covild-19-vaccinations.js       | 44 ++++++++++++-------
 1 file changed, 29 insertions(+), 15 deletions(-)

diff --git a/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js b/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
index 49f3fb2..62c8bc7 100644
--- a/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
+++ b/number-of-covild-19-vaccinations/number-of-covild-19-vaccinations.js
@@ -1,7 +1,3 @@
-// Home: https://github.com/ThisIsBenny/iOS-Widgets/tree/main/number-of-covild-19-vaccinations
-// Variables used by Scriptable.
-// These must be at the very top of the file. Do not edit.
-// icon-color: red; icon-glyph: syringe;
 // Variables used by Scriptable.
 // These must be at the very top of the file. Do not edit.
 // icon-color: red; icon-glyph: syringe;
@@ -12,7 +8,8 @@ Version 1.0.2
 Changelog:
   v1.0.2
           - prevent error if user deletes Widget-"Parameter"
-          - format numbers with more readable units and their correct abbreviation
+          - optionally format numbers with more readable units and their correct abbreviation
+            add ",1" to the widget "Parameter" to enable.
           
   v1.0.1:
           - fix sorting issue
@@ -30,6 +27,7 @@ const cacheMinutes = 6 * 60
 //////////////////////////         Dev Settings         ////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 
+let altUnits = false
 const debug = true
 config.widgetFamily = config.widgetFamily || 'large'
 
@@ -37,9 +35,14 @@ config.widgetFamily = config.widgetFamily || 'large'
 //////////////////////////         System Settings         /////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 
-let widgetInputRAW = args.widgetParameter;
+let widgetInputRAW = args.widgetParameter
 let selectedState
 
+if ( widgetInputRAW && widgetInputRAW.toString().indexOf(",") !== -1 ){
+  // multiple args
+  altUnits = widgetInputRAW.toString().split(",")[1] == "1" ? true : false
+  widgetInputRAW = widgetInputRAW.split(",")[0]
+}
 if (widgetInputRAW !== null && widgetInputRAW !== "" ) {
   if (/^Baden-Württemberg|Bayern|Berlin|Brandenburg|Bremen|Hamburg|Hessen|Mecklenburg-Vorpommern|Niedersachsen|Nordrhein-Westfalen|Rheinland-Pfalz|Saarland|Sachsen|Sachsen-Anhalt|Schleswig-Holstein|Thüringen$/.test(widgetInputRAW.toString().trim()) === false) {
      throw new Error('Kein gültiges Bundesland. Bitte prüfen Sie die Eingabe.') 
@@ -288,21 +291,26 @@ if (config.widgetFamily === 'large') {
     column.addSpacer(2)
     
     let total1 = (result.states[selectedState].total / 1000).toFixed(0)
-    let total1unit = " Tsd."
+    let total1unit = "t"
+    if ( altUnits ){
+      total1unit = " Tsd."
+    }
     // if total is a million or more, format as millions and not thousands
-    if ( result.states[selectedState].total > 999999 ){
+    if ( altUnits && result.states[selectedState].total > 999999 ){
       total1 =  (result.states[selectedState].total / 1000000).toLocaleString(Device.language(), {maximumFractionDigits: 1})
       total1unit = " Mio."
     }
     let vaccinated1
-    let vaccinated1unit = ""
-    if ( result.states[selectedState].vaccinated > 999999){
+    let vaccinated1unit = "t"
+    if ( altUnits && result.states[selectedState].vaccinated > 999999){
       vaccinated1 =  (result.states[selectedState].vaccinated / 1000000).toLocaleString(Device.language(), {maximumFractionDigits: 2})
       vaccinated1unit = " Mio."
     }
     else if ( result.states[selectedState].vaccinated > 999 ) {
       vaccinated1 =  (result.states[selectedState].vaccinated / 1000).toFixed(0)
-      vaccinated1unit = " Tsd."
+      if ( altUnits ){
+      	vaccinated1unit = " Tsd."
+      }
     } else {
       vaccinated1 = result.states[selectedState].vaccinated
       
@@ -338,15 +346,21 @@ if (config.widgetFamily === 'large') {
     imageStack2.addSpacer()
     
     let total2 = (result.total / 1000).toFixed(0)
-    let total2unit = " Tsd."
+    let total2unit = "t"
+    if ( altUnits ){
+      total2unit = " Tsd."
+    }
     // if total is a million or more, format as millions and not thousands
-    if ( result.total > 999999 ){
+    if ( altUnits && result.total > 999999 ){
     	total2 = (result.total / 1000000).toLocaleString(Device.language(), {maximumFractionDigits: 1})
     	total2unit = " Mio."
     }
     let vaccinated2 = (result.vaccinated / 1000).toFixed(0)
-    let vaccinated2unit = " Tsd."
-    if ( result.vaccinated > 999999 ){
+    let vaccinated2unit = "t"
+    if ( altUnits ){
+      vaccinated2unit = " Tsd."
+    }
+    if ( altUnits && result.vaccinated > 999999 ){
     	vaccinated2 = (result.vaccinated / 1000000).toLocaleString(Device.language(), {maximumFractionDigits: 2})
     	vaccinated2unit = " Mio."
     }