processLocationData method

void processLocationData({
  1. required int interval,
  2. required Isar isar,
  3. required String createdBy,
})

Implementation

void processLocationData(
    {required int interval,
    required Isar isar,
    required String createdBy}) async {
  try {
    _timer ??= makePeriodicTimer(Duration(seconds: interval), (timer) async {
      final file = File(await readLocationFileInBackgroundService());
      if (file.existsSync()) {
        final currentModified = await file.lastModified();
        if (lastModified == null || currentModified.isAfter(lastModified!)) {
          lastModified = currentModified;
          final f = await file.readAsString();
          final logs = f.characters
              .toString()
              .split('\n')
              .where((line) => line.isNotEmpty)
              .toList();

          if (logs.isEmpty) {
            return;
          }

          List<UserActionModel> locationList = await parseLocationData(logs);

          for (var location in locationList) {
            final oplog = OpLogEntry(
              location,
              DataOperation.create,
              createdAt: DateTime.now(),
              createdBy: createdBy,
              clientReferenceId: IdGen.instance.identifier,
              type: DataModelType.userLocation,
            ).oplog;
            isar.writeTxnSync(() {
              isar.opLogs.putSync(oplog);
            });
          }
        }
        file.deleteSync();
      }
    });
  } catch (e) {
    if (kDebugMode) {
      print('oplog entry error $e');
    }
  }
}