Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // background process
- function OWM_Update(responseCode as Number, data as Dictionary or String or Null) as Void {
- // backgroundData should be a local variable. There shouldn't be any need
- // to declare it as a global variable or class variable
- var backgroundData = {
- "responseCode" => responseCode,
- "temperature" => null // Numeric?
- };
- if (responseCode != 200) {
- System.println("NO WEATHER DATA");
- } else {
- System.println("WEATHER DATA OK");
- backgroundData["temperature"] = ((data as Dictionary)["main"] as Dictionary)["temp"];
- }
- Background.exit(backgroundData);
- }
- // ...
- // app class
- class MyAppClass extends Application.AppBase {
- // ...
- var view as MyViewClass? = null;
- function getInitialView() as [WatchUi.Views] or [WatchUi.Views, WatchUi.InputDelegates] {
- view = new MyViewClass();
- return [view];
- }
- function onBackgroundData(data) {
- var d = data as Dictionary;
- // no need for null check as the background process doesn't return null,
- // and if it did, onBackgroundData() wouldn't be called anyway
- var temperature = d["temperature"] as Numeric?;
- // do you want to persist a null temperature or not?
- //
- // if you are going to display a null temperature as "--"
- // perhaps it would be consistent to persist a null temperature as well
- //
- // On the other hand if you are not going to persist a null temperature
- // (instead storing the last good temperature), perhaps it would
- // be consistent to display the last good temperature instead of "--",
- // when temperature is null
- // code which doesn't persist null temperature:
- // if (temperature != null) {
- // Application.Storage.setValue("temperature", temperature);
- // }
- // code which does persist null temperature:
- Application.Storage.setValue("temperature", temperature);
- if (view != null) {
- view.updateTemperature(temperature);
- }
- }
- }
- class MyViewClass extends WatchUi.View {
- // ...
- var temperature = null
- function updateTemperature(newTemperature as Numeric?) {
- temperature = newTemperature;
- WatchUi.requestUpdate(); // trigger call to onUpdate
- }
- function onUpdate() {
- // ...
- var temperatureString = "--";
- if (temperature != null) {
- temperatureString = temperature.format("%.1f"); // format to 1 decimal place
- }
- // display temperatureString
- // ...
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement