Ver Fonte

refactored view showing, added back navigation

Veloe há 1 ano atrás
pai
commit
da1e7cb3fd

+ 1 - 0
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp.Android/MainActivity.cs

@@ -3,6 +3,7 @@ using Android.Content.PM;
 using Avalonia;
 using Avalonia.Android;
 using Avalonia.ReactiveUI;
+using VeloeAvaloniaKemonoPartyApp.ViewModels;
 
 namespace VeloeAvaloniaKemonoPartyApp.Android
 {

+ 2 - 2
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp.Android/VeloeAvaloniaKemonoPartyApp.Android.csproj

@@ -9,8 +9,8 @@
     <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
     <AndroidPackageFormat>apk</AndroidPackageFormat>
     <AndroidEnableProfiledAot>False</AndroidEnableProfiledAot>
-    <AssemblyVersion>1.0.0.29</AssemblyVersion>
-    <FileVersion>1.0.0.29</FileVersion>
+    <AssemblyVersion>1.0.0.46</AssemblyVersion>
+    <FileVersion>1.0.0.46</FileVersion>
   </PropertyGroup>
 
   <ItemGroup>

+ 25 - 14
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/App.axaml.cs

@@ -1,6 +1,10 @@
 using Avalonia;
 using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Markup.Xaml;
+using HanumanInstitute.MvvmDialogs;
+using HanumanInstitute.MvvmDialogs.Avalonia;
+using HanumanInstitute.MvvmDialogs.Avalonia.MessageBox;
+using Splat;
 using VeloeAvaloniaKemonoPartyApp.ViewModels;
 using VeloeAvaloniaKemonoPartyApp.Views;
 
@@ -11,26 +15,33 @@ namespace VeloeAvaloniaKemonoPartyApp
         public override void Initialize()
         {
             AvaloniaXamlLoader.Load(this);
+
+            var build = Locator.CurrentMutable;
+            build.RegisterLazySingleton(() => (IDialogService)new DialogService(
+                new DialogManager(
+                    viewLocator: new VeloeAvaloniaKemonoPartyApp.Dialogs.ViewLocator(),
+                    dialogFactory: new DialogFactory().AddMessageBox(MessageBoxMode.Popup)),
+                viewModelFactory: x => Locator.Current.GetService(x)));
+
+            SplatRegistrations.Register<CreatorsViewModel>();
+            SplatRegistrations.Register<CreatorPostsViewModel>();
+            SplatRegistrations.SetupIOC();
         }
 
         public override void OnFrameworkInitializationCompleted()
         {
-            if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
-            {
-                desktop.MainWindow = new MainWindow
-                {
-                    DataContext = new MainViewModel()
-                };
-            }
-            else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
-            {
-                singleViewPlatform.MainView = new MainView
-                {
-                    DataContext = new MainViewModel()
-                };
-            }
+            DialogService.Show(null, CreatorsViewModel);
 
             base.OnFrameworkInitializationCompleted();
         }
+
+        public static MainViewModel MainViewModel => Locator.Current.GetService<MainViewModel>()!;
+
+        public static CreatorsViewModel CreatorsViewModel => Locator.Current.GetService<CreatorsViewModel>()!;
+
+        public static CreatorPostsViewModel CreatorPostsViewModel => Locator.Current.GetService<CreatorPostsViewModel>()!;
+
+        private static IDialogService DialogService => Locator.Current.GetService<IDialogService>()!;
+        public static StrongViewLocator ViewLocator { get; private set; } = default!;
     }
 }

+ 1 - 4
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/Services/HttpClient.cs

@@ -27,10 +27,7 @@ namespace VeloeKemonoPartyApp.Services
             using (HttpResponseMessage response = await httpClient.GetAsync("https://kemono.su/api/v1/creators.txt"))
             using (HttpContent content = response.Content)
             {
-                string jsonString = await response.Content.ReadAsStringAsync();
-
-                // ... Read the string.
-                return JsonSerializer.Deserialize<List<Creator>>(jsonString);
+                return await JsonSerializer.DeserializeAsync<List<Creator>>(await response.Content.ReadAsStreamAsync());
             }
         }
 

+ 8 - 2
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp.csproj

