1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-23 05:19:22 +00:00
comunicmobile/lib/ui/routes/account_settings/account_image_settings.dart

226 lines
6.5 KiB
Dart

import 'package:comunic/helpers/database/users_database_helper.dart';
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';
import 'package:comunic/utils/files_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
import 'package:identicon/identicon.dart';
import 'package:random_string/random_string.dart';
import 'package:settings_ui/settings_ui.dart';
/// Account image settings section
///
/// @author Pierre Hubert
class AccountImageSettingsScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(tr("Account image settings")),
),
body: _AccountImageSettingsBody(),
);
}
}
class _AccountImageSettingsBody extends StatefulWidget {
@override
__AccountImageSettingsBodyState createState() =>
__AccountImageSettingsBodyState();
}
class __AccountImageSettingsBodyState extends State<_AccountImageSettingsBody> {
AccountImageSettings _settings;
final _key = GlobalKey<AsyncScreenWidgetState>();
@override
void dispose() {
// Remove current user information to force refresh of account image
UsersDatabaseHelper().delete(userID());
super.dispose();
}
@override
Widget build(BuildContext context) {
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
List<SettingsTile> _buildNoAccountImageTiles() {
return [
SettingsTile(
title: tr("No account image yet..."),
leading: NetworkImageWidget(
url: _settings.imageURL,
width: 40,
),
),
// Upload new account image
SettingsTile(
title: tr("Upload an account image"),
onTap: () => _uploadAccountImage(),
),
// Generate a random account image
SettingsTile(
title: tr("Generate a random account image"),
onTap: () => _generateRandomAccountImage(),
),
];
}
/// 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
SettingsTile(
title: tr("Upload new account image"),
onTap: () => _uploadAccountImage(),
),
// 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"),
})),
// Delete account image
SettingsTile(
title: tr("Delete account image"),
onTap: () => _deleteAccountImage(),
),
];
}
/// 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();
}
/// 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();
}
/// 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();
}
}