run method

  1. @override
Future<void> run()
override

Method that will be executed in the functionality. Remember when overrided to establish async.

Implementation

@override
Future<void> run() async {
  List<File> historyFiles = [];
  List<String> result = [];

  // Remove all
  if (this._date == null && this._entry == null) {
    Directory(this._historyFolderPath).deleteSync(recursive: true);
    return;
  }

  // Filter by date
  if (this._date != null) {
    historyFiles = await _getFilesByDate();
    if (historyFiles.isEmpty) return;
    for (File file in historyFiles) result.addAll(file.readAsLinesSync());
    result = _getResultByTime(result);
  } else {
    historyFiles =
        _getAllFilesFromDirectory(Directory(this._historyFolderPath));
    if (historyFiles.isEmpty) return;
    for (File file in historyFiles) result.addAll(file.readAsLinesSync());
  }

  // Filter by entry
  if (this._entry != null)
    for (int i = result.length - 1; i >= 0; i--)
      if (!result[i].contains(this._entry!)) result.remove(result[i]);

  // delete entries in files or file if no entries are left
  if (result.isEmpty) return;
  for (File file in historyFiles) {
    List<String> lines = file.readAsLinesSync();
    for (int ii = lines.length - 1; ii >= 0; ii--)
      for (String r in result) if (lines[ii].contains(r)) lines.removeAt(ii);
    lines.isEmpty
        ? file.deleteSync()
        : file.writeAsStringSync(lines.join('\n'));
  }
}