Skip to content

Commit

Permalink
refactor(id): Removes id from the the todo key values #34
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaegel committed May 3, 2024
1 parent d731848 commit a42c43b
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 415 deletions.
2 changes: 1 addition & 1 deletion lib/common_widgets/tag_dialog.dart
Expand Up @@ -12,7 +12,7 @@ class Tag {
});

@override
String toString() => name;
String toString() => '$name ($selected)';
}

class TagDialog extends StatefulWidget {
Expand Down
43 changes: 14 additions & 29 deletions lib/domain/todo/todo_model.dart
@@ -1,5 +1,7 @@
import 'dart:convert';
import 'dart:math';

import 'package:crypto/crypto.dart';
import 'package:equatable/equatable.dart';
import 'package:ntodotxt/exceptions/exceptions.dart';

Expand Down Expand Up @@ -106,10 +108,7 @@ class Todo extends Equatable {
// Prevent +, @ at the beginning and additional ':' within.
static final RegExp patternKeyValue =
RegExp(r'^([^\+\@].*[^:\s]):(.*[^:\s])$'); // @todo: a:b failed
static final RegExp patternId = RegExp(r'^id:[a-zA-Z0-9\-]+$');

/// Unique [id]
/// [id] is mandatory.
final String id;

/// Whether the [Todo] is completed.
Expand All @@ -133,8 +132,6 @@ class Todo extends Equatable {
/// Defaults to null (unset).
final String? _description;

String get fmtId => 'id:$id';

/// Whether the [Todo] is completed.
/// Defaults to false.
bool get completion => _completion ?? false;
Expand Down Expand Up @@ -172,7 +169,6 @@ class Todo extends Equatable {
String get fmtDescription {
final List<String> descriptionList = [];
for (String item in description.split(' ')) {
if (patternId.hasMatch(item)) continue;
if (matchProject(item)) continue;
if (matchContext(item)) continue;
if (matchKeyValue(item)) continue;
Expand Down Expand Up @@ -248,7 +244,6 @@ class Todo extends Equatable {
if (matchKeyValue(item)) {
List<String> kvSplitted = item.split(':');
if (kvSplitted.length > 2) continue;
if (kvSplitted[0] == 'id') continue; // Exclude id here.
keyValues.add(item.toLowerCase());
}
}
Expand Down Expand Up @@ -358,6 +353,7 @@ class Todo extends Equatable {
}

factory Todo.fromString({
String? id,
required String value,
}) {
final todoStr = _trim(value);
Expand Down Expand Up @@ -422,7 +418,7 @@ class Todo extends Equatable {
}

return Todo(
id: _str2Id(fullDescriptionList) ?? Todo.genId(),
id: id ?? Todo.genId(),
completion: completion,
priority: priority,
completionDate: completionDate,
Expand Down Expand Up @@ -487,7 +483,6 @@ class Todo extends Equatable {

@override
List<Object?> get props => [
id,
completion,
completionDate,
priority,
Expand All @@ -496,26 +491,25 @@ class Todo extends Equatable {
];

@override
String toString({
bool includeId = true,
}) {
String toString() {
final List<String> items = [
fmtCompletion,
fmtCompletionDate,
fmtPriority,
fmtCreationDate,
description,
if (includeId) fmtId,
]..removeWhere((value) => value.isEmpty);

return items.join(' ');
}

static String genId({int len = 10}) {
static String genId({int len = 32}) {
final Random r = Random();
const chars =
'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
return List.generate(len, (index) => chars[r.nextInt(chars.length)]).join();
final String randomId =
List.generate(len, (index) => chars[r.nextInt(chars.length)]).join();
return sha256.convert(utf8.encode(randomId)).toString();
}

static DateTime? str2date(String value) {
Expand Down Expand Up @@ -599,25 +593,16 @@ class Todo extends Equatable {
return Priority.none;
}

/// Trim projects, contexts and key-values from description.
static String _str2description(List<String> strList) {
final List<String> descriptionList = [];
for (var item in strList) {
if (patternId.hasMatch(item)) continue;
descriptionList.add(item);
}

return descriptionList.join(' ');
}

static String? _str2Id(List<String> strList) {
for (var item in strList) {
if (patternId.hasMatch(item)) {
final List<String> splittedId = item.split(':');
return splittedId[1];
if (matchProject(item) || matchContext(item) || matchKeyValue(item)) {
descriptionList.add(item.toLowerCase());
} else {
descriptionList.add(item);
}
}

return null;
return descriptionList.join(' ');
}
}
2 changes: 1 addition & 1 deletion pubspec.lock
Expand Up @@ -90,7 +90,7 @@ packages:
source: hosted
version: "3.1.1"
crypto:
dependency: transitive
dependency: "direct main"
description:
name: crypto
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Expand Up @@ -14,6 +14,7 @@ dependencies:
flutter:
sdk: flutter
collection: ^1.17.2
crypto: ^3.0.3
cupertino_icons: ^1.0.6
dio: ^5.1.0
equatable: ^2.0.5
Expand Down

0 comments on commit a42c43b

Please sign in to comment.