using JiDangJia.Core;
using JiDangJia.Services;
using Microsoft.Maui.Devices.Sensors;
namespace JiDangJia.Views;
internal sealed class CityPickerPage : ContentPage
{
private readonly TaskCompletionSource<string?> result=new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly Picker province=new(){Title="选择省份(拼音排序)"},city=new(){Title="选择城市(拼音排序)"};
public static async Task<string?> Choose(ContentPage parent,string current,bool all=true,Action<string>? selected=null)
{
var page=new CityPickerPage(current,all,selected);await parent.Navigation.PushAsync(page);return await page.result.Task;
}
private CityPickerPage(string current,bool all,Action<string>? selected)
{
Title="选择城市";province.ItemsSource=CityCatalog.Provinces.Keys.ToList();
void Cities(){city.ItemsSource=province.SelectedIndex>=0?CityCatalog.Provinces.Values.ElementAt(province.SelectedIndex):new List<string>();city.SelectedIndex=city.Items.Count>0?0:-1;}
province.SelectedIndexChanged+=(_,_)=>Cities();province.SelectedIndex=Math.Max(0,CityCatalog.Provinces.Keys.ToList().IndexOf(CityCatalog.Province(current)));Cities();
city.SelectedIndex=Math.Max(0,city.Items.ToList().FindIndex(c=>CityCatalog.Normalize(c)==CityCatalog.Normalize(current)));
async Task Apply(string value){selected?.Invoke(value);result.TrySetResult(value);await Navigation.PopAsync();}
var body=Ui.Stack(Ui.Section("省份与城市"),province,city,Ui.Button("确定城市",async()=>{if(city.SelectedIndex<0)throw new ArgumentException("请选择城市");await Apply(CityCatalog.Normalize(city.Items[city.SelectedIndex]));}));
if(all)body.Add(Ui.Button("全部城市",()=>Apply(""),false));body.Padding=18;Content=body;
}
protected override void OnDisappearing(){base.OnDisappearing();result.TrySetResult(null);}
}
internal static class CommunityLocator
{
private static DateTime refreshed;
private static string lastAccount="",lastCity="";
public static async Task<string> Current(CancellationToken token,bool force=false)
{
var account=AuthClient.Current.AccountId;
if(!force&&lastAccount==account&&lastCity.Length>0&&DateTime.UtcNow-refreshed<TimeSpan.FromMinutes(5))return lastCity;
if(await Permissions.RequestAsync<Permissions.LocationWhenInUse>()!=PermissionStatus.Granted)throw new InvalidOperationException("未开启定位,可点城市选择所在地");
var position=await Geolocation.Default.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Low,TimeSpan.FromSeconds(10)),token)??throw new InvalidOperationException("暂未获取位置,可稍后重试或选择城市");
var marks=await Geocoding.Default.GetPlacemarksAsync(position.Latitude,position.Longitude).WaitAsync(TimeSpan.FromSeconds(10),token);
var place=marks.FirstOrDefault();var name=place?.Locality;
if(string.IsNullOrWhiteSpace(name)&&place?.AdminArea is { } province&&new[]{"北京","天津","上海","重庆"}.Any(v=>province.Contains(v)))name=province;
if(string.IsNullOrWhiteSpace(name))throw new InvalidOperationException("暂未识别所在城市,可选择城市");
token.ThrowIfCancellationRequested();if(account!=AuthClient.Current.AccountId)throw new OperationCanceledException();
lastAccount=account;lastCity=CityCatalog.Normalize(name);refreshed=DateTime.UtcNow;CommunityLocation.City=lastCity;return lastCity;
}
}
using JiDangJia.Core;
using JiDangJia.Services;
using Microsoft.Maui.Devices.Sensors;
namespace JiDangJia.Views;
internal sealed class CityPickerPage : ContentPage
{
private readonly TaskCompletionSource<string?> result=new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly Picker province=new(){Title="选择省份(拼音排序)"},city=new(){Title="选择城市(拼音排序)"};
public static async Task<string?> Choose(ContentPage parent,string current,bool all=true,Action<string>? selected=null)
{
var page=new CityPickerPage(current,all,selected);await parent.Navigation.PushAsync(page);return await page.result.Task;
}
private CityPickerPage(string current,bool all,Action<string>? selected)
{
Title="选择城市";province.ItemsSource=CityCatalog.Provinces.Keys.ToList();
void Cities(){city.ItemsSource=province.SelectedIndex>=0?CityCatalog.Provinces.Values.ElementAt(province.SelectedIndex):new List<string>();city.SelectedIndex=city.Items.Count>0?0:-1;}
province.SelectedIndexChanged+=(_,_)=>Cities();province.SelectedIndex=Math.Max(0,CityCatalog.Provinces.Keys.ToList().IndexOf(CityCatalog.Province(current)));Cities();
city.SelectedIndex=Math.Max(0,city.Items.ToList().FindIndex(c=>CityCatalog.Normalize(c)==CityCatalog.Normalize(current)));
async Task Apply(string value){selected?.Invoke(value);result.TrySetResult(value);await Navigation.PopAsync();}
var body=Ui.Stack(Ui.Section("省份与城市"),province,city,Ui.Button("确定城市",async()=>{if(city.SelectedIndex<0)throw new ArgumentException("请选择城市");await Apply(CityCatalog.Normalize(city.Items[city.SelectedIndex]));}));
if(all)body.Add(Ui.Button("全部城市",()=>Apply(""),false));body.Padding=18;Content=body;
}
protected override void OnDisappearing(){base.OnDisappearing();result.TrySetResult(null);}
}
internal static class CommunityLocator
{
private static DateTime refreshed;
private static string lastAccount="",lastCity="";
public static async Task<string> Current(CancellationToken token,bool force=false)
{
var account=AuthClient.Current.AccountId;
if(!force&&lastAccount==account&&lastCity.Length>0&&DateTime.UtcNow-refreshed<TimeSpan.FromMinutes(5))return lastCity;
if(await Permissions.RequestAsync<Permissions.LocationWhenInUse>()!=PermissionStatus.Granted)throw new InvalidOperationException("未开启定位,可点城市选择所在地");
var position=await Geolocation.Default.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Low,TimeSpan.FromSeconds(10)),token)??throw new InvalidOperationException("暂未获取位置,可稍后重试或选择城市");
var marks=await Geocoding.Default.GetPlacemarksAsync(position.Latitude,position.Longitude).WaitAsync(TimeSpan.FromSeconds(10),token);
var place=marks.FirstOrDefault();var name=place?.Locality;
if(string.IsNullOrWhiteSpace(name)&&place?.AdminArea is { } province&&new[]{"北京","天津","上海","重庆"}.Any(v=>province.Contains(v)))name=province;
if(string.IsNullOrWhiteSpace(name))throw new InvalidOperationException("暂未识别所在城市,可选择城市");
token.ThrowIfCancellationRequested();if(account!=AuthClient.Current.AccountId)throw new OperationCanceledException();
lastAccount=account;lastCity=CityCatalog.Normalize(name);refreshed=DateTime.UtcNow;CommunityLocation.City=lastCity;return lastCity;
}
}