public async Task Initialize(object navigationData) { CurrentDate = GlobalSettings.EventDate; IsBusy = true; try { var weather = _weatherService.GetDemoWeatherInfoAsync(); var events = _eventsService.GetEvents(); var suggestions = _ridesService.GetSuggestions(); var tasks = new List <Task> { weather, events, suggestions }; while (tasks.Count > 0) { var finishedTask = await Task.WhenAny(tasks); tasks.Remove(finishedTask); if (finishedTask.Status == TaskStatus.RanToCompletion) { if (finishedTask == weather) { var weatherResults = await weather; if (weatherResults is WeatherInfo) { var weatherInfo = weatherResults as WeatherInfo; Location = weatherInfo.LocationName; Temp = Math.Round(weatherInfo.Temp).ToString(); } } else if (finishedTask == events) { var eventsResults = await events; Events = new ObservableCollection <Event>(eventsResults); } else if (finishedTask == suggestions) { var suggestionsResults = await suggestions; Suggestions = new ObservableCollection <Suggestion>(suggestionsResults); } } } var traditions = new List <Tradition>(); var restaurants = new List <Restaurant>(); //销量 //traditions = await _censusService.GetTraditions(10, 0); //restaurants = await _censusService.GetRestaurants(10, 0); try { var traditionsModel = await _cacheManager.Get <List <Tradition> >(GlobalSettings.traditions_key); if (traditionsModel != null) { traditions = traditionsModel; } else { traditions = await _censusService.GetTraditions(10, 0); await _cacheManager.Set <List <Tradition> >(GlobalSettings.traditions_key, traditions); } // var restaurantsModel = await _cacheManager.Get <List <Restaurant> >(GlobalSettings.restaurants_key); if (restaurantsModel != null) { restaurants = restaurantsModel; } else { restaurants = await _censusService.GetRestaurants(10, 0); await _cacheManager.Set <List <Restaurant> >(GlobalSettings.restaurants_key, restaurants); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } var ridesResult = new List <Stats>(); foreach (var t in traditions) { if (t != null) { ridesResult.Add(new Stats { Title = t.BaseInfo.EndPointStorsName, Label1 = string.Format("{0}月销量", DateTime.Now.AddMonths(-1).Month), Label2 = string.Format("{0}月销量", DateTime.Now.Month), Value1 = "0", Value2 = "0" }); } } foreach (var t in restaurants) { if (t != null) { ridesResult.Add(new Stats { Title = t.BaseInfo.EndPointStorsName, Label1 = string.Format("{0}月", DateTime.Now.AddMonths(-1).Month), Label2 = string.Format("{0}月", DateTime.Now.Month), Value1 = "0", Value2 = "0" }); } } Statistics = new ObservableCollection <Stats>(ridesResult); //合计 TotalSum = 0; var statistics = await _censusService.GetStatistics(); statistics.ForEach(s => { TotalSum += s.EndPointSum; }); } catch (Exception ex) when(ex is WebException || ex is HttpRequestException) { //await _dialogService.ShowAlertAsync("Communication error", "Error", "Ok"); } catch (Exception ex) { Debug.WriteLine($"Error loading data in: {ex}"); } IsBusy = false; }
private async Task LoadData() { IsBusy = true; try { if (CrossConnectivity.Current.IsConnected) { var traditions = new List <Tradition>(); var restaurants = new List <Restaurant>(); //销量 try { var cacheTraditions = await _cacheManager.Get <List <Tradition> >(GlobalSettings.network_traditions_key); if (cacheTraditions != null) { traditions = cacheTraditions; } else { traditions = await _censusService.GetTraditions(200, 0); await _cacheManager.Set <List <Tradition> >(GlobalSettings.network_traditions_key, traditions); } // var cacheRestaurants = await _cacheManager.Get <List <Restaurant> >(GlobalSettings.network_restaurants_key); if (cacheRestaurants != null) { restaurants = cacheRestaurants; } else { restaurants = await _censusService.GetRestaurants(200, 0); await _cacheManager.Set <List <Restaurant> >(GlobalSettings.network_restaurants_key, restaurants); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } var ridesResult = new List <Ride>(); foreach (var t in traditions) { var photo = t.DoorheadPhotos.FirstOrDefault(); var track = t.TrackLocation; ridesResult.Add(new Ride() { Id = t.Id, EventId = t.Id, RideType = RideType.Tradition, Name = "传统普查", Start = DateTime.Now, Stop = t.UpdateDate, Duration = track != null ? track.Duration : 0, Distance = 0, From = "", FromStation = new Station(), To = "", ToStation = new Station(), City = t.BaseInfo.City, TraditionInfo = new Tradition() { BaseInfo = new TraditionBaseInfo() { EndPointStorsName = t.BaseInfo.EndPointStorsName, EndPointAddress = t.BaseInfo.EndPointAddress, }, BusinessInfo = new TraditionBusinessInfo() { EndPointType = t.BusinessInfo.EndPointType }, UpdateDate = t.UpdateDate }, ThumbnailPhoto = photo != null ? photo.StoragePath : "noimg.jpg" }); } foreach (var t in restaurants) { var photo = t.DoorheadPhotos.FirstOrDefault(); var track = t.TrackLocation; ridesResult.Add(new Ride() { Id = t.Id, EventId = t.Id, RideType = RideType.Restaurant, Name = "餐饮普查", Start = DateTime.Now, Stop = t.UpdateDate, Duration = track != null ? track.Duration : 0, Distance = 0, From = "", FromStation = new Station(), To = "", ToStation = new Station(), City = t.BaseInfo.City, RestaurantInfo = new Restaurant() { BaseInfo = new RestaurantBaseInfo() { EndPointStorsName = t.BaseInfo.EndPointStorsName, EndPointAddress = t.BaseInfo.EndPointAddress, }, BusinessInfo = new RestaurantBusinessInfo() { EndPointType = t.BusinessInfo.EndPointType }, UpdateDate = t.UpdateDate }, ThumbnailPhoto = photo != null ? photo.StoragePath : "noimg.jpg" }); } MyRides = new ObservableRangeCollection <Ride>(ridesResult); } else { _dialogService.LongAlert("网络连接失败"); } } catch (Exception ex) when(ex is WebException || ex is HttpRequestException) { // await _dialogService.ShowAlertAsync("Communication error", "Error", "Ok"); } catch (Exception ex) { Debug.WriteLine($"Error in: {ex}"); } finally { IsBusy = false; } }