-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathProgressNotifications.cs
199 lines (182 loc) · 6.52 KB
/
ProgressNotifications.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using NUnit.Framework;
using Realms;
using Realms.Sync;
namespace Examples
{
public class ProgressNotifications
{
App app;
Realms.Sync.User user;
PartitionSyncConfiguration config;
string myRealmAppId = Config.AppId;
public class ProgressObj : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public int Id { get; set; }
public string Name { get; set; }
}
[Test]
public async Task TestWaitForChangesToDownloadAsync()
{
var appConfig = new AppConfiguration(myRealmAppId)
{
DefaultRequestTimeout = TimeSpan.FromMilliseconds(1500)
};
app = App.Create(appConfig);
user = app.LogInAsync(Credentials.Anonymous()).Result;
config = new PartitionSyncConfiguration("myPartition", user);
try
{
// :snippet-start: wait-for-changes-to-download-async-progress-notification
// :uncomment-start:
// using Realms.Sync;
// :uncomment-end:
var realm = Realm.GetInstance(config);
await realm.SyncSession.WaitForDownloadAsync();
// :snippet-end:
realm.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
[Test]
public void TestUploadDownloadProgressNotification()
{
//var progressNotificationTriggered = false;
var appConfig = new AppConfiguration(myRealmAppId)
{
DefaultRequestTimeout = TimeSpan.FromMilliseconds(1500)
};
app = App.Create(appConfig);
user = app.LogInAsync(Credentials.Anonymous()).Result;
config = new PartitionSyncConfiguration("myPartition", user);
var realm = Realm.GetInstance(config);
// :snippet-start: upload-download-progress-notification
var session = realm.SyncSession;
var token = session.GetProgressObservable(ProgressDirection.Upload,
ProgressMode.ReportIndefinitely)
.Subscribe(progress =>
{
Console.WriteLine($@"Current upload progress:
{progress.ProgressEstimate * 100}%");
});
token.Dispose();
// :snippet-end: upload-download-progress-notification
var id = 2;
var myObj = new ProgressObj
{
Id = id
};
realm.Write(() =>
{
realm.Add(myObj);
});
realm.Write(() =>
{
realm.RemoveAll<ProgressObj>();
});
}
[Test]
// :snippet-start: connection-state
// :replace-start: {
// "terms": {
// "TestSessionConnnectionState": "SetupRealm"}
// }
public void TestSessionConnnectionState()
{
var appConfig = new AppConfiguration(myRealmAppId);
app = App.Create(appConfig);
user = app.LogInAsync(Credentials.Anonymous()).Result;
config = new PartitionSyncConfiguration("myPartition", user);
try
{
var realm = Realm.GetInstance(config);
var session = realm.SyncSession;
session.PropertyChanged += SyncSessionPropertyChanged!;
realm.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void SyncSessionPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Session.ConnectionState))
{
var session = (Session)sender;
var currentState = session.ConnectionState;
if (currentState == ConnectionState.Connecting)
{
//session is connecting
}
if (currentState == ConnectionState.Connected)
{
//session is connected
}
if (currentState == ConnectionState.Disconnected)
{
//session has been disconnected
}
}
}
// :replace-end:
// :snippet-end:
public void CollectionChanges()
{
using var realm = Realm.GetInstance();
var container = realm.Write(() =>
{
return realm.Add(new Container());
});
// :snippet-start: notify-set-change
var stringSet = container.StringSet.AsRealmCollection();
stringSet.CollectionChanged += (sender, e) =>
{
Console.WriteLine($"Set {sender} changed: {e.Action}");
};
stringSet.PropertyChanged += (sender, e) =>
{
Console.WriteLine($"Property changed on {sender}: {e.PropertyName}");
};
// :snippet-end:
// :snippet-start: notify-list-change
var list = container.StringList.AsRealmCollection();
list.CollectionChanged += (sender, e) =>
{
Console.WriteLine($"List {sender} changed: {e.Action}");
};
list.PropertyChanged += (sender, e) =>
{
Console.WriteLine($"Property changed on {sender}: {e.PropertyName}");
};
// :snippet-end:
// :snippet-start: notify-dictionary-change
var dictionary = container.IntDictionary.AsRealmCollection();
dictionary.CollectionChanged += (sender, e) =>
{
Console.WriteLine($"Collection {sender} changed: {e.Action}");
};
dictionary.PropertyChanged += (sender, e) =>
{
Console.WriteLine($"Property changed on {sender}: {e.PropertyName}");
};
// :snippet-end:
}
}
public partial class Container : IRealmObject
{
[PrimaryKey]
public string _id { get; set; }
public ISet<string> StringSet { get; } = null!;
public IDictionary<string, int> IntDictionary { get; } = null!;
public IList<string> StringList { get; } = null!;
}
}