1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00
comunicmobile/lib/ui/routes/settings/account_image_settings.dart

209 lines
6.2 KiB
Dart
Raw Normal View History

2021-03-14 17:15:38 +00:00
import 'package:comunic/helpers/serialization/user_list_serialization_helper.dart';
2020-04-16 17:16:44 +00:00
import 'package:comunic/helpers/settings_helper.dart';
import 'package:comunic/models/account_image_settings.dart';
2021-02-12 21:45:17 +00:00
import 'package:comunic/ui/dialogs/multi_choices_dialog.dart';
2020-04-16 17:16:44 +00:00
import 'package:comunic/ui/widgets/async_screen_widget.dart';
import 'package:comunic/ui/widgets/network_image_widget.dart';
2021-02-12 21:36:22 +00:00
import 'package:comunic/ui/widgets/settings/header_spacer_section.dart';
2021-02-12 21:45:17 +00:00
import 'package:comunic/ui/widgets/settings/multi_choices_settings_tile.dart';
import 'package:comunic/utils/account_utils.dart';
2020-04-16 17:16:44 +00:00
import 'package:comunic/utils/files_utils.dart';
2021-12-28 15:34:14 +00:00
import 'package:comunic/utils/identicon_utils.dart';
2020-04-16 14:33:44 +00:00
import 'package:comunic/utils/intl_utils.dart';
2020-04-16 17:16:44 +00:00
import 'package:comunic/utils/ui_utils.dart';
2020-04-16 14:33:44 +00:00
import 'package:flutter/material.dart';
2021-12-28 14:23:08 +00:00
import 'package:flutter_settings_ui/flutter_settings_ui.dart';
2021-03-13 17:11:28 +00:00
import 'package:image_cropper/image_cropper.dart';
2020-04-16 14:33:44 +00:00
2021-03-13 17:03:20 +00:00
import '../../../utils/log_utils.dart';
import '../../../utils/ui_utils.dart';
2020-04-16 14:33:44 +00:00
/// Account image settings section
///
/// @author Pierre Hubert
2020-05-13 16:12:45 +00:00
class AccountImageSettingsScreen extends StatefulWidget {
2020-04-16 14:33:44 +00:00
@override
2020-05-13 16:12:45 +00:00
_AccountImageSettingsScreenState createState() =>
_AccountImageSettingsScreenState();
2020-04-16 14:33:44 +00:00
}
2020-05-13 16:12:45 +00:00
class _AccountImageSettingsScreenState
extends State<AccountImageSettingsScreen> {
late AccountImageSettings _settings;
2020-04-16 17:16:44 +00:00
final _key = GlobalKey<AsyncScreenWidgetState>();
2020-04-16 14:33:44 +00:00
@override
void dispose() {
// Remove current user information to force refresh of account image
2021-03-14 17:15:38 +00:00
UsersListSerialisationHelper().removeUserByID(userID());
super.dispose();
}
@override
2020-04-16 14:33:44 +00:00
Widget build(BuildContext context) {
2020-04-16 17:16:44 +00:00
return AsyncScreenWidget(
key: _key,
onReload: () async =>
_settings = await SettingsHelper.getAccountImageSettings(),
onBuild: () => _buildLayout(),
errorMessage: tr("Could not get account image settings!")!,
2020-04-16 17:16:44 +00:00
);
}
Widget _buildLayout() {
return SettingsList(
sections: [
2021-02-12 21:36:22 +00:00
HeadSpacerSection(),
2020-04-16 17:16:44 +00:00
SettingsSection(
title: tr("General"),
tiles: _settings.hasImage
? _buildHasAccountImageTiles()
: _buildNoAccountImageTiles(),
)
],
);
}
/// When user has no account image yet
2020-04-16 17:36:34 +00:00
List<SettingsTile> _buildNoAccountImageTiles() {
return [
SettingsTile(
title: tr("No account image yet..."),
leading: NetworkImageWidget(
url: _settings.imageURL,
width: 40,
),
),
2020-04-17 06:44:57 +00:00
2020-04-16 17:36:34 +00:00
// Upload new account image
SettingsTile(
title: tr("Upload an account image"),
2021-02-07 16:09:08 +00:00
onPressed: (_) => _uploadAccountImage(),
2020-04-16 17:36:34 +00:00
),
2020-04-17 06:44:57 +00:00
// Generate a random account image
SettingsTile(
title: tr("Generate a random account image"),
2021-02-07 16:09:08 +00:00
onPressed: (_) => _generateRandomAccountImage(),
2020-04-17 06:44:57 +00:00
),
2020-04-16 17:36:34 +00:00
];
}
2020-04-16 17:16:44 +00:00
2021-02-12 21:45:17 +00:00
List<MultiChoiceEntry<AccountImageVisibilityLevels>> get _visibilityLevels =>
[
MultiChoiceEntry(
id: AccountImageVisibilityLevels.EVERYONE,
title: tr("Everyone")!,
2021-02-12 21:45:17 +00:00
subtitle: tr(
"Your account image is visible by everyone, including users external to Comunic."),
),
MultiChoiceEntry(
id: AccountImageVisibilityLevels.COMUNIC_USERS,
title: tr("Connected users")!,
2021-02-12 21:45:17 +00:00
subtitle: tr(
"Your account image is visible only to connected Comunic users."),
),
MultiChoiceEntry(
id: AccountImageVisibilityLevels.FRIENDS_ONLY,
title: tr("My friends")!,
2021-02-12 21:45:17 +00:00
subtitle: tr("Your account image is visible only by your friends."),
),
];
2020-04-16 17:16:44 +00:00
/// When the user has an account image
List<SettingsTile> _buildHasAccountImageTiles() {
return [
// Current account image (if any)
SettingsTile(
title: tr("Current account image"),
leading: NetworkImageWidget(
url: _settings.imageURL,
width: 40,
),
),
// Upload new account image
2020-04-16 17:16:44 +00:00
SettingsTile(
title: tr("Upload new account image"),
2021-02-07 16:09:08 +00:00
onPressed: (_) => _uploadAccountImage(),
2020-04-16 17:16:44 +00:00
),
2020-04-17 06:44:57 +00:00
// Generate a random account image
SettingsTile(
title: tr("Generate a random account image"),
2021-02-07 16:09:08 +00:00
onPressed: (_) => _generateRandomAccountImage(),
2020-04-17 06:44:57 +00:00
),
// Change account image visibility
2021-02-12 21:45:17 +00:00
MultiChoicesSettingsTile(
title: tr("Account image visibility")!,
2021-02-12 21:45:17 +00:00
choices: _visibilityLevels,
currentValue: _settings.visibility,
onChanged: (dynamic newLevel) async {
2021-02-12 21:45:17 +00:00
if (!await SettingsHelper.setAccountImageVisibilityLevel(newLevel))
showSimpleSnack(context,
tr("Could not update account image visibility level!")!);
_key.currentState!.refresh();
2021-02-12 21:45:17 +00:00
}),
2020-04-16 17:34:55 +00:00
// Delete account image
SettingsTile(
title: tr("Delete account image"),
2021-02-07 16:09:08 +00:00
onPressed: (_) => _deleteAccountImage(),
2020-04-16 17:34:55 +00:00
),
2020-04-16 17:16:44 +00:00
];
}
/// Upload a new account image
void _uploadAccountImage() async {
2021-04-17 16:33:08 +00:00
await uploadNewAccountImage(context);
_key.currentState!.refresh();
2020-04-16 14:33:44 +00:00
}
2020-04-17 06:44:57 +00:00
/// Generate a random account image
void _generateRandomAccountImage() async {
// Generate emoticon
2021-12-28 15:34:14 +00:00
final bytes = await genIdenticon(context);
2020-04-17 06:44:57 +00:00
if (!await SettingsHelper.uploadAccountImageFromMemory(bytes)) {
showSimpleSnack(
context, tr("Could not upload your generated account image!")!);
2020-04-17 06:44:57 +00:00
return;
}
_key.currentState!.refresh();
2020-04-17 06:44:57 +00:00
}
2020-04-16 17:34:55 +00:00
/// Delete user account image
void _deleteAccountImage() async {
if (!await showConfirmDialog(
context: context,
message: tr("Do you really want to delete your account image ?")))
return;
if (!await SettingsHelper.deleteAccountImage()) {
showSimpleSnack(context, tr("Could not delete user account image!")!);
2020-04-16 17:34:55 +00:00
return;
}
_key.currentState!.refresh();
2020-04-16 17:34:55 +00:00
}
2020-04-16 14:33:44 +00:00
}
2021-04-17 16:33:08 +00:00
Future<void> uploadNewAccountImage(BuildContext context) async {
try {
final image = await pickImage(context,
aspectRatio: CropAspectRatio(ratioX: 5, ratioY: 5));
if (image == null) return;
await SettingsHelper.uploadAccountImage(image);
} catch (e, s) {
logError(e, s);
snack(context, tr("Failed to upload new account image!")!);
2021-04-17 16:33:08 +00:00
}
}