public async Task LoadData() { this.IsLoadingRestaurantDetail = true; this.IsBusy = true; Menus.Clear(); ListMenuItemViews.Clear(); if (StackLayoutMenus != null) { StackLayoutMenus.Children.Clear(); } GridMenuItems.Children.Clear(); GridMenus.Children.Clear(); await Task.Run(() => { Utils.IReloadPageCurrent = this; try { var dateUTC = DateTime.UtcNow; var date = SelectedDate; var nextTimeSlot = date; bool isNext = false; var menus = Shared.APIs.IRestaurant.GetMenus(RestaurantVM.LocationId); RefreshMenuAvailables = restaurantFacade.RefreshMenus(menus, RestaurantVM.LocationId, dateUTC, date.AddDays(CheckNextDays), IsDeliverySelected, GroupedDeliveryDestination); MenuOrderingAvailables = restaurantFacade.SearchAvailableOrderTimes(RefreshMenuAvailables) .OrderBy(t => t.StartTime) .ToList(); //MenuOrderingAvailables = restaurantFacade.SearchAvailableOrderTimes(RestaurantVM.LocationId, date.AddDays(days).ToString("MM/dd/yyyy"), date.AddDays(CheckNextDays).ToString("MM/dd/yyyy"), IsDeliverySelected) // .OrderBy(t => t.StartTime) // .ToList(); foreach (var menuOrderingAvailable in MenuOrderingAvailables.ToList()) { string timeString = menuOrderingAvailable.CutoffTime.Time; var time = GetDateTimeRestaurantFromLocal(timeString); if (date < time) { timeString = menuOrderingAvailable.StartTime.Time; time = GetDateTimeRestaurantFromLocal(timeString); nextTimeSlot = time; isNext = true; break; } } IsSelectedDateDisplay = true; DateRestaurantCloses = restaurantFacade.GetDateRestaurantCloses(RestaurantTimeZone, RestaurantVM.LocationId, dateUTC, date.AddDays(CheckNextDays)); SearchHoliday(RestaurantVM.LocationId); if (menus != null) { menus = menus.OrderBy((arg) => arg.ID).ToList(); foreach (var menu in menus) { RMenuVM menuVM = new RMenuVM(); menuVM.Menu = menu; menuVM.MenuGroups = new List <RMenuGroupVM>(); Task.Run(() => { try { List <RMenuGroupVM> MenuGroups = new List <RMenuGroupVM>(); var menusgroup = Shared.APIs.IRestaurant.GetMenuGroups(RestaurantVM.LocationId, menu.ID); foreach (var menugroup in menusgroup) { RMenuGroupVM GroupMenu = new RMenuGroupVM { Group = menugroup, }; MenuGroups.Add(GroupMenu); int retryGroup = 0; while (retryGroup < 3) { try { var menuItems = Shared.APIs.IRestaurant.GetChildMenuItems(RestaurantVM.LocationId, menu.ID, menugroup.ID); GroupMenu.MenuItems = menuItems.Select(s => MapMenuItemBasePrice(new RMenuItemVM { LocationId = RestaurantVM.LocationId, MenuId = menu.ID, MenuItem = s })).ToList(); break; } catch (Exception) { System.GC.Collect(); new System.Threading.ManualResetEvent(false).WaitOne(1000); retryGroup++; } } Device.BeginInvokeOnMainThread(() => { var index = Menus.IndexOf(menuVM); if (index >= 0 && ListMenuItemViews.Count > index) { var menuItemViewModels = CreateMenuItemViewModels(GroupMenu, menu.ID); if (!ListMenuItemViews.ContainsKey(menu.ID)) { ListMenuItemViews.Add(menu.ID, new ObservableCollection <MenuItemViewModel>()); (GridMenuItems.Children[index] as RestaurantMenuListView).ListViewMenus.ItemsSource = ListMenuItemViews[menu.ID]; } foreach (var menuItemViewModel in menuItemViewModels) { ListMenuItemViews[menu.ID].Add(menuItemViewModel); menuItemViewModel.NeedUpdateSize(); } (GridMenuItems.Children[index] as RestaurantMenuListView).ListViewMenus.IsVisible = ListMenuItemViews[menu.ID].Count > 0; if (MenuTabIndex == index) { RefreshMenus(RestaurantVM.LocationId, Menus[index].Menu.ID); } //(StackLayoutMenus.Children[index] as Layout<View>).Children[2].IsVisible = false; } }); } menuVM.MenuGroups = MenuGroups; } catch (Exception ex) { var message = ex.Message; Device.BeginInvokeOnMainThread(async() => { await Utils.ShowErrorMessage(string.IsNullOrEmpty(message) ? AppResources.SomethingWentWrong : message); }); } }).ContinueWith((arg) => { Device.BeginInvokeOnMainThread(() => { var index = Menus.IndexOf(menuVM); if (index >= 0 && ListMenuItemViews.Count > index) { (GridMenuItems.Children[index] as RestaurantMenuListView).ListViewMenus.IsVisible = true; (GridMenuItems.Children[index] as RestaurantMenuListView).IndicatorView.IsVisible = false; (StackLayoutMenus.Children[index] as Layout <View>).Children[2].IsVisible = false; } }); }); Menus.Add(menuVM); } } } catch (Exception ex) { if (!this.IsErrorPage && Utils.IReloadPageCurrent == this) { Device.BeginInvokeOnMainThread(async() => { await Utils.ShowErrorMessage(string.IsNullOrEmpty(ex.Message) ? AppResources.SomethingWentWrong : ex.Message); }); } } if (Utils.IReloadPageCurrent == this) { Utils.IReloadPageCurrent = null; } }).ContinueWith(t => { if (Menus != null) { CalculateFees(); StackLayoutMenus = new StackLayout { Orientation = StackOrientation.Horizontal, Spacing = 0, }; ScrollViewMenus = new ScrollView { Orientation = ScrollOrientation.Horizontal, IsClippedToBounds = true, }; ScrollViewMenus.Content = StackLayoutMenus; GridMenus.Children.Add(ScrollViewMenus); foreach (var menu in Menus) { var gridTab = new Grid(); gridTab.BackgroundColor = Color.FromRgb(80, 80, 80); gridTab.HorizontalOptions = LayoutOptions.Start; gridTab.VerticalOptions = LayoutOptions.Start; gridTab.HeightRequest = Device.GetNamedSize(NamedSize.Medium, typeof(Label)) * 2.2; Label labelTab = new Label(); labelTab.FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)); labelTab.VerticalTextAlignment = TextAlignment.Center; labelTab.HorizontalTextAlignment = TextAlignment.Center; labelTab.VerticalOptions = LayoutOptions.Center; labelTab.HorizontalOptions = LayoutOptions.Center; labelTab.Text = menu.Menu.DisplayName; labelTab.TextColor = Color.Gray; labelTab.Margin = new Thickness(18, 0, 20, 0); ActivityIndicator activityIndicator = new ActivityIndicator(); activityIndicator.IsRunning = false; activityIndicator.IsVisible = false; if (Device.RuntimePlatform == Device.iOS) { labelTab.Margin = new Thickness(19, 0, 23, 0); activityIndicator.HeightRequest = 14; activityIndicator.WidthRequest = 14; activityIndicator.Margin = new Thickness(0, 4, 4, 0); } else { activityIndicator.HeightRequest = 14; activityIndicator.WidthRequest = 14; activityIndicator.Margin = new Thickness(0, 4, 4, 0); } activityIndicator.HorizontalOptions = LayoutOptions.End; activityIndicator.VerticalOptions = LayoutOptions.Start; activityIndicator.Color = AppearanceBase.Instance.PrimaryColor; gridTab.GestureRecognizers.Add(new TapGestureRecognizer() { Command = new Command(() => { if (StackLayoutMenus != null) { var index = StackLayoutMenus.Children.IndexOf(gridTab); if (index >= 0) { ChangeTabMenu(index); double scrollX = 0; for (int i = 0; i < index; i++) { scrollX += StackLayoutMenus.Children[i].Width; } scrollX += StackLayoutMenus.Children[index].Width / 2; scrollX = Math.Min(Math.Max(0, (scrollX - ScrollViewMenus.Width / 2)), StackLayoutMenus.Width - ScrollViewMenus.Width); ScrollViewMenus.ScrollToAsync(scrollX, ScrollViewMenus.ScrollY, true); } } }), }); gridTab.Children.Add(labelTab); gridTab.Children.Add(new Grid { HeightRequest = 2, BackgroundColor = Color.Transparent, VerticalOptions = LayoutOptions.End, IsVisible = false, }); gridTab.Children.Add(activityIndicator); StackLayoutMenus.Children.Add(gridTab); } RenderListView(Menus); int menuIndex = 0; bool isMenuAvailable = false; while (Menus.Count > menuIndex) { MenuTabIndex = menuIndex; var menuId = Menus[menuIndex].Menu.ID; SearchAvailableOrderTimes(menuId); RefreshMenus(RestaurantVM.LocationId, menuId, false); if (!CheckMenuAvailableVisible) { isMenuAvailable = true; break; } else { menuIndex++; } } if (!isMenuAvailable && Menus != null && Menus.Count > 0) { MenuTabIndex = 0; var menuId = Menus[0].Menu.ID; SearchAvailableOrderTimes(menuId); RefreshMenus(RestaurantVM.LocationId, menuId, false); } ChangeTabMenu(MenuTabIndex, true); } this.IsLoadingRestaurantDetail = false; this.IsBusy = false; }, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()); }