Refactor the connection loading of traces, to not reload every time.
authorAndy Allan <gravitystorm@gmail.com>
Mon, 18 Oct 2010 13:13:47 +0000 (13:13 +0000)
committerAndy Allan <gravitystorm@gmail.com>
Mon, 18 Oct 2010 13:13:47 +0000 (13:13 +0000)
Also:
* Provide a mechanism to force a reload, and use it in MyGPX
* Move the once-only stuff in MyGPX to the init method1
* Rework the displayed text slightly
* Squish a timing-related bug with calling fetchUserTraces and *then* setting the display text

net/systemeD/halcyon/connection/Connection.as
net/systemeD/halcyon/connection/XMLConnection.as
net/systemeD/potlatch2/mygpx/MyGpxDialog.mxml

index dfc8aa0..a2bbc68 100755 (executable)
@@ -97,6 +97,7 @@ package net.systemeD.halcyon.connection {
                public var waycount:int=0;
                public var relationcount:int=0;
         private var traces:Array = [];
+        protected var traces_loaded:Boolean = false;
 
         protected function get nextNegative():Number {
             return negativeID--;
@@ -363,7 +364,7 @@ package net.systemeD.halcyon.connection {
            public function createChangeset(tags:Object):void {}
                public function closeChangeset():void {}
            public function uploadChanges():void {}
-        public function fetchUserTraces():void {}
+        public function fetchUserTraces(refresh:Boolean=false):void {}
         public function fetchTrace(id:Number, callback:Function):void {}
     }
 
index 63ea309..30d3915 100644 (file)
@@ -320,10 +320,14 @@ package net.systemeD.halcyon.connection {
             }
         }
 
-        override public function fetchUserTraces():void {
-            sendOAuthGet(Connection.apiBaseURL+"user/gpx_files",
-                         tracesLoadComplete, errorOnMapLoad, mapLoadStatus); //needs error handlers
-            dispatchEvent(new Event(LOAD_STARTED)); //specific to map or reusable?
+        override public function fetchUserTraces(refresh:Boolean=false):void {
+            if (traces_loaded && !refresh) {
+              dispatchEvent(new Event(TRACES_LOADED));
+            } else {
+              sendOAuthGet(Connection.apiBaseURL+"user/gpx_files",
+                          tracesLoadComplete, errorOnMapLoad, mapLoadStatus); //needs error handlers
+              dispatchEvent(new Event(LOAD_STARTED)); //specific to map or reusable?
+            }
         }
 
         private function tracesLoadComplete(event:Event):void {
@@ -333,6 +337,7 @@ package net.systemeD.halcyon.connection {
               var t:Trace = new Trace().fromXML(traceData);
               addTrace(t);
             }
+            traces_loaded = true;
             dispatchEvent(new Event(LOAD_COMPLETED));
             dispatchEvent(new Event(TRACES_LOADED));
         }
index c856891..11292b6 100644 (file)
     import mx.controls.Alert;
 
     private var map:Map;
+    private var conn:Connection;
 
     public function init():void {
         PopUpManager.addPopUp(this, Application(Application.application), true);
         PopUpManager.centerPopUp(this);
         this.addEventListener(CloseEvent.CLOSE, myGpxDialog_close);
 
+        conn = Connection.getConnectionInstance();
         map = Globals.vars.root;
+
+        SaveManager.getAccessSorted();
+        conn.addEventListener(Connection.TRACES_LOADED, onTracesLoaded);
         fetchList();
     }
 
     private function myGpxDialog_close(evt:CloseEvent):void {
         PopUpManager.removePopUp(this);
+        conn.removeEventListener(Connection.TRACES_LOADED, onTracesLoaded);
     }
 
-    private function fetchList():void {
-        SaveManager.getAccessSorted();
-        var conn:Connection = Connection.getConnectionInstance();
-        conn.addEventListener(Connection.TRACES_LOADED, onTracesLoaded);
-        conn.fetchUserTraces();
-        
-        results.text = "fetching...";
+    private function fetchList(refresh:Boolean=false):void {
+        results.text = "Fetching Traces...";
+        conn.fetchUserTraces(refresh);
     }
 
     private function onTracesLoaded(e:Event):void {
-        results.text = "loaded!!";
+        results.text = "Traces Loaded";
         dispatchEvent(new Event("traces_loaded"));
     }
 
     [Bindable(event="traces_loaded")]
     private function get traces():Array {
-        var conn:Connection = Connection.getConnectionInstance();
         return conn.getTraces();
     }
 
@@ -68,7 +69,7 @@
               ]]>
   </mx:Script>
 
-  <mx:Text text="foo" id="results" />
+  <mx:Text text="Traces" id="results" />
   <mx:DataGrid dataProvider="{traces}" width="100%" height="100%" id="traceGrid">
     <mx:columns>
             <mx:DataGridColumn editable="false" dataField="id" headerText="id" width="45"/>
@@ -88,6 +89,6 @@
     </mx:columns>
   </mx:DataGrid>
   <mx:ControlBar>
-    <mx:Button label="Refresh List" click="fetchList()" />
+    <mx:Button label="Refresh List" click="fetchList(true)" />
   </mx:ControlBar>
 </mx:TitleWindow>
\ No newline at end of file