Skip to content

Commit 20a0443

Browse files
committed
rustlib integrated
1 parent 431bc41 commit 20a0443

File tree

2 files changed

+188
-2
lines changed

2 files changed

+188
-2
lines changed

lib/app/modules/home/views/add_task_bottom_sheet_new.dart

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ import 'package:taskwarrior/app/utils/taskfunctions/add_task_dialog_utils.dart';
1616
import 'package:taskwarrior/app/utils/taskfunctions/tags.dart';
1717
import 'package:taskwarrior/app/utils/taskfunctions/taskparser.dart';
1818
import 'package:taskwarrior/app/utils/themes/theme_extension.dart';
19+
import 'package:taskwarrior/app/v3/champion/Replica.dart';
1920
import 'package:taskwarrior/app/v3/models/task.dart';
2021

2122
class AddTaskBottomSheet extends StatelessWidget {
2223
final HomeController homeController;
2324
final bool forTaskC;
25+
final bool forReplica;
2426
const AddTaskBottomSheet(
25-
{required this.homeController, super.key, this.forTaskC = false});
27+
{required this.homeController,
28+
super.key,
29+
this.forTaskC = false,
30+
this.forReplica = false});
2631

2732
@override
2833
Widget build(BuildContext context) {
@@ -66,6 +71,8 @@ class AddTaskBottomSheet extends StatelessWidget {
6671
onPressed: () {
6772
if (forTaskC) {
6873
onSaveButtonClickedTaskC(context);
74+
} else if (forReplica) {
75+
onSaveButtonClickedForReplica(context);
6976
} else {
7077
onSaveButtonClicked(context);
7178
}
@@ -354,7 +361,6 @@ class AddTaskBottomSheet extends StatelessWidget {
354361
}
355362

356363
void onSaveButtonClicked(BuildContext context) async {
357-
// print(homeController.formKey.currentState);
358364
if (homeController.formKey.currentState!.validate()) {
359365
try {
360366
var task = taskParser(homeController.namecontroller.text)
@@ -432,4 +438,83 @@ class AddTaskBottomSheet extends StatelessWidget {
432438
}
433439
}
434440
}
441+
442+
void onSaveButtonClickedForReplica(BuildContext context) async {
443+
if (homeController.formKey.currentState!.validate()) {
444+
try {
445+
var task = taskParser(homeController.namecontroller.text)
446+
.rebuild((b) =>
447+
b..due = getDueDate(homeController.selectedDates)?.toUtc())
448+
.rebuild((p) => p..priority = homeController.priority.value)
449+
.rebuild((t) => t..project = homeController.projectcontroller.text)
450+
.rebuild((t) =>
451+
t..wait = getWaitDate(homeController.selectedDates)?.toUtc())
452+
.rebuild((t) =>
453+
t..until = getUntilDate(homeController.selectedDates)?.toUtc())
454+
.rebuild((t) => t
455+
..scheduled =
456+
getSchedDate(homeController.selectedDates)?.toUtc());
457+
if (homeController.tags.isNotEmpty) {
458+
task = task.rebuild((t) => t..tags.replace(homeController.tags));
459+
}
460+
await Replica.addTaskToReplica(task);
461+
homeController.namecontroller.text = '';
462+
homeController.projectcontroller.text = '';
463+
homeController.dueString.value = "";
464+
homeController.priority.value = 'X';
465+
homeController.tagcontroller.text = '';
466+
homeController.tags.value = [];
467+
homeController.update();
468+
Get.back();
469+
if (Platform.isAndroid) {
470+
WidgetController widgetController = Get.put(WidgetController());
471+
widgetController.fetchAllData();
472+
widgetController.update();
473+
}
474+
475+
homeController.update();
476+
477+
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
478+
content: Text(
479+
SentenceManager(
480+
currentLanguage: homeController.selectedLanguage.value)
481+
.sentences
482+
.addTaskTaskAddedSuccessfully,
483+
style: TextStyle(
484+
color: AppSettings.isDarkMode
485+
? TaskWarriorColors.kprimaryTextColor
486+
: TaskWarriorColors.kLightPrimaryTextColor,
487+
),
488+
),
489+
backgroundColor: AppSettings.isDarkMode
490+
? TaskWarriorColors.ksecondaryBackgroundColor
491+
: TaskWarriorColors.kLightSecondaryBackgroundColor,
492+
duration: const Duration(seconds: 2)));
493+
494+
final SharedPreferences prefs = await SharedPreferences.getInstance();
495+
bool? value;
496+
value = prefs.getBool('sync-OnTaskCreate') ?? false;
497+
// late InheritedStorage storageWidget;
498+
// storageWidget = StorageWidget.of(context);
499+
var storageWidget = Get.find<HomeController>();
500+
if (value) {
501+
storageWidget.synchronize(context, true);
502+
}
503+
} on FormatException catch (e) {
504+
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
505+
content: Text(
506+
e.message,
507+
style: TextStyle(
508+
color: AppSettings.isDarkMode
509+
? TaskWarriorColors.kprimaryTextColor
510+
: TaskWarriorColors.kLightPrimaryTextColor,
511+
),
512+
),
513+
backgroundColor: AppSettings.isDarkMode
514+
? TaskWarriorColors.ksecondaryBackgroundColor
515+
: TaskWarriorColors.kLightSecondaryBackgroundColor,
516+
duration: const Duration(seconds: 2)));
517+
}
518+
}
519+
}
435520
}

