diff --git a/README.md b/README.md index 21cec1f..1d91731 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,151 @@ # lazy-loading-json-data-to-listview-net-maui Lazy loading of data from JSON to .NET MAUI ListView + +## XAML + + + + ... + + ... + + + + +## C# + +public class ListViewModel : INotifyPropertyChanged + { + #region Fields + + private IList items; + private bool isDownloaded; + private int totalItems = 22; + #endregion + + #region Properties + + public IList Items + { + get { return items; } + set + { + items = value; + OnPropertyChanged("Items"); + } + } + + public IList JSONCollection { get; set; } + + public DataServices DataService { get; set; } + + public Command LoadMoreItemsCommand { get; set; } + + private bool isBusy; + public bool IsBusy + { + get { return isBusy; } + set + { + isBusy = value; + OnPropertyChanged("IsBusy"); + } + } + + #endregion + + #region Constructor + + public ListViewModel() + { + JSONCollection = new ObservableCollection(); + Items = new ObservableCollection(); + DataService = new DataServices(); + GetDataAsync(); + LoadMoreItemsCommand = new Command(LoadMoreItems, CanLoadMoreItems); + + } + #endregion + + #region Methods + private async void GetDataAsync() + { + isDownloaded = await DataService.DownloadJsonAsync(); + if (isDownloaded) + { + var localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + var fileText = File.ReadAllText(Path.Combine(localFolder, "Data.json")); + + //Read data from the local path and set it to the collection bound to the ListView. + JSONCollection = JsonConvert.DeserializeObject>(fileText); + totalItems = JSONCollection.Count; + } + } + + private bool CanLoadMoreItems(object obj) + { + if (Items.Count >= totalItems) + return false; + return true; + } + + private async void LoadMoreItems(object obj) + { + var listview = obj as SfListView; + try + { + IsBusy = true; + await Task.Delay(1000); + var index = Items.Count; + var count = index + 3 >= totalItems ? totalItems - index : 3; + AddProducts(index, count); + } + catch + { + + } + finally + { + IsBusy = false; + } + } + + private void AddProducts(int index, int count) + { + for (int i = index; i < index + count; i++) + { + Items.Add(JSONCollection[i]); + } + } + #endregion + + #region Interface Member + + public event PropertyChangedEventHandler PropertyChanged; + + public void OnPropertyChanged(string name) + { + if (PropertyChanged != null) + PropertyChanged(this, new PropertyChangedEventArgs(name)); + } + + #endregion + } + +## Requirements to run the demo + +* [Visual Studio 2017](https://visualstudio.microsoft.com/downloads/) or [Visual Studio for Mac](https://visualstudio.microsoft.com/vs/mac/) +* Xamarin add-ons for Visual Studio (available via the Visual Studio installer). + +## Troubleshooting + +### Path too long exception + +If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project. \ No newline at end of file