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';
|
2020-04-17 10:00:42 +00:00
|
|
|
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> {
|
2022-03-10 18:39:57 +00:00
|
|
|
late AccountImageSettings _settings;
|
2020-04-16 17:16:44 +00:00
|
|
|
|
|
|
|
final _key = GlobalKey<AsyncScreenWidgetState>();
|
|
|
|
|
2020-04-16 14:33:44 +00:00
|
|
|
@override
|
2020-04-17 10:00:42 +00:00
|
|
|
void dispose() {
|
|
|
|
// Remove current user information to force refresh of account image
|
2021-03-14 17:15:38 +00:00
|
|
|
UsersListSerialisationHelper().removeUserByID(userID());
|
2020-04-17 10:00:42 +00:00
|
|
|
|
|
|
|
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(),
|
2022-03-10 18:39:57 +00:00
|
|
|
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,
|
2022-03-10 18:39:57 +00:00
|
|
|
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,
|
2022-03-10 18:39:57 +00:00
|
|
|
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,
|
2022-03-10 18:39:57 +00:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
),
|
2020-04-16 17:30:25 +00:00
|
|
|
|
|
|
|
// 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-16 17:30:25 +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:30:25 +00:00
|
|
|
// Change account image visibility
|
2021-02-12 21:45:17 +00:00
|
|
|
MultiChoicesSettingsTile(
|
2022-03-10 18:39:57 +00:00
|
|
|
title: tr("Account image visibility")!,
|
2021-02-12 21:45:17 +00:00
|
|
|
choices: _visibilityLevels,
|
|
|
|
currentValue: _settings.visibility,
|
2022-03-10 18:39:57 +00:00
|
|
|
onChanged: (dynamic newLevel) async {
|
2021-02-12 21:45:17 +00:00
|
|
|
if (!await SettingsHelper.setAccountImageVisibilityLevel(newLevel))
|
|
|
|
showSimpleSnack(context,
|
2022-03-10 18:39:57 +00:00
|
|
|
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);
|
2022-03-10 18:39:57 +00:00
|
|
|
_key.currentState!.refresh();
|
2020-04-16 14:33:44 +00:00
|
|
|
}
|
2020-04-16 17:30:25 +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(
|
2022-03-10 18:39:57 +00:00
|
|
|
context, tr("Could not upload your generated account image!")!);
|
2020-04-17 06:44:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
_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()) {
|
2022-03-10 18:39:57 +00:00
|
|
|
showSimpleSnack(context, tr("Could not delete user account image!")!);
|
2020-04-16 17:34:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
_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);
|
2022-03-10 18:39:57 +00:00
|
|
|
snack(context, tr("Failed to upload new account image!")!);
|
2021-04-17 16:33:08 +00:00
|
|
|
}
|
|
|
|
}
|