lib/app/v3/champion/Replica.dart

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import 'dart:io';
2+
import 'dart:collection';
3+
4+
import 'package:path_provider/path_provider.dart';
5+
import 'package:shared_preferences/shared_preferences.dart';
6+
import 'package:taskwarrior/app/models/models.dart';
7+
import 'package:taskwarrior/rust_bridge/api.dart';
8+
9+
class Replica {
10+
static List<String> attrs = [
11+
"description",
12+
"due",
13+
"start",
14+
"wait",
15+
"priority",
16+
"project"
17+
];
18+
static Future<String> addTaskToReplica(Task newTask) async {
19+
var taskdbDirPath = await getReplicaPath();
20+
HashMap<String, String> map = HashMap<String, String>();
21+
if (newTask.uuid.isEmpty) {
22+
return "err";
23+
}
24+
String tags = "";
25+
if (newTask.tags != null) {
26+
tags = newTask.tags!.join(" ");
27+
map["tags"] = tags;
28+
}
29+
var json = newTask.toJson();
30+
for (String attr in attrs) {
31+
if (json[attr]) map[attr] = json[attr];
32+
}
33+
try {
34+
await addTask(taskdbDirPath: taskdbDirPath, map: map);
35+
} catch (e) {
36+
return "err";
37+
}
38+
return "scc";
39+
}
40+
41+
static Future<String> modifyTaskInReplica(Task newTask) async {
42+
var taskdbDirPath = await getReplicaPath();
43+
HashMap<String, String> map = HashMap<String, String>();
44+
if (newTask.uuid.isEmpty) {
45+
return "err";
46+
}
47+
String tags = "";
48+
if (newTask.tags != null) {
49+
tags = newTask.tags!.join(" ");
50+
map["tags"] = tags;
51+
}
52+
var json = newTask.toJson();
53+
for (String attr in attrs) {
54+
if (json[attr]) map[attr] = json[attr];
55+
}
56+
try {
57+
await updateTask(
58+
uuidSt: newTask.uuid, taskdbDirPath: taskdbDirPath, map: map);
59+
} catch (e) {
60+
return "err";
61+
}
62+
return "scc";
63+
}
64+
65+
static Future<String> deleteTaskFromReplica(String uuid) async {
66+
var taskdbDirPath = await getReplicaPath();
67+
try {
68+
await deleteTask(uuidSt: uuid, taskdbDirPath: taskdbDirPath);
69+
} catch (e) {
70+
return "err";
71+
}
72+
return "scc";
73+
}
74+
75+
static Future<String> getReplicaPath() async {
76+
String? profile = await getCurrentProfile();
77+
Directory base = await getBaseDire();
78+
return '${base.path}/profiles/$profile/replica';
79+
}
80+
81+
static Future<String?> getCurrentProfile() async {
82+
Directory base = await getBaseDire();
83+
if (File('${base.path}/current-profile').existsSync()) {
84+
return File('${base.path}/current-profile').readAsStringSync();
85+
}
86+
return null;
87+
}
88+
89+
static Future<Directory> getBaseDire() async {
90+
SharedPreferences prefs = await SharedPreferences.getInstance();
91+
String? directory = prefs.getString('baseDirectory');
92+
Directory dir = (directory != null)
93+
? Directory(directory)
94+
: await getDefaultDirectory();
95+
return dir;
96+
}
97+
98+
static Future<Directory> getDefaultDirectory() async {
99+
return await getApplicationDocumentsDirectory();
100+
}
101+
}

0 commit comments

Comments
 (0)