2020-04-17 10:00:42 +00:00
|
|
|
import 'package:comunic/helpers/database/users_database_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';
|
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';
|
2020-04-17 06:44:57 +00:00
|
|
|
import 'package:identicon/identicon.dart';
|
2021-03-13 17:11:28 +00:00
|
|
|
import 'package:image_cropper/image_cropper.dart';
|
2020-04-17 06:44:57 +00:00
|
|
|
import 'package:random_string/random_string.dart';
|
2020-04-16 17:16:44 +00:00
|
|
|
import 'package:settings_ui/settings_ui.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> {
|
2020-04-16 17:16:44 +00:00
|
|
|
AccountImageSettings _settings;
|
|
|
|
|
|
|
|
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
|
|
|
|
UsersDatabaseHelper().delete(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!"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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"),
|
|
|
|
subtitle: tr(
|
|
|
|
"Your account image is visible by everyone, including users external to Comunic."),
|
|
|
|
),
|
|
|
|
MultiChoiceEntry(
|
|
|
|
id: AccountImageVisibilityLevels.COMUNIC_USERS,
|
|
|
|
title: tr("Connected users"),
|
|
|
|
subtitle: tr(
|
|
|
|
"Your account image is visible only to connected Comunic users."),
|
|
|
|
),
|
|
|
|
MultiChoiceEntry(
|
|
|
|
id: AccountImageVisibilityLevels.FRIENDS_ONLY,
|
|
|
|
title: tr("My friends"),
|
|
|
|
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(
|
|
|
|
title: tr("Account image visibility"),
|
|
|
|
choices: _visibilityLevels,
|
|
|
|
currentValue: _settings.visibility,
|
|
|
|
onChanged: (newLevel) async {
|
|
|
|
if (!await SettingsHelper.setAccountImageVisibilityLevel(newLevel))
|
|
|
|
showSimpleSnack(context,
|
|
|
|
tr("Could not update account image visibility level!"));
|
|
|
|
_key.currentState.refresh();
|
|
|
|
}),
|
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-03-13 17:03:20 +00:00
|
|
|
try {
|
2021-03-13 17:11:28 +00:00
|
|
|
final image = await pickImage(context,
|
|
|
|
aspectRatio: CropAspectRatio(ratioX: 5, ratioY: 5));
|
2020-04-16 17:16:44 +00:00
|
|
|
|
2021-03-13 17:03:20 +00:00
|
|
|
if (image == null) return;
|
2020-04-16 17:16:44 +00:00
|
|
|
|
2021-03-13 17:03:20 +00:00
|
|
|
await SettingsHelper.uploadAccountImage(image);
|
|
|
|
} catch (e, s) {
|
|
|
|
logError(e, s);
|
|
|
|
snack(context, tr("Failed to upload new account image!"));
|
2020-04-16 17:16:44 +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
|
|
|
|
final bytes = Identicon().generate(randomString(10));
|
|
|
|
|
|
|
|
if (!await SettingsHelper.uploadAccountImageFromMemory(bytes)) {
|
|
|
|
showSimpleSnack(
|
|
|
|
context, tr("Could not upload your generated account image!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_key.currentState.refresh();
|
|
|
|
}
|
|
|
|
|
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()) {
|
2021-02-04 15:08:27 +00:00
|
|
|
showSimpleSnack(context, tr("Could not delete user account image!"));
|
2020-04-16 17:34:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_key.currentState.refresh();
|
|
|
|
}
|
2020-04-16 14:33:44 +00:00
|
|
|
}
|