@@ -4,8 +4,8 @@
     <Nullable>enable</Nullable>
     <LangVersion>latest</LangVersion>
     <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
-    <AssemblyVersion>1.0.0.30</AssemblyVersion>
-    <FileVersion>1.0.0.30</FileVersion>
+    <AssemblyVersion>1.0.0.51</AssemblyVersion>
+    <FileVersion>1.0.0.51</FileVersion>
   </PropertyGroup>
 
   
@@ -23,5 +23,11 @@
     <PackageReference Include="Avalonia.ReactiveUI" Version="11.0.6" />
     <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
     <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.6" />
+    <PackageReference Include="HanumanInstitute.MvvmDialogs.Avalonia" Version="2.0.0" />
+    <PackageReference Include="HanumanInstitute.MvvmDialogs.Avalonia.MessageBox" Version="2.0.0" />
+    <PackageReference Include="Splat.DependencyInjection.SourceGenerator" Version="1.1.93">
+      <PrivateAssets>all</PrivateAssets>
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+    </PackageReference>
   </ItemGroup>
 </Project>

+ 10 - 0
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/ViewLocator.cs

@@ -1,5 +1,6 @@
 using Avalonia.Controls;
 using Avalonia.Controls.Templates;
+using HanumanInstitute.MvvmDialogs.Avalonia;
 using System;
 using VeloeAvaloniaKemonoPartyApp.ViewModels;
 
@@ -28,4 +29,13 @@ namespace VeloeAvaloniaKemonoPartyApp
             return data is ViewModelBase;
         }
     }
+}
+
+namespace VeloeAvaloniaKemonoPartyApp.Dialogs
+{
+    public class ViewLocator : ViewLocatorBase
+    {
+        /// <inheritdoc />
+        protected override string GetViewName(object viewModel) => viewModel.GetType().FullName!.Replace("ViewModel", "View");
+    }
 }

+ 43 - 5
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/ViewModels/CreatorPostsViewModel.cs

@@ -1,25 +1,34 @@
-using ReactiveUI;
+using HanumanInstitute.MvvmDialogs;
+using HanumanInstitute.MvvmDialogs.FrameworkDialogs;
+using ReactiveUI;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
+using System.ComponentModel;
 using System.Linq;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
 using VeloeAvaloniaKemonoPartyApp.Models;
+using static System.Net.Mime.MediaTypeNames;
 
 namespace VeloeAvaloniaKemonoPartyApp.ViewModels
 {
-    public class CreatorPostsViewModel : ViewModelBase
+    public class CreatorPostsViewModel : ViewModelBase, IModalDialogViewModel, IViewClosing, IViewLoaded, ICloseable
     {
+        private readonly IDialogService _dialogService;
+        public event EventHandler? RequestClose;
+        public bool? DialogResult => true;
+
+
         private Creator _creator;
 
         public ObservableCollection<PostViewModel> Posts { get; } = new();
 
-        public CreatorPostsViewModel(Creator creator) 
+        public CreatorPostsViewModel(IDialogService dialogService) 
         {
-            _creator = creator;
-            InitPosts();
+            _dialogService = dialogService;
+            Close = ReactiveCommand.Create(CloseImpl);
         }
 
         private CancellationTokenSource? _cancellationTokenSource;
@@ -31,6 +40,11 @@ namespace VeloeAvaloniaKemonoPartyApp.ViewModels
             set => this.RaiseAndSetIfChanged(ref _isBusy, value);
         }
 
+        public Creator Creator { get => _creator; set {
+                _creator = value;
+                InitPosts();
+            } }
+
         private async void InitPosts()
         {
             _cancellationTokenSource?.Cancel();
@@ -71,5 +85,29 @@ namespace VeloeAvaloniaKemonoPartyApp.ViewModels
                 }
             }
         }
+
+        public ReactiveCommand<System.Reactive.Unit, System.Reactive.Unit> Close { get; }
+
+        public void OnLoaded()
+        {
+           
+        }
+
+        public void OnClosing(CancelEventArgs e)
+        {
+            e.Cancel = true;
+        }
+
+        private void CloseImpl()
+        {
+            RequestClose?.Invoke(this, EventArgs.Empty);
+        }
+
+        public async Task OnClosingAsync(CancelEventArgs e)
+        {
+            //var result = await _dialogService.ShowMessageBoxAsync(this, "Do you want to close it?", "Confirmation", MessageBoxButton.YesNo);
+            _cancellationTokenSource?.Cancel();
+            e.Cancel = false;
+        }
     }
 }

