Fix list selector on load
[Hacks.git] / Main.js
diff --git a/Main.js b/Main.js
index c8a2c19..086d9eb 100644 (file)
--- a/Main.js
+++ b/Main.js
@@ -32,12 +32,14 @@ enyo.kind({
        ],
        create: function() {
                this.inherited(arguments);
-               /*
-               this.index = localStorage.getItem('hack.index');
-               if (!this.index)
+               try {
+                       this.index = parseInt(localStorage.getItem('hack.index'));
+                       if (!this.index)
+                               this.index = 0;
+               } catch(e) {
+                       enyo.log("Could not load last hack index");
                        this.index = 0;
-               */
-               this.index = 0;
+               }
                this.lastScrollPos = 0;
                this.$.hacksCarousel.setCenterView(this.getHack(this.index));
        },
@@ -50,8 +52,10 @@ enyo.kind({
                        });
                }
                this.$.hacksListSelector.setItems(displayList);
+               this.$.hacksListSelector.setValue(this.index);
                this.startHack();
                window.addEventListener('resize', this.resizeHack.bind(this), false);
+               this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
                this.setNotice('Swipe for more...');
        },
        setNotice: function(notice) {
@@ -83,11 +87,17 @@ enyo.kind({
                }
        },
        savePreferences: function(inSender, prefs) {
-               enyo.log("Saving prefs: " + JSON.stringify(prefs));
-               this.back();
                var view = this.$.hacksCarousel.fetchView('center');
-               view.setPreferences(prefs);
-               view.start();
+               if (prefs == null) {
+                       enyo.log("Clearing prefs");
+                       view.resetPreferences(prefs);
+                       this.selectHack(this, this.index);
+               } else {
+                       enyo.log("Saving prefs: " + JSON.stringify(prefs));
+                       view.setPreferences(prefs);
+                       view.start();
+               }
+               this.back();
        },
        windowActivated: function() {
                this.startHack();
@@ -95,7 +105,7 @@ enyo.kind({
        windowDeactivated: function() {
                this.stopHack();
        },
-       selectHack: function(inSender, inValue, inOldValue) {
+       selectHack: function(inSender, inValue) {
                this.stopHack();
                this.index = inValue;
                this.lastScrollPos = 0;
@@ -111,7 +121,11 @@ enyo.kind({
                //enyo.log('starting view ' + view);
                if (view)
                        view.start();
-               //localStorage.setItem('hack.index', this.index);
+               try {
+                       localStorage.setItem('hack.index', this.index);
+               } catch(e) {
+                       enyo.log("Could not set hack index");
+               }
        },
        stopHack: function(direction) {
                var view = this.$.hacksCarousel.fetchView(direction || 'center');