using System.Collections.Generic; using System.Threading.Tasks; using Gallery.Resources; using Gallery.Resources.UI; using Gallery.Util; using Gallery.Util.Interface; using Gallery.Util.Model; using Xamarin.Essentials; using Xamarin.Forms; namespace Gallery.Views { public partial class GalleryPage : GalleryCollectionPage { public static readonly BindableProperty ToolbarCommandProperty = BindableProperty.Create(nameof(ToolbarCommand), typeof(Command), typeof(GalleryPage)); public Command ToolbarCommand { get => (Command)GetValue(ToolbarCommandProperty); } private int currentPage; public GalleryPage(IGallerySource source) : base(source) { Title = Helper.GetResource(source.Name); Resources.Add("cardView", GetCardViewTemplate()); SetValue(ToolbarCommandProperty, new Command(DoRefresh, () => !IsLoading && !IsBottomLoading)); InitializeComponent(); currentPage = 1; } protected override ActivityIndicator LoadingIndicator => activityLoading; protected override void OnAppearing() { if (currentPage != 1 && Gallery == null) { currentPage = 1; } if (Store.FavoriteList.Changed && Source is Sources.FavoriteGallerySource) { Store.FavoriteList.Reload(); LastUpdated = default; } base.OnAppearing(); } protected override void BeforeLoading() { MainThread.BeginInvokeOnMainThread(ToolbarCommand.ChangeCanExecute); } protected override void AfterLoaded() { MainThread.BeginInvokeOnMainThread(ToolbarCommand.ChangeCanExecute); } protected override async Task> DoloadGalleryData(bool force) { var result = await Source.GetRecentItemsAsync(currentPage); return result; } protected override IEnumerable DoGetGalleryList(IEnumerable data, out int tag) { tag = currentPage; return data; } protected override void OnGalleryItemTapped(GalleryItem item) { if (item == null) { return; } Start(async () => { var page = new GalleryItemPage(item); await Navigation.PushAsync(page); }); } private void DoRefresh() { if (IsLoading) { return; } Start(async () => { await ScrollToTopAsync(scrollView); StartLoading(force: true); }); } private void FlowLayout_MaxHeightChanged(object sender, HeightEventArgs e) { SetOffset(e.ContentHeight - scrollView.Bounds.Height - SCROLL_OFFSET); } protected override bool CheckRefresh() { if (!Source.IsScrollable) { return false; } currentPage++; #if DEBUG Log.Print($"loading page: {currentPage}"); #endif return true; } private void ScrollView_Scrolled(object sender, ScrolledEventArgs e) { var y = e.ScrollY; OnScrolled(y); } } }