Sfoglia il codice sorgente

Covered httpexception

flabbet 3 anni fa
parent
commit
ef79e02238

+ 1 - 0
PixiEditor/Models/DataHolders/LospecPalette/PaletteList.cs

@@ -4,6 +4,7 @@ namespace PixiEditor.Models.DataHolders
 {
     public class PaletteList
     {
+        public bool FetchedCorrectly { get; set; }
         public List<Palette> Palettes { get; set; }
     }
 }

+ 16 - 8
PixiEditor/Models/ExternalServices/LospecPaletteFetcher.cs

@@ -12,18 +12,26 @@ namespace PixiEditor.Models.ExternalServices
         public const string LospecApiUrl = "https://lospec.com/palette-list";
         public static async Task<PaletteList> FetchPage(int page)
         {
-            using (HttpClient client = new HttpClient())
+            try
             {
-                string url = @$"{LospecApiUrl}/load?colorNumberFilterType=any&page={page}&tag=&sortingType=default";
-                HttpResponseMessage response = await client.GetAsync(url);
-                if (response.StatusCode == HttpStatusCode.OK)
+                using (HttpClient client = new HttpClient())
                 {
-                    string content = await response.Content.ReadAsStringAsync();
-                    var obj = JsonConvert.DeserializeObject<PaletteList>(content);
-                    obj.Palettes.ForEach(x => ReadjustColors(x.Colors));
-                    return obj;
+                    string url = @$"{LospecApiUrl}/load?colorNumberFilterType=any&page={page}&tag=&sortingType=default";
+                    HttpResponseMessage response = await client.GetAsync(url);
+                    if (response.StatusCode == HttpStatusCode.OK)
+                    {
+                        string content = await response.Content.ReadAsStringAsync();
+                        var obj = JsonConvert.DeserializeObject<PaletteList>(content);
+                        obj.FetchedCorrectly = true;
+                        obj.Palettes.ForEach(x => ReadjustColors(x.Colors));
+                        return obj;
+                    }
                 }
             }
+            catch(HttpRequestException)
+            {
+                return new PaletteList() { FetchedCorrectly = false };
+            }
 
             return null;
         }

+ 20 - 10
PixiEditor/Views/UserControls/Lospec/LospecPalettesBrowser.xaml

@@ -3,16 +3,26 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-             xmlns:local="clr-namespace:PixiEditor.Views.UserControls.Lospec"
+             xmlns:local="clr-namespace:PixiEditor.Views.UserControls.Lospec" xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
              mc:Ignorable="d" 
              d:DesignHeight="400" d:DesignWidth="250" GotFocus="UserControl_GotFocus" Name="lospecPalettesBrowser">
-    <ScrollViewer Margin="5" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
-        <ItemsControl ItemsSource="{Binding ElementName=lospecPalettesBrowser, Path=PaletteList.Palettes}">
-            <ItemsControl.ItemTemplate>
-                <DataTemplate>
-                    <local:LospecPaletteItem Palette="{Binding}" ImportPaletteCommand="{Binding ImportPaletteCommand, ElementName=lospecPalettesBrowser}"/>
-                </DataTemplate>
-            </ItemsControl.ItemTemplate>
-        </ItemsControl>   
-    </ScrollViewer>
+    <UserControl.Resources>
+        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
+    </UserControl.Resources>
+    <Grid>
+        <TextBlock Text="Couldn't fetch palettes" Foreground="White" FontSize="20" HorizontalAlignment="Center" 
+                   VerticalAlignment="Center" Visibility="{Binding ElementName=itemsControl, 
+            Path=Visibility, Converter={converters:OppositeVisibilityConverter}}"/>
+        <ScrollViewer Margin="5" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
+            <ItemsControl Name="itemsControl" ItemsSource="{Binding ElementName=lospecPalettesBrowser, Path=PaletteList.Palettes}" 
+                      Visibility="{Binding ElementName=lospecPalettesBrowser, Path=PaletteList.FetchedCorrectly,
+            Converter={StaticResource BoolToVisibilityConverter}}">
+                <ItemsControl.ItemTemplate>
+                    <DataTemplate>
+                        <local:LospecPaletteItem Palette="{Binding}" ImportPaletteCommand="{Binding ImportPaletteCommand, ElementName=lospecPalettesBrowser}"/>
+                    </DataTemplate>
+                </ItemsControl.ItemTemplate>
+            </ItemsControl>
+        </ScrollViewer>
+    </Grid>
 </UserControl>