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

226 lines
6.5 KiB
Dart
Raw Normal View History

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';
import 'package:comunic/ui/widgets/async_screen_widget.dart';
import 'package:comunic/ui/widgets/network_image_widget.dart';
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';
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
/// Account image settings section
///
/// @author Pierre Hubert
2020-04-16 17:16:44 +00:00
class AccountImageSettingsScreen extends StatelessWidget {
2020-04-16 14:33:44 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(tr("Account image settings")),
),
body: _AccountImageSettingsBody(),
);
}
}
class _AccountImageSettingsBody extends StatefulWidget {
@override
2020-04-16 17:16:44 +00:00
__AccountImageSettingsBodyState createState() =>
__AccountImageSettingsBodyState();
2020-04-16 14:33:44 +00:00
}
class __AccountImageSettingsBodyState extends State<_AccountImageSettingsBody> {
2020-04-16 17:16:44 +00:00
AccountImageSettings _settings;
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
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: [
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"),
onTap: () => _uploadAccountImage(),
),
2020-04-17 06:44:57 +00:00
// Generate a random account image
SettingsTile(
title: tr("Generate a random account image"),
onTap: () => _generateRandomAccountImage(),
),
2020-04-16 17:36:34 +00:00
];
}
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"),
onTap: () => _uploadAccountImage(),
),
2020-04-17 06:44:57 +00:00
// Generate a random account image
SettingsTile(
title: tr("Generate a random account image"),
onTap: () => _generateRandomAccountImage(),
),
// Change account image visibility
SettingsTile(
title: tr("Change account image visibility"),
onTap: () => _chooseAccountImageVisibility(),
subtitle: tr("Current level: %level%", args: {
"level":
_settings.visibility == AccountImageVisibilityLevels.EVERYONE
? tr("Everyone")
: _settings.visibility ==
AccountImageVisibilityLevels.COMUNIC_USERS
? tr("Comunic users")
: tr("My friends only"),
})),
2020-04-16 17:34:55 +00:00
// Delete account image
SettingsTile(
title: tr("Delete account image"),
onTap: () => _deleteAccountImage(),
),
2020-04-16 17:16:44 +00:00
];
}
/// Upload a new account image
void _uploadAccountImage() async {
final image = await pickImage(context);
if (image == null) return;
if (!await SettingsHelper.uploadAccountImage(image)) {
showSimpleSnack(context, tr("Could not upload your account image!"));
return;
}
_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
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();
}
/// Change account image visibility
void _chooseAccountImageVisibility() async {
final newLevel = await showDialog<AccountImageVisibilityLevels>(
context: context,
builder: (c) => AlertDialog(
title: Text(tr("Account image visiblity")),
content:
Text(tr("Please choose new account image visibility level:")),
actions: <Widget>[
MaterialButton(
child: Text(tr("Everyone")),
onPressed: () =>
Navigator.pop(c, AccountImageVisibilityLevels.EVERYONE),
),
MaterialButton(
child: Text(tr("Connected users")),
onPressed: () => Navigator.pop(
c, AccountImageVisibilityLevels.COMUNIC_USERS),
),
MaterialButton(
child: Text(tr("My friends")),
onPressed: () => Navigator.pop(
c, AccountImageVisibilityLevels.FRIENDS_ONLY),
)
],
));
if (newLevel == null) return;
if (!await SettingsHelper.setAccountImageVisibilityLevel(newLevel)) {
showSimpleSnack(
context, tr("Could not update account image visibility level!"));
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()) {
showSimpleSnack(context, tr("Could not user account image!"));
return;
}
_key.currentState.refresh();
}
2020-04-16 14:33:44 +00:00
}