control and databse stuff for documents
This commit is contained in:
@@ -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=""
|
||||||
|
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="" />
|
||||||
|
<Button x:Name="DeleteButton" Classes="icon" Content="" Foreground="Red" />
|
||||||
|
</StackPanel>
|
||||||
|
</Grid>
|
||||||
|
</UserControl>
|
||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,5 +9,6 @@ public class Document
|
|||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public int CritterId { get; set; }
|
public int CritterId { get; set; }
|
||||||
public string Name { get; set; } = "Document";
|
public string Name { get; set; } = "Document";
|
||||||
|
public string Path { get; set; } = "";
|
||||||
public string Description { get; set; } = "A Document";
|
public string Description { get; set; } = "A Document";
|
||||||
}
|
}
|
||||||
@@ -6,11 +6,17 @@
|
|||||||
x:Class="CritterFolio.Pages.CritterPage">
|
x:Class="CritterFolio.Pages.CritterPage">
|
||||||
<ScrollViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
|
<ScrollViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
|
||||||
<StackPanel Spacing="10">
|
<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>
|
<Label Classes="heading1">Info</Label>
|
||||||
<Image x:Name="ProfImage" Width="150" Height="150" Stretch="UniformToFill" Source="avares://CritterFolio/Assets/Icon.png" />
|
|
||||||
<StackPanel HorizontalAlignment="Stretch">
|
<StackPanel HorizontalAlignment="Stretch">
|
||||||
<Label VerticalAlignment="Center">Name: </Label>
|
<Label VerticalAlignment="Center">Name: </Label>
|
||||||
<TextBox x:Name="NameBox" />
|
<TextBox x:Name="NameBox" HorizontalAlignment="Stretch"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<StackPanel HorizontalAlignment="Stretch">
|
<StackPanel HorizontalAlignment="Stretch">
|
||||||
<Label VerticalAlignment="Center">Gender: </Label>
|
<Label VerticalAlignment="Center">Gender: </Label>
|
||||||
@@ -32,10 +38,13 @@
|
|||||||
<Label VerticalAlignment="Center">Notes: </Label>
|
<Label VerticalAlignment="Center">Notes: </Label>
|
||||||
<TextBox x:Name="NotesBox" MinHeight="100" AcceptsReturn="True" TextWrapping="Wrap" />
|
<TextBox x:Name="NotesBox" MinHeight="100" AcceptsReturn="True" TextWrapping="Wrap" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<StackPanel>
|
|
||||||
<Button x:Name="PfpButton">Choose Profile Picture</Button>
|
<Grid ColumnDefinitions="Auto, *, Auto" Margin="0, 10">
|
||||||
</StackPanel>
|
<Label Grid.Column="0" Classes="heading1">Documents</Label>
|
||||||
<!-- <Button x:Name="AddButton" Classes="icon" HorizontalAlignment="Stretch"></Button> -->
|
<Button Grid.Column="2">Add New</Button>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|||||||
@@ -278,11 +278,7 @@ public partial class CritterPage : Page
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
await using var stream = await file.OpenReadAsync();
|
await using var stream = await file.OpenReadAsync();
|
||||||
// using var streamReader = new StreamReader(stream);
|
|
||||||
// var fileContent = await streamReader.ReadToEndAsync();
|
|
||||||
|
|
||||||
var bmp = new Bitmap(stream);
|
var bmp = new Bitmap(stream);
|
||||||
var path = Path.Combine(Sys.UserDataPath, "ImageCache", $"{_critter.Id}-{_critter.Name}.jpg");
|
var path = Path.Combine(Sys.UserDataPath, "ImageCache", $"{_critter.Id}-{_critter.Name}.jpg");
|
||||||
bmp.Save(path, 5);
|
bmp.Save(path, 5);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CritterFolio.DataModels;
|
using CritterFolio.DataModels;
|
||||||
using Microsoft.VisualBasic.FileIO;
|
using Microsoft.VisualBasic.FileIO;
|
||||||
@@ -79,4 +80,53 @@ public static class DatabaseService
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#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
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user