1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-29 06:18:51 +00:00
comunicmobile/lib/ui/routes/account_settings/general_account_settings.dart

250 lines
6.9 KiB
Dart
Raw Normal View History

2020-04-27 16:44:41 +00:00
import 'package:comunic/enums/user_page_visibility.dart';
2020-04-27 11:34:25 +00:00
import 'package:comunic/helpers/database/users_database_helper.dart';
2020-04-27 11:27:37 +00:00
import 'package:comunic/helpers/settings_helper.dart';
import 'package:comunic/models/general_settings.dart';
2020-04-27 16:44:41 +00:00
import 'package:comunic/ui/dialogs/multi_choices_dialog.dart';
2020-04-28 15:52:22 +00:00
import 'package:comunic/ui/dialogs/virtual_directory_dialog.dart';
2020-04-27 11:27:37 +00:00
import 'package:comunic/ui/widgets/async_screen_widget.dart';
import 'package:comunic/ui/widgets/settings/text_settings_edit_tile.dart';
import 'package:comunic/utils/account_utils.dart';
2020-04-27 17:05:36 +00:00
import 'package:comunic/utils/input_utils.dart';
2020-04-27 11:27:37 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
import 'package:settings_ui/settings_ui.dart';
/// General account settings
///
/// @author Pierre HUBERT
class GeneralAccountSettingsScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("General settings"),
),
body: _GeneralAccountSettingsBody(),
);
}
}
class _GeneralAccountSettingsBody extends StatefulWidget {
@override
__GeneralAccountSettingsBodyState createState() =>
__GeneralAccountSettingsBodyState();
}
class __GeneralAccountSettingsBodyState
extends State<_GeneralAccountSettingsBody> {
GeneralSettings _settings;
final _key = GlobalKey<AsyncScreenWidgetState>();
2020-04-27 11:34:25 +00:00
@override
void dispose() {
// Remove current user information to force refresh of account image
UsersDatabaseHelper().delete(userID());
super.dispose();
}
2020-04-27 11:27:37 +00:00
@override
Widget build(BuildContext context) {
return AsyncScreenWidget(
key: _key,
onReload: () async =>
_settings = await SettingsHelper.getGeneralSettings(),
onBuild: _buildSettings,
errorMessage: tr("Could not load general settings!"),
showOldDataWhileUpdating: true,
);
2020-04-27 11:27:37 +00:00
}
Widget _buildSettings() {
return SettingsList(
sections: [
SettingsSection(
title: tr("Main account information"),
tiles: _mainInformationTiles(),
2020-04-27 16:44:41 +00:00
),
SettingsSection(
title: tr("Your page settings"),
tiles: _pageSettingsTiles(),
2020-04-27 11:27:37 +00:00
)
],
);
}
List<SettingsTile> _mainInformationTiles() {
return [
SettingsTile(
title: tr("User ID"),
subtitle: "${userID()}",
),
SettingsTile(
title: tr("Email address"),
subtitle: _settings.email,
),
// First name
TextEditSettingsTile(
title: tr("First name"),
currValue: _settings.firstName,
onChanged: (s) {
_settings.firstName = s;
_updateSettings();
},
2020-04-27 11:34:25 +00:00
checkInput: (s) => s.length >= 3,
2020-04-27 11:27:37 +00:00
),
// Last name
TextEditSettingsTile(
title: tr("Last name"),
currValue: _settings.lastName,
onChanged: (s) {
_settings.lastName = s;
_updateSettings();
},
2020-04-27 11:34:25 +00:00
checkInput: (s) => s.length >= 3,
2020-04-27 11:27:37 +00:00
),
2020-04-27 11:37:43 +00:00
// Emails settings
SettingsTile.switchTile(
title: tr("Allow comunic to send emails"),
onToggle: (s) {
_settings.allowComunicEmails = s;
_updateSettings();
},
switchValue: _settings.allowComunicEmails,
),
2020-04-27 11:27:37 +00:00
];
}
2020-04-27 16:44:41 +00:00
List<MultiChoiceEntry> get _visibilityChoices => [
MultiChoiceEntry(
id: UserPageVisibility.PRIVATE,
title: tr("Private"),
subtitle: tr("Private, accessible only to your friends")),
MultiChoiceEntry(
id: UserPageVisibility.PUBLIC,
title: tr("Public"),
subtitle: tr("Public, accessible to all Comunic members")),
MultiChoiceEntry(
id: UserPageVisibility.OPEN,
title: tr("Open"),
subtitle:
tr("Accessible to everyone, including non-Comunic users")),
];
/// Build page settings tile
List<SettingsTile> _pageSettingsTiles() {
return [
// Page visibility
SettingsTile(
title: tr("Page visibility"),
subtitle: _visibilityChoices
.firstWhere((f) => f.id == _settings.pageVisibility)
.subtitle,
onTap: () async {
final choice = await showMultiChoicesDialog(
context: context,
choices: _visibilityChoices,
defaultChoice: _settings.pageVisibility,
title: tr("Change your page visibility"),
);
if (choice == null) return;
_settings.pageVisibility = choice;
_updateSettings();
},
),
2020-04-27 16:51:54 +00:00
// Allow comments on user page ?
SettingsTile.switchTile(
title: tr("Allow comments on your page"),
onToggle: (v) {
_settings.allowComments = v;
_updateSettings();
},
switchValue: _settings.allowComments,
),
// Allow posts from friends
SettingsTile.switchTile(
title: tr("Allow posts from your friends on your page"),
onToggle: (v) {
_settings.allowPostsFromFriends = v;
_updateSettings();
},
switchValue: _settings.allowPostsFromFriends,
),
// Make friends list public
SettingsTile.switchTile(
title: tr("Make your friends list public"),
onToggle: (v) {
_settings.publicFriendsList = v;
_updateSettings();
},
switchValue: _settings.publicFriendsList,
),
2020-04-27 17:05:36 +00:00
// Personal website
TextEditSettingsTile(
2020-04-27 17:21:30 +00:00
title: tr("Personal website URL (optional)"),
2020-04-27 17:05:36 +00:00
currValue: _settings.personalWebsite,
onChanged: (v) {
_settings.personalWebsite = v;
_updateSettings();
},
checkInput: (s) => validateUrl(s),
allowEmptyValues: true,
2020-04-27 17:21:30 +00:00
),
// Public notes
TextEditSettingsTile(
title: tr("Public note (optional)"),
currValue: _settings.publicNote,
onChanged: (v) {
_settings.publicNote = v;
_updateSettings();
},
allowEmptyValues: true,
maxLength: 255,
maxLines: 3,
),
2020-04-28 15:52:22 +00:00
// Virtual directory
SettingsTile(
title: tr("Virtual directory (optional)"),
subtitle: _settings.virtualDirectory,
onTap: () async {
final dir = await showVirtualDirectoryDialog(
context: context,
initialDirectory: _settings.virtualDirectory,
type: VirtualDirectoryTargetType.USER,
id: userID());
if (dir == null) return;
_settings.virtualDirectory = dir;
_updateSettings();
},
),
2020-04-27 16:44:41 +00:00
];
}
2020-04-27 11:27:37 +00:00
/// Apply new settings
Future<void> _updateSettings() async {
try {
await SettingsHelper.updateGeneralSettings(_settings);
} catch (e, stack) {
print("Error while updating settings! $e/n$stack");
showSimpleSnack(context, tr("Could not update general settings!"));
}
_key.currentState.refresh();
}
}