-
Notifications
You must be signed in to change notification settings - Fork 204
/
Serialization.cs
514 lines (406 loc) · 13.5 KB
/
Serialization.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#region Copyright Syncfusion Inc. 2001-2024.
// Copyright Syncfusion Inc. 2001-2024. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using Syncfusion.SfImageEditor.iOS;
using Foundation;
using UIKit;
using CoreGraphics;
using System.Drawing;
using Photos;
using System.IO;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Collections.Generic;
namespace SampleBrowser
{
public class Serialization : SampleView
{
CustomCollectionView CustomCollectionView = new CustomCollectionView();
SerializationViewModel ViewModel = new SerializationViewModel();
UINavigationController navigationController;
List<SerializationModel> list = new List<SerializationModel>();
UIButton deleteIcon;
public override void MovedToSuperview()
{
base.MovedToSuperview();
var view = Superview;
var window = UIApplication.SharedApplication.KeyWindow;
navigationController = window.RootViewController as UINavigationController;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
CustomCollectionView.Frame = new CGRect(0, 70, Frame.Width, Frame.Height - 150);
CustomCollectionView.DataSource = ViewModel.ModelCollection;
CustomCollectionView.ItemSelected += (sender, e) =>
{
var model = e as SerializationModel;
ValidateItem(model);
};
CustomCollectionView.LongPressed += (sender, e) =>
{
if ((e as SerializationModel).Name != "Create New")
{
deleteIcon.Alpha = 1;
EnableSelection();
}
};
this.BackgroundColor = UIColor.FromRGB(242, 242, 242);
/*------Header Label-------*/
UILabel label = new UILabel(new CGRect(20, 50, Frame.Width, 25));
label.BackgroundColor = UIColor.Clear;
label.Font = UIFont.SystemFontOfSize(25);
label.TextAlignment = UITextAlignment.Left;
label.TextColor = UIColor.Gray;
label.Text = "Serialization";
/*------Delete------------*/
deleteIcon = new UIButton(new CGRect(Frame.Width / 2 - 50, Frame.Height - 60, 100, 50));
deleteIcon.Alpha = 0;
deleteIcon.BackgroundColor = UIColor.Clear;
deleteIcon.TouchUpInside += (sender, e) =>
{
for (int i = 0; i < list.Count; i++)
{
if (ViewModel.ModelCollection.Contains(list[i]))
{
ViewModel.ModelCollection.Remove(list[i]);
}
}
foreach (var item1 in ViewModel.ModelCollection)
{
item1.IsImageSelected = false;
item1.IsItemSelectedToDelete = false;
list = new List<SerializationModel>();
}
CustomCollectionView.DataSource = ViewModel.ModelCollection;
deleteIcon.Alpha = 0;
};
UIImageView DeleteButton = new UIImageView(new CGRect(0,0, 100, 50));
DeleteButton.Image = UIImage.FromBundle("Images/ImageEditor/Delete1.png");
DeleteButton.ContentMode = UIViewContentMode.ScaleAspectFit;
deleteIcon.AddSubview(DeleteButton);
AddSubview(label);
AddSubview(CustomCollectionView);
AddSubview(deleteIcon);
}
void ValidateItem(SerializationModel model)
{
if (model.Name == "Create New")
{
deleteIcon.Alpha = 0;
navigationController.PushViewController(new EditorViewController(model, CustomCollectionView, ViewModel), false);
foreach (var item1 in ViewModel.ModelCollection)
{
item1.IsImageSelected = false;
item1.IsItemSelectedToDelete = false;
}
CustomCollectionView.DataSource = ViewModel.ModelCollection;
list = new List<SerializationModel>();
}
else if (!model.IsImageSelected)
navigationController.PushViewController(new EditorViewController(model, CustomCollectionView, ViewModel), false);
else
{
if (!model.IsItemSelectedToDelete)
{
model.IsItemSelectedToDelete = true;
list.Add(model);
}
else
{
model.IsItemSelectedToDelete = false;
if(list.Count!=0)
list.Remove(model);
}
CustomCollectionView.DataSource = ViewModel.ModelCollection;
}
}
void EnableSelection()
{
foreach (var item in ViewModel.ModelCollection)
{
item.IsImageSelected = true;
}
CustomCollectionView.DataSource = ViewModel.ModelCollection;
}
}
public class EditorViewController : UIViewController
{
SerializationModel _model;
SfImageEditor sfImageEditor;
SerializationViewModel _serializationViewModel;
CustomCollectionView _collectionView;
string itemName = "";
public EditorViewController(SerializationModel model, CustomCollectionView collectionView, SerializationViewModel serializationViewModel)
{
_model = model;
_serializationViewModel = serializationViewModel;
_collectionView = collectionView;
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
sfImageEditor = new SfImageEditor(new CGRect(View.Frame.Location.X, 60, View.Frame.Size.Width, View.Frame.Size.Height - 60));
sfImageEditor.Image = UIImage.FromBundle("Images/ImageEditor/WhiteImage.png");
var sampleTimer = NSTimer.CreateTimer(TimeSpan.FromSeconds(3.0), delegate
{
ReadFile();
Dispose();
});
sampleTimer.Fire();
sfImageEditor.ImageSaving += (sender, e) =>
{
e.Cancel = true;
var stream = e.Stream;
var byteArray = ReadFully(stream);
NSData data = NSData.FromArray(byteArray);
if (_model.Name == "Create New")
{
var count = _collectionView.DataSource.Count;
SerializationModel item = new SerializationModel()
{
Name = itemName != "" ? itemName : ValidateName(),
ImageAlignment = UIViewContentMode.Center,
ImageBackgroundColor = UIColor.FromRGB(255, 255, 255),
id = count - 1,
EditsStrm = null
};
item.EditsStrm = sfImageEditor.SaveEdits();
item.Image = UIImage.LoadFromData(data);
item.ImageAlignment = UIViewContentMode.ScaleAspectFit;
_serializationViewModel.ModelCollection.Add(item);
EditsViewModel.EditsCollection.Add(new EditsModel() { Name = item.Name });
}
else
{
_model.EditsStrm = sfImageEditor.SaveEdits();
_model.Image = UIImage.LoadFromData(data);
_model.ImageAlignment = UIViewContentMode.ScaleAspectFit;
}
_collectionView.DataSource = _serializationViewModel.ModelCollection;
};
this.View.Add(sfImageEditor);
}
string ValidateName()
{
String Name = "NewItem";
int j = 1;
for (int i = 0; i < _serializationViewModel.ModelCollection.Count; i++)
{
if ( _serializationViewModel.ModelCollection[i].Name == Name)
{
Name = "NewItem " + j;
j++;
}
}
return Name;
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
private void ReadFile()
{
if (_model.id != -1)
{
if (_model.EditsStrm == null )
sfImageEditor.LoadEdits(EditsViewModel.EditsCollection[_model.id].Stream);
else
sfImageEditor.LoadEdits(_model.EditsStrm);
}
}
}
public class EditsViewModel
{
public static ObservableCollection<EditsModel> EditsCollection = new ObservableCollection<EditsModel>()
{
new EditsModel(){Name = "Chart"},
new EditsModel(){Name = "Venn"},
new EditsModel(){Name = "FreeHand"},
};
}
public class EditsModel
{
private string name;
public string Name
{
get { return name; }
set
{
name = value;
}
}
private Stream stream;
public Stream Stream
{
get
{
return this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Resources.Images.ImageEditor." + name + ".txt");
}
set
{
stream = value;
}
}
}
public class SerializationModel
{
public string Name { get; set; }
public int id { get; set; }
public UIImage Image { get; set; }
public UIViewContentMode ImageAlignment { get; set; }
public UIColor ImageBackgroundColor { get; set; }
public Stream Strm { get; set; }
public Stream EditsStrm { get; set; }
public bool IsImageSelected { get; set; }
public bool IsItemSelectedToDelete { get; set; }
}
public class SerializationViewModel
{
public ObservableCollection<SerializationModel> ModelCollection { get; set; }
public SerializationViewModel()
{
ModelCollection = new ObservableCollection<SerializationModel>() {
new SerializationModel(){ Name ="Create New", Image = UIImage.FromBundle("Images/ImageEditor/CreateNew.png"),ImageAlignment= UIViewContentMode.Center,ImageBackgroundColor=UIColor.FromRGB(6,93,199),id=-1,EditsStrm=null,IsItemSelectedToDelete=false },
new SerializationModel(){ Name ="Chart", Image = UIImage.FromBundle("Images/ImageEditor/Chart.jpg"),ImageAlignment= UIViewContentMode.ScaleAspectFit,ImageBackgroundColor=UIColor.FromRGB(255,255,255),Strm=EditsViewModel.EditsCollection[0].Stream,id=0,EditsStrm=null,IsImageSelected =false,IsItemSelectedToDelete=false},
new SerializationModel(){ Name ="Venn Diagram", Image = UIImage.FromBundle("Images/ImageEditor/Venn.jpg"),ImageAlignment= UIViewContentMode.ScaleAspectFit,ImageBackgroundColor=UIColor.FromRGB(255,255,255),Strm=EditsViewModel.EditsCollection[1].Stream,id=1,EditsStrm=null,IsImageSelected = false,IsItemSelectedToDelete=false},
new SerializationModel(){ Name ="Freehand", Image = UIImage.FromBundle("Images/ImageEditor/Freehand.jpg"),ImageAlignment= UIViewContentMode.ScaleAspectFit,ImageBackgroundColor=UIColor.FromRGB(255,255,255),Strm=EditsViewModel.EditsCollection[2].Stream,id=2,EditsStrm=null,IsImageSelected =false ,IsItemSelectedToDelete=false},
};
}
}
public class CustomCollectionView : UIScrollView
{
ObservableCollection<SerializationModel> dataSource;
public CustomCollectionView()
{
}
public ObservableCollection<SerializationModel> DataSource
{
get { return dataSource; }
set
{
dataSource = value;
GenerateCollectionView();
}
}
public int NumberofColums = 2;
public event CollectionViewSelectionEventHandler ItemSelected;
public event CollectionViewSelectionEventHandler LongPressed;
void GenerateCollectionView()
{
//Remove subViews
foreach (UIView view in this.Subviews)
{
view.RemoveFromSuperview();
}
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
NumberofColums = 3;
}
int index = 0, x = 0, y = 30;
int padding = 30;
nfloat ItemSize = (Frame.Width / NumberofColums) - padding;
x = padding / 2;
ContentSize = new CGSize(Frame.Width, (ItemSize + padding) * ((DataSource.Count / NumberofColums) + 1));
foreach (var model in DataSource)
{
CGRect rect = new CGRect(x, y, ItemSize, ItemSize);
this.AddSubview(GetItemView(model, rect));
index++;
if (index % NumberofColums != 0)
{
x += (15 + (int)ItemSize);
}
else
{
x = padding / 2;
y += (int)ItemSize + 10;
}
}
}
UIView GetItemView(SerializationModel model, CGRect frame)
{
UIButton button = new UIButton(frame);
UIImageView imageView = new UIImageView(new CGRect(0, 5, frame.Width, frame.Height - 20));
imageView.Alpha = 1f;
imageView.Image = model.Image;
imageView.BackgroundColor = model.ImageBackgroundColor;
imageView.ContentMode = model.ImageAlignment;
button.AddSubview(imageView);
UILabel label = new UILabel();
if (model.Name != "Create New")
{
label.Frame = new CGRect(0, frame.Height - 40, frame.Width, 25);
label.Alpha = 0.5f;
label.BackgroundColor = UIColor.LightGray;
if (model.IsImageSelected)
{
UIImageView SelectedView = new UIImageView(new CGRect(frame.Width - 25, 10, 20, 20));
if (!model.IsItemSelectedToDelete)
{
SelectedView.Image = UIImage.FromBundle("Images/ImageEditor/NotSelected.png");
imageView.Alpha = 1f;
}
else
{
SelectedView.Image = UIImage.FromBundle("Images/ImageEditor/Selected.png");
imageView.Alpha = 0.3f;
}
SelectedView.BackgroundColor = UIColor.Clear;
SelectedView.ContentMode = UIViewContentMode.ScaleAspectFit;
button.AddSubview(SelectedView);
}
}
else
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
label.Frame = new CGRect(0, frame.Height / 2 + 50, frame.Width, 25);
}
else
label.Frame = new CGRect(0, frame.Height / 2 + 20, frame.Width, 25);
label.Alpha = 1f;
label.BackgroundColor = UIColor.Clear;
label.TextColor = UIColor.White;
}
label.Font = UIFont.SystemFontOfSize(18);
label.TextAlignment = UITextAlignment.Center;
label.Text = model.Name;
button.AddSubview(label);
UILongPressGestureRecognizer detector = new UILongPressGestureRecognizer((UILongPressGestureRecognizer obj) =>
{
OnLongPressed(this, model);
});
button.AddGestureRecognizer(detector);
button.TouchUpInside += (sender, e) =>
{
OnItemSelected(this, model);
};
return button;
}
internal void OnItemSelected(object sender, SerializationModel args)
{
this.ItemSelected?.Invoke(sender, args);
}
internal void OnLongPressed(object sender, SerializationModel args)
{
this.LongPressed?.Invoke(sender, args);
}
}
public delegate void CollectionViewSelectionEventHandler(object sender, SerializationModel e);
public delegate void CollectionViewLongPressEventHandler(object sender, SerializationModel e);
}