control and databse stuff for documents

This commit is contained in:
2026-01-06 19:48:48 -06:00
parent aaa33bbb04
commit ab800124e8
6 changed files with 169 additions and 10 deletions

View File

@@ -0,0 +1,30 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="CritterFolio.Controls.DocumentItem">
<Grid HorizontalAlignment="Stretch" Height="50"
Background="{DynamicResource AccentBackground}"
x:Name="MainGrid"
ColumnDefinitions="Auto, *, Auto">
<StackPanel Grid.Column="0" Orientation="Horizontal">
<Label FontFamily="{DynamicResource Phosphor}"
Foreground="{DynamicResource PrimaryForeground}"
VerticalAlignment="Center"
Content="&#xe710;"
FontSize="25"/>
<Label x:Name="NameLabel"
Margin="10, 0"
Content="Document"
Foreground="{DynamicResource PrimaryForeground}"
VerticalAlignment="Center"
FontSize="20" />
</StackPanel>
<StackPanel Grid.Column="2" Orientation="Horizontal">
<Button x:Name="ViewButton" Classes="icon" Content="&#xe220;" />
<Button x:Name="DeleteButton" Classes="icon" Content="&#xe4a6;" Foreground="Red" />
</StackPanel>
</Grid>
</UserControl>

View File

@@ -0,0 +1,73 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using CritterFolio.DataModels;
using CritterFolio.Services;
namespace CritterFolio.Controls;
public partial class DocumentItem : UserControl
{
private Document? _document;
public DocumentItem()
{
InitializeComponent();
}
public void Init(Document document)
{
_document = document;
NameLabel.Content = document.Name;
ViewButton.Click += ViewButtonOnClick;
DeleteButton.Click += DeleteButtonOnClick;
}
private async void DeleteButtonOnClick(object? sender, RoutedEventArgs e)
{
if (_document is null) return;
if (File.Exists(_document.Path))
{
var result = await DialogHelper.ShowConfirmationDialog("Are you sure you want to delete this document?");
if (result) File.Delete(_document.Path);
}
else
{
await DialogHelper.ShowMessage("Document does not exist");
}
}
private async void ViewButtonOnClick(object? sender, RoutedEventArgs e)
{
if (_document is null) return;
if (File.Exists(_document.Path))
{
var launcher = TopLevel.GetTopLevel(this)?.Launcher;
if (launcher != null)
{
var launchResult = await launcher.LaunchUriAsync(new Uri(_document.Path));
if (launchResult) return;
await DialogHelper.ShowMessage($"Couldn't launch the file: {_document.Path}");
Logger.LogToFile($"[ERROR] Couldn't launch the file: {_document.Path}");
}
else
{
await DialogHelper.ShowMessage("Launcher was null for some reason. Please report this to the developer.");
Logger.LogToFile("Launcher was null for some reason. Please report this to the developer.");
}
}
else
{
await DialogHelper.ShowMessage("Document does not exist");
Logger.LogToFile("Document does not exist");
}
}
}

View File

@@ -9,5 +9,6 @@ public class Document
public int Id { get; set; }
public int CritterId { get; set; }
public string Name { get; set; } = "Document";
public string Path { get; set; } = "";
public string Description { get; set; } = "A Document";
}

View File

@@ -6,11 +6,17 @@
x:Class="CritterFolio.Pages.CritterPage">
<ScrollViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<StackPanel Spacing="10">
<StackPanel >
<Image x:Name="ProfImage" Width="150" Height="150" Stretch="UniformToFill" Source="avares://CritterFolio/Assets/Icon.png" />
<Button HorizontalAlignment="Center" x:Name="PfpButton">Choose Profile Picture</Button>
</StackPanel>
<Label Classes="heading1">Info</Label>
<Image x:Name="ProfImage" Width="150" Height="150" Stretch="UniformToFill" Source="avares://CritterFolio/Assets/Icon.png" />
<StackPanel HorizontalAlignment="Stretch">
<Label VerticalAlignment="Center">Name: </Label>
<TextBox x:Name="NameBox" />
<TextBox x:Name="NameBox" HorizontalAlignment="Stretch"/>
</StackPanel>
<StackPanel HorizontalAlignment="Stretch">
<Label VerticalAlignment="Center">Gender: </Label>
@@ -32,10 +38,13 @@
<Label VerticalAlignment="Center">Notes: </Label>
<TextBox x:Name="NotesBox" MinHeight="100" AcceptsReturn="True" TextWrapping="Wrap" />
</StackPanel>
<StackPanel>
<Button x:Name="PfpButton">Choose Profile Picture</Button>
</StackPanel>
<!-- <Button x:Name="AddButton" Classes="icon" HorizontalAlignment="Stretch">&#xe3d4;</Button> -->
<Grid ColumnDefinitions="Auto, *, Auto" Margin="0, 10">
<Label Grid.Column="0" Classes="heading1">Documents</Label>
<Button Grid.Column="2">Add New</Button>
</Grid>
</StackPanel>
</ScrollViewer>
</UserControl>

View File

@@ -278,11 +278,7 @@ public partial class CritterPage : Page
try
{
await using var stream = await file.OpenReadAsync();
// using var streamReader = new StreamReader(stream);
// var fileContent = await streamReader.ReadToEndAsync();
var bmp = new Bitmap(stream);
var path = Path.Combine(Sys.UserDataPath, "ImageCache", $"{_critter.Id}-{_critter.Name}.jpg");
bmp.Save(path, 5);

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CritterFolio.DataModels;
using Microsoft.VisualBasic.FileIO;
@@ -79,4 +80,53 @@ public static class DatabaseService
}
#endregion
#region Document operations
public static async Task<bool> AddDocument(Document document)
{
await Init();
var result = await _db?.InsertAsync(document)!;
return !(result <= 0);
}
public static async Task<bool> UpdateDocument(Document document)
{
await Init();
return await _db?.UpdateAsync(document)! != 0;
}
public static async Task<bool> DeleteDocument(Document document)
{
await Init();
return await _db?.DeleteAsync<Document>(document)! != 0;
}
public static async Task<bool> DeleteDocument(int id)
{
await Init();
return await _db?.DeleteAsync<Document>(id)! != 0;
}
public static async Task<List<Document>> GetAllDocuments()
{
await Init();
var result = await _db?.Table<Document>().ToListAsync()!;
return result ?? [];
}
public static async Task<List<Document>> GetAllDocumentsForCritter(int critterId)
{
await Init();
var result = await _db?.Table<Document>().Where(d => d.CritterId == critterId).ToListAsync()!;
return result ?? [];
}
public static async Task<Document?> GetDocument(int id)
{
await Init();
return _db?.GetAsync<Document>(id).Result;
}
#endregion
}