diff --git a/Bokhantarare/BareBookInfo.cs b/Bokhantarare/BareBookInfo.cs index 81daade..8f7f97c 100644 --- a/Bokhantarare/BareBookInfo.cs +++ b/Bokhantarare/BareBookInfo.cs @@ -13,7 +13,7 @@ public class BareBookInfo(string editionId, string title, string? workId = null) public Book getBook() { Dictionary? responce = JsonSerializer.Deserialize>( - Helper.Request($"https://openlibrary.org/books/{EditionID}.json")); + Helper.Request($"https://openlibrary.org/books/{EditionID}.json") ?? throw new Exception("Network Error")); if (responce == null) { diff --git a/Bokhantarare/Bokhantarare.csproj b/Bokhantarare/Bokhantarare.csproj index c4a3262..a485e62 100644 --- a/Bokhantarare/Bokhantarare.csproj +++ b/Bokhantarare/Bokhantarare.csproj @@ -8,4 +8,10 @@ true + + + Always + + + diff --git a/Bokhantarare/Book.cs b/Bokhantarare/Book.cs index 2c9b616..64ef837 100644 --- a/Bokhantarare/Book.cs +++ b/Bokhantarare/Book.cs @@ -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? responce = JsonSerializer.Deserialize>( - 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? responce = JsonSerializer.Deserialize>( - 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) { diff --git a/Bokhantarare/BookSelectionPage.xaml b/Bokhantarare/BookSelectionPage.xaml index 91b0b5c..f348b8e 100644 --- a/Bokhantarare/BookSelectionPage.xaml +++ b/Bokhantarare/BookSelectionPage.xaml @@ -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"> diff --git a/Bokhantarare/Helper.cs b/Bokhantarare/Helper.cs index 85e909a..2d403b4 100644 --- a/Bokhantarare/Helper.cs +++ b/Bokhantarare/Helper.cs @@ -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; + } } } \ No newline at end of file diff --git a/Bokhantarare/MainWindow.xaml b/Bokhantarare/MainWindow.xaml index 6e0bc76..ba6092f 100644 --- a/Bokhantarare/MainWindow.xaml +++ b/Bokhantarare/MainWindow.xaml @@ -6,7 +6,7 @@ xmlns:local="clr-namespace:Bokhantarare" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> - + diff --git a/Bokhantarare/MainWindow.xaml.cs b/Bokhantarare/MainWindow.xaml.cs index 59a9f3e..22a5450 100644 --- a/Bokhantarare/MainWindow.xaml.cs +++ b/Bokhantarare/MainWindow.xaml.cs @@ -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); diff --git a/Bokhantarare/recycle-bin.png b/Bokhantarare/recycle-bin.png new file mode 100644 index 0000000..f6c0d18 Binary files /dev/null and b/Bokhantarare/recycle-bin.png differ