/
ScriptsDataSource.lua
240 lines (173 loc) · 6.17 KB
/
ScriptsDataSource.lua
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
--
-- ScriptsDataSource.lua : UITableViewSource implementation to ScriptListViewController
--
-- Authors:
-- Vinicius Jarina (vinicius.jarina@xamarin.com)
-- Copyright 2013-2014 Xamarin Inc.
--
-- Licensed under MIT License
--
ScriptStore = require ('ScriptStore')
ScriptViewController = require ('ScriptViewController')
local ScriptsDataSource = {}
ScriptsDataSource.mt = {__call = function(self, ...)
return self.new (...)
end
}
setmetatable (ScriptsDataSource,ScriptsDataSource.mt)
g_CellIdentifier = NSString ("Cell");
function ScriptsDataSource.new (controller)
ScriptsDataSource.m = {}; -- create a table members to store Lua fields
ScriptsDataSource.m.scripts = CreateListString();
ScriptsDataSource.m.sources = CreateListString();
if (controller == nil) then
error ("ArgumentError: controller cannot be nil");
end
ScriptsDataSource.m.controller = controller;
local scritpPath = LocalPathPrepare.ScriptsPath;
local sourcePath = LocalPathPrepare.SourcePath;
ScriptsDataSource.m.store = ScriptStore (scritpPath, sourcePath);
luanet.make_object (ScriptsDataSource, 'MonoTouch.UIKit.UITableViewSource');
return ScriptsDataSource;
end
function ScriptsDataSource:IsSourceCodeNumber(number)
return number == 1;
end
function ScriptsDataSource:IsSourceCode(indexPath)
return self:IsSourceCodeNumber(indexPath.Section);
end
function ScriptsDataSource:GetScriptsStore ()
return self.m.store;
end
-- Customize the number of sections in the table view.
function ScriptsDataSource:NumberOfSections (tableView)
return 2;
end
function ScriptsDataSource:TitleForHeader (tableView, section)
if (self:IsSourceCodeNumber (section))then
return "NLuaBox Source";
end
return "Lua Scripts";
end
function ScriptsDataSource:RowsInSection ( tableview, section)
if (self:IsSourceCodeNumber (section))then
return self.m.sources.Count;
end
return self.m.scripts.Count;
end
-- Customize the appearance of table view cells.
function ScriptsDataSource:GetCell ( tableView, indexPath)
local cell = tableView:DequeueReusableCell (g_CellIdentifier);
if (cell == nil) then
cell = UITableViewCell (UITableViewCellStyle.Default, g_CellIdentifier);
end
if (self:IsSourceCode (indexPath)) then
cell.TextLabel.Text = self.m.sources[indexPath.Row];
cell.Accessory = UITableViewCellAccessory.None;
else
cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
cell.TextLabel.Text = self.m.scripts[indexPath.Row];
end
return cell;
end
function ScriptsDataSource:CanEditRow (tableView, indexPath)
return not self:IsSourceCode (indexPath);
-- Return false if you do not want the specified item to be editable.
end
function ScriptsDataSource:CommitEditingStyle (tableView, editingStyle, indexPath)
if (editingStyle == UITableViewCellEditingStyle.Delete) then
-- Delete the row from the data source.
local controller = self.m.controller;
if (controller.m.ScriptViewController ~= nil and self.m.scripts[indexPath.Row] == controller.m.ScriptViewController.ScriptName) then
controller.m.ScriptViewController:LoadScript (nil, false);
end
self:RemoveFile (indexPath.Row);
controller.TableView:DeleteRows (luanet.make_array(NSIndexPath, { indexPath }), UITableViewRowAnimation.Fade);
elseif (editingStyle == UITableViewCellEditingStyle.Insert) then
-- Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
end
end
function ScriptsDataSource:RowSelected (tableView, indexPath)
local name = nil;
local isSource = self:IsSourceCode (indexPath);
local controller = self.m.controller;
if (isSource) then
name = self.m.sources[indexPath.Row];
else
name = self.m.scripts[indexPath.Row];
end
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) then
if (controller.m.ScriptViewController == nil) then
controller.m.ScriptViewController = ScriptViewController (self:GetScriptsStore());
end
controller.m.ScriptViewController:LoadScript (name, isSource);
-- Pass the selected object to the new view controller.
controller.NavigationController:PushViewController (controller.m.ScriptViewController, true);
else
controller.m.ScriptViewController:LoadScript (name, isSource);
end
end
function ScriptsDataSource:AccessoryButtonTapped ( tableView, indexPath)
self.m.controller:OnAccessoryButtonTapped (tableView, indexPath);
end
function ScriptsDataSource:Reload ()
local store = self:GetScriptsStore ();
local scripts = store:GetScripts();
local sources = store:GetSources();
local each = luanet.each;
self.m.scripts:Clear();
self.m.sources:Clear();
for script in each(scripts) do
local name = Path.GetFileName (script);
self.m.scripts:Add (name);
end
self.m.scripts:Sort();
for source in each(sources) do
local name = Path.GetFileName (source);
self.m.sources:Add (name);
end
self.m.sources:Sort();
end
function ScriptsDataSource:Exists (file)
return self.m.store:Exists (file);
end
function ScriptsDataSource:IsValidName (file)
return not String.IsNullOrWhiteSpace (file);
end
function ScriptsDataSource:RenameFile (indexPath, newName)
local removeIndex = -1;
if (self:Exists (newName)) then
removeIndex = self.m.scripts:IndexOf (newName);
end
local row = indexPath.Row;
local oldName = self.m.scripts [row];
self.m.scripts [row] = newName;
self.m.store:RenameFile (oldName, newName);
if (removeIndex ~= -1) then
self.m.scripts:RemoveAt (removeIndex);
end
if (self.m.controller.m.ScriptViewController ~= nil) then
self.m.controller.m.ScriptViewController:LoadScript (newName, false);
end
end
function ScriptsDataSource:AddFile (file)
if (not self:Exists (file)) then
self.m.scripts:Add (file);
end
self.m.store:SaveScriptContent (file, "");
if (self.m.controller.m.ScriptViewController ~= nil) then
self.m.controller.m.ScriptViewController:LoadScript (file, false);
end
end
function ScriptsDataSource:RemoveFile (row)
local file = self.m.scripts [row];
self.m.scripts:RemoveAt (row);
self.m.store:RemoveFile (file);
end
function ScriptsDataSource:GetScriptName (index)
if (self:IsSourceCode (index)) then
return self.m.sources [index.Row];
end
return self.m.scripts [index.Row];
end
return ScriptsDataSource;