Skip to content

Commit 8aa192f

Browse files
committed
tags error from rustlib fixed
also other refactorization in dart codebase reflected
1 parent 7fa2a97 commit 8aa192f

File tree

6 files changed

+84
-7
lines changed

6 files changed

+84
-7
lines changed
4.23 KB
Binary file not shown.

lib/app/modules/home/controllers/home_controller.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ class HomeController extends GetxController {
117117
widgetController.updateWidget();
118118
}
119119
});
120+
tryRust();
121+
}
122+
123+
Future<void> tryRust() async {
124+
Directory? someDir = await getDownloadsDirectory();
125+
126+
addTask(taskdbDirPath: someDir != null ? someDir.path : "", map: {
127+
'description': "some task from bridge 2",
128+
"uuid": "270750a0-1801-4a24-8b29-a7aaf62fc74d",
129+
"tags": "t1 t2 t3"
130+
});
131+
132+
debugPrint(
133+
"tryRustHere: ${await getAllTasksJson(taskdbDirPath: someDir != null ? someDir.path : "")}");
120134
}
121135

122136
Future<List<String>> getUniqueProjects() async {

lib/app/modules/taskc_details/controllers/taskc_details_controller.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class TaskcDetailsController extends GetxController {
7474
due = formatDate(task.due).obs;
7575
start = "".obs;
7676
wait = "".obs;
77+
debugPrint(
78+
'Replica task tags while init: ${task.tags ?? task.tags?.join(", ")}');
7779
tags = task.tags != null
7880
? task.tags!.map((e) => e.toString()).toList().obs
7981
: <String>[].obs;
@@ -236,6 +238,8 @@ class TaskcDetailsController extends GetxController {
236238
tags.toList(),
237239
);
238240
} else if (initialTask is TaskForReplica) {
241+
debugPrint(
242+
'Saving replica task changes... status ${status.string} ${tags.join(", ")}');
239243
final int nowEpoch = DateTime.now().millisecondsSinceEpoch ~/ 1000;
240244
final modifiedTask = TaskForReplica(
241245
modified: nowEpoch,

lib/app/v3/champion/Replica.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class Replica {
1919
"start",
2020
"wait",
2121
"priority",
22-
"project"
22+
"project",
23+
"status"
2324
];
2425
static Future<String> addTaskToReplica(
2526
HashMap<String, dynamic> newTask) async {
@@ -41,7 +42,6 @@ class Replica {
4142
}
4243
}
4344
map['uuid'] = UuidV4().generate();
44-
debugPrint("Adding to Replica: $map");
4545
try {
4646
await addTask(taskdbDirPath: taskdbDirPath, map: map);
4747
await getAllTasksFromReplica(); //to update the db
@@ -62,13 +62,14 @@ class Replica {
6262
String tags = "";
6363
if (newTask.tags != null) {
6464
tags = newTask.tags!.join(" ");
65+
debugPrint("Modifying Replica Task Tags: $tags");
6566
map["tags"] = tags;
6667
}
6768
var json = newTask.toJson();
6869
for (String attr in attrs) {
6970
if (json[attr] != null) map[attr] = json[attr].toString();
7071
}
71-
debugPrint("Modifying Replica Task: $map");
72+
debugPrint("Modifying Replica Task JSON the map: $map");
7273
try {
7374
await updateTask(
7475
uuidSt: newTask.uuid, taskdbDirPath: taskdbDirPath, map: map);
@@ -102,7 +103,6 @@ class Replica {
102103
debugPrint("Parsed from Replica: $tasks");
103104
} catch (e) {
104105
debugPrint("Error fetching from Replica $e");
105-
106106
return [];
107107
}
108108
return tasks;

lib/app/v3/champion/models/task_for_replica.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class TaskForReplica {
3838
description: json['description']?.toString(),
3939
tags: (json['tags'] is List)
4040
? (json['tags'] as List).map((e) => e.toString()).toList()
41-
: null,
41+
: (json['tags'] is String)
42+
? json['tags'].toString().split(' ')
43+
: null,
4244
uuid: json['uuid']?.toString() ?? '',
4345
priority: json['priority']?.toString(),
4446
project: json['project']?.toString(),

rust/src/api.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ fn get_all_tasks(taskdb_dir_path: String) -> Vec<HashMap<String, String>> {
4242
for (k, v) in value.get_taskmap() {
4343
if k.contains("tag_") {
4444
if let Some(stripped) = k.strip_prefix("tag_") {
45-
tags.push_str(stripped);
45+
tags.push_str(stripped);
46+
tags.push(' ');
4647
}
4748
} else {
4849
map.insert(k.into(), v.into());
@@ -116,14 +117,37 @@ pub fn update_task(
116117
let _ = t.set_priority(value, &mut ops);
117118
}
118119
"tags" => {
119-
for part in value.split_whitespace() {
120+
let existing_tags: Vec<String> = t
121+
.get_taskmap()
122+
.iter()
123+
.filter_map(|(k, _)| k.strip_prefix("tag_").map(|s| s.to_string()))
124+
.collect();
125+
for tag_name in existing_tags {
126+
println!("removing tag at rust side {}", tag_name);
127+
let mut tag = Tag::from_str(&tag_name).unwrap();
128+
let _ = t.remove_tag(&mut tag, &mut ops);
129+
}
130+
131+
for part in value.split_whitespace() {
132+
println!("tag at rust side {}", part);
120133
let mut tag = Tag::from_str(part).unwrap();
121134
let _ = t.add_tag(&mut tag, &mut ops);
122135
}
123136
}
124137
"project" => {
125138
let _ = t.set_user_defined_attribute("project", value, &mut ops);
126139
}
140+
"status" => {
141+
let status = match value.as_str() {
142+
"pending" => taskchampion::Status::Pending,
143+
"completed" => taskchampion::Status::Completed,
144+
"deleted" => taskchampion::Status::Deleted,
145+
_ => taskchampion::Status::Pending,
146+
};
147+
// print!("status at rust side {}", value);
148+
println!("status at rust side {}", value);
149+
let _ = t.set_status(status, &mut ops);
150+
}
127151
_ => {}
128152
}
129153
}
@@ -213,3 +237,36 @@ pub async fn sync(
213237
replica.sync(&mut server, false).unwrap();
214238
0
215239
}
240+
241+
#[test]
242+
fn test_add_task_with_tags() {
243+
use std::{collections::HashMap, env, fs};
244+
// create unique temporary directory for taskdb
245+
let tmp = env::temp_dir().join(format!("taskdb_test_{}", Uuid::new_v4()));
246+
let taskdb_path = tmp.to_string_lossy().into_owned();
247+
fs::create_dir_all(&tmp).expect("create temp taskdb dir");
248+
249+
// prepare task map with tags
250+
let mut map: HashMap<String, String> = HashMap::new();
251+
let uuid = Uuid::new_v4().to_string();
252+
map.insert("uuid".to_string(), uuid.clone());
253+
map.insert("description".to_string(), "test task".to_string());
254+
map.insert("tags".to_string(), "tag1 tag2".to_string());
255+
256+
// add task
257+
let res = add_task(taskdb_path.clone(), map);
258+
assert_eq!(res, 0);
259+
260+
// read tasks as json and verify tags are present
261+
let json = get_all_tasks_json(taskdb_path.clone()).expect("get_all_tasks_json");
262+
let tasks: Vec<HashMap<String, String>> = serde_json::from_str(&json).expect("parse json");
263+
let found = tasks.into_iter().find(|m| m.get("uuid").map(|s| s == &uuid).unwrap_or(false));
264+
assert!(found.is_some(), "task with uuid not found");
265+
let task = found.unwrap();
266+
let tags = task.get("tags").map(|s| s.as_str()).unwrap_or("");
267+
assert!(tags.contains("tag1"), "tag1 missing in tags: {}", tags);
268+
assert!(tags.contains("tag2"), "tag2 missing in tags: {}", tags);
269+
270+
// cleanup
271+
fs::remove_dir_all(&tmp).ok();
272+
}

0 commit comments

Comments
 (0)