395 lines
13 KiB
C#
395 lines
13 KiB
C#
using System.IO;
|
|
using System.Net.Cache;
|
|
using System.Net.Mime;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Controls.Primitives;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Navigation;
|
|
using System.Windows.Shapes;
|
|
|
|
namespace Bokhantarare;
|
|
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
private List<Book> books = new();
|
|
private List<Book> booksFiltered = new();
|
|
public MainWindow()
|
|
{
|
|
if (File.Exists("./books.json"))
|
|
{
|
|
books = JsonSerializer.Deserialize<List<Book>>(File.ReadAllText("./books.json"));
|
|
}
|
|
|
|
InitializeComponent();
|
|
|
|
ScrollBar.Minimum = 0;
|
|
ScrollBar.Maximum = books.Count-6;
|
|
|
|
if (ScrollBar.Maximum <= 0)
|
|
{
|
|
ScrollBar.Visibility = Visibility.Collapsed;
|
|
}
|
|
|
|
uppdateListedBooks(0);
|
|
|
|
Console.WriteLine(Book.GetBookFromISBN("978-1-97470719-5"));
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += (_, __) =>
|
|
{
|
|
File.WriteAllText("books.json", JsonSerializer.Serialize(books));
|
|
};
|
|
}
|
|
|
|
public void uppdateListedBooks(int offset, List<Book> listedBooks = null)
|
|
{
|
|
if (listedBooks == null || listedBooks.Count == 0)
|
|
{
|
|
listedBooks = books;
|
|
}
|
|
|
|
if (offset > listedBooks.Count)
|
|
{
|
|
offset = 0;
|
|
ScrollBar.Value = 0;
|
|
}
|
|
|
|
List<Book> shown = listedBooks.GetRange(offset, Math.Min(6, listedBooks.Count - offset));
|
|
|
|
int i = 0;
|
|
|
|
Library.Children.Clear();
|
|
|
|
foreach (Book book in shown)
|
|
{
|
|
|
|
Grid grid = new Grid();
|
|
|
|
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();
|
|
|
|
bookInfo.SetValue(Grid.ColumnProperty, 1);
|
|
|
|
RowDefinition def3 = new RowDefinition();
|
|
def3.Height = GridLength.Auto;
|
|
bookInfo.RowDefinitions.Add(def3);
|
|
|
|
RowDefinition def4 = new RowDefinition();
|
|
bookInfo.RowDefinitions.Add(def4);
|
|
|
|
TextBlock title = new TextBlock();
|
|
title.Text = book.Title;
|
|
title.SetValue(Grid.RowProperty, 0);
|
|
bookInfo.Children.Add(title);
|
|
|
|
Grid spesificInfo = new Grid();
|
|
spesificInfo.SetValue(Grid.RowProperty, 1);
|
|
|
|
spesificInfo.RowDefinitions.Add(new RowDefinition());
|
|
spesificInfo.RowDefinitions.Add(new RowDefinition());
|
|
|
|
TextBlock author = new TextBlock();
|
|
author.Text = $"Author{(book.Authors.Length == 1 ? "" : "s")} {(book.Authors.Length == 1 ? book.Authors[0] : string.Join(", ", book.Authors))}";
|
|
author.SetValue(Grid.RowProperty, 0);
|
|
spesificInfo.Children.Add(author);
|
|
|
|
TextBlock year = new TextBlock();
|
|
year.Text = $"Year {(book.PubYear)}";
|
|
year.SetValue(Grid.RowProperty, 1);
|
|
spesificInfo.Children.Add(year);
|
|
|
|
bookInfo.Children.Add(spesificInfo);
|
|
|
|
grid.Children.Add(bookInfo);
|
|
|
|
BitmapImage removeBitmap = new BitmapImage();
|
|
removeBitmap.BeginInit();
|
|
removeBitmap.UriSource = new Uri($"{Environment.CurrentDirectory}/recycle-bin.png");
|
|
removeBitmap.CacheOption = BitmapCacheOption.OnLoad; // Ensure full load before use
|
|
//removeBitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache; // Prevent caching issues
|
|
removeBitmap.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.Default); // Network-friendly caching
|
|
removeBitmap.EndInit();
|
|
|
|
Image removeImage = new Image();
|
|
removeImage.SetValue(Grid.ColumnProperty, 0);
|
|
removeImage.Stretch = Stretch.Uniform;
|
|
removeImage.Source = removeBitmap;
|
|
removeImage.MaxHeight = 20;
|
|
removeImage.MaxWidth = 20;
|
|
removeImage.Margin = new Thickness(0, 0, 5, 0);
|
|
|
|
Button remove = new Button();
|
|
remove.Click += (_, __) => RemoveBook(book);
|
|
remove.Content = removeImage;
|
|
remove.MaxWidth = 30;
|
|
remove.SetValue(Grid.ColumnProperty, 2);
|
|
|
|
grid.Children.Add(remove);
|
|
|
|
Library.Children.Add(grid);
|
|
}
|
|
|
|
Library.UpdateLayout();
|
|
}
|
|
|
|
private void ISBN_OnTextChange(object sender, TextChangedEventArgs e)
|
|
{
|
|
if (sender is TextBox textBox)
|
|
{
|
|
ISBNPlaceholderText.Visibility = string.IsNullOrEmpty(textBox.Text)
|
|
? Visibility.Visible
|
|
: Visibility.Collapsed;
|
|
}
|
|
}
|
|
private void Title_OnTextChange(object sender, TextChangedEventArgs e)
|
|
{
|
|
if (sender is TextBox textBox)
|
|
{
|
|
TitlePlaceholderText.Visibility = string.IsNullOrEmpty(textBox.Text)
|
|
? Visibility.Visible
|
|
: Visibility.Collapsed;
|
|
}
|
|
}
|
|
|
|
private void CustomTitle_OnTextChange(object sender, TextChangedEventArgs e)
|
|
{
|
|
if (sender is TextBox textBox)
|
|
{
|
|
CustomTitlePlaceholderText.Visibility = string.IsNullOrEmpty(textBox.Text)
|
|
? Visibility.Visible
|
|
: Visibility.Collapsed;
|
|
}
|
|
}
|
|
|
|
private void CustomISBN_OnTextChange(object sender, TextChangedEventArgs e)
|
|
{
|
|
if (sender is TextBox textBox)
|
|
{
|
|
CustomISBNPlaceholderText.Visibility = string.IsNullOrEmpty(textBox.Text)
|
|
? Visibility.Visible
|
|
: Visibility.Collapsed;
|
|
}
|
|
}
|
|
|
|
private void CustomAuthor_OnTextChange(object sender, TextChangedEventArgs e)
|
|
{
|
|
if (sender is TextBox textBox)
|
|
{
|
|
CustomAuthorPlaceholderText.Visibility = string.IsNullOrEmpty(textBox.Text)
|
|
? Visibility.Visible
|
|
: Visibility.Collapsed;
|
|
}
|
|
}
|
|
|
|
private void CustomPubYear_OnTextChange(object sender, TextChangedEventArgs e)
|
|
{
|
|
if (sender is TextBox textBox)
|
|
{
|
|
CustomPubYearPlaceholderText.Visibility = string.IsNullOrEmpty(textBox.Text)
|
|
? Visibility.Visible
|
|
: Visibility.Collapsed;
|
|
}
|
|
}
|
|
|
|
private void CustomEntry(object sender, RoutedEventArgs e)
|
|
{
|
|
Book book = new Book(CustomISBN.Text, CustomTitle.Text, CustomAuthor.Text.Split(", "), CustomPubYear.Text);
|
|
string messageBoxText = $"Are you sure you want to save the book?\nTitle: {book.Title}\nAuthors: {string.Join(", ", book.Authors)}\nPublising year: {book.PubYear}";
|
|
string caption = "Add Entry";
|
|
MessageBoxButton button = MessageBoxButton.YesNo;
|
|
MessageBoxImage icon = MessageBoxImage.Question;
|
|
MessageBoxResult result;
|
|
|
|
result = MessageBox.Show(messageBoxText, caption, button, icon, MessageBoxResult.Yes);
|
|
|
|
if (result == MessageBoxResult.Yes)
|
|
{
|
|
if (books.Any(b => b.Title == book.Title))
|
|
{
|
|
MessageBox.Show("Book already in Library", "Found book", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
}
|
|
else
|
|
{
|
|
AddBook(book);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SearchByISBN(object sender, RoutedEventArgs e)
|
|
{
|
|
Book book;
|
|
try
|
|
{
|
|
book = Book.GetBookFromISBN(ISBN.Text);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
return;
|
|
}
|
|
|
|
MessageBox.Show($"Found: {book.Title}", "Found book", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
if (books.Any(b => b.Title == book.Title))
|
|
{
|
|
MessageBox.Show("Book already in Library", "Found book", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
}
|
|
else
|
|
{
|
|
AddBook(book);
|
|
}
|
|
}
|
|
|
|
private void AddBook(Book book)
|
|
{
|
|
books.Add(book);
|
|
ScrollBar.Maximum = books.Count-6;
|
|
uppdateListedBooks((int)Math.Floor(ScrollBar.Value*books.Count), booksFiltered);
|
|
if (ScrollBar.Maximum > 6)
|
|
{
|
|
ScrollBar.Visibility = Visibility.Visible;
|
|
}
|
|
}
|
|
|
|
private void RemoveBook(Book book)
|
|
{
|
|
books.Remove(book);
|
|
ScrollBar.Maximum = books.Count-6;
|
|
uppdateListedBooks((int)Math.Floor(ScrollBar.Value*books.Count));
|
|
if (ScrollBar.Maximum == 0)
|
|
{
|
|
ScrollBar.Visibility = Visibility.Collapsed;
|
|
}
|
|
}
|
|
|
|
private void SearchByTitle(object sender, RoutedEventArgs e)
|
|
{
|
|
|
|
try
|
|
{
|
|
BareBookInfo[]? books = Book.GetBooksFromTitle(Title.Text);
|
|
|
|
if (books == null)
|
|
{
|
|
MessageBox.Show("No books found", "No books", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
return;
|
|
}
|
|
|
|
Console.WriteLine($"Found {books.Length} with the title \"{Title.Text}\"");
|
|
|
|
BookSelectionPage selectionPage = new BookSelectionPage(books);
|
|
selectionPage.ShowDialog();
|
|
|
|
if (selectionPage.DialogResult.HasValue && selectionPage.DialogResult.Value)
|
|
{
|
|
BareBookInfo selectedBook = selectionPage.getSelectedBook();
|
|
AddBook(selectedBook.getBook());
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("No books found", "No books", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
private void ScrollView(object sender, ScrollEventArgs e)
|
|
{
|
|
if (sender is ScrollBar scrollBar)
|
|
{
|
|
uppdateListedBooks((int)Math.Floor(scrollBar.Value*books.Count), booksFiltered);
|
|
}
|
|
}
|
|
|
|
private void Search_OnTextChange(object sender, TextChangedEventArgs e)
|
|
{
|
|
if (sender is TextBox textBox)
|
|
{
|
|
SearchPlaceholderText.Visibility = string.IsNullOrEmpty(textBox.Text)
|
|
? Visibility.Visible
|
|
: Visibility.Collapsed;
|
|
|
|
if (string.IsNullOrEmpty(textBox.Text))
|
|
{
|
|
ScrollBar.Maximum = books.Count - 6;
|
|
if (ScrollBar.Maximum <= 0)
|
|
{
|
|
ScrollBar.Visibility = Visibility.Collapsed;
|
|
}
|
|
else
|
|
{
|
|
ScrollBar.Visibility = Visibility.Visible;
|
|
}
|
|
ScrollBar.Value = 0;
|
|
booksFiltered = null;
|
|
uppdateListedBooks((int)Math.Floor(ScrollBar.Value*books.Count), booksFiltered);
|
|
}
|
|
else
|
|
{
|
|
booksFiltered = books.Where(b => b.Title.ToLower().Contains(textBox.Text.ToLower())).ToList();
|
|
ScrollBar.Maximum = booksFiltered.Count - 6;
|
|
if (ScrollBar.Maximum <= 0)
|
|
{
|
|
ScrollBar.Visibility = Visibility.Collapsed;
|
|
}
|
|
else
|
|
{
|
|
ScrollBar.Visibility = Visibility.Visible;
|
|
}
|
|
ScrollBar.Value = 0;
|
|
uppdateListedBooks((int)Math.Floor(ScrollBar.Value*books.Count), booksFiltered);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ScrolLibrary(object sender, MouseWheelEventArgs e)
|
|
{
|
|
int value = (int)Math.Floor(ScrollBar.Value + e.Delta / 120);
|
|
|
|
ScrollBar.Value = Math.Min(ScrollBar.Maximum, value);
|
|
uppdateListedBooks((int)Math.Floor(ScrollBar.Value*books.Count), booksFiltered);
|
|
}
|
|
} |