version up

This commit is contained in:
2023-07-31 17:11:39 +08:00
parent befbc7fc9b
commit 8419c9d389
41 changed files with 1053 additions and 286 deletions

View File

@ -1,59 +1,82 @@
namespace Blahblah.FlowerApp;
using Blahblah.FlowerApp.Data;
using Microsoft.Extensions.Logging;
public class AppContentPage : ContentPage
namespace Blahblah.FlowerApp;
public class AppContentPage : ContentPage, ILoggerContent
{
protected static string L(string key, string defaultValue = "")
{
return LocalizationResource.GetText(key, defaultValue);
}
public ILogger Logger { get; init; } = null!;
protected static Task<T?> FetchAsync<T>(string url, CancellationToken cancellation = default)
{
return Extensions.FetchAsync<T>(url, cancellation);
}
public FlowerDatabase Database { get; init; } = null!;
protected T GetValue<T>(BindableProperty property)
{
return (T)GetValue(property);
}
protected Task AlertError(string error)
bool hasLoading = true;
ContentView? loading;
#if __IOS__
private async Task DoLoading(bool flag)
#else
private Task DoLoading(bool flag)
#endif
{
return Alert(LocalizationResource.GetText("error", "Error"), error);
if (loading == null && hasLoading)
{
try
{
loading = (ContentView)FindByName("loading");
}
catch
{
hasLoading = false;
}
}
if (loading != null)
{
if (flag)
{
#if __IOS__
loading.IsVisible = true;
await loading.FadeTo(1, easing: Easing.CubicOut);
#else
loading.Opacity = 1;
loading.IsVisible = true;
#endif
}
else
{
#if __IOS__
await loading.FadeTo(0, easing: Easing.CubicIn);
loading.IsVisible = false;
#else
loading.IsVisible = false;
loading.Opacity = 0;
#endif
}
}
#if __ANDROID__
return Task.CompletedTask;
#endif
}
protected Task Alert(string title, string message, string? cancel = null)
protected Task Loading(bool flag)
{
cancel ??= LocalizationResource.GetText("ok", "Ok");
IsBusy = flag;
if (MainThread.IsMainThread)
{
return DisplayAlert(title, message, cancel);
return DoLoading(flag);
}
var taskSource = new TaskCompletionSource();
var source = new TaskCompletionSource();
MainThread.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert(title, message, cancel);
taskSource.TrySetResult();
await DoLoading(flag);
source.TrySetResult();
});
return taskSource.Task;
}
protected Task<bool> Confirm(string title, string question, string? accept = null, string? cancel = null)
{
accept ??= LocalizationResource.GetText("yes", "Yes");
cancel ??= LocalizationResource.GetText("no", "No");
if (MainThread.IsMainThread)
{
return DisplayAlert(title, question, accept, cancel);
}
var taskSource = new TaskCompletionSource<bool>();
MainThread.BeginInvokeOnMainThread(async () =>
{
var result = await DisplayAlert(title, question, accept, cancel);
taskSource.TrySetResult(result);
});
return taskSource.Task;
return source.Task;
}
}

View File

@ -0,0 +1,20 @@
using System.Globalization;
namespace Blahblah.FlowerApp;
internal class VisibleIfNotNullConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string s)
{
return !string.IsNullOrEmpty(s);
}
return value != null;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

View File

@ -1,8 +1,29 @@
namespace Blahblah.FlowerApp;
using Blahblah.FlowerApp.Data.Model;
using static Blahblah.FlowerApp.Extensions;
namespace Blahblah.FlowerApp;
internal class AppResources
{
public const string EmptyCover = "empty_flower.jpg";
public const int EmptyUserId = -1;
public static readonly Size EmptySize = new(512, 339);
static readonly UserItem emptyUser = new()
{
Id = EmptyUserId,
Name = L("guest", "Guest")
};
static UserItem? user;
public static UserItem User => user ?? emptyUser;
public static bool IsLogined => user != null;
public static void SetUser(UserItem user)
{
AppResources.user = user;
}
}

View File

@ -1,12 +1,12 @@
using Blahblah.FlowerApp.Data.Model;
using static Blahblah.FlowerApp.PropertyExtension;
using static Blahblah.FlowerApp.Extensions;
namespace Blahblah.FlowerApp.Controls;
public class FlowerClientItem : BindableObject
{
static readonly BindableProperty NameProperty = CreateProperty<string, FlowerClientItem>(nameof(Name));
static readonly BindableProperty CategoryProperty = CreateProperty<int, FlowerClientItem>(nameof(Category));
static readonly BindableProperty CategoryIdProperty = CreateProperty<int, FlowerClientItem>(nameof(CategoryId));
static readonly BindableProperty CoverProperty = CreateProperty<ImageSource?, FlowerClientItem>(nameof(Cover));
static readonly BindableProperty BoundsProperty = CreateProperty<Rect, FlowerClientItem>(nameof(Bounds));
@ -18,10 +18,10 @@ public class FlowerClientItem : BindableObject
get => (string)GetValue(NameProperty);
set => SetValue(NameProperty, value);
}
public int Category
public int CategoryId
{
get => (int)GetValue(CategoryProperty);
set => SetValue(CategoryProperty, value);
get => (int)GetValue(CategoryIdProperty);
set => SetValue(CategoryIdProperty, value);
}
public ImageSource? Cover
{
@ -46,7 +46,7 @@ public class FlowerClientItem : BindableObject
{
FlowerItem = item;
Name = item.Name;
Category = item.Category;
CategoryId = item.CategoryId;
if (item.Photos?.Length > 0 && item.Photos[0] is PhotoItem cover)
{

View File

@ -0,0 +1,11 @@
using Blahblah.FlowerApp.Data;
using Microsoft.Extensions.Logging;
namespace Blahblah.FlowerApp;
public interface ILoggerContent
{
public ILogger Logger { get; }
public FlowerDatabase Database { get; }
}

View File

@ -0,0 +1,34 @@
using Blahblah.FlowerApp.Controls;
using static Blahblah.FlowerApp.Extensions;
namespace Blahblah.FlowerApp;
public class ItemSearchHandler : SearchHandler
{
public static readonly BindableProperty FlowersProperty = CreateProperty<FlowerClientItem[], ItemSearchHandler>(nameof(Flowers));
public FlowerClientItem[] Flowers
{
get => (FlowerClientItem[])GetValue(FlowersProperty);
set => SetValue(FlowersProperty, value);
}
protected override void OnQueryChanged(string oldValue, string newValue)
{
base.OnQueryChanged(oldValue, newValue);
if (string.IsNullOrWhiteSpace(newValue))
{
ItemsSource = null;
}
else
{
ItemsSource = Flowers?.Where(f => f.Name.Contains(newValue, StringComparison.OrdinalIgnoreCase)).ToList();
}
}
protected override void OnItemSelected(object item)
{
base.OnItemSelected(item);
}
}

View File

@ -1,9 +0,0 @@
namespace Blahblah.FlowerApp;
internal sealed class PropertyExtension
{
public static BindableProperty CreateProperty<T, V>(string propertyName, T? defaultValue = default)
{
return BindableProperty.Create(propertyName, typeof(T), typeof(V), defaultValue);
}
}