forked from xamarin/mac-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrowseData.cs
128 lines (111 loc) · 3.47 KB
/
BrowseData.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using AppKit;
using ImageKit;
namespace ImageKitDemo
{
public class BrowseData : IKImageBrowserDataSource
{
public BrowseData ()
{
}
List<BrowseItem> images = new List<BrowseItem>();
#region Required IKImageBrowserDataSource methods
public override nint ItemCount (IKImageBrowserView aBrowser)
{
//Console.WriteLine ("DataSource: Image Count was requested");
return images.Count;
}
public override IIKImageBrowserItem GetItem (IKImageBrowserView aBrowser, nint index)
{
return images[(int)index];
}
#endregion
#region optional IKImageBrowserDataSource methods
public override bool MoveItems (IKImageBrowserView aBrowser, NSIndexSet indexes, nint destinationIndex)
{
//indexes are not sequential, and may be on both sides of destinationIndex.
//indexes will change, but I will put the items in after the item at destination
//FIXME - missing methods on NSIndexSet
//FIXME make an extension method on List<>
int destination = (int) destinationIndex - (int)indexes.Where (x =>(int) x <(int) destinationIndex).Count ();
List<BrowseItem> movingImages = new List<BrowseItem> ();
foreach (int index in indexes)
movingImages.Add (images[index]);
foreach (BrowseItem item in movingImages)
images.Remove (item);
images.InsertRange (destination, movingImages);
aBrowser.ReloadData();
return true;
}
public override void RemoveItems (IKImageBrowserView aBrowser, NSIndexSet indexes)
{
//FIXME - add extension for List<T>
//images.RemoveAt(indexes)
List<BrowseItem> movingImages = new List<BrowseItem> ();
foreach (int index in indexes)
movingImages.Add (images[index]);
foreach (BrowseItem item in movingImages)
images.Remove (item);
aBrowser.ReloadData();
}
#endregion
#region Searching/filtering
public void SetFilter (string searchText)
{
// This simple filtering solution is simple but flawed.
// The user can use the UI to add, remove and reorganize the image list while it is filtered.
// However these UI changes are lost when the filter is changed.
// Since a more robust solution potentially using Linq, and a visible attribute on the
// browse item would not enhance the demo of ImageKit, I will leave it as an exercise
// for the reader.
if (unfilteredImages == null)
unfilteredImages = images.ToList ();
if (string.IsNullOrEmpty (searchText))
images = unfilteredImages.ToList ();
else
images = unfilteredImages.Where (i => i.ImageTitle.Contains (searchText)).ToList ();
}
List<BrowseItem> unfilteredImages;
#endregion
public void AddImages (NSUrl path)
{
AddImages (path, -1);
}
public void AddImages (NSUrl uri, int index)
{
if (uri.IsFileUrl)
{
string path = uri.Path;
if (Directory.Exists (path))
{
foreach (var file in Directory.GetFiles (path))
{
AddImageFile (file, index);
}
}
if (File.Exists (path))
{
AddImageFile (path, index);
}
} else {
images.Add (new BrowseItem (uri));
}
}
private void AddImageFile (string path, int index)
{
string name = Path.GetFileNameWithoutExtension (path);
//Skip .* files
if (name.IndexOf ('.') != 0)
{
if (-1 < index && index < images.Count)
images.Insert (index, new BrowseItem (NSUrl.FromFilename (path)));
else
images.Add (new BrowseItem (NSUrl.FromFilename (path)));
}
}
}
}