XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

STHApplication

【MAUI】寄当家——安卓端交易管理系统

公开
关注 0 Fork 0 Star 0
UTF-8
using JiDangJia.Services;
using JiDangJia.Views;

namespace JiDangJia;

// This root has no reference to the accounting engine or any local accounting database.
public sealed class MainPage : ContentPage
{
    private readonly List<(int Index,CommunityHub Hub)> hubs=[];
    private readonly IDispatcherTimer timer;
    private int selected=2;
    private CommunityHub? current;
    private bool suspended,checking;
    private DateTime nextAccountCheck=DateTime.UtcNow.AddMinutes(2),nextUpdate=DateTime.UtcNow.AddMinutes(30);
    public MainPage()
    {
        Title=AppProduct.Name;NavigationPage.SetHasNavigationBar(this,false);
        var brand=Ui.Row(Ui.Icon("people",Ui.Gold,24),Ui.Text(AppProduct.Name,20,Ui.Paper,true));
        View Action(string icon,string title,Func<Task> action)=>Ui.Tap(new Grid{WidthRequest=44,HeightRequest=44,Children={Ui.Icon(icon,Ui.Gold,22)}},action,title);
        var actions=Ui.Row(Action("support","客服与反馈",()=>Navigation.PushAsync(new SupportPage())),Action("settings","设置",()=>Navigation.PushAsync(new CommunitySettingsPage())));
        var hero=Ui.Stack(Ui.Spread(brand,actions),Ui.Text(AppProduct.Tagline,12,Ui.Gold));hero.Padding=new Thickness(18,12,18,14);hero.Spacing=3;hero.BackgroundColor=Ui.Ink;
        View Pane(int index)
        {
            var hub=new CommunityHub(this,index,_=>{},embedded:true,active:false);hubs.Add((index,hub));if(index==selected)Activate(index);
            var refresh=new PageScroller(()=>{hub.Dispose();hubs.RemoveAll(p=>ReferenceEquals(p.Hub,hub));}){Content=new ScrollView{Content=hub},RefreshColor=Ui.Bronze};
            refresh.Refreshing+=async(_,_)=>{try{await hub.Reload();}finally{refresh.IsRefreshing=false;}};return refresh;
        }
        void Activate(int index){selected=index;current=hubs.LastOrDefault(p=>p.Index==index).Hub;foreach(var pane in hubs)pane.Hub.SetActive(!suspended&&ReferenceEquals(pane.Hub,current));}
        var pager=new LoopPager(["关注","同城","推荐","供需","服务","广场","私信"],selected,Pane,Activate);
        var layout=new Grid{RowDefinitions=[new(GridLength.Auto),new(GridLength.Star)]};layout.Add(hero);layout.Add(pager,0,1);Content=layout;
        timer=Dispatcher.CreateTimer();timer.Interval=TimeSpan.FromSeconds(5);timer.Tick+=async(_,_)=>
        {
            if(suspended||checking||AuthClient.Current.Locked)return;
            if(DateTime.UtcNow>=nextUpdate){nextUpdate=DateTime.UtcNow.AddMinutes(30);_=AppUpdates.Check(this);}
            if(DateTime.UtcNow<nextAccountCheck)return;checking=true;nextAccountCheck=DateTime.UtcNow.AddMinutes(2);
            try{await AuthClient.Current.RefreshAccount();}
            catch{if(AuthClient.Current.AccountId.Length==0){Suspend();LoginPage.ShowLogin();}}
            finally{checking=false;}
        };
        AppNotifications.Start(Dispatcher);timer.Start();
    }
    protected override void OnAppearing(){base.OnAppearing();_=AppUpdates.Check(this);}
    public void Suspend(){suspended=true;timer.Stop();AppNotifications.Pause();foreach(var pane in hubs)pane.Hub.SetActive(false);CommunityVideo.StopAll();}
    public void Resume(){suspended=false;timer.Start();AppNotifications.Resume();nextAccountCheck=DateTime.UtcNow;foreach(var pane in hubs)pane.Hub.SetActive(ReferenceEquals(pane.Hub,current));}
}