|
| 1 | +part of flutter_parse_sdk; |
| 2 | + |
| 3 | +class ParseInstallation extends ParseObject { |
| 4 | + static final String keyTimeZone = 'timeZone'; |
| 5 | + static final String keyLocaleIdentifier = 'localeIdentifier'; |
| 6 | + static final String keyDeviceToken = 'deviceToken'; |
| 7 | + static final String keyDeviceType = 'deviceType'; |
| 8 | + static final String keyInstallationId = 'installationId'; |
| 9 | + static final String keyAppName = 'appName'; |
| 10 | + static final String keyAppVersion = 'appVersion'; |
| 11 | + static final String keyAppIdentifier = 'appIdentifier'; |
| 12 | + static final String keyParseVersion = 'parseVersion'; |
| 13 | + static final List<String> readOnlyKeys = [ //TODO |
| 14 | + keyDeviceToken, keyDeviceType, keyInstallationId, |
| 15 | + keyAppName, keyAppVersion, keyAppIdentifier, keyParseVersion |
| 16 | + ]; |
| 17 | + static String _currentInstallationId; |
| 18 | + |
| 19 | + //Getters/setters |
| 20 | + |
| 21 | + Map get acl => super.get<Map>(keyVarAcl); |
| 22 | + |
| 23 | + set acl(Map acl) => set<Map>(keyVarAcl, acl); |
| 24 | + |
| 25 | + String get deviceToken => super.get<String>(keyDeviceToken); |
| 26 | + |
| 27 | + set deviceToken(String deviceToken) => set<String>(keyDeviceToken, deviceToken); |
| 28 | + |
| 29 | + String get deviceType => super.get<String>(keyDeviceType); |
| 30 | + |
| 31 | + String get installationId => super.get<String>(keyInstallationId); |
| 32 | + |
| 33 | + set _installationId(String installationId) => set<String>(keyInstallationId, installationId); |
| 34 | + |
| 35 | + String get appName => super.get<String>(keyAppName); |
| 36 | + |
| 37 | + String get appVersion => super.get<String>(keyAppVersion); |
| 38 | + |
| 39 | + String get appIdentifier => super.get<String>(keyAppIdentifier); |
| 40 | + |
| 41 | + String get parseVersion => super.get<String>(keyParseVersion); |
| 42 | + |
| 43 | + /// Creates an instance of ParseInstallation |
| 44 | + ParseInstallation( |
| 45 | + {bool debug, |
| 46 | + ParseHTTPClient client, |
| 47 | + bool autoSendSessionId}) |
| 48 | + : super(keyClassInstallation) { |
| 49 | + _debug = isDebugEnabled(objectLevelDebug: debug); |
| 50 | + _client = client ?? |
| 51 | + ParseHTTPClient( |
| 52 | + autoSendSessionId: |
| 53 | + autoSendSessionId ?? ParseCoreData().autoSendSessionId, |
| 54 | + securityContext: ParseCoreData().securityContext); |
| 55 | + } |
| 56 | + |
| 57 | + ParseInstallation.forQuery() : super(keyClassUser); |
| 58 | + |
| 59 | + static Future<bool> isCurrent(ParseInstallation installation) async { |
| 60 | + if (_currentInstallationId == null) { |
| 61 | + _currentInstallationId = (await _getFromLocalStore()).installationId; |
| 62 | + } |
| 63 | + return _currentInstallationId != null && installation.installationId == _currentInstallationId; |
| 64 | + } |
| 65 | + |
| 66 | + /// Gets the current installation from storage |
| 67 | + static Future<ParseInstallation> currentInstallation() async { |
| 68 | + var installation = await _getFromLocalStore(); |
| 69 | + if (installation == null) { |
| 70 | + installation = await _createInstallation(); |
| 71 | + } |
| 72 | + return installation; |
| 73 | + } |
| 74 | + |
| 75 | + /// Updates the installation with current device data |
| 76 | + _updateInstallation() async { |
| 77 | + //Device type |
| 78 | + if (Platform.isAndroid) set<String>(keyDeviceType, "android"); |
| 79 | + else if (Platform.isIOS) set<String>(keyDeviceType, "ios"); |
| 80 | + else throw Exception("Unsupported platform/operating system"); |
| 81 | + |
| 82 | + //Locale |
| 83 | + String locale = await Devicelocale.currentLocale; |
| 84 | + if (locale != null && locale.isNotEmpty) { |
| 85 | + set<String>(keyLocaleIdentifier, locale); |
| 86 | + } |
| 87 | + |
| 88 | + //Timezone |
| 89 | + //TODO set<String>(keyTimeZone, ); |
| 90 | + |
| 91 | + //App info |
| 92 | + PackageInfo packageInfo = await PackageInfo.fromPlatform(); |
| 93 | + set<String>(keyAppName, packageInfo.appName); |
| 94 | + set<String>(keyAppVersion, packageInfo.version); |
| 95 | + set<String>(keyAppIdentifier, packageInfo.packageName); |
| 96 | + set<String>(keyParseVersion, keySdkVersion); |
| 97 | + } |
| 98 | + |
| 99 | + Future<ParseResponse> create() async { |
| 100 | + var isCurrent = await ParseInstallation.isCurrent(this); |
| 101 | + if (isCurrent) await _updateInstallation(); |
| 102 | + ParseResponse parseResponse = await super.create(); |
| 103 | + if (parseResponse.success && isCurrent) { |
| 104 | + saveInStorage(keyParseStoreInstallation); |
| 105 | + } |
| 106 | + return parseResponse; |
| 107 | + } |
| 108 | + |
| 109 | + /// Saves the current installation |
| 110 | + Future<ParseResponse> save() async { |
| 111 | + var isCurrent = await ParseInstallation.isCurrent(this); |
| 112 | + if (isCurrent) await _updateInstallation(); |
| 113 | + ParseResponse parseResponse = await super.save(); |
| 114 | + if (parseResponse.success && isCurrent) { |
| 115 | + saveInStorage(keyParseStoreInstallation); |
| 116 | + } |
| 117 | + return parseResponse; |
| 118 | + } |
| 119 | + |
| 120 | + /// Gets the locally stored installation |
| 121 | + static Future<ParseInstallation> _getFromLocalStore() async { |
| 122 | + var installationJson = |
| 123 | + (await ParseCoreData().getStore()).getString(keyParseStoreInstallation); |
| 124 | + |
| 125 | + if (installationJson != null) { |
| 126 | + var installationMap = parseDecode(json.decode(installationJson)); |
| 127 | + |
| 128 | + if (installationMap != null) { |
| 129 | + return new ParseInstallation()..fromJson(installationMap); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + /// Creates a installation for current device |
| 137 | + /// Assumes that this is called because there is no previous installation |
| 138 | + /// so it creates and sets the static current installation UUID |
| 139 | + static Future<ParseInstallation> _createInstallation() async { |
| 140 | + if (_currentInstallationId == null) { |
| 141 | + _currentInstallationId = Uuid().v4(); |
| 142 | + } |
| 143 | + var installation = new ParseInstallation(); |
| 144 | + installation._installationId = _currentInstallationId; |
| 145 | + await installation._updateInstallation(); |
| 146 | + return installation; |
| 147 | + } |
| 148 | +} |
0 commit comments