Chaged colors
Added handelrs for networking exceptions Made Book selector a bit bigger Fixed issue with publishing date
This commit is contained in:
@@ -13,7 +13,7 @@ public class BareBookInfo(string editionId, string title, string? workId = null)
|
||||
public Book getBook()
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -8,4 +8,10 @@
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="recycle-bin.png">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Bokhantarare;
|
||||
|
||||
@@ -46,7 +48,7 @@ public class Book
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -86,7 +88,16 @@ public class Book
|
||||
string pubDate = "";
|
||||
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
|
||||
{
|
||||
@@ -100,7 +111,7 @@ public class Book
|
||||
title = title.Trim();
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:Bokhantarare"
|
||||
mc:Ignorable="d"
|
||||
Title="BookSelectionPage" Height="90" Width="180">
|
||||
Title="BookSelectionPage" Height="100" Width="180">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
|
||||
@@ -5,17 +5,25 @@ namespace Bokhantarare;
|
||||
|
||||
public class Helper
|
||||
{
|
||||
public static string Request(string requestUrl)
|
||||
public static string? Request(string requestUrl)
|
||||
{
|
||||
HttpClient client = new HttpClient();
|
||||
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl);
|
||||
requestMessage.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
|
||||
try
|
||||
{
|
||||
HttpClient client = new HttpClient();
|
||||
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl);
|
||||
requestMessage.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
|
||||
|
||||
//Connect to the URL
|
||||
HttpResponseMessage response = client.SendAsync(requestMessage).Result;
|
||||
//Connect to the URL
|
||||
HttpResponseMessage response = client.SendAsync(requestMessage).Result;
|
||||
|
||||
// Get the response
|
||||
string Output = response.Content.ReadAsStringAsync().Result;
|
||||
return Output;
|
||||
// Get the response
|
||||
string Output = response.Content.ReadAsStringAsync().Result;
|
||||
return Output;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
xmlns:local="clr-namespace:Bokhantarare"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
<Grid>
|
||||
<Grid Background="LightGray">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="*"/>
|
||||
|
||||
@@ -42,7 +42,7 @@ public partial class MainWindow : Window
|
||||
|
||||
uppdateListedBooks(0);
|
||||
|
||||
Console.WriteLine(Book.GetBookFromISBN("978-1-97470719-5"));
|
||||
//Console.WriteLine(Book.GetBookFromISBN("978-1-97470719-5"));
|
||||
|
||||
AppDomain.CurrentDomain.ProcessExit += (_, __) =>
|
||||
{
|
||||
@@ -68,45 +68,65 @@ public partial class MainWindow : Window
|
||||
int i = 0;
|
||||
|
||||
Library.Children.Clear();
|
||||
|
||||
|
||||
foreach (Book book in shown)
|
||||
{
|
||||
|
||||
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.SetValue(Grid.RowProperty, i);
|
||||
i++;
|
||||
|
||||
|
||||
ColumnDefinition def1 = new ColumnDefinition();
|
||||
def1.Width = new GridLength(40);
|
||||
grid.ColumnDefinitions.Add(def1);
|
||||
|
||||
|
||||
ColumnDefinition def2 = new ColumnDefinition();
|
||||
def2.Width = GridLength.Auto;
|
||||
grid.ColumnDefinitions.Add(def2);
|
||||
|
||||
|
||||
ColumnDefinition def7 = new ColumnDefinition();
|
||||
def2.Width = GridLength.Auto;
|
||||
grid.ColumnDefinitions.Add(def7);
|
||||
|
||||
BitmapImage coverBitmap = new BitmapImage();
|
||||
coverBitmap.BeginInit();
|
||||
coverBitmap.UriSource = new Uri($"https://covers.openlibrary.org/b/isbn/{book.ISBN}-S.jpg");
|
||||
coverBitmap.CacheOption = BitmapCacheOption.OnLoad; // Ensure full load before use
|
||||
//coverBitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache; // Prevent caching issues
|
||||
coverBitmap.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.Default); // Network-friendly caching
|
||||
coverBitmap.EndInit();
|
||||
|
||||
Image cover = new Image();
|
||||
cover.SetValue(Grid.ColumnProperty, 0);
|
||||
cover.Stretch = Stretch.Uniform;
|
||||
cover.Source = coverBitmap;
|
||||
cover.Margin = new Thickness(0, 0, 5, 0);
|
||||
|
||||
grid.Children.Add(cover);
|
||||
|
||||
Grid bookInfo = new Grid();
|
||||
try
|
||||
{
|
||||
|
||||
BitmapImage coverBitmap = new BitmapImage();
|
||||
coverBitmap.BeginInit();
|
||||
coverBitmap.UriSource = new Uri($"https://covers.openlibrary.org/b/isbn/{book.ISBN}-S.jpg");
|
||||
coverBitmap.CacheOption = BitmapCacheOption.OnLoad; // Ensure full load before use
|
||||
//coverBitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache; // Prevent caching issues
|
||||
coverBitmap.UriCachePolicy =
|
||||
new RequestCachePolicy(RequestCacheLevel.Default); // Network-friendly caching
|
||||
coverBitmap.EndInit();
|
||||
|
||||
Image cover = new Image();
|
||||
cover.SetValue(Grid.ColumnProperty, 0);
|
||||
cover.Stretch = Stretch.Uniform;
|
||||
cover.Source = coverBitmap;
|
||||
cover.Margin = new Thickness(0, 0, 5, 0);
|
||||
|
||||
grid.Children.Add(cover);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Grid bookInfo = new Grid();
|
||||
|
||||
bookInfo.SetValue(Grid.ColumnProperty, 1);
|
||||
|
||||
@@ -156,12 +176,13 @@ public partial class MainWindow : Window
|
||||
removeImage.Source = removeBitmap;
|
||||
removeImage.MaxHeight = 20;
|
||||
removeImage.MaxWidth = 20;
|
||||
removeImage.Margin = new Thickness(0, 0, 5, 0);
|
||||
//removeImage.Margin = new Thickness(0, 0, 5, 0);
|
||||
|
||||
Button remove = new Button();
|
||||
remove.Click += (_, __) => RemoveBook(book);
|
||||
remove.Content = removeImage;
|
||||
remove.MaxWidth = 30;
|
||||
remove.HorizontalAlignment = HorizontalAlignment.Right;
|
||||
remove.SetValue(Grid.ColumnProperty, 2);
|
||||
|
||||
grid.Children.Add(remove);
|
||||
|
||||
BIN
Bokhantarare/recycle-bin.png
Normal file
BIN
Bokhantarare/recycle-bin.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
Reference in New Issue
Block a user