Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

List model Items in a categorized list in view xamarin

$
0
0

with a Resturent model as below i am retrieving the data to appear in list view as lists with two colomns

the model

    public class ResturentItems
        {
            [JsonProperty("Resturent")]
            public List<Resturent> Resturent { get; set; }
        }

        public class Resturent : ModelBase
        {
            [JsonIgnore]
            private static readonly ILocalFileToBase64StringConverter _base64Converter = DependencyService.Get<ILocalFileToBase64StringConverter>();

            [JsonProperty("ID")]
            public string ID { get; set; }

            [JsonProperty("Category")]
            public string Category { get; set; }

            [JsonProperty("Name")]
            public string Name { get; set; }

            [JsonProperty("Code")]
            public string Code { get; set; }

            [JsonProperty("Description")]
            public string Description { get; set; }

            [JsonProperty("Price")]
            public string Price { get; set; }

            [JsonProperty("isservicecharge")]
            public bool isservicecharge { get; set; }

            [JsonProperty("isaddonapplicable")]
            public bool isaddonapplicable { get; set; }

            [JsonProperty("CostPrice")]
            public string CostPrice { get; set; }

            [JsonProperty("Date")]
            public string Date { get; set; }

            [JsonProperty("Image")]
            public string Image { get; set; }

            [JsonIgnore]
            public ImageSource ImageSource => string.IsNullOrEmpty(Image) ? "userpic.jpg" : ImageSource.FromStream(() => new MemoryStream(_base64Converter.ConvertBack(Image)));

        }

and the data retrieve by the below code

private List<ResturentPair> GetDishItems(List<Resturent> list)
        {         

            var res = new List<ResturentPair>();
            for (int i = 0; i < list.Count-1; i += 2)
            {
                res.Add(new ResturentPair { Item1 = list[i], Item2 = list[i+1] });
            }
            return res;
        }   

for your convenience i will show below the model as well

public class ResturentPair
    {
        public Resturent Item1 { get; set; }

        public Resturent Item2 { get; set; }
    }

the full code below on the viewmodel

public class RootViewModel : ContextViewModelBase
    {
        private readonly IRestService _restService;
        private RootPage _page;

        public RootViewModel(IAppContext context, IRestService restService, IStatusBarService statusBarService) : base(context)
        {
            _restService = restService;
            statusBarService.ShowStatusBar();
            statusBarService.SetDefaultFontColor();
            ActionCommand = new Command(OnActionCommand);
            PageAppearingCommand = new Command<RootPage>(OnPageAppearingCommand);
            PageDisappearingCommand = new Command(OnPageDisappearingCommand);
        }

        public List<ResturentPair> Items { get; set; }

        public List<Resturent> CarouselItems { get; set; }

        public string Photo { get; set; } = "Photo.png";

        public bool IsLoading { get; set; } = true;

        public ICommand ActionCommand { get; }

        public string PagingText { get; set; }

        private async void OnPageAppearingCommand(RootPage page)
        {
            _page = page;
            Photo = $"{DefaultValues.ServerUrl}{Context.User.PhotoUrl}";
            IsLoading = true;
            var items = await _restService.GetFoodMenuInfo();
            CarouselItems = GetCarouselItems(items.Resturent);
            Items = GetDishItems(items.Resturent);
            OnCarouselViewItemSelected(this, new SelectedItemChangedEventArgs(CarouselItems.First())); // we call it for setting paging
            IsLoading = false;
            _page.CarouselView.ItemSelected += OnCarouselViewItemSelected;
        }

        private void OnPageDisappearingCommand()
        {
            _page.CarouselView.ItemSelected -= OnCarouselViewItemSelected;
        }

        private List<Resturent> GetCarouselItems(List<Resturent> items)
        {         

            var carouselViewItems = new List<Resturent>();
            // put your logic here for selecting items for carouselView
            for (var i = 0; i < 4; i++)
            {
                carouselViewItems.Add(items[i]);
            }
            return carouselViewItems;
        }       

        private List<ResturentPair> GetDishItems(List<Resturent> list)
        {         

            var res = new List<ResturentPair>();
            for (int i = 0; i < list.Count-1; i += 2)
            {
                res.Add(new ResturentPair { Item1 = list[i], Item2 = list[i+1] });
            }
            return res;
        }


        private void OnCarouselViewItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var selectedIndex = CarouselItems.IndexOf(e.SelectedItem as Resturent);
            var pagingResult = string.Empty;
            for (var i = 0; i < CarouselItems.Count; i++)
            {
                if (i == selectedIndex)
                {
                    pagingResult += $" \uf26d ";
                }
                else
                {
                    pagingResult += $" \uf26c ";
                }
            }
            PagingText = pagingResult;
        }

        private void OnActionCommand()
        {
            Navigator.PushAsync<SettingsViewModel>();
        }
    }

so finally the Restaurant details appear with all the details with name and the category , ETC, but my requirement i only want to show the categories ( the Restaurant items list need to be listed as categories, so any idea how to retrieve only to list by only categories of the restaurant items ?? thank you advance for your support


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>