-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathsettings_controller.dart
189 lines (168 loc) · 6.78 KB
/
settings_controller.dart
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
// 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';
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) {
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;
} else {
showDialog(
context: context,
builder: (BuildContext context) {
return Utils.showAlertDialog(
title: Text(
'Error',
style: GoogleFonts.poppins(
fontWeight: FontWeight.bold,
fontSize: TaskWarriorFonts.fontSizeMedium,
color: AppSettings.isDarkMode
? TaskWarriorColors.white
: TaskWarriorColors.black,
),
),
content: Text(
value == "nested"
? "Cannot move to a nested directory"
: value == "not-empty"
? "Destination directory is not empty"
: "An error occurred",
style: GoogleFonts.poppins(
color: TaskWarriorColors.grey,
fontSize: TaskWarriorFonts.fontSizeSmall,
),
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(
'OK',
style: GoogleFonts.poppins(
color: AppSettings.isDarkMode
? TaskWarriorColors.white
: TaskWarriorColors.black,
),
),
)
],
);
},
);
}
});
}
});
}
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);
final length = await toDir.list().length;
if (length > 0) {
return "not-empty";
}
await moveDirectoryRecurse(fromDirectory, toDirectory);
return "success";
}
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()) {
if (entity is File) {
// If it's a file, move it to the toDirectory
File file = entity;
String newPath = path.join(
toDirectory, path.relative(file.path, from: fromDirectory));
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, path.relative(dir.path, from: fromDirectory));
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 change24hr = 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;
change24hr.value = prefs.getBool('24hourformate') ?? false;
taskchampion.value = prefs.getBool('settings_taskc') ?? false;
initDarkMode();
baseDirectory.value = await getBaseDirectory();
super.onInit();
}
}