예제 #1
0
        public MainViewModel()
        {
            SwitchFromToLocationsCommand = new RelayCommand(() =>
            {
                var d = DirectionsCriteria.From;
                DirectionsCriteria.From = DirectionsCriteria.To;
                DirectionsCriteria.From = d;
            });

            #if DEBUG
            if (IsInDesignMode)
            {
                UserPinLog = new UserPinLog();
                UserPinLog.Pins.Add(
                    new UserPin
                    {
                        Address = "South Dowling St, NSW 2017",
                        Color = SettingsViewModel.DefaultPushpinColor.Value,
                        Icon = SettingsViewModel.DefaultPushpinIcon.Value,
                        Key = new Guid("4596fb13-aeda-4608-97f0-c1d688ce2eea"),
                        Location = new Location {Latitude = -33.91, Longitude = 151.21},
                        Title = "this is a very long name, to test a good max height of the name box of user pin page"
                    }
                    );
                TemporaryPins = new ObservableCollection<UserPin>
                {
                    new UserPin
                    {
                        Address = "South Dowling St, NSW 2017",
                        Color = SettingsViewModel.DefaultPushpinColor.Value,
                        Icon = SettingsViewModel.DefaultPushpinIcon.Value,
                        Key = new Guid("4596fb13-aeda-4608-97f0-c1d688ce2eeb"),
                        Location = new Location {Latitude = -35, Longitude = 151.21},
                        Title = "this is a very long name, just to mess around",
                        IsTemporary = true
                    }
                };
                Itineraries = new Collection<ItineraryItem>
                {
                    new ItineraryItem
                    {
                        Distance = 0.1D,
                        Instruction = "DepartSouth Dowling St toward O'Dea Ave",
                        Location = new Location
                        {
                            Latitude = -33.91,
                            Longitude = 151.21
                        }
                    }
                };
                MapCenter = new GeoCoordinate {Latitude = -33.91, Longitude = 151.21};
                ZoomLevel = 8;
                EditingUserPin = UserPinLog.Pins.First();

                return;
            }
            #endif

            GeoWatcher.PositionChanged += (s, e) => CurrentLocation = GeoWatcher.Position.Location;
        }
예제 #2
0
        public static void Sync(UserLogin login, UserPinLog log, Action<HttpStatusCode> callback)
        {
            // Download data from server first.
            Download(login, downloadRsp =>
            {
                // Check if download is successful.
                if (downloadRsp.StatusCode == HttpStatusCode.NotFound)
                {
                    // If there is no online data, just upload local.
                    Upload(login, log.Pins, uploadRsp => SyncCallback(ref log, uploadRsp.StatusCode, callback));
                }
                else if (downloadRsp.StatusCode != HttpStatusCode.OK)
                {
                    SyncCallback(ref log, downloadRsp.StatusCode, callback);
                }
                else
                {
                    // If download is successful, try deserialize the data.
                    ObservableCollection<UserPin> onlinePins;
                    try
                    {
                        onlinePins = (ObservableCollection<UserPin>)SilverlightSerializer.Deserialize(downloadRsp.RawBytes);
                    }
                    catch
                    {
                        // If serialization fails, then the online data file might be corrupted.
                        // In this case, re-upload local pins to overwrite the online data file.
                        Upload(login, log.Pins, uploadRsp => SyncCallback(ref log, uploadRsp.StatusCode, callback));
                        return;
                    }
                    if (onlinePins.Count == 0)
                    {
                        // If online doesn't have pins, just upload local.
                        Upload(login, log.Pins, uploadRsp => SyncCallback(ref log, uploadRsp.StatusCode, callback));
                    }
                    else if (log.Pins.Count == 0 && log.Changes.Count == 0)
                    {
                        // If local doesn't have pins, just replace with online.
                        log.Pins = onlinePins;
                        callback(HttpStatusCode.OK);
                    }
                    else
                    {
                        // Worst case, do delta checks and update accordingly.
                        UserPin tempPin;
                        UserPinChange change;

                        // Process each online pin.
                        foreach (var onlinePin in onlinePins)
                        {
                            // Check if the local pin list contains the current online pin.
                            tempPin = log.GetPin(onlinePin.Key);
                            if (tempPin != null)
                            {
                                continue;
                            }
                            // If local doesn't have this online pin, check if this pin has been deleted locally.
                            change = log.GetChange(onlinePin.Key);
                            if (change == null)
                            {
                                // If the pin is not in the chagne list, then this pin is added by another source and synced to the online place.
                                // Add this pin to the local list, without logging it into the change list.
                                onlinePin.IsEditing = false;
                                log.Pins.Add(onlinePin);
                            }
                            else if (change.Action == UserPinAction.Delete)
                            {
                                // If the pin is in the chagne list, and the pin is marked for deletion,
                                // ignore adding this pin as it's supposed to be deleted.
                                continue;
                            }
                            else
                            {
                                // If none of the above happens, then there's something wrong, throw the exception.
                                throw new InvalidOperationException("There shouldn't be the case that an online user pin is missing from local list and the registered local change action for this pin is not deletion.");
                            }
                        }

                        var removeList = new Collection<UserPin>();
                        // Process each local pin.
                        foreach (var localPin in log.Pins)
                        {
                            // Check if the online pin list contains the current local pin.
                            tempPin = onlinePins.GetUserPin(localPin.Key);
                            if (tempPin != null)
                            {
                                continue;
                            }
                            // If online doesn't have this local pin, check if this pin is newly added.
                            change = log.GetChange(localPin.Key);
                            if (change == null)
                            {
                                // If this local pin is not in the change list, then this pin is removed by another source and synced to the online place.
                                // Add it to the pending remove list.
                                removeList.Add(localPin);
                            }
                            else if (change.Action == UserPinAction.Add)
                            {
                                // If the pin is in the change list, and the pin is marked as newly added,
                                // ignore removing this pin as it's supposed to be added.
                                continue;
                            }
                            else
                            {
                                // If none of the above happens, then there's somehting wrong, throw the exception.
                                throw new InvalidOperationException("There shouldn't be the case that an local user pin is missing from the online list and the registered local change action is not addition.");
                            }
                        }

                        // Remove pins from the pending removal list.
                        foreach (var localPin in removeList)
                        {
                            log.Pins.Remove(localPin);
                        }

                        // Clear pending removal list.
                        removeList.Clear();

                        // We are done syncing the pins locally, upload them now.
                        Upload(login, log.Pins, uploadRsp => SyncCallback(ref log, uploadRsp.StatusCode, callback));
                    }
                }
            });
        }
예제 #3
0
 private static void SyncCallback(ref UserPinLog log, HttpStatusCode statusCode, Action<HttpStatusCode> callback)
 {
     if (statusCode == HttpStatusCode.OK)
     {
         log.Changes.Clear();
     }
     callback(statusCode);
 }