Chaged colors

Added handelrs for networking exceptions
Made Book selector a bit bigger
Fixed issue with publishing date
This commit is contained in:
2025-02-04 09:03:10 +01:00
parent 13c26eb3d8
commit 3c44c8dc8a
8 changed files with 84 additions and 38 deletions

View File

@@ -13,7 +13,7 @@ public class BareBookInfo(string editionId, string title, string? workId = null)
public Book getBook() public Book getBook()
{ {
Dictionary<String, Object>? responce = JsonSerializer.Deserialize<Dictionary<String, Object>>( Dictionary<String, Object>? responce = JsonSerializer.Deserialize<Dictionary<String, Object>>(
Helper.Request($"https://openlibrary.org/books/{EditionID}.json")); Helper.Request($"https://openlibrary.org/books/{EditionID}.json") ?? throw new Exception("Network Error"));
if (responce == null) if (responce == null)
{ {

View File

@@ -8,4 +8,10 @@
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Update="recycle-bin.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@@ -1,5 +1,7 @@
using System.Globalization;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
namespace Bokhantarare; namespace Bokhantarare;
@@ -46,7 +48,7 @@ public class Book
} }
Dictionary<String, Object>? responce = JsonSerializer.Deserialize<Dictionary<String, Object>>( Dictionary<String, Object>? responce = JsonSerializer.Deserialize<Dictionary<String, Object>>(
Helper.Request($"https://openlibrary.org/api/books?bibkeys=ISBN:{ISBN}&jscmd=details&format=json")); Helper.Request($"https://openlibrary.org/api/books?bibkeys=ISBN:{ISBN}&jscmd=details&format=json") ?? throw new Exception("Network Error"));
if (responce == null) if (responce == null)
{ {
@@ -86,7 +88,16 @@ public class Book
string pubDate = ""; string pubDate = "";
if (details.ContainsKey("publish_date")) if (details.ContainsKey("publish_date"))
{ {
details["publish_date"].ToString(); pubDate = details["publish_date"].ToString();
if (!Regex.IsMatch(pubDate, @"^\d{4}$"))
{
if (DateTime.TryParse(pubDate, CultureInfo.InvariantCulture, DateTimeStyles.None,
out DateTime date))
{
pubDate = date.ToString("yyyy");
}
}
} }
else else
{ {
@@ -100,7 +111,7 @@ public class Book
title = title.Trim(); title = title.Trim();
Dictionary<String, Object>? responce = JsonSerializer.Deserialize<Dictionary<String, Object>>( Dictionary<String, Object>? responce = JsonSerializer.Deserialize<Dictionary<String, Object>>(
Helper.Request($"https://openlibrary.org/search.json?q={title}")); Helper.Request($"https://openlibrary.org/search.json?q={title}") ?? throw new Exception("Network Error"));
if (responce == null) if (responce == null)
{ {

View File

@@ -5,7 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Bokhantarare" xmlns:local="clr-namespace:Bokhantarare"
mc:Ignorable="d" mc:Ignorable="d"
Title="BookSelectionPage" Height="90" Width="180"> Title="BookSelectionPage" Height="100" Width="180">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>

View File

@@ -5,7 +5,9 @@ namespace Bokhantarare;
public class Helper public class Helper
{ {
public static string Request(string requestUrl) public static string? Request(string requestUrl)
{
try
{ {
HttpClient client = new HttpClient(); HttpClient client = new HttpClient();
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl);
@@ -18,4 +20,10 @@ public class Helper
string Output = response.Content.ReadAsStringAsync().Result; string Output = response.Content.ReadAsStringAsync().Result;
return Output; return Output;
} }
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return null;
}
}
} }

View File

@@ -6,7 +6,7 @@
xmlns:local="clr-namespace:Bokhantarare" xmlns:local="clr-namespace:Bokhantarare"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800">
<Grid> <Grid Background="LightGray">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="*"/> <RowDefinition Height="*"/>

View File

@@ -42,7 +42,7 @@ public partial class MainWindow : Window
uppdateListedBooks(0); uppdateListedBooks(0);
Console.WriteLine(Book.GetBookFromISBN("978-1-97470719-5")); //Console.WriteLine(Book.GetBookFromISBN("978-1-97470719-5"));
AppDomain.CurrentDomain.ProcessExit += (_, __) => AppDomain.CurrentDomain.ProcessExit += (_, __) =>
{ {
@@ -74,6 +74,17 @@ public partial class MainWindow : Window
Grid grid = new Grid(); Grid grid = new Grid();
Color c = new Color();
if (i % 2 == 0)
{
grid.Background = Brushes.DarkGray;
}
else
{
grid.Background = Brushes.LightGray;
}
grid.Margin = new Thickness(0, 5, 0, 0); grid.Margin = new Thickness(0, 5, 0, 0);
grid.SetValue(Grid.RowProperty, i); grid.SetValue(Grid.RowProperty, i);
i++; i++;
@@ -90,12 +101,16 @@ public partial class MainWindow : Window
def2.Width = GridLength.Auto; def2.Width = GridLength.Auto;
grid.ColumnDefinitions.Add(def7); grid.ColumnDefinitions.Add(def7);
try
{
BitmapImage coverBitmap = new BitmapImage(); BitmapImage coverBitmap = new BitmapImage();
coverBitmap.BeginInit(); coverBitmap.BeginInit();
coverBitmap.UriSource = new Uri($"https://covers.openlibrary.org/b/isbn/{book.ISBN}-S.jpg"); coverBitmap.UriSource = new Uri($"https://covers.openlibrary.org/b/isbn/{book.ISBN}-S.jpg");
coverBitmap.CacheOption = BitmapCacheOption.OnLoad; // Ensure full load before use coverBitmap.CacheOption = BitmapCacheOption.OnLoad; // Ensure full load before use
//coverBitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache; // Prevent caching issues //coverBitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache; // Prevent caching issues
coverBitmap.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.Default); // Network-friendly caching coverBitmap.UriCachePolicy =
new RequestCachePolicy(RequestCacheLevel.Default); // Network-friendly caching
coverBitmap.EndInit(); coverBitmap.EndInit();
Image cover = new Image(); Image cover = new Image();
@@ -105,6 +120,11 @@ public partial class MainWindow : Window
cover.Margin = new Thickness(0, 0, 5, 0); cover.Margin = new Thickness(0, 0, 5, 0);
grid.Children.Add(cover); grid.Children.Add(cover);
}
catch(Exception e)
{
}
Grid bookInfo = new Grid(); Grid bookInfo = new Grid();
@@ -156,12 +176,13 @@ public partial class MainWindow : Window
removeImage.Source = removeBitmap; removeImage.Source = removeBitmap;
removeImage.MaxHeight = 20; removeImage.MaxHeight = 20;
removeImage.MaxWidth = 20; removeImage.MaxWidth = 20;
removeImage.Margin = new Thickness(0, 0, 5, 0); //removeImage.Margin = new Thickness(0, 0, 5, 0);
Button remove = new Button(); Button remove = new Button();
remove.Click += (_, __) => RemoveBook(book); remove.Click += (_, __) => RemoveBook(book);
remove.Content = removeImage; remove.Content = removeImage;
remove.MaxWidth = 30; remove.MaxWidth = 30;
remove.HorizontalAlignment = HorizontalAlignment.Right;
remove.SetValue(Grid.ColumnProperty, 2); remove.SetValue(Grid.ColumnProperty, 2);
grid.Children.Add(remove); grid.Children.Add(remove);

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB