forked from parse-community/Parse-SDK-Flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_installation.dart
More file actions
247 lines (208 loc) · 8.05 KB
/
Copy pathparse_installation.dart
File metadata and controls
247 lines (208 loc) · 8.05 KB
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
part of flutter_parse_sdk;
class ParseInstallation extends ParseObject {
/// Creates an instance of ParseInstallation
ParseInstallation(
{bool debug, ParseHTTPClient client, bool autoSendSessionId})
: super(keyClassInstallation) {
_debug = isDebugEnabled(objectLevelDebug: debug);
_client = client ??
ParseHTTPClient(
sendSessionId:
autoSendSessionId ?? ParseCoreData().autoSendSessionId,
securityContext: ParseCoreData().securityContext);
}
ParseInstallation.forQuery() : super(keyClassUser);
static final List<String> readOnlyKeys = <String>[
keyDeviceToken,
keyDeviceType,
keyInstallationId,
keyAppName,
keyAppVersion,
keyAppIdentifier,
keyParseVersion
];
static String _currentInstallationId;
//Getters/setters
Map<String, dynamic> get acl => super.get<Map<String, dynamic>>(keyVarAcl);
set acl(Map<String, dynamic> acl) =>
set<Map<String, dynamic>>(keyVarAcl, acl);
String get deviceToken => super.get<String>(keyDeviceToken);
set deviceToken(String deviceToken) =>
set<String>(keyDeviceToken, deviceToken);
String get deviceType => super.get<String>(keyDeviceType);
String get installationId => super.get<String>(keyInstallationId);
set _installationId(String installationId) =>
set<String>(keyInstallationId, installationId);
String get appName => super.get<String>(keyAppName);
String get appVersion => super.get<String>(keyAppVersion);
String get appIdentifier => super.get<String>(keyAppIdentifier);
String get parseVersion => super.get<String>(keyParseVersion);
static Future<bool> isCurrent(ParseInstallation installation) async {
_currentInstallationId ??= (await _getFromLocalStore()).installationId;
return _currentInstallationId != null &&
installation.installationId == _currentInstallationId;
}
/// Gets the current installation from storage
static Future<ParseInstallation> currentInstallation() async {
ParseInstallation installation = await _getFromLocalStore();
installation ??= await _createInstallation();
return installation;
}
/// Updates the installation with current device data
Future<void> _updateInstallation() async {
//Device type
if (parseIsWeb) {
set<String>(keyDeviceType, 'web');
} else if (Platform.isAndroid) {
set<String>(keyDeviceType, 'android');
} else if (Platform.isIOS) {
set<String>(keyDeviceType, 'ios');
} else if (Platform.isLinux) {
set<String>(keyDeviceType, 'Linux');
} else if (Platform.isMacOS) {
set<String>(keyDeviceType, 'MacOS');
} else if (Platform.isWindows) {
set<String>(keyDeviceType, 'Windows');
}
//Locale
final String locale = parseIsWeb
? ui.window.locale.toString()
: await Devicelocale.currentLocale;
if (locale != null && locale.isNotEmpty) {
set<String>(keyLocaleIdentifier, locale);
}
//Timezone
//App info
if (!parseIsWeb) {
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
set<String>(keyAppName, packageInfo.appName);
set<String>(keyAppVersion, packageInfo.version);
set<String>(keyAppIdentifier, packageInfo.packageName);
}
set<String>(keyParseVersion, keySdkVersion);
}
@override
Future<ParseResponse> create() async {
final bool isCurrent = await ParseInstallation.isCurrent(this);
if (isCurrent) {
await _updateInstallation();
}
final ParseResponse parseResponse = await _create();
if (parseResponse.success && isCurrent) {
saveInStorage(keyParseStoreInstallation);
}
return parseResponse;
}
/// Saves the current installation
@override
Future<ParseResponse> save() async {
final bool isCurrent = await ParseInstallation.isCurrent(this);
if (isCurrent) {
await _updateInstallation();
}
//ParseResponse parseResponse = await super.save();
final ParseResponse parseResponse = await _save();
if (parseResponse.success && isCurrent) {
saveInStorage(keyParseStoreInstallation);
}
return parseResponse;
}
/// Gets the locally stored installation
static Future<ParseInstallation> _getFromLocalStore() async {
final CoreStore coreStore = ParseCoreData().getStore();
final String installationJson =
await coreStore.getString(keyParseStoreInstallation);
if (installationJson != null) {
final Map<String, dynamic> installationMap =
json.decode(installationJson);
if (installationMap != null) {
return ParseInstallation()..fromJson(installationMap);
}
}
return null;
}
/// Creates a installation for current device
/// Assumes that this is called because there is no previous installation
/// so it creates and sets the static current installation UUID
static Future<ParseInstallation> _createInstallation() async {
_currentInstallationId ??= Uuid().v4();
final ParseInstallation installation = ParseInstallation();
installation._installationId = _currentInstallationId;
await installation._updateInstallation();
await ParseCoreData().getStore().setString(keyParseStoreInstallation,
json.encode(installation.toJson(full: true)));
return installation;
}
/// Creates a new object and saves it online
Future<ParseResponse> _create() async {
try {
final String uri = '${_client.data.serverUrl}$keyEndPointInstallations';
final String body = json.encode(toJson(forApiRQ: true));
final Map<String, String> headers = <String, String>{
keyHeaderContentType: keyHeaderContentTypeJson
};
if (_debug) {
logRequest(ParseCoreData().appName, parseClassName,
ParseApiRQ.create.toString(), uri, body);
}
final Response result =
await _client.post(uri, body: body, headers: headers);
//Set the objectId on the object after it is created.
//This allows you to perform operations on the object after creation
if (result.statusCode == 201) {
final Map<String, dynamic> map = json.decode(result.body);
objectId = map['objectId'].toString();
}
return handleResponse<ParseInstallation>(
this, result, ParseApiRQ.create, _debug, parseClassName);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.create, _debug, parseClassName);
}
}
/// Saves the current object online
Future<ParseResponse> _save() async {
if (objectId == null) {
return create();
} else {
try {
final String uri =
'${ParseCoreData().serverUrl}$keyEndPointInstallations/$objectId';
final String body = json.encode(toJson(forApiRQ: true));
if (_debug) {
logRequest(ParseCoreData().appName, parseClassName,
ParseApiRQ.save.toString(), uri, body);
}
final Response result = await _client.put(uri, body: body);
return handleResponse<ParseInstallation>(
this, result, ParseApiRQ.save, _debug, parseClassName);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.save, _debug, parseClassName);
}
}
}
///Subscribes the device to a channel of push notifications.
Future<void> subscribeToChannel(String value) async {
final List<dynamic> channel = <String>[value];
setAddAllUnique('channels', channel);
await save();
}
///Unsubscribes the device to a channel of push notifications.
Future<void> unsubscribeFromChannel(String value) async {
final List<dynamic> channel = <String>[value];
setRemove('channels', channel);
await save();
}
///Returns an <List<String>> containing all the channel names this device is subscribed to.
Future<List<dynamic>> getSubscribedChannels() async {
print('getSubscribedChannels');
final ParseResponse apiResponse =
await ParseObject(keyClassInstallation).getObject(objectId);
if (apiResponse.success) {
final ParseObject installation = apiResponse.result;
return Future<List<dynamic>>.value(
installation.get<List<dynamic>>('channels'));
} else {
return null;
}
}
}