Ejemplo n.º 1
0
        private async void LocationsPanel_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
        {
            if (!(args.Items.First() is LocationPanelViewModel panel))
            {
                return;
            }

            var data = await Settings.GetFavorites();

            int newIndex = LocationPanels.IndexOf(panel);
            int oldIndex = data.FindIndex(location => location.query == panel.LocationData.query);

            if (oldIndex != newIndex)
            {
                MoveData(panel, oldIndex, newIndex);
            }

            // Make sure we're still in EditMode after
            if (!EditMode)
            {
                ToggleEditMode();
            }

            if (oldIndex != newIndex)
            {
                DataChanged = true;
            }
            if (newIndex == App.HomeIdx)
            {
                HomeChanged = true;
            }
        }
Ejemplo n.º 2
0
        private async void LocationPanel_DeleteClick(object sender, RoutedEventArgs e)
        {
            FrameworkElement button = sender as FrameworkElement;

            if (button == null || (button != null && button.DataContext == null))
            {
                return;
            }

            LocationPanelViewModel view = button.DataContext as LocationPanelViewModel;
            LocationData           data = view.LocationData;

            // Remove location from list
            await Settings.DeleteLocation(data.query);

            // Remove panel
            LocationPanels.Remove(view);

            // Remove secondary tile if it exists
            if (SecondaryTileUtils.Exists(data.query))
            {
                await new SecondaryTile(
                    SecondaryTileUtils.GetTileId(data.query)).RequestDeleteAsync();
            }
        }
Ejemplo n.º 3
0
        private void ToggleEditMode()
        {
            // Toggle EditMode
            EditMode = !EditMode;

            EditButton.Icon  = new SymbolIcon(EditMode ? Symbol.Accept : Symbol.Edit);
            EditButton.Label = EditMode ? App.ResLoader.GetString("Label_Done") : App.ResLoader.GetString("Label_Edit");
            LocationsPanel.IsItemClickEnabled = !EditMode;

            foreach (LocationPanelViewModel view in LocationPanels)
            {
                view.EditMode = EditMode;

                if (!EditMode && DataChanged)
                {
                    string query = view.LocationData.query;
                    int    pos   = LocationPanels.IndexOf(view);
                    Task.Run(() => Settings.MoveLocation(query, pos));
                }
            }

            if (!EditMode && HomeChanged)
            {
                Task.Run(WeatherUpdateBackgroundTask.RequestAppTrigger);
            }

            DataChanged = false;
            HomeChanged = false;
        }
Ejemplo n.º 4
0
        private async Task LoadLocations()
        {
            // Disable EditMode button
            EditButton.IsEnabled = false;

            // Lets load it up...
            var locations = await Settings.GetFavorites();

            LocationPanels.Clear();

            // Setup saved favorite locations
            await LoadGPSPanel();

            foreach (LocationData location in locations)
            {
                var panel = new LocationPanelViewModel()
                {
                    // Save index to tag (to easily retreive)
                    LocationData = location
                };

                LocationPanels.Add(panel);
            }

            foreach (LocationData location in locations)
            {
                var wLoader = new WeatherDataLoader(location, this, this);
                await wLoader.LoadWeatherData(false);
            }

            // Enable EditMode button
            EditButton.IsEnabled = true;
        }
Ejemplo n.º 5
0
 private void MoveData(LocationPanelViewModel view, int fromIdx, int toIdx)
 {
     // Only move panels if we haven't already
     if (LocationPanels.IndexOf(view) != toIdx)
     {
         LocationPanels.Move(fromIdx, toIdx);
     }
 }
Ejemplo n.º 6
0
        private async Task RefreshLocations()
        {
            // Disable EditMode button
            EditButton.IsEnabled = false;

            // Reload all panels if needed
            var locations = await Settings.GetLocationData();

            var homeData = await Settings.GetLastGPSLocData();

            bool reload = (locations.Count != LocationPanels.Count || (Settings.FollowGPS && (GPSPanelViewModel.First() == null)));

            // Reload if weather source differs
            if ((GPSPanelViewModel.First() != null && GPSPanelViewModel.First().WeatherSource != Settings.API) ||
                (LocationPanels.Count >= 1 && LocationPanels[0].WeatherSource != Settings.API))
            {
                reload = true;
            }

            // Reload if panel queries dont match
            if (!reload && (GPSPanelViewModel.First() != null && homeData.query != GPSPanelViewModel.First().LocationData.query))
            {
                reload = true;
            }

            if (reload)
            {
                LocationPanels.Clear();
                await LoadLocations();
            }
            else
            {
                var dataset = LocationPanels.ToList();
                if (GPSPanelViewModel.First() != null)
                {
                    dataset.Add(GPSPanelViewModel.First());
                }

                foreach (LocationPanelViewModel view in dataset)
                {
                    var wLoader = new WeatherDataLoader(view.LocationData, this, this);
                    await wLoader.LoadWeatherData(false);
                }
            }

            // Enable EditMode button
            EditButton.IsEnabled = true;
        }
