1
0
mirror of https://github.com/go-vikunja/app synced 2024-11-08 05:50:00 +00:00
app-mirror-github/lib/pages/settings.dart

257 lines
9.6 KiB
Dart
Raw Permalink Normal View History

2022-04-12 22:32:21 +00:00
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
2022-04-12 22:32:21 +00:00
import 'package:vikunja_app/global.dart';
import 'package:collection/collection.dart';
2022-04-12 22:32:21 +00:00
2023-05-17 10:52:26 +00:00
import '../main.dart';
import '../models/project.dart';
2023-12-23 21:38:16 +00:00
import '../models/user.dart';
import '../service/services.dart';
2023-05-17 10:52:26 +00:00
2022-04-12 22:32:21 +00:00
class SettingsPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => SettingsPageState();
}
class SettingsPageState extends State<SettingsPage> {
List<Project>? projectList;
int? defaultProject;
2022-08-27 21:04:43 +00:00
bool? ignoreCertificates;
bool? sentryEnabled;
bool? getVersionNotifications;
String? versionTag, newestVersionTag;
2023-01-07 15:55:40 +00:00
late TextEditingController durationTextController;
bool initialized = false;
FlutterThemeMode? themeMode;
2023-12-23 21:38:16 +00:00
User? currentUser;
2023-05-17 10:52:26 +00:00
2023-01-07 15:55:40 +00:00
void init() {
durationTextController = TextEditingController();
VikunjaGlobal.of(context)
.projectService
2023-01-07 15:55:40 +00:00
.getAll()
.then((value) => setState(() => projectList = value));
2023-01-07 15:55:40 +00:00
VikunjaGlobal.of(context).settingsManager.getIgnoreCertificates().then(
(value) =>
setState(() => ignoreCertificates = value == "1" ? true : false));
VikunjaGlobal.of(context)
.settingsManager
.getSentryEnabled()
.then((value) => setState(() => sentryEnabled = value));
2023-01-07 15:55:40 +00:00
VikunjaGlobal.of(context).settingsManager.getVersionNotifications().then(
(value) => setState(
() => getVersionNotifications = value == "1" ? true : false));
2023-01-07 15:55:40 +00:00
VikunjaGlobal.of(context)
.versionChecker
.getCurrentVersionTag()
.then((value) => setState(() => versionTag = value));
VikunjaGlobal.of(context).settingsManager.getWorkmanagerDuration().then(
(value) => setState(
() => durationTextController.text = (value.inMinutes.toString())));
2023-01-07 15:55:40 +00:00
VikunjaGlobal.of(context)
.settingsManager
.getThemeMode()
.then((value) => setState(() => themeMode = value));
2023-05-17 10:52:26 +00:00
2023-12-23 21:38:16 +00:00
VikunjaGlobal.of(context).newUserService?.getCurrentUser().then((value) => {
setState(() {
currentUser = value!;
defaultProject = value.settings?.default_project_id;
}),
});
2023-12-23 21:38:16 +00:00
2023-01-07 15:55:40 +00:00
initialized = true;
}
2023-01-07 15:55:40 +00:00
@override
Widget build(BuildContext context) {
2023-12-23 21:38:16 +00:00
final global = VikunjaGlobal.of(context);
2023-01-07 15:55:40 +00:00
if (!initialized) init();
2022-04-12 22:32:21 +00:00
return new Scaffold(
appBar: AppBar(
title: Text("Settings"),
),
body: ListView(
2022-04-12 22:32:21 +00:00
children: [
UserAccountsDrawerHeader(
accountName:
currentUser != null ? Text(currentUser!.username) : null,
2023-12-23 21:38:16 +00:00
accountEmail: currentUser != null ? Text(currentUser!.name) : null,
currentAccountPicture: currentUser == null
? null
: CircleAvatar(
backgroundImage: (currentUser?.username != "")
? NetworkImage(currentUser!.avatarUrl(context))
: null,
),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/graphics/hypnotize.png"),
repeat: ImageRepeat.repeat,
colorFilter: ColorFilter.mode(
Theme.of(context).primaryColor, BlendMode.multiply)),
),
),
projectList != null
? ListTile(
title: Text("Default List"),
trailing: DropdownButton<int>(
items: [
DropdownMenuItem(
child: Text("None"),
value: null,
),
...projectList!
.map((e) => DropdownMenuItem(
child: Text(e.title), value: e.id))
.toList()
],
value: projectList?.firstWhereOrNull(
(element) => element.id == defaultProject) !=
null
? defaultProject
: null,
onChanged: (int? value) {
setState(() => defaultProject = value);
global.newUserService
?.setCurrentUserSettings(currentUser!.settings!
.copyWith(default_project_id: value))
.then((value) => currentUser!.settings = value);
2023-12-23 21:38:16 +00:00
//VikunjaGlobal.of(context).userManager.setDefaultList(value);
},
),
)
: ListTile(
title: Text("..."),
),
Divider(),
2023-05-17 10:52:26 +00:00
ListTile(
title: Text("Theme"),
trailing: DropdownButton<FlutterThemeMode>(
2023-05-17 10:52:26 +00:00
items: [
DropdownMenuItem(
child: Text("System"),
value: FlutterThemeMode.system,
2023-05-17 10:52:26 +00:00
),
DropdownMenuItem(
child: Text("Light"),
value: FlutterThemeMode.light,
2023-05-17 10:52:26 +00:00
),
DropdownMenuItem(
child: Text("Dark"),
value: FlutterThemeMode.dark,
),
DropdownMenuItem(
child: Text("Material You Light"),
value: FlutterThemeMode.materialYouLight,
),
DropdownMenuItem(
child: Text("Material You Dark"),
value: FlutterThemeMode.materialYouDark,
2023-05-17 10:52:26 +00:00
),
],
value: themeMode,
onChanged: (FlutterThemeMode? value) {
VikunjaGlobal.of(context).settingsManager.setThemeMode(value!);
2023-05-17 10:52:26 +00:00
setState(() => themeMode = value);
if (themeMode != null) themeModel.themeMode = themeMode!;
2023-05-17 10:52:26 +00:00
},
),
),
Divider(),
ignoreCertificates != null
? CheckboxListTile(
title: Text("Ignore Certificates"),
value: ignoreCertificates,
onChanged: (value) {
setState(() => ignoreCertificates = value);
VikunjaGlobal.of(context).client.reloadIgnoreCerts(value);
})
: ListTile(title: Text("...")),
Divider(),
sentryEnabled != null
? CheckboxListTile(
title: Text("Enable Sentry"),
subtitle: Text(
"Help us debug errors better and faster by sending bug reports to us directly. This is completely anonymous."),
value: sentryEnabled,
onChanged: (value) {
if (value == null) return;
setState(() => sentryEnabled = value);
VikunjaGlobal.of(context)
.settingsManager
.setSentryEnabled(value)
.then((_) => themeModel.notify());
})
: ListTile(title: Text("...")),
Divider(),
Padding(
padding: EdgeInsets.only(left: 15, right: 15),
2023-01-07 15:55:40 +00:00
child: Row(children: [
Flexible(
child: TextField(
controller: durationTextController,
decoration: InputDecoration(
labelText: 'Background Refresh Interval (minutes): ',
helperText: 'Minimum: 15, Set limit of 0 for no refresh',
),
)),
TextButton(
onPressed: () => VikunjaGlobal.of(context)
.settingsManager
.setWorkmanagerDuration(Duration(
minutes: int.parse(durationTextController.text)))
.then((value) => VikunjaGlobal.of(context)
.updateWorkmanagerDuration()),
child: Text("Save")),
])),
Divider(),
getVersionNotifications != null
? CheckboxListTile(
title: Text("Get Version Notifications"),
value: getVersionNotifications,
onChanged: (value) {
setState(() => getVersionNotifications = value);
2023-01-07 15:55:40 +00:00
if (value != null)
VikunjaGlobal.of(context)
.settingsManager
.setVersionNotifications(value);
})
: ListTile(title: Text("...")),
2023-01-07 15:55:40 +00:00
TextButton(
onPressed: () async {
await Permission.notification.isDenied.then((value) {
if (value) {
Permission.notification.request();
}
});
VikunjaGlobal.of(context).notifications.sendTestNotification();
2023-01-07 15:55:40 +00:00
},
child: Text("Send test notification")),
TextButton(
onPressed: () => VikunjaGlobal.of(context)
.versionChecker
.getLatestVersionTag()
2023-01-07 22:00:31 +00:00
.then((value) => setState(() => newestVersionTag = value)),
child: Text("Check for latest version")),
Text("Current version: ${versionTag ?? "loading"}"),
Text(newestVersionTag != null
? "Latest version: $newestVersionTag"
: ""),
Divider(),
TextButton(
2023-05-16 23:22:59 +00:00
onPressed: () => VikunjaGlobal.of(context).logoutUser(context),
child: Text("Logout")),
2022-04-12 22:32:21 +00:00
],
),
);
}
}