Skip to content

Commit 6603ef9

Browse files
committed
changed taskchampion setting to be profile dependant
1 parent 27a1eb7 commit 6603ef9

File tree

12 files changed

+379
-143
lines changed

12 files changed

+379
-143
lines changed

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

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,6 @@ class NavDrawer extends StatelessWidget {
7878
color: tColors.dialogBackgroundColor,
7979
height: Get.height * 0.03,
8080
),
81-
Visibility(
82-
visible: homeController.taskchampion.value,
83-
child: NavDrawerMenuItem(
84-
icon: Icons.task_alt,
85-
text: SentenceManager(
86-
currentLanguage: homeController.selectedLanguage.value,
87-
).sentences.ccsyncCredentials,
88-
onTap: () {
89-
Get.toNamed(Routes.MANAGE_TASK_CHAMPION_CREDS);
90-
},
91-
),
92-
),
9381
Visibility(
9482
visible: homeController.taskchampion.value,
9583
child: NavDrawerMenuItem(
@@ -156,18 +144,15 @@ class NavDrawer extends StatelessWidget {
156144
);
157145
}),
158146
),
159-
Visibility(
160-
visible: !homeController.taskchampion.value,
161-
child: Obx(
162-
() => NavDrawerMenuItem(
163-
icon: Icons.person_rounded,
164-
text: SentenceManager(
165-
currentLanguage: homeController.selectedLanguage.value,
166-
).sentences.navDrawerProfile,
167-
onTap: () {
168-
Get.toNamed(Routes.PROFILE);
169-
},
170-
),
147+
Obx(
148+
() => NavDrawerMenuItem(
149+
icon: Icons.person_rounded,
150+
text: SentenceManager(
151+
currentLanguage: homeController.selectedLanguage.value,
152+
).sentences.navDrawerProfile,
153+
onTap: () {
154+
Get.toNamed(Routes.PROFILE);
155+
},
171156
),
172157
),
173158
Visibility(

lib/app/modules/manage_task_champion_creds/controllers/manage_task_champion_creds_controller.dart

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,65 @@
1+
import 'dart:convert';
2+
13
import 'package:flutter/material.dart';
24
import 'package:get/get.dart';
3-
import 'package:shared_preferences/shared_preferences.dart';
5+
import 'package:taskwarrior/app/modules/splash/controllers/splash_controller.dart';
6+
import 'package:taskwarrior/app/utils/taskchampion/credentials_storage.dart';
7+
import 'package:taskwarrior/app/v3/net/origin.dart';
8+
import 'package:http/http.dart' as http;
49

510
class ManageTaskChampionCredsController extends GetxController {
611
final encryptionSecretController = TextEditingController();
712
final clientIdController = TextEditingController();
813
final ccsyncBackendUrlController = TextEditingController();
9-
14+
var profilesWidget = Get.find<SplashController>();
15+
RxBool isCheckingCreds = false.obs;
1016
@override
1117
void onInit() {
1218
super.onInit();
1319
loadCredentials();
1420
}
1521

1622
Future<void> loadCredentials() async {
17-
SharedPreferences prefs = await SharedPreferences.getInstance();
18-
encryptionSecretController.text = prefs.getString('encryptionSecret') ?? '';
19-
clientIdController.text = prefs.getString('clientId') ?? '';
20-
ccsyncBackendUrlController.text = prefs.getString('ccsyncBackendUrl') ?? '';
23+
encryptionSecretController.text =
24+
await CredentialsStorage.getEncryptionSecret() ?? '';
25+
clientIdController.text = await CredentialsStorage.getClientId() ?? '';
26+
ccsyncBackendUrlController.text =
27+
await CredentialsStorage.getApiUrl() ?? '';
2128
}
2229

23-
Future<void> saveCredentials() async {
24-
SharedPreferences prefs = await SharedPreferences.getInstance();
25-
await prefs.setString('encryptionSecret', encryptionSecretController.text);
26-
await prefs.setString('clientId', clientIdController.text);
27-
await prefs.setString('ccsyncBackendUrl', ccsyncBackendUrlController.text);
30+
Future<int> saveCredentials() async {
31+
isCheckingCreds.value = true;
32+
String baseUrl = ccsyncBackendUrlController.text;
33+
String uuid = clientIdController.text;
34+
String encryptionSecret = encryptionSecretController.text;
35+
try {
36+
String url =
37+
'$baseUrl/tasks?email=email&origin=$origin&UUID=$uuid&encryptionSecret=$encryptionSecret';
38+
39+
var response = await http.get(Uri.parse(url), headers: {
40+
"Content-Type": "application/json",
41+
}).timeout(const Duration(seconds: 10000));
42+
debugPrint("Fetch tasks response: ${response.statusCode}");
43+
debugPrint("Fetch tasks body: ${response.body}");
44+
if (response.statusCode == 200) {
45+
List<dynamic> allTasks = jsonDecode(response.body);
46+
debugPrint(allTasks.toString());
47+
profilesWidget.setTaskcCreds(
48+
profilesWidget.currentProfile.value,
49+
clientIdController.text,
50+
encryptionSecretController.text,
51+
ccsyncBackendUrlController.text);
52+
53+
isCheckingCreds.value = false;
54+
return 0;
55+
} else {
56+
throw Exception('Failed to load tasks');
57+
}
58+
} catch (e, s) {
59+
debugPrint('Error fetching tasks: $e\n $s');
60+
isCheckingCreds.value = false;
61+
return 1;
62+
}
2863
}
2964

3065
@override

lib/app/modules/manage_task_champion_creds/views/manage_task_champion_creds_view.dart

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,25 @@ class ManageTaskChampionCredsView
6767
children: [
6868
TextField(
6969
style: TextStyle(color: tColors.primaryTextColor),
70-
controller: controller.encryptionSecretController,
70+
controller: controller.clientIdController,
7171
decoration: InputDecoration(
7272
labelText: SentenceManager(
7373
currentLanguage: AppSettings.selectedLanguage)
7474
.sentences
75-
.encryptionSecret,
75+
.ccsyncClientId,
7676
labelStyle: TextStyle(color: tColors.primaryTextColor),
7777
border: const OutlineInputBorder(),
7878
),
7979
),
8080
const SizedBox(height: 10),
8181
TextField(
8282
style: TextStyle(color: tColors.primaryTextColor),
83-
controller: controller.clientIdController,
83+
controller: controller.encryptionSecretController,
8484
decoration: InputDecoration(
8585
labelText: SentenceManager(
8686
currentLanguage: AppSettings.selectedLanguage)
8787
.sentences
88-
.ccsyncClientId,
88+
.encryptionSecret,
8989
labelStyle: TextStyle(color: tColors.primaryTextColor),
9090
border: const OutlineInputBorder(),
9191
),
@@ -104,24 +104,54 @@ class ManageTaskChampionCredsView
104104
),
105105
),
106106
const SizedBox(height: 20),
107-
ElevatedButton(
108-
onPressed: () async {
109-
await controller.saveCredentials();
110-
Get.snackbar(
111-
SentenceManager(
112-
currentLanguage: AppSettings.selectedLanguage)
113-
.sentences
114-
.success,
115-
SentenceManager(
116-
currentLanguage: AppSettings.selectedLanguage)
117-
.sentences
118-
.credentialsSavedSuccessfully,
119-
snackPosition: SnackPosition.BOTTOM,
120-
duration: Duration(seconds: 2),
121-
);
122-
},
123-
child: const Text('Save Credentials'),
124-
),
107+
Obx(() => Center(
108+
child: ElevatedButton(
109+
onPressed: controller.isCheckingCreds.value
110+
? null
111+
: () async {
112+
int status = await controller.saveCredentials();
113+
if (status == 0) {
114+
ScaffoldMessenger.of(context).showSnackBar(
115+
SnackBar(
116+
content: Text(
117+
SentenceManager(
118+
currentLanguage: AppSettings
119+
.selectedLanguage)
120+
.sentences
121+
.credentialsSavedSuccessfully,
122+
style: TextStyle(
123+
color: tColors.primaryTextColor,
124+
),
125+
),
126+
backgroundColor:
127+
tColors.secondaryBackgroundColor,
128+
duration:
129+
const Duration(seconds: 2)));
130+
return;
131+
}
132+
ScaffoldMessenger.of(context).showSnackBar(
133+
SnackBar(
134+
content: Text(
135+
"Unable to fetch tasks with it ! Check creds",
136+
style: TextStyle(
137+
color: tColors.primaryTextColor,
138+
),
139+
),
140+
backgroundColor:
141+
tColors.secondaryBackgroundColor,
142+
duration: const Duration(seconds: 2)));
143+
},
144+
child: controller.isCheckingCreds.value
145+
? const SizedBox(
146+
height: 20,
147+
width: 20,
148+
child: CircularProgressIndicator(
149+
color: Color.fromARGB(255, 83, 83, 83),
150+
strokeWidth: 2.0,
151+
),
152+
)
153+
: const Text('Save Credentials'),
154+
))),
125155
const SizedBox(height: 10),
126156
Text(
127157
SentenceManager(

lib/app/modules/profile/views/deleteprofiledialog.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class DeleteProfileDialog extends StatelessWidget {
2222
final String? profileName;
2323
@override
2424
Widget build(BuildContext context) {
25-
TaskwarriorColorTheme tColors = Theme.of(context).extension<TaskwarriorColorTheme>()!;
25+
TaskwarriorColorTheme tColors =
26+
Theme.of(context).extension<TaskwarriorColorTheme>()!;
2627
return Center(
2728
child: SingleChildScrollView(
2829
child: Center(
@@ -54,11 +55,17 @@ class DeleteProfileDialog extends StatelessWidget {
5455
),
5556
),
5657
TextButton(
57-
onPressed: () {
58+
onPressed: () async {
5859
try {
59-
Get.find<SplashController>().deleteProfile(profile);
60+
var splashController = Get.find<SplashController>();
61+
await splashController.deleteProfile(profile);
6062
// Navigator.of(context).pop();
61-
Get.find<HomeController>().refreshTaskWithNewProfile();
63+
if (splashController
64+
.getMode(splashController.currentProfile.value) !=
65+
"TW3") {
66+
Get.find<HomeController>().refreshTaskWithNewProfile();
67+
}
68+
6269
Get.back();
6370
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
6471
content: Text(

lib/app/modules/profile/views/profile_view.dart

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:taskwarrior/app/utils/constants/utilites.dart';
1313
import 'package:taskwarrior/app/utils/app_settings/app_settings.dart';
1414
import 'package:taskwarrior/app/utils/language/sentence_manager.dart';
1515
import 'package:taskwarrior/app/utils/themes/theme_extension.dart';
16+
import 'package:taskwarrior/app/v3/db/task_database.dart';
1617

1718
import '../controllers/profile_controller.dart';
1819

@@ -91,10 +92,25 @@ class ProfileView extends GetView<ProfileController> {
9192
),
9293
),
9394
),
94-
() => Get.toNamed(Routes.MANAGE_TASK_SERVER),
95-
(profile) {
96-
var tasks =
97-
controller.profilesWidget.getStorage(profile).data.export();
95+
() {
96+
if (controller.profilesWidget
97+
.getMode(controller.currentProfile.value) ==
98+
'TW3') {
99+
Get.toNamed(Routes.MANAGE_TASK_CHAMPION_CREDS);
100+
return;
101+
}
102+
Get.toNamed(Routes.MANAGE_TASK_SERVER);
103+
},
104+
(profile) async {
105+
String tasks;
106+
if (controller.profilesWidget.getMode(profile) == "TW2") {
107+
tasks =
108+
controller.profilesWidget.getStorage(profile).data.export();
109+
} else {
110+
TaskDatabase db = TaskDatabase();
111+
await db.openForProfile(profile);
112+
tasks = await db.exportAllTasks();
113+
}
98114
var now = DateTime.now()
99115
.toIso8601String()
100116
.replaceAll(RegExp(r'[-:]'), '')
@@ -160,17 +176,17 @@ class ProfileView extends GetView<ProfileController> {
160176
},
161177
);
162178
},
163-
(profile) {
179+
(profile) async {
164180
try {
165-
controller.profilesWidget.copyConfigToNewProfile(
181+
await controller.profilesWidget.copyConfigToNewProfile(
166182
profile,
167183
);
168184
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
169185
content: Text(
170186
SentenceManager(
171-
currentLanguage: AppSettings.selectedLanguage)
172-
.sentences
173-
.profileConfigCopied,
187+
currentLanguage: AppSettings.selectedLanguage)
188+
.sentences
189+
.profileConfigCopied,
174190
style: TextStyle(
175191
color: tColors.primaryTextColor,
176192
),
@@ -181,9 +197,9 @@ class ProfileView extends GetView<ProfileController> {
181197
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
182198
content: Text(
183199
SentenceManager(
184-
currentLanguage: AppSettings.selectedLanguage)
185-
.sentences
186-
.profileConfigCopyFailed,
200+
currentLanguage: AppSettings.selectedLanguage)
201+
.sentences
202+
.profileConfigCopyFailed,
187203
style: TextStyle(
188204
color: tColors.primaryTextColor,
189205
),
@@ -205,6 +221,58 @@ class ProfileView extends GetView<ProfileController> {
205221
),
206222
);
207223
},
224+
(profile) {
225+
String currentMode = controller.profilesWidget.getMode(profile);
226+
showDialog(
227+
context: context,
228+
builder: (BuildContext context) {
229+
return Utils.showAlertDialog(
230+
title: Text(
231+
"Change TW Mode",
232+
),
233+
content: Text("Change mode to"),
234+
actions: <Widget>[
235+
TextButton(
236+
onPressed: currentMode != "TW3"
237+
? () {
238+
// Navigator.of(context).pop();
239+
Get.back();
240+
controller.profilesWidget.changeModeTo(
241+
profile,
242+
'TW3',
243+
);
244+
}
245+
: null,
246+
child: Text(
247+
"TW3",
248+
style: TextStyle(
249+
color: tColors.primaryTextColor,
250+
),
251+
),
252+
),
253+
TextButton(
254+
onPressed: currentMode != "TW2"
255+
? () {
256+
// Navigator.of(context).pop();
257+
Get.back();
258+
controller.profilesWidget.changeModeTo(
259+
profile,
260+
'TW2',
261+
);
262+
}
263+
: null,
264+
child: Text(
265+
"TW2",
266+
style: TextStyle(
267+
color: tColors.primaryTextColor,
268+
),
269+
),
270+
),
271+
],
272+
);
273+
},
274+
);
275+
},
208276
)),
209277
);
210278
}

0 commit comments

Comments
 (0)