public override void ViewDidLoad() { base.ViewDidLoad(); LoadTimerUserDefaults(); if (Running) { StartButton.SetImage(Resources.StopButton, UIControlState.Normal); clockTimer = NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromSeconds(1), UpdateTimerDisplay); } else { StartButton.SetImage(Resources.StartButton, UIControlState.Normal); } StartButton.TouchUpInside += delegate { if (Running) { Running = false; SetTimerUserDefaults(); TimeSpan elapsedTime = DateTime.Now - StartDate; int totalSeconds = (int)Math.Truncate(elapsedTime.TotalSeconds); TripLog.StopTripItem(totalSeconds); ClearNotifications(); StartButton.SetImage(Resources.StartButton, UIControlState.Normal); //StartButton.SetTitle("Start", UIControlState.Normal); clockTimer.Invalidate(); clockTimer = null; //CostLabel.Text = "£0.00"; //ElapsedLabel.Text = "0h 00m"; PriceIncreaseLabel.Text = "0m"; StartedLabel.Text = "00:00"; Analytics.TrackEvent(Analytics.CATEGORY_ACTION, Analytics.ACTION_TIMER_STOP, "", 1); Analytics.Dispatch(); //BackgroundLocationManager.Instance.StopLocationManager(); } else { StartDate = DateTime.Now; Running = true; TripLog.StartNewTripItem(); SetNotification(StartDate.AddMinutes(30)); SetTimerUserDefaults(); StartButton.SetImage(Resources.StopButton, UIControlState.Normal); //StartButton.SetTitle("Stop", UIControlState.Normal); UpdateTimerDisplay(); clockTimer = NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromSeconds(1), UpdateTimerDisplay); //BackgroundLocationManager.Instance.StartLocationManager(); Analytics.TrackPageView("/timerstart"); Analytics.TrackEvent(Analytics.CATEGORY_ACTION, Analytics.ACTION_TIMER_START, "", 1); Analytics.Dispatch(); } }; }
public static bool UpdateFromWebsite(NSAction onDone) { if (isUpdating) { return(false); } isUpdating = true; Analytics.TrackPageView("/download"); Analytics.TrackEvent(Analytics.CATEGORY_ACTION, Analytics.ACTION_DOWNLOAD, "", 1); ThreadPool.QueueUserWorkItem(delegate { using (NSAutoreleasePool pool = new NSAutoreleasePool()) { Util.Log("updating....."); if (Util.IsReachable("www.fastchicken.co.nz")) { //Thread.Sleep(500); Util.TurnOnNetworkActivity(); try { string s = ""; try { var wc = new WebClient(); //this would normally call the live service. This one is the same, but a static file s = wc.DownloadString("http://www.fastchicken.co.nz/lba/docks.txt"); } catch (Exception ex) { Util.Log("Error downloading from the server."); Util.Log(ex.Message); return; } if (s == "KEY NOT VALID") { return; } string[] items = s.Split('^'); foreach (BikeLocation bike in AllBikes) { bike.Touched = false; } foreach (var item in items) { string[] bits = item.Split('|'); try { BikeLocation location = null; if (bikesById.ContainsKey(bits[0])) { location = bikesById[bits[0]]; } else { location = new BikeLocation(bits, true); AddBike(location); } location.DocksAvailable = -1; location.IsAvailable = bits[4].ToLower() == "true"; location.DocksAvailable = Int32.Parse(bits[6]); location.Capacity = Int32.Parse(bits[5]); if (bits.Length == 8) { location.BikesAvailable = Int32.Parse(bits[7]); } else { location.BikesAvailable = location.Capacity - location.DocksAvailable; } location.Touched = true; } catch { //do nothing } } List <BikeLocation> DocksToRemove = new List <BikeLocation>(); foreach (BikeLocation bike in AllBikes) { if (!bike.Touched) { Util.Log("Found untouched bike: {0}", bike.Name); DocksToRemove.Add(bike); } } foreach (BikeLocation bike in DocksToRemove) { RemoveBike(bike); } WriteToPersistentFile(); Util.Log("updated"); } finally { Util.TurnOffNetworkActivity(); isUpdating = false; } if (onDone != null) { onDone(); } } else { Util.Log("No network or website not reachable"); } } }); return(true); }