1using CommunityToolkit.Mvvm.ComponentModel;
2using CommunityToolkit.Mvvm.Input;
8using Microsoft.Maui.Storage;
17 private readonly
object qrSync =
new();
28 private string? qrTempFilePath;
39 if (this.QrCode is not
null &&
string.Equals(this.QrCodeUri, Uri, StringComparison.Ordinal))
45 if (this.QrCodeWidth == 0 || this.QrCodeHeight == 0)
51 if (this.QrResolutionScale == 0)
54 ServiceRef.
LogService.LogDebug($
"GenerateQrCode start: {Uri} ({this.QrCodeWidth}x{this.QrCodeHeight} scale={this.QrResolutionScale})");
55 byte[] Bin = Services.UI.QR.QrCode.GeneratePng(Uri, this.QrCodeWidth * this.QrResolutionScale, this.QrCodeHeight * this.QrResolutionScale);
61 string CacheDir = FileSystem.CacheDirectory;
62 string FileName = $
"qr_{Guid.NewGuid():N}.png";
63 string FullPath = Path.Combine(CacheDir, FileName);
64 File.WriteAllBytes(FullPath, Bin);
65 this.qrTempFilePath = FullPath;
66 this.QrCode = ImageSource.FromFile(FullPath);
72 this.QrCode = ImageSource.FromStream(() =>
new MemoryStream(Bin));
77 this.HasQrCode =
true;
90 this.QrCodeBin =
null;
91 this.QrCodeContentType =
null;
92 this.QrCodeUri =
null;
93 this.HasQrCode =
false;
94 if (!
string.IsNullOrEmpty(this.qrTempFilePath))
98 if (File.Exists(
this.qrTempFilePath))
99 File.Delete(this.qrTempFilePath);
107 this.qrTempFilePath =
null;
120 if (this.QrCodeBin is
null)
return;
124 if (!
string.IsNullOrEmpty(
Title))
125 QrPopup =
new(this.QrCodeBin, this.QrCodeUri,
Title);
127 QrPopup =
new(this.QrCodeBin, this.QrCodeUri);
138 private ImageSource? qrCode;
144 private bool hasQrCode;
150 private string? qrCodeUri;
156 private int qrCodeWidth;
162 private int qrCodeHeight;
170 private int qrResolutionScale;
176 private byte[]? qrCodeBin;
182 private string? qrCodeContentType;
188 #region ILinkableView
203 public virtual string?
Link => this.QrCodeUri;
208 public abstract Task<string>
Title {
get; }
218 public virtual byte[]?
Media => this.QrCodeBin;
const string Png
The PNG MIME type.
const int DefaultImageHeight
The default height to use when generating QR Code images.
const int DefaultImageWidth
The default width to use when generating QR Code images.
const int DefaultResolutionScale
The default scale factor to apply to the QR Code image resolution.
A set of never changing property constants and helpful values.
Base class that references services in the app.
static ILogService LogService
Log service.
static IPopupService PopupService
Popup service for presenting application popups.
A view model that holds the XMPP state.
virtual bool HasMedia
If linkable view has media associated with link.
QrXmppViewModel()
Creates an instance of a XmppViewModel.
virtual bool IsLinkable
If the current view is linkable.
virtual ? string MediaContentType
Content-Type of associated media.
abstract Task< string > Title
Title of the current view
void RemoveQrCode()
Removes the QR-code
async Task OpenQrPopup(string? Title)
Open QR Popup
void GenerateQrCode(string Uri)
Generates a QR-code
virtual ? byte[] Media
Encoded media, if available.
virtual bool EncodeAppLinks
If App links should be encoded with the link.
A view model that holds the XMPP state.
Interface for linkable views.