This repository has been archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
SignatureListViewModel.cs
119 lines (94 loc) · 3.01 KB
/
SignatureListViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Acr.XamForms.SignaturePad;
using Samples.Models;
using Xamarin.Forms;
namespace Samples.ViewModels {
public class SignatureListViewModel : ViewModel {
private const string FILE_FORMAT = "{0:dd-MM-yyyy_hh-mm-ss_tt}.jpg";
private readonly ISignatureService signatureService;
private readonly IFileSystem fileSystem;
private readonly IUserDialogService dialogs;
public SignatureListViewModel(ISignatureService signatureService, IUserDialogService dialogs, IFileSystem fileSystem) {
this.signatureService = signatureService;
this.dialogs = dialogs;
this.fileSystem = fileSystem;
this.Create = new Command(async () => await this.OnCreate());
this.List = new ObservableList<Signature>();
}
public override void OnAppearing() {
this.List.Clear();
var signatures = this.fileSystem
.AppData
.Files
.Select(x => new Signature {
FileName = x.Name,
FilePath = x.FullName,
FileSize = x.Length
})
.ToList();
this.List.AddRange(signatures);
this.NoData = !this.List.Any();
}
public ObservableList<Signature> List { get; private set; }
private bool noData;
public bool NoData {
get { return this.noData; }
set { this.SetProperty(ref this.noData, value); }
}
public ICommand Create { get; private set; }
private async Task OnCreate() {
var result = await this.signatureService.Request();
if (result.Cancelled)
this.dialogs.Alert("Cancelled Signature");
else {
var fileName = String.Format(FILE_FORMAT, DateTime.Now);
IFile file = null;
using (var stream = result.GetStream()) {
file = this.fileSystem.Temp.CreateFile(fileName);
using (var fs = file.OpenWrite())
stream.CopyTo(fs);
}
this.List.Add(new Signature {
FilePath = file.FullName,
FileName = file.Name,
FileSize = file.Length
});
this.dialogs.Alert(String.Format("Draw Points: {0}", result.Points.Count()));
this.NoData = !this.List.Any();
}
}
private Command<Signature> selectCmd;
public Command<Signature> Select {
get {
this.selectCmd = this.selectCmd ?? new Command<Signature>(s =>
this.dialogs.ActionSheet(new ActionSheetConfig()
.Add("View", () => {
try {
Device.OpenUri(new Uri("file://" + s.FilePath));
}
catch {
this.dialogs.Alert("Cannot open file");
}
})
.Add("Delete", async () => {
var r = await this.dialogs.ConfirmAsync(String.Format("Are you sure you want to delete {0}", s.FileName));
if (!r)
return;
var file = this.fileSystem.GetFile(s.FilePath);
file.Delete();
this.List.Remove(s);
this.NoData = !this.List.Any();
})
.Add("Cancel")
)
);
return this.selectCmd;
}
}
}
}