addUser method

Future<void> addUser(
  1. String userId
)

Implementation

Future<void> addUser(String userId) async {
  final userInfoFileContent = await AppticsDataStore.getFileContents(FileType.userInfo);
    if (userInfoFileContent != null) {
      Map<String, dynamic> users = jsonDecode(userInfoFileContent);
      if (users.containsKey(userId)) {
        // user already exist
       (users[userId] as Map<String, dynamic>).putIfAbsent("isActive", () => true);
      } else {
        Map<String, dynamic> userProps = {
          "isActive": true
        };
        users.putIfAbsent(userId, () => userProps);
      }

      // mark other users inactive
      users.forEach((key, value) {
        if (key != userId) {
          (users[key] as Map<String, dynamic>).putIfAbsent("isActive", () => false);
        }
      });
      await AppticsDataStore.writeContent(jsonEncode(users), FileType.userInfo);
    } else {
      Map<String, dynamic> userProps = {
          "isActive": true
        };
      Map<String, dynamic> user = {};
      user.putIfAbsent(userId, () => userProps);
      await AppticsDataStore.writeContent(jsonEncode(user), FileType.userInfo);
    }

    syncUser(userId);
}