Bläddra i källkod

added cache to creators list

Veloe 1 år sedan
förälder
incheckning
c1a1cd4262

+ 2 - 2
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp.Desktop/VeloeAvaloniaKemonoPartyApp.Desktop.csproj

@@ -10,8 +10,8 @@
 
   <PropertyGroup>
     <ApplicationManifest>app.manifest</ApplicationManifest>
-    <AssemblyVersion>1.0.0.18</AssemblyVersion>
-    <FileVersion>1.0.0.18</FileVersion>
+    <AssemblyVersion>1.0.0.20</AssemblyVersion>
+    <FileVersion>1.0.0.20</FileVersion>
   </PropertyGroup>
 
   <ItemGroup>

+ 4 - 4
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/Models/Creator.cs

@@ -18,11 +18,11 @@ namespace VeloeAvaloniaKemonoPartyApp.Models
 
         public static IList<Creator> Source = new List<Creator>(66000);
 
-        public static async Task<IEnumerable<Creator>> SearchAsync(string searchTerm)
+        public static async Task<IEnumerable<Creator>> SearchAsync(string searchTerm, bool reloadSource)
         {
-            //TODO create disk cache
-            if (Source.Count == 0)
-                Source = await s_httpClient.GetCreatorsList();
+            if (Source.Count == 0 || reloadSource)
+                Source = await s_httpClient.GetCreatorsList(reloadSource);
+              
 
             return Source.Where(x=>x.name.StartsWith(searchTerm,System.StringComparison.OrdinalIgnoreCase)).Take(50);
         }

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

@@ -4,11 +4,14 @@ using System.Net.Http;
 using System.Threading.Tasks;
 using System.Text.Json;
 using VeloeAvaloniaKemonoPartyApp.Models;
+using VeloeAvaloniaKemonoPartyApp.Services;
 
 namespace VeloeKemonoPartyApp.Services
 {
     public class KemonoHttpClient : IDisposable
     {
+        private StorageService _storageService = new();
+
         public HttpClient httpClient;
 
         public List<Post> posts;
@@ -22,11 +25,31 @@ namespace VeloeKemonoPartyApp.Services
             posts = new List<Post>();
         }
 
-        public async Task<List<Creator>> GetCreatorsList()
+        public async Task<List<Creator>> GetCreatorsList(bool reloadSource)
         {
+            if (System.IO.File.Exists(await _storageService.GetDocumentsFolderAsync() + "/creators.json") && new System.IO.FileInfo(await _storageService.GetDocumentsFolderAsync() + "/creators.json").CreationTime > DateTime.Now.AddDays(-7) && !reloadSource)
+            {
+                try
+                {
+                    using (var stream = System.IO.File.OpenRead(await _storageService.GetDocumentsFolderAsync() + "/creators.json"))
+                    {
+                        return await JsonSerializer.DeserializeAsync<List<Creator>>(stream);
+                    }
+                }
+                catch 
+                {
+                    System.IO.File.Delete(await _storageService.GetDocumentsFolderAsync() + "/creators.json");
+                }
+            }
+
             using (HttpResponseMessage response = await httpClient.GetAsync("https://kemono.su/api/v1/creators.txt"))
             using (HttpContent content = response.Content)
             {
+                using (var stream = System.IO.File.OpenWrite(await _storageService.GetDocumentsFolderAsync() + "/creators.json"))
+                {
+                    stream.Write(await response.Content.ReadAsByteArrayAsync());
+                }
+
                 return await JsonSerializer.DeserializeAsync<List<Creator>>(await response.Content.ReadAsStreamAsync());
             }
         }

+ 2 - 2
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp.csproj

@@ -4,8 +4,8 @@
     <Nullable>enable</Nullable>
     <LangVersion>latest</LangVersion>
     <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
-    <AssemblyVersion>1.0.0.98</AssemblyVersion>
-    <FileVersion>1.0.0.98</FileVersion>
+    <AssemblyVersion>1.0.0.122</AssemblyVersion>
+    <FileVersion>1.0.0.122</FileVersion>
   </PropertyGroup>
 
   

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

@@ -1,4 +1,6 @@
 using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Input;
+using Avalonia.Interactivity;
 using Avalonia.Threading;
 using DynamicData.Binding;
 using HanumanInstitute.MvvmDialogs;
@@ -80,7 +82,37 @@ namespace VeloeAvaloniaKemonoPartyApp.ViewModels
 
             if (!string.IsNullOrWhiteSpace(s))
             {
-                var creators = await Creator.SearchAsync(s);
+                var creators = await Creator.SearchAsync(s,false);
+
+                foreach (var creator in creators)
+                {
+                    var vm = new CreatorViewModel(creator);
+                    SearchResults.Add(vm);
+                }
+
+                if (!cancellationToken.IsCancellationRequested)
+                {
+                    LoadAvatars(cancellationToken);
+                }
+            }
+
+            IsBusy = false;
+        }
+
+        public async Task ReloadCreatorsSource(object? sender, PullGestureEventArgs e)
+        {
+            if (IsBusy) return;
+
+            _cancellationTokenSource?.Cancel();
+            _cancellationTokenSource = new CancellationTokenSource();
+            var cancellationToken = _cancellationTokenSource.Token;
+
+            IsBusy = true;
+            SearchResults.Clear();
+
+            if (!string.IsNullOrWhiteSpace(SearchText))
+            {
+                var creators = await Creator.SearchAsync(SearchText,true);
 
                 foreach (var creator in creators)
                 {

+ 6 - 2
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/Views/CreatorsView.axaml

@@ -18,10 +18,14 @@
 			<ProgressBar IsIndeterminate="True" IsVisible="{Binding IsBusy}"/>
 		</StackPanel>
 		<ListBox ItemsSource="{Binding SearchResults}" SelectedItem="{Binding SelectedAlbum}" SelectionMode="Single"
-				 Background="Transparent" Margin="0 20">
+				 Background="Transparent" Margin="0 20" Gestures.PullGesture="ReloadCreatorsSource">
 			<ListBox.ItemsPanel>
 				<ItemsPanelTemplate>
-					<WrapPanel />
+					<WrapPanel>
+						<WrapPanel.GestureRecognizers>
+							<PullGestureRecognizer PullDirection="TopToBottom"/>
+						</WrapPanel.GestureRecognizers>
+					</WrapPanel>
 				</ItemsPanelTemplate>
 			</ListBox.ItemsPanel>
 		</ListBox>

+ 11 - 0
VeloeAvaloniaKemonoPartyApp/VeloeAvaloniaKemonoPartyApp/Views/CreatorsView.axaml.cs

@@ -1,6 +1,12 @@
 using Avalonia;
 using Avalonia.Controls;
+using Avalonia.Input;
+using Avalonia.Interactivity;
 using Avalonia.Markup.Xaml;
+using System.Diagnostics;
+using System.Threading;
+using VeloeAvaloniaKemonoPartyApp.Models;
+using VeloeAvaloniaKemonoPartyApp.ViewModels;
 
 namespace VeloeAvaloniaKemonoPartyApp.Views;
 
@@ -10,4 +16,9 @@ public partial class CreatorsView : UserControl
     {
         InitializeComponent();
     }
+
+    public async void ReloadCreatorsSource(object? sender, PullGestureEventArgs e)
+    {
+        await ((CreatorsViewModel)this.DataContext).ReloadCreatorsSource(sender, e); 
+    }
 }