Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ShowQRViewModel.cs
1using System;
3using System.Linq;
4using System.Runtime.CompilerServices;
5using System.Text;
6using System.Threading.Tasks;
7using CommunityToolkit.Mvvm.ComponentModel;
8using CommunityToolkit.Mvvm.Input;
12
14{
15 public partial class ShowQRViewModel : BasePopupViewModel
16 {
17 #region Private Properties
18
19 private byte[] qrCodeBin = [];
20 private readonly IDispatcherTimer? timer;
21 private int timerSeconds = 60;
22
23 [ObservableProperty]
24 private string title;
25
29 [ObservableProperty]
30 private string? legalId;
31
35 [ObservableProperty]
36 private ImageSource? qrCode;
37
41 [ObservableProperty]
42 private string? qrCodeUri;
43
47 [ObservableProperty]
48 private int qrCodeWidth;
49
53 [ObservableProperty]
54 private int qrCodeHeight;
55
61 [ObservableProperty]
62 private int qrResolutionScale;
63
67 [ObservableProperty]
68 private string? qrCodeContentType;
69
70 #endregion
71 #region Public Properties
72
73 public byte[] QrCodeBin
74 {
75 get => this.qrCodeBin;
76 set
77 {
78 if (value == this.qrCodeBin) return;
79 this.qrCodeBin = value;
80 this.OnPropertyChanged();
81 }
82 }
83
87 public static double CameraIconBackgroundSize => 120.0;
88
93
97 public static double CameraIconSize => 60.0;
98
99 #endregion
100
101 public ShowQRViewModel(byte[] QrCodeBin, string? QrCodeUri) : this(QrCodeBin, QrCodeUri, "QR Code")
102 {
103 }
104
105 public ShowQRViewModel(byte[] QrCodeBin, string? QrCodeUri, string Title)
106 {
107 this.Title = Title;
108
109 this.QrCodeBin = QrCodeBin;
110 this.QrCodeUri = QrCodeUri;
111 this.LegalId = ServiceRef.TagProfile.LegalIdentity?.Id;
112
113 if (this.QrCodeWidth == 0 || this.QrCodeHeight == 0)
114 {
115 this.QrCodeWidth = Constants.QrCode.DefaultImageWidth;
116 this.QrCodeHeight = Constants.QrCode.DefaultImageHeight;
117 }
118
119 if (this.QrResolutionScale == 0)
120 this.QrResolutionScale = Constants.QrCode.DefaultResolutionScale;
121
122 this.QrCode = ImageSource.FromStream(() => new MemoryStream(QrCodeBin));
123
124 this.timerSeconds = Convert.ToInt32(Constants.Timeouts.IdentityAllowedWatch.TotalSeconds);
125
126 this.timer = Application.Current?.Dispatcher.CreateTimer();
127 if (this.timer is null)
128 return;
129 this.timer.Interval = TimeSpan.FromSeconds(1);
130 this.timer.Tick += this.OnTimerTick;
131 this.timer.Start();
132
133 }
134
135 #region Commands
136
137 [RelayCommand]
138 private async Task Close()
139 {
140 this.timer?.Stop();
141 await ServiceRef.PopupService.PopAsync();
142 }
143
144 [RelayCommand]
145 private async Task ShareQR()
146 {
147 if (this.QrCodeBin is null)
148 return;
149
150 try
151 {
152 // Generate a random filename with a suitable extension (e.g., ".tmp", ".dat")
153 string FileName = $"{Guid.NewGuid()}.png";
154
155 // Define the path to save the file in the cache directory
156 string FilePath = Path.Combine(FileSystem.CacheDirectory, FileName);
157
158 // Save the byte array as a file
159 await File.WriteAllBytesAsync(FilePath, this.QrCodeBin);
160
161 // Share the file
162 await Share.Default.RequestAsync(new ShareFileRequest
163 {
164 Title = this.Title ?? "QR Code",
165 File = new ShareFile(FilePath, "image/png")
166 });
167
168 }
169 catch (Exception ex)
170 {
171 // Handle exceptions
172 ServiceRef.LogService.LogException(ex);
173 }
174 }
175
179 [RelayCommand]
180 private async Task Copy()
181 {
182 try
183 {
184 this.SetIsBusy(true);
185
186 await Clipboard.SetTextAsync(this.QrCodeUri);
187 await ServiceRef.UiService.DisplayAlert(
190 }
191 catch (Exception Ex)
192 {
193 ServiceRef.LogService.LogException(Ex);
194 await ServiceRef.UiService.DisplayException(Ex);
195 }
196 finally
197 {
198 this.SetIsBusy(false);
199 }
200 }
201
202 private void OnTimerTick(object? sender, EventArgs e)
203 {
204 MainThread.BeginInvokeOnMainThread(async () =>
205 {
206 if (this.timerSeconds > 0)
207 {
208 this.timerSeconds--;
209 }
210 else
211 {
212 try
213 {
214 this.timer?.Stop();
215 await this.CloseCommand.ExecuteAsync(null);
216 }
217 catch (Exception Ex)
218 {
219 ServiceRef.LogService.LogException(Ex);
220 }
221 }
222 });
223 }
224
225 protected override Task OnPopAsync()
226 {
227 this.timer?.Stop();
228 return base.OnPopAsync();
229 }
230
231 #endregion
232 }
233}
const int DefaultImageHeight
The default height to use when generating QR Code images.
Definition: Constants.cs:1013
const int DefaultImageWidth
The default width to use when generating QR Code images.
Definition: Constants.cs:1009
const int DefaultResolutionScale
The default scale factor to apply to the QR Code image resolution.
Definition: Constants.cs:1017
static readonly TimeSpan IdentityAllowedWatch
Allowed time to watch an Identity
Definition: Constants.cs:722
A set of never changing property constants and helpful values.
Definition: Constants.cs:24
A strongly-typed resource class, for looking up localized strings, etc.
static string SuccessfullyCopiedToClipboard
Looks up a localized string similar to Successfully copied to clipboard.
static string SuccessTitle
Looks up a localized string similar to Success.
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:130
static IPopupService PopupService
Popup service for presenting application popups.
Definition: ServiceRef.cs:142
static ITagProfile TagProfile
TAG Profile service.
Definition: ServiceRef.cs:202
static IReportingStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:370
virtual void SetIsBusy(bool IsBusy)
Sets the IsBusy property.
Base class for popup view models/>.
static double CameraIconBackgroundSize
Gets the size of the background for Camera Icon
static double CameraIconSize
Gets the size of the Camera Icon
static double CameraIconBackgroundCornerRadius
Gets the Readius of the background for Camera Icon