+ 14 - 1
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/ViewModels/CreatorsViewModel.cs

@@ -1,5 +1,7 @@
 using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Threading;
 using DynamicData.Binding;
+using HanumanInstitute.MvvmDialogs;
 using ReactiveUI;
 using System;
 using System.Collections.Generic;
@@ -16,19 +18,30 @@ namespace VeloeAvaloniaKemonoPartyApp.ViewModels
     public class CreatorsViewModel : ViewModelBase
     {
         private CancellationTokenSource? _cancellationTokenSource;
+        private readonly IDialogService _dialogService;
 
-        public CreatorsViewModel()
+        public CreatorsViewModel(IDialogService dialogService)
         {
             /*
             BuyMusicCommand = ReactiveCommand.Create(() =>
             {
                 return SelectedAlbum;
             });*/
+            this._dialogService = dialogService;
 
             this.WhenAnyValue(x => x.SearchText)
                 .Throttle(TimeSpan.FromMilliseconds(400))
                 .ObserveOn(RxApp.MainThreadScheduler)
                 .Subscribe(DoSearch!);
+
+            this.WhenPropertyChanged(x => x.SelectedAlbum).Subscribe(async x => 
+            {
+                if (x.Value is null) return;
+                var vm = _dialogService.CreateViewModel<CreatorPostsViewModel>();
+                vm.Creator = x.Value.Creator;
+                Dispatcher.UIThread.Post(() => { SelectedAlbum = null; });
+                await _dialogService.ShowDialogAsync(this, vm);
+            });
         }
 
         private string _searchText = string.Empty;

+ 20 - 4
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/ViewModels/MainViewModel.cs

@@ -1,6 +1,7 @@
 using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Input;
 using DynamicData.Binding;
 using ReactiveUI;
 using System;
@@ -24,10 +25,10 @@ namespace VeloeAvaloniaKemonoPartyApp.ViewModels
 
         public MainViewModel()
         {
-            ActualMainViewModel = new CreatorsViewModel();
+            /**ActualMainViewModel = new CreatorsViewModel();*/
             _contentViewModel = ActualMainViewModel;
 
-            ActualMainViewModel.WhenAnyValue(x => x.SelectedAlbum).Subscribe(AddItem!);
+            //ActualMainViewModel.WhenAnyValue(x => x.SelectedAlbum).Subscribe(AddItem!);
         }
 
         public CreatorsViewModel ActualMainViewModel { get; }
@@ -40,11 +41,26 @@ namespace VeloeAvaloniaKemonoPartyApp.ViewModels
 
         public void AddItem(CreatorViewModel creatorViewModel)
         {
-            if (creatorViewModel == null) return;
+            /*if (creatorViewModel == null) return;
 
             Debug.WriteLine("Call new view");
 
-            ContentViewModel = new CreatorPostsViewModel(creatorViewModel.Creator);
+            ContentViewModel = new CreatorPostsViewModel(creatorViewModel.Creator);*/
+        }
+
+        public override bool OnBackButtonPressed()
+        {
+            if(ContentViewModel.OnBackButtonPressed())
+            {
+                SetCreatorsView();
+                return true;
+            }
+            return false;
+        }
+
+        public void SetCreatorsView()
+        {
+            ContentViewModel = ActualMainViewModel;
         }
 
     }

+ 4 - 0
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/ViewModels/ViewModelBase.cs

@@ -4,5 +4,9 @@ namespace VeloeAvaloniaKemonoPartyApp.ViewModels
 {
     public class ViewModelBase : ReactiveObject
     {
+        public virtual bool OnBackButtonPressed()
+        {
+            return false;
+        }
     }
 }

+ 3 - 0
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/Views/MainView.axaml.cs

@@ -1,4 +1,7 @@
 using Avalonia.Controls;
+using Avalonia.Input;
+using System;
+using VeloeAvaloniaKemonoPartyApp.ViewModels;
 
 namespace VeloeAvaloniaKemonoPartyApp.Views
 {