Ejemplo n.º 7
0
 public void OnWeatherLoaded(LocationData location, Weather weather)
 {
     if (weather?.IsValid() == true)
     {
         if (Settings.FollowGPS && location.locationType == LocationType.GPS)
         {
             GPSPanelViewModel.First()?.SetWeather(weather);
         }
         else
         {
             var panelView = LocationPanels.First(panelVM => panelVM.LocationData.query == location.query);
             // Just in case
             if (panelView == null)
             {
                 panelView = LocationPanels.First(panelVM => panelVM.LocationData.name.Equals(location.name) &&
                                                  panelVM.LocationData.latitude.Equals(location.latitude) &&
                                                  panelVM.LocationData.longitude.Equals(location.longitude) &&
                                                  panelVM.LocationData.tz_long.Equals(location.tz_long));
             }
             panelView?.SetWeather(weather);
         }
     }
 }
Ejemplo n.º 8
0
        private async void Location_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
        {
            LocationQueryViewModel query_vm = null;

            if (args.ChosenSuggestion != null)
            {
                // User selected an item from the suggestion list, take an action on it here.
                var theChosenOne = args.ChosenSuggestion as LocationQueryViewModel;

                if (!String.IsNullOrEmpty(theChosenOne.LocationQuery))
                {
                    query_vm = theChosenOne;
                }
                else
                {
                    query_vm = new LocationQueryViewModel();
                }
            }
            else if (!String.IsNullOrEmpty(args.QueryText))
            {
                // Use args.QueryText to determine what to do.
                var result = (await wm.GetLocations(args.QueryText)).First();

                if (result != null && !String.IsNullOrWhiteSpace(result.LocationQuery))
                {
                    sender.Text = result.LocationName;
                    query_vm    = result;
                }
                else
                {
                    query_vm = new LocationQueryViewModel();
                }
            }
            else if (String.IsNullOrWhiteSpace(args.QueryText))
            {
                // Stop since there is no valid query
                return;
            }

            if (String.IsNullOrWhiteSpace(query_vm.LocationQuery))
            {
                // Stop since there is no valid query
                return;
            }

            // Cancel other tasks
            cts.Cancel();
            cts = new CancellationTokenSource();
            var ctsToken = cts.Token;

            LoadingRing.IsActive = true;

            if (ctsToken.IsCancellationRequested)
            {
                LoadingRing.IsActive = false;
                return;
            }

            // Check if location already exists
            var locData = await Settings.GetLocationData();

            if (locData.Exists(l => l.query == query_vm.LocationQuery))
            {
                LoadingRing.IsActive = false;
                ShowAddLocationsPanel(false);
                return;
            }

            if (ctsToken.IsCancellationRequested)
            {
                LoadingRing.IsActive = false;
                return;
            }

            var location = new LocationData(query_vm);

            if (!location.IsValid())
            {
                await Toast.ShowToastAsync(App.ResLoader.GetString("WError_NoWeather"), ToastDuration.Short);

                LoadingRing.IsActive = false;
                return;
            }
            var weather = await Settings.GetWeatherData(location.query);

            if (weather == null)
            {
                try
                {
                    weather = await wm.GetWeather(location);
                }
                catch (WeatherException wEx)
                {
                    weather = null;
                    await Toast.ShowToastAsync(wEx.Message, ToastDuration.Short);
                }
            }

            if (weather == null)
            {
                LoadingRing.IsActive = false;
                return;
            }

            // We got our data so disable controls just in case
            sender.IsSuggestionListOpen = false;

            // Save data
            await Settings.AddLocation(location);

            if (wm.SupportsAlerts && weather.weather_alerts != null)
            {
                await Settings.SaveWeatherAlerts(location, weather.weather_alerts);
            }
            await Settings.SaveWeatherData(weather);

            var panelView = new LocationPanelViewModel(weather)
            {
                LocationData = location
            };

            // Set properties if necessary
            if (EditMode)
            {
                panelView.EditMode = true;
            }

            // Add to collection
            LocationPanels.Add(panelView);

            // Hide add locations panel
            LoadingRing.IsActive = false;
            ShowAddLocationsPanel(false);
        }