/
SyncCloudViewController.cs
245 lines (216 loc) · 8.71 KB
/
SyncCloudViewController.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
// Copyright © 2011-2013 Yanick Castonguay
//
// This file is part of MPfm.
//
// MPfm is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// MPfm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MPfm. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MPfm.iOS.Classes.Controllers.Base;
using MPfm.MVP.Views;
using MPfm.iOS.Classes.Controls;
using DropBoxSync.iOS;
using MPfm.iOS.Classes.Delegates;
using MPfm.MVP.Bootstrap;
using MPfm.MVP.Navigation;
using MPfm.Library;
using System.Linq;
namespace MPfm.iOS
{
public partial class SyncCloudViewController : BaseViewController, ISyncCloudView
{
private ISyncDeviceSpecifications _deviceSpecs;
private DBDatastore _store = null;
public SyncCloudViewController(Action<IBaseView> onViewReady)
: base (onViewReady, UserInterfaceIdiomIsPhone ? "SyncCloudViewController_iPhone" : "SyncCloudViewController_iPad", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
_deviceSpecs = Bootstrapper.GetContainer().Resolve<ISyncDeviceSpecifications>();
if (_store != null && _store.Open)
return;
DBError error = null;
var account = DBAccountManager.SharedManager.LinkedAccount;
// TODO: account will be null when the user delogs out
_store = DBDatastore.OpenDefaultStoreForAccount(account, out error);
if(error != null)
throw new Exception(error.Description);
_store.AddObserver (_store, () => {
Console.WriteLine("SyncCloudViewController - DBDatastore - Data changed!");
if (_store.Status.HasFlag(DBDatastoreStatus.Incoming)) {
// Handle the updated data
Console.WriteLine("SyncCloudViewController - DBDatastore - Incoming data!");
try
{
DBError error2 = null;
var changes = _store.Sync(error2);
if(error2 != null)
throw new Exception(error2.Description);
if (!changes.ContainsKey(new NSString("stuff")))
return;
var records = (NSSet)changes["stuff"];
foreach(var record in records)
{
var theRecord = (DBRecord)record;
var timestamp = theRecord.ObjectForKey("timestamp");
var deviceType = theRecord.ObjectForKey("deviceType");
var deviceName = theRecord.ObjectForKey("deviceName");
lblValue.Text = string.Format("{0} {1} {2}", deviceType, deviceName, timestamp);
}
}
catch (Exception ex)
{
Console.WriteLine("SyncCloudActivity - OnDatastoreStatusChange exception: {0}", ex);
lblValue.Text = string.Format("Error: {0}", ex);
}
}
});
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
MPfmNavigationController navCtrl = (MPfmNavigationController)this.NavigationController;
navCtrl.SetTitle("Sync (Cloud)", "Sync with Dropbox");
}
partial void actionEnableSync(NSObject sender)
{
try
{
var account = DBAccountManager.SharedManager.LinkedAccount;
if(account == null)
{
var appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
DBAccountManager.SharedManager.LinkFromController(appDelegate.TabBarController);
}
}
catch(Exception ex)
{
UIAlertView alertView = new UIAlertView("SyncCloud Error", ex.Message, null, "OK", null);
alertView.Show();
}
}
partial void actionDisableSync(NSObject sender)
{
try
{
// No such thing on iOS?
// https://www.dropbox.com/developers/datastore/tutorial/ios
}
catch(Exception ex)
{
UIAlertView alertView = new UIAlertView("SyncCloud Error", ex.Message, null, "OK", null);
alertView.Show();
}
}
partial void actionPull(NSObject sender)
{
try
{
DBError error = null;
var table = _store.GetTable("stuff");
//var record = table.GetRecord("H8f7Q9JRhAkzzzmBa-dDww", out error);
var records = table.Query(null, out error);
if(error != null)
throw new Exception(error.Description);
if (records.Length == 0)
{
lblValue.Text = "No value!";
return;
}
var firstResult = records[0];
var timestamp = firstResult.ObjectForKey("timestamp");
var deviceType = firstResult.ObjectForKey("deviceType");
var deviceName = firstResult.ObjectForKey("deviceName");
lblValue.Text = string.Format("{0} {1} {2}", deviceType, deviceName, timestamp);
}
catch(Exception ex)
{
UIAlertView alertView = new UIAlertView("SyncCloud Error", ex.Message, null, "OK", null);
alertView.Show();
}
}
partial void actionPush(NSObject sender)
{
try
{
DBError error = null;
var table = _store.GetTable("stuff");
var keys = new NSString[] {
new NSString("hello"),
new NSString("deviceType"),
new NSString("deviceName"),
new NSString("ip"),
new NSString("test"),
new NSString("timestamp")
};
var values = new NSObject[] {
new NSString("world"),
new NSString(_deviceSpecs.GetDeviceType().ToString()),
new NSString(_deviceSpecs.GetDeviceName()),
new NSString(_deviceSpecs.GetIPAddress()),
NSObject.FromObject(true),
new NSString(DateTime.Now.ToLongTimeString())
};
NSDictionary data = NSDictionary.FromObjectsAndKeys(values, keys);
DBRecord stuff = table.Insert(data);
_store.Sync(error);
if(error != null)
throw new Exception(error.Description);
}
catch(Exception ex)
{
UIAlertView alertView = new UIAlertView("SyncCloud Error", ex.Message, null, "OK", null);
alertView.Show();
}
}
partial void actionDelete(NSObject sender)
{
try
{
DBError error = null;
var table = _store.GetTable("stuff");
var records = table.Query(null, out error);
if (records.Length == 0)
{
lblValue.Text = "No value!";
return;
}
foreach(var record in records)
{
record.DeleteRecord();
}
_store.Sync(error);
if(error != null)
throw new Exception(error.Description);
}
catch(Exception ex)
{
UIAlertView alertView = new UIAlertView("SyncCloud Error", ex.Message, null, "OK", null);
alertView.Show();
}
}
#region ISyncCloudView implementation
public void SyncCloudError(Exception ex)
{
InvokeOnMainThread(() => {
UIAlertView alertView = new UIAlertView("SyncCloud Error", ex.Message, null, "OK", null);
alertView.Show();
});
}
#endregion
}
}