-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewControllerDataResult.cs
317 lines (287 loc) · 15.8 KB
/
ViewControllerDataResult.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using Foundation;
using UIKit;
//using DocutainSdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CoreGraphics;
using System.Security.Policy;
using CoreFoundation;
using static AVFoundation.AVMetadataIdentifiers;
using System.Text.Json;
using System.Text.RegularExpressions;
using Docutain.SDK.Xamarin.iOS;
namespace Docutain_SDK_Example_Xamarin_iOS
{
public class ViewControllerDataResult : UIViewController
{
private UIActivityIndicatorView loadingIndicator = new UIActivityIndicatorView();
private UITextView textFieldName1 = GetTextView();
private UITextView textFieldName2 = GetTextView();
private UITextView textFieldName3 = GetTextView();
private UITextView textFieldZipcode = GetTextView();
private UITextView textFieldCity = GetTextView();
private UITextView textFieldStreet = GetTextView();
private UITextView textFieldPhone = GetTextView();
private UITextView textFieldCustomer = GetTextView();
private UITextView textFieldIBAN = GetTextView();
private UITextView textFieldBIC = GetTextView();
private UITextView textFieldDate = GetTextView();
private UITextView textFieldAmount = GetTextView();
private UITextView textFieldInvoiceID = GetTextView();
private UITextView textFieldReference = GetTextView();
private UITextView textFieldPaymentState = GetTextView();
private UILabel labelName1, labelName2, labelName3, labelZipcode, labelCity, labelStreet, labelIBAN, labelPhone,
labelCustomer, labelBIC, labelDate, labelAmount, labelInvoiceID, labelReference, labelPaymentState;
private UIStackView stackView;
private UIScrollView scrollview = new UIScrollView();
private NSLayoutConstraint stackViewWidthConstraint;
private nfloat padding = 16.0f;
private string filePath;
public ViewControllerDataResult(string filePath)
{
this.filePath = filePath;
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
NavigationItem.BackButtonTitle = "";
Title = "Docutain SDK Example";
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
View.BackgroundColor = UIColor.SystemBackground;
else
View.BackgroundColor = UIColor.White;
scrollview.TranslatesAutoresizingMaskIntoConstraints = false;
View.AddSubview(scrollview);
NSLayoutConstraint.ActivateConstraints(new[]
{
scrollview.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
scrollview.LeadingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.LeadingAnchor),
scrollview.TrailingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TrailingAnchor),
scrollview.BottomAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.BottomAnchor)
});
labelName1 = GetHeaderLabel();
labelName2 = GetHeaderLabel();
labelName3 = GetHeaderLabel();
labelZipcode = GetHeaderLabel();
labelCity = GetHeaderLabel();
labelStreet = GetHeaderLabel();
labelIBAN = GetHeaderLabel();
labelPhone = GetHeaderLabel();
labelCustomer = GetHeaderLabel();
labelBIC = GetHeaderLabel();
labelDate = GetHeaderLabel();
labelAmount = GetHeaderLabel();
labelInvoiceID = GetHeaderLabel();
labelReference = GetHeaderLabel();
labelPaymentState = GetHeaderLabel();
labelName1.Text = "Name1".Localized();
labelName2.Text = "Name2".Localized();
labelName3.Text = "Name3".Localized();
labelZipcode.Text = "ZipCode".Localized();
labelCity.Text = "City".Localized();
labelStreet.Text = "Street".Localized();
labelPhone.Text = "Phone".Localized();
labelCustomer.Text = "CustomerID".Localized();
labelIBAN.Text = "IBAN".Localized();
labelBIC.Text = "BIC".Localized();
labelDate.Text = "Date".Localized();
labelAmount.Text = "Amount".Localized();
labelInvoiceID.Text = "InvoiceID".Localized();
labelReference.Text = "Reference".Localized();
labelPaymentState.Text = "PaymentState".Localized();
stackView = new UIStackView();
stackView.Axis = UILayoutConstraintAxis.Vertical;
stackView.Spacing = 4;
stackView.AddArrangedSubview(labelName1);
stackView.AddArrangedSubview(textFieldName1);
stackView.AddArrangedSubview(labelName2);
stackView.AddArrangedSubview(textFieldName2);
stackView.AddArrangedSubview(labelName3);
stackView.AddArrangedSubview(textFieldName3);
stackView.AddArrangedSubview(labelZipcode);
stackView.AddArrangedSubview(textFieldZipcode);
stackView.AddArrangedSubview(labelCity);
stackView.AddArrangedSubview(textFieldCity);
stackView.AddArrangedSubview(labelStreet);
stackView.AddArrangedSubview(textFieldStreet);
stackView.AddArrangedSubview(labelCustomer);
stackView.AddArrangedSubview(textFieldCustomer);
stackView.AddArrangedSubview(labelIBAN);
stackView.AddArrangedSubview(textFieldIBAN);
stackView.AddArrangedSubview(labelBIC);
stackView.AddArrangedSubview(textFieldBIC);
stackView.AddArrangedSubview(labelDate);
stackView.AddArrangedSubview(textFieldDate);
stackView.AddArrangedSubview(labelAmount);
stackView.AddArrangedSubview(textFieldAmount);
stackView.AddArrangedSubview(labelInvoiceID);
stackView.AddArrangedSubview(textFieldInvoiceID);
stackView.AddArrangedSubview(labelReference);
stackView.AddArrangedSubview(textFieldReference);
stackView.AddArrangedSubview(labelPaymentState);
stackView.AddArrangedSubview(textFieldPaymentState);
stackView.TranslatesAutoresizingMaskIntoConstraints = false;
scrollview.AddSubview(stackView);
NSLayoutConstraint.ActivateConstraints(new NSLayoutConstraint[]
{
stackView.TopAnchor.ConstraintEqualTo(scrollview.TopAnchor, padding),
stackView.LeadingAnchor.ConstraintEqualTo(scrollview.LeadingAnchor, padding),
stackView.TrailingAnchor.ConstraintEqualTo(scrollview.TrailingAnchor, -padding),
stackView.BottomAnchor.ConstraintEqualTo(scrollview.BottomAnchor, -padding)
});
stackViewWidthConstraint = stackView.WidthAnchor.ConstraintGreaterThanOrEqualTo(0.0f);
stackViewWidthConstraint.Active = true;
loadingIndicator.HidesWhenStopped = true;
loadingIndicator.StartAnimating();
View.AddSubview(loadingIndicator);
loadingIndicator.TranslatesAutoresizingMaskIntoConstraints = false;
loadingIndicator.CenterXAnchor.ConstraintEqualTo(View.CenterXAnchor).Active = true;
loadingIndicator.CenterYAnchor.ConstraintEqualTo(View.CenterYAnchor).Active = true;
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
LoadData();
else
Console.WriteLine("Data Extraction is only available for iOS 13+");
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
stackViewWidthConstraint.Constant = View.SafeAreaLayoutGuide.LayoutFrame.Width - 2 * padding;
}
private void LoadData()
{
DispatchQueue.GetGlobalQueue(DispatchQueuePriority.Default).DispatchAsync(() =>
{
if (!string.IsNullOrEmpty(filePath))
{
//if a filePath is available it means we have imported a file. If so, we need to load it into the SDK first
if (!DocumentDataReader.LoadFile(NSUrl.FromFilename(filePath)))
{
//an error occurred, get the latest error message
Console.WriteLine($"DocumentDataReader.LoadFile failed with error: {DocutainSDK.LastError}");
return;
}
}
//analyze the currently loaded document and get the detected data
string analyzeData = DocumentDataReader.Analyze();
if (string.IsNullOrEmpty(analyzeData))
{
Console.WriteLine("no data detected");
return;
}
try
{
//detected data is returned as JSON, so deserializing the data to extract the key-value pairs
//see https://docs.docutain.com/docs/Xamarin/dataExtraction for more information
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
Dictionary<string, object> jsonArray = JsonSerializer.Deserialize<Dictionary<string, object>>(analyzeData, options);
Dictionary<string, object> address = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonArray["Address"].ToString(), options);
var name1 = address.TryGetValue("Name1", out var name1Obj) ? name1Obj.ToString() : "";
var name2 = address.TryGetValue("Name2", out var name2Obj) ? name2Obj.ToString() : "";
var name3 = address.TryGetValue("Name3", out var name3Obj) ? name3Obj.ToString() : "";
var zipcode = address.TryGetValue("Zipcode", out var zipcodeObj) ? zipcodeObj.ToString() : "";
var city = address.TryGetValue("City", out var cityObj) ? cityObj.ToString() : "";
var street = address.TryGetValue("Street", out var streetObj) ? streetObj.ToString() : "";
var phone = address.TryGetValue("Phone", out var phoneObj) ? phoneObj.ToString() : "";
var customerId = address.TryGetValue("CustomerId", out var customerIdObj) ? customerIdObj.ToString() : "";
Dictionary<string, object> bank = new Dictionary<string, object>();
if (jsonArray.ContainsKey("Bank"))
bank = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonArray["Bank"].ToString(), options);
var IBAN = "";
var BIC = "";
if (bank.Count > 0)
{
IBAN = bank.TryGetValue("IBAN", out var IBANObj) ? IBANObj.ToString() : "";
BIC = bank.TryGetValue("BIC", out var BICObj) ? BICObj.ToString() : "";
}
var date = jsonArray.TryGetValue("Date", out var dateObj) ? dateObj.ToString() : "";
var amount = jsonArray.TryGetValue("Amount", out var amountObj) ? amountObj.ToString() : "";
var invoiceId = jsonArray.TryGetValue("InvoiceId", out var invoiceIdObj) ? invoiceIdObj.ToString() : "";
var reference = jsonArray.TryGetValue("Reference", out var referenceObj) ? referenceObj.ToString() : "";
var paid = jsonArray.TryGetValue("PaymentState", out var paidObj) ? paidObj.ToString() : "";
DispatchQueue.MainQueue.DispatchAsync(() =>
{
loadingIndicator.StopAnimating();
textFieldName1.Text = name1;
textFieldName1.Hidden = string.IsNullOrEmpty(name1);
labelName1.Hidden = string.IsNullOrEmpty(name1);
textFieldName2.Text = name2;
textFieldName2.Hidden = string.IsNullOrEmpty(name2);
labelName2.Hidden = string.IsNullOrEmpty(name2);
textFieldName3.Text = name3;
textFieldName3.Hidden = string.IsNullOrEmpty(name3);
labelName3.Hidden = string.IsNullOrEmpty(name3);
textFieldZipcode.Text = zipcode;
textFieldZipcode.Hidden = string.IsNullOrEmpty(zipcode);
labelZipcode.Hidden = string.IsNullOrEmpty(zipcode);
textFieldCity.Text = city;
textFieldCity.Hidden = string.IsNullOrEmpty(city);
labelCity.Hidden = string.IsNullOrEmpty(city);
textFieldStreet.Text = street;
textFieldStreet.Hidden = string.IsNullOrEmpty(street);
labelStreet.Hidden = string.IsNullOrEmpty(street);
textFieldPhone.Text = phone;
textFieldPhone.Hidden = string.IsNullOrEmpty(phone);
labelPhone.Hidden = string.IsNullOrEmpty(phone);
textFieldCustomer.Text = customerId;
textFieldCustomer.Hidden = string.IsNullOrEmpty(customerId);
labelCustomer.Hidden = string.IsNullOrEmpty(customerId);
var IBANRegex = new Regex(".{4}");
IBAN = IBANRegex.Replace(IBAN, "$0 ");
textFieldIBAN.Text = IBAN;
textFieldIBAN.Hidden = string.IsNullOrEmpty(IBAN);
labelIBAN.Hidden = string.IsNullOrEmpty(IBAN);
textFieldBIC.Text = BIC;
textFieldBIC.Hidden = string.IsNullOrEmpty(BIC);
labelBIC.Hidden = string.IsNullOrEmpty(BIC);
textFieldDate.Text = date;
textFieldDate.Hidden = string.IsNullOrEmpty(date);
labelDate.Hidden = string.IsNullOrEmpty(date);
textFieldAmount.Text = amount;
textFieldAmount.Hidden = string.IsNullOrEmpty(amount) || amount == "0.00";
labelAmount.Hidden = string.IsNullOrEmpty(amount) || amount == "0.00";
textFieldInvoiceID.Text = invoiceId;
textFieldInvoiceID.Hidden = string.IsNullOrEmpty(invoiceId);
labelInvoiceID.Hidden = string.IsNullOrEmpty(invoiceId);
textFieldReference.Text = reference;
textFieldReference.Hidden = string.IsNullOrEmpty(reference);
labelReference.Hidden = string.IsNullOrEmpty(reference);
textFieldPaymentState.Text = paid;
textFieldPaymentState.Hidden = string.IsNullOrEmpty(paid);
labelPaymentState.Hidden = string.IsNullOrEmpty(paid);
});
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
});
}
private UILabel GetHeaderLabel()
{
var label = new UILabel();
label.Font = UIFont.SystemFontOfSize(14);
label.TextColor = UIColor.SystemGray;
label.Hidden = true;
return label;
}
private static UITextView GetTextView()
{
var textView = new UITextView();
textView.ScrollEnabled = false;
textView.TextContainerInset = new UIEdgeInsets(8, 8, 8, 8);
textView.Editable = false;
textView.Font = UIFont.SystemFontOfSize(16);
textView.Layer.BorderColor = UIColor.SystemGray.CGColor;
textView.Layer.BorderWidth = 1;
textView.Layer.CornerRadius = 8;
textView.Hidden = true;
return textView;
}
}
}