Advertisement
Guest User

thxgarmin

a guest
Jan 12th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // background process
  2. function OWM_Update(responseCode as Number, data as Dictionary or String or Null) as Void {
  3.     // backgroundData should be a local variable. There shouldn't be any need
  4.     // to declare it as a global variable or class variable
  5.     var backgroundData = {
  6.         "responseCode" => responseCode,
  7.         "temperature" => null // Numeric?
  8.     };
  9.  
  10.     if (responseCode != 200) {
  11.         System.println("NO WEATHER DATA");
  12.     } else {
  13.         System.println("WEATHER DATA OK");
  14.  
  15.         backgroundData["temperature"] = ((data as Dictionary)["main"] as Dictionary)["temp"];
  16.     }
  17.     Background.exit(backgroundData);
  18. }
  19.  
  20. // ...
  21.  
  22. // app class
  23. class MyAppClass extends Application.AppBase {
  24.     // ...
  25.  
  26.     var view as MyViewClass? = null;
  27.  
  28.     function getInitialView() as [WatchUi.Views] or [WatchUi.Views, WatchUi.InputDelegates] {
  29.         view = new MyViewClass();
  30.         return [view];
  31.     }
  32.  
  33.     function onBackgroundData(data) {
  34.         var d = data as Dictionary;
  35.         // no need for null check as the background process doesn't return null,
  36.         // and if it did, onBackgroundData() wouldn't be called anyway
  37.  
  38.         var temperature = d["temperature"] as Numeric?;
  39.  
  40.         // do you want to persist a null temperature or not?
  41.         //
  42.         // if you are going to display a null temperature as "--"
  43.         // perhaps it would be consistent to persist a null temperature as well
  44.         //
  45.         // On the other hand if you are not going to persist a null temperature
  46.         // (instead storing the last good temperature), perhaps it would
  47.         // be consistent to display the last good temperature instead of "--",
  48.         // when temperature is null
  49.  
  50.         // code which doesn't persist null temperature:
  51.         // if (temperature != null) {
  52.         //     Application.Storage.setValue("temperature", temperature);
  53.         // }
  54.  
  55.         // code which does persist null temperature:
  56.         Application.Storage.setValue("temperature", temperature);
  57.  
  58.         if (view != null) {
  59.           view.updateTemperature(temperature);
  60.         }
  61.     }
  62. }
  63.  
  64. class MyViewClass extends WatchUi.View {
  65.     // ...
  66.     var temperature = null
  67.     function updateTemperature(newTemperature as Numeric?) {
  68.         temperature = newTemperature;
  69.         WatchUi.requestUpdate(); // trigger call to onUpdate
  70.     }
  71.  
  72.     function onUpdate() {
  73.         // ...
  74.         var temperatureString = "--";
  75.         if (temperature != null) {
  76.             temperatureString = temperature.format("%.1f"); // format to 1 decimal place
  77.         }
  78.  
  79.         // display temperatureString
  80.         // ...
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement