-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76c0472
commit fe0d2c3
Showing
12 changed files
with
542 additions
and
222 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.IO; | ||
using Avalonia.Data.Converters; | ||
using Avalonia.Media.Imaging; | ||
|
||
namespace ChatAAC.Converters; | ||
|
||
public class Base64ToBitmapConverter : IValueConverter | ||
{ | ||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
if (value is not string base64String || string.IsNullOrEmpty(base64String)) | ||
return null; // fallback if there's no valid base64 | ||
try | ||
{ | ||
// Convert the base64 string to a byte array | ||
var imageBytes = System.Convert.FromBase64String(base64String); | ||
|
||
// Use a MemoryStream to create a Bitmap | ||
using var ms = new MemoryStream(imageBytes); | ||
var bmp = new Bitmap(ms); | ||
return bmp; | ||
} | ||
catch | ||
{ | ||
// If the string is invalid base64, or decoding fails, return null | ||
} | ||
return null; // fallback if there's no valid base64 | ||
} | ||
|
||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
// Typically not needed for a one-way binding | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using ReactiveUI; | ||
using System.Reactive; | ||
using ChatAAC.Models.Obf; | ||
using Avalonia; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
|
||
namespace ChatAAC.ViewModels; | ||
|
||
public class AddImageViewModel : ReactiveObject | ||
{ | ||
private string _id = ""; | ||
private string _url = ""; | ||
private string _dataUrl = ""; | ||
private string _path = ""; | ||
private string _contentType = "image/png"; | ||
private int _width; | ||
private int _height; | ||
|
||
public bool IsConfirmed { get; private set; } | ||
|
||
public ReactiveCommand<Unit, Unit> ConfirmCommand { get; } | ||
public ReactiveCommand<Unit, Unit> CancelCommand { get; } | ||
|
||
public AddImageViewModel() | ||
{ | ||
ConfirmCommand = ReactiveCommand.Create(Confirm); | ||
CancelCommand = ReactiveCommand.Create(Cancel); | ||
} | ||
|
||
#region Properties | ||
public string Id | ||
{ | ||
get => _id; | ||
set => this.RaiseAndSetIfChanged(ref _id, value); | ||
} | ||
|
||
public string Url | ||
{ | ||
get => _url; | ||
set => this.RaiseAndSetIfChanged(ref _url, value); | ||
} | ||
|
||
public string DataUrl | ||
{ | ||
get => _dataUrl; | ||
set => this.RaiseAndSetIfChanged(ref _dataUrl, value); | ||
} | ||
|
||
public string Path | ||
{ | ||
get => _path; | ||
set => this.RaiseAndSetIfChanged(ref _path, value); | ||
} | ||
|
||
public string ContentType | ||
{ | ||
get => _contentType; | ||
set => this.RaiseAndSetIfChanged(ref _contentType, value); | ||
} | ||
|
||
public int Width | ||
{ | ||
get => _width; | ||
set => this.RaiseAndSetIfChanged(ref _width, value); | ||
} | ||
|
||
public int Height | ||
{ | ||
get => _height; | ||
set => this.RaiseAndSetIfChanged(ref _height, value); | ||
} | ||
#endregion | ||
|
||
private void Confirm() | ||
{ | ||
IsConfirmed = true; | ||
CloseWindow(); | ||
} | ||
|
||
private void Cancel() | ||
{ | ||
IsConfirmed = false; | ||
CloseWindow(); | ||
} | ||
|
||
private void CloseWindow() | ||
{ | ||
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) | ||
{ | ||
desktop.Windows[^1].Close(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new Image object from the user inputs. | ||
/// </summary> | ||
public Image CreateImage() | ||
{ | ||
return new Image | ||
{ | ||
Id = _id, | ||
Url = _url, | ||
DataUrl = _dataUrl, | ||
Path = _path, | ||
ContentType = _contentType, | ||
Width = _width, | ||
Height = _height | ||
}; | ||
} | ||
} |
Oops, something went wrong.