diff --git a/app/controllers/index.js b/app/controllers/index.js index 8a580b9..b0652c3 100755 --- a/app/controllers/index.js +++ b/app/controllers/index.js @@ -3,21 +3,32 @@ import TiWidgetKit from 'ti.widgetkit'; const GROUP_IDENTIFIER = 'group.io.tidev.sample-widgetkit'; const USER_DEFAULTS_IDENTIFIER = 'kSampleAppMyData'; +const userDefaults = Ti.App.iOS.createUserDefaults({ suiteName: GROUP_IDENTIFIER }); + +function onWindowFocus() { + refreshUI(); +} + +function refreshUI() { + const data = userDefaults.getObject(USER_DEFAULTS_IDENTIFIER); + + $.title.text = data.title; + $.count.text = `${data.count}`; +} + function saveData() { - const payload = { title: 'My Title', count: 1337 }; + const data = userDefaults.getObject(USER_DEFAULTS_IDENTIFIER); + const payload = { title: data.title, count: data.count + 1 }; // 1) Save data - const userDefaults = Ti.App.iOS.createUserDefaults({ suiteName: GROUP_IDENTIFIER }); userDefaults.setObject(USER_DEFAULTS_IDENTIFIER, payload); // 2) Refresh widget UI TiWidgetKit.reloadAllTimelines(); + refreshUI(); - // NOTE: The widget will show the "Default" title until your app groups are properly linked to your - // main app and extension. Please read this article very carefully before jumping into more detail: + // NOTE: Please read this article very carefully before jumping into more detail: // https://developer.apple.com/documentation/widgetkit/creating-a-widget-extension - - alert('Done!'); } $.getView().open(); diff --git a/app/views/index.xml b/app/views/index.xml index fd2b69f..7632147 100755 --- a/app/views/index.xml +++ b/app/views/index.xml @@ -1,7 +1,11 @@ - -