addOrUpdateToken method

Future<void> addOrUpdateToken(
  1. String deviceId,
  2. String refreshDeviceId,
  3. String token,
  4. int fetchTime,
  5. String? anonTime,
  6. String? userId,
)

Implementation

Future<void> addOrUpdateToken(
  String deviceId,
  String refreshDeviceId,
  String token,
  int fetchTime,
  String? anonTime,
  String? userId
) async {
  Map<String, dynamic> tokens = {};

  final tokenFileContent = await AppticsDataStore.getFileContents(FileType.bearerInfo);
  if (tokenFileContent != null) {
    tokens = jsonDecode(tokenFileContent);
  }
  var tokenForDevice = tokens.putIfAbsent(deviceId, () => {}) as Map<dynamic, dynamic>;
  tokenForDevice["token"] = token;
  tokenForDevice["refreshDeviceId"] = refreshDeviceId;
  tokenForDevice["fetchtime"] = fetchTime;
  if (anonTime != null) {
    tokenForDevice["anontime"] = anonTime;
  }
  if (userId != null) {
    var users = tokenForDevice.putIfAbsent("users", () => []) as List<dynamic>;
    users.add(userId);
  }
  await AppticsDataStore.writeContent(jsonEncode(tokens), FileType.bearerInfo);
}