-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathsettings_controller.dart
More file actions
225 lines (202 loc) · 8.24 KB
/
settings_controller.dart
File metadata and controls
225 lines (202 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// ignore_for_file: depend_on_referenced_packages
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:taskwarrior/app/modules/home/controllers/home_controller.dart';
import 'package:taskwarrior/app/utils/language/supported_language.dart';
import 'package:taskwarrior/app/utils/constants/taskwarrior_fonts.dart';
import 'package:taskwarrior/app/utils/constants/utilites.dart';
import 'package:taskwarrior/app/utils/constants/taskwarrior_colors.dart';
import 'package:taskwarrior/app/utils/app_settings/app_settings.dart';
import 'package:path/path.dart' as path;
import 'package:taskwarrior/app/modules/splash/controllers/splash_controller.dart';
import 'package:taskwarrior/app/utils/themes/theme_extension.dart';
class SettingsController extends GetxController {
RxBool isMovingDirectory = false.obs;
Rx<SupportedLanguage> selectedLanguage = AppSettings.selectedLanguage.obs;
RxString baseDirectory = "".obs;
void setSelectedLanguage(SupportedLanguage language) async {
await SelectedLanguage.saveSelectedLanguage(language);
selectedLanguage.value = language;
AppSettings.selectedLanguage = language;
Get.find<HomeController>().selectedLanguage.value = language;
}
Future<String> getBaseDirectory() async {
SplashController profilesWidget = Get.find<SplashController>();
Directory baseDir = profilesWidget.baseDirectory();
Directory defaultDirectory = await profilesWidget.getDefaultDirectory();
if (baseDir.path == defaultDirectory.path) {
return 'Default';
} else {
return baseDir.path;
}
}
void pickDirectory(BuildContext context) {
TaskwarriorColorTheme tColors = Theme.of(context).extension<TaskwarriorColorTheme>()!;
FilePicker.platform.getDirectoryPath().then((value) async {
if (value != null) {
isMovingDirectory.value = true;
update();
// InheritedProfiles profilesWidget = ProfilesWidget.of(context);
var profilesWidget = Get.find<SplashController>();
Directory source = profilesWidget.baseDirectory();
Directory destination = Directory(value);
moveDirectory(source.path, destination.path).then((value) async {
isMovingDirectory.value = false;
update();
if (value == "same") {
return;
} else if (value == "success") {
profilesWidget.setBaseDirectory(destination);
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('baseDirectory', destination.path);
baseDirectory.value = destination.path;
Get.snackbar(
'Success',
'Base directory moved successfully',
snackPosition: SnackPosition.BOTTOM,
duration: const Duration(seconds: 2),
);
} else {
Get.dialog(
Utils.showAlertDialog(
title: Text(
'Error',
style: GoogleFonts.poppins(
fontWeight: FontWeight.bold,
fontSize: TaskWarriorFonts.fontSizeMedium,
color: tColors.primaryTextColor,
),
),
content: Text(
value == "nested"
? "Cannot move to a nested directory"
: value == "not-empty"
? "Destination directory is not empty"
: value == "not-permitted"
? "Selected folder can't be written to (Android SAF). Please choose a different folder."
: "An error occurred",
style: GoogleFonts.poppins(
color: TaskWarriorColors.grey,
fontSize: TaskWarriorFonts.fontSizeSmall,
),
),
actions: [
TextButton(
onPressed: () {
Get.back();
},
child: Text(
'OK',
style: GoogleFonts.poppins(
color: tColors.primaryTextColor,
),
),
)
],
),
);
}
});
}
});
}
Future<String> moveDirectory(String fromDirectory, String toDirectory) async {
if (path.canonicalize(fromDirectory) == path.canonicalize(toDirectory)) {
return "same";
}
if (path.isWithin(fromDirectory, toDirectory)) {
return "nested";
}
Directory toDir = Directory(toDirectory);
// Ensure destination exists before checking contents
await toDir.create(recursive: true);
final length = await toDir.list().length;
if (length > 0) {
return "not-empty";
}
// Preflight: on Android, check that we can actually write to the chosen directory
// to avoid crashing with Operation not permitted when a SAF tree URI was selected.
try {
final testFile = File(path.join(toDirectory, ".tw_write_test"));
await toDir.create(recursive: true);
await testFile.writeAsString("ok");
await testFile.delete();
} on FileSystemException catch (e) {
// Map common permission error to a friendly status
if (e.osError?.errorCode == 1 ||
(e.osError?.message.toLowerCase().contains("operation not permitted") ?? false)) {
return "not-permitted";
}
return "error";
} catch (_) {
return "error";
}
try {
await moveDirectoryRecurse(fromDirectory, toDirectory);
return "success";
} on FileSystemException catch (e) {
if (e.osError?.errorCode == 1 ||
(e.osError?.message.toLowerCase().contains("operation not permitted") ?? false)) {
return "not-permitted";
}
return "error";
} catch (_) {
return "error";
}
}
// ... no hardcoded SAF path mapping; rely on guard and proper APIs if enabled in future
Future<void> moveDirectoryRecurse(
String fromDirectory, String toDirectory) async {
Directory fromDir = Directory(fromDirectory);
Directory toDir = Directory(toDirectory);
// Create the toDirectory if it doesn't exist
await toDir.create(recursive: true);
// Loop through each file and directory and move it to the toDirectory
await for (final entity in fromDir.list()) {
// Skip flutter runtime assets – they should not be moved
final relativePath = path.relative(entity.path, from: fromDirectory);
if (relativePath.split(path.separator).contains('flutter_assets')) {
continue;
}
if (entity is File) {
// If it's a file, move it to the toDirectory
File file = entity;
String newPath = path.join(toDirectory, relativePath);
await File(newPath).writeAsBytes(await file.readAsBytes());
await file.delete();
} else if (entity is Directory) {
// If it's a directory, create it in the toDirectory and recursively move its contents
Directory dir = entity;
String newPath = path.join(toDirectory, relativePath);
Directory newDir = Directory(newPath);
await newDir.create(recursive: true);
await moveDirectoryRecurse(dir.path, newPath);
await dir.delete();
}
}
}
RxBool isSyncOnStartActivel = false.obs;
RxBool isSyncOnTaskCreateActivel = false.obs;
RxBool delaytask = false.obs;
RxBool taskchampion = false.obs;
RxBool isDarkModeOn = false.obs;
void initDarkMode() {
isDarkModeOn.value = AppSettings.isDarkMode;
}
@override
void onInit() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
isSyncOnStartActivel.value = prefs.getBool('sync-onStart') ?? false;
isSyncOnTaskCreateActivel.value =
prefs.getBool('sync-OnTaskCreate') ?? false;
delaytask.value = prefs.getBool('delaytask') ?? false;
taskchampion.value = prefs.getBool('settings_taskc') ?? false;
initDarkMode();
baseDirectory.value = await getBaseDirectory();
super.onInit();
}
}