mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 08:15:16 +00:00
Start to integrate data conservation policy
This commit is contained in:
@ -1,6 +1,13 @@
|
||||
import 'package:comunic/helpers/account_helper.dart';
|
||||
import 'package:comunic/helpers/server_config_helper.dart';
|
||||
import 'package:comunic/helpers/settings_helper.dart';
|
||||
import 'package:comunic/models/data_conservation_policy_settings.dart';
|
||||
import 'package:comunic/models/server_config.dart';
|
||||
import 'package:comunic/ui/dialogs/input_user_password_dialog.dart';
|
||||
import 'package:comunic/ui/dialogs/multi_choices_dialog.dart';
|
||||
import 'package:comunic/ui/widgets/async_screen_widget.dart';
|
||||
import 'package:comunic/ui/widgets/settings/header_spacer_section.dart';
|
||||
import 'package:comunic/ui/widgets/settings/multi_choices_settings_tile.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:comunic/utils/ui_utils.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
@ -17,19 +24,68 @@ class AccountPrivacySettings extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _AccountPrivacySettingsState extends State<AccountPrivacySettings> {
|
||||
final _key = GlobalKey<AsyncScreenWidgetState>();
|
||||
ServerConfig _serverConfig;
|
||||
DataConservationPolicySettings _userSettings;
|
||||
String _cachedPassword;
|
||||
|
||||
Future<void> _loadSettings() async {
|
||||
await ServerConfigurationHelper.ensureLoaded();
|
||||
_serverConfig = ServerConfigurationHelper.config;
|
||||
_userSettings = await SettingsHelper.getDataConservationPolicy();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SettingsList(sections: [
|
||||
HeadSpacerSection(),
|
||||
SettingsSection(title: tr("Privacy settings"), tiles: [
|
||||
SettingsTile(
|
||||
title: tr("Delete your account"),
|
||||
subtitle:
|
||||
tr("Permanently delete your account and all data related to it."),
|
||||
onPressed: (_) => _deleteAccount(),
|
||||
)
|
||||
])
|
||||
]);
|
||||
return AsyncScreenWidget(
|
||||
key: _key,
|
||||
onReload: _loadSettings,
|
||||
onBuild: () => SettingsList(sections: [
|
||||
HeadSpacerSection(),
|
||||
SettingsSection(
|
||||
title: tr("Data conservation policy"),
|
||||
tiles: _dataConservationPolicyTiles,
|
||||
),
|
||||
SettingsSection(title: tr("Danger zone"), tiles: [
|
||||
SettingsTile(
|
||||
title: tr("Delete your account"),
|
||||
subtitle: tr(
|
||||
"Permanently delete your account and all data related to it."),
|
||||
onPressed: (_) => _deleteAccount(),
|
||||
)
|
||||
])
|
||||
]),
|
||||
errorMessage: tr("Failed to load privacy settings!"),
|
||||
);
|
||||
}
|
||||
|
||||
List<SettingsTile> get _dataConservationPolicyTiles => [
|
||||
DataConservationPolicyTile(
|
||||
value: _userSettings.notificationLifetime,
|
||||
title: tr("Automatically delete unread notification after"),
|
||||
onChange: (val) {
|
||||
_userSettings.notificationLifetime = val;
|
||||
_updateDataConservationPolicy();
|
||||
},
|
||||
minValue:
|
||||
_serverConfig.dataConservationPolicy.minNotificationLifetime,
|
||||
),
|
||||
];
|
||||
|
||||
void _updateDataConservationPolicy() async {
|
||||
try {
|
||||
if (_cachedPassword == null)
|
||||
_cachedPassword = await showUserPasswordDialog(context);
|
||||
|
||||
await SettingsHelper.setDataConservationPolicy(
|
||||
_cachedPassword, _userSettings);
|
||||
|
||||
_key.currentState.refresh();
|
||||
} catch (e, s) {
|
||||
print("Could not update data conservation policy! $e\n$s");
|
||||
showSimpleSnack(
|
||||
context, tr("Failed to update data conservation policy!"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Permanently delete user account
|
||||
@ -61,6 +117,98 @@ class _AccountPrivacySettingsState extends State<AccountPrivacySettings> {
|
||||
}
|
||||
}
|
||||
|
||||
class DataConservationPolicyTile extends SettingsTile {
|
||||
final int value;
|
||||
final String title;
|
||||
final Function(int) onChange;
|
||||
final int minValue;
|
||||
|
||||
const DataConservationPolicyTile({
|
||||
@required this.value,
|
||||
@required this.title,
|
||||
@required this.onChange,
|
||||
@required this.minValue,
|
||||
}) : assert(title != null),
|
||||
assert(onChange != null),
|
||||
assert(minValue != null);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MultiChoicesSettingsTile(
|
||||
title: title,
|
||||
choices: _choices,
|
||||
currentValue: _roundValue,
|
||||
onChanged: onChange,
|
||||
);
|
||||
}
|
||||
|
||||
int get _day => 60 * 60 * 24;
|
||||
|
||||
int get _month => _day * 30;
|
||||
|
||||
int get _year => _day * 365;
|
||||
|
||||
int get _roundValue {
|
||||
if (this.value == null) return 0;
|
||||
|
||||
return _choices.firstWhere((element) => element.id >= this.value).id;
|
||||
}
|
||||
|
||||
List<MultiChoiceEntry<int>> get _choices => [
|
||||
MultiChoiceEntry(id: 0, title: tr("Never"), hidden: false),
|
||||
MultiChoiceEntry(
|
||||
id: _day * 7,
|
||||
title: tr("7 days"),
|
||||
hidden: _day * 7 < minValue,
|
||||
),
|
||||
MultiChoiceEntry(
|
||||
id: _day * 15,
|
||||
title: tr("15 days"),
|
||||
hidden: _day * 15 < minValue,
|
||||
),
|
||||
MultiChoiceEntry(
|
||||
id: _month,
|
||||
title: tr("1 month"),
|
||||
hidden: _month < minValue,
|
||||
),
|
||||
MultiChoiceEntry(
|
||||
id: _month * 3,
|
||||
title: tr("3 months"),
|
||||
hidden: _month * 3 < minValue,
|
||||
),
|
||||
MultiChoiceEntry(
|
||||
id: _month * 6,
|
||||
title: tr("6 months"),
|
||||
hidden: _month * 6 < minValue,
|
||||
),
|
||||
MultiChoiceEntry(
|
||||
id: _year,
|
||||
title: tr("1 year"),
|
||||
hidden: _year < minValue,
|
||||
),
|
||||
MultiChoiceEntry(
|
||||
id: _year * 2,
|
||||
title: tr("2 years"),
|
||||
hidden: _year * 5 < minValue,
|
||||
),
|
||||
MultiChoiceEntry(
|
||||
id: _year * 5,
|
||||
title: tr("5 years"),
|
||||
hidden: _year * 5 < minValue,
|
||||
),
|
||||
MultiChoiceEntry(
|
||||
id: _year * 10,
|
||||
title: tr("10 years"),
|
||||
hidden: _year * 10 < minValue,
|
||||
),
|
||||
MultiChoiceEntry(
|
||||
id: _year * 50,
|
||||
title: tr("50 years"),
|
||||
hidden: _year * 50 < minValue,
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
class _LastChanceDeleteAccountDialog extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
Reference in New Issue
Block a user