mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-12-26 04:48:51 +00:00
Can update virtual directory
This commit is contained in:
parent
75e15f9f83
commit
c42fe7f806
@ -64,6 +64,16 @@ class SettingsHelper {
|
||||
.assertOk();
|
||||
}
|
||||
|
||||
/// Check out whether a virtual directory is available for a user or not
|
||||
///
|
||||
/// Throws in case of failure
|
||||
static Future<void> checkUserDirectoryAvailability(String dir) async =>
|
||||
(await APIRequest(
|
||||
uri: "settings/check_user_directory_availability",
|
||||
needLogin: true,
|
||||
args: {"directory": dir}).exec())
|
||||
.assertOk();
|
||||
|
||||
/// Get & return account image settings
|
||||
static Future<AccountImageSettings> getAccountImageSettings() async {
|
||||
final response =
|
||||
|
@ -14,7 +14,7 @@ class GeneralSettings {
|
||||
bool allowPostsFromFriends;
|
||||
bool allowComunicEmails;
|
||||
bool publicFriendsList;
|
||||
final String virtualDirectory;
|
||||
String virtualDirectory;
|
||||
String personalWebsite;
|
||||
String publicNote;
|
||||
|
||||
|
137
lib/ui/dialogs/virtual_directory_dialog.dart
Normal file
137
lib/ui/dialogs/virtual_directory_dialog.dart
Normal file
@ -0,0 +1,137 @@
|
||||
import 'package:comunic/helpers/settings_helper.dart';
|
||||
import 'package:comunic/ui/widgets/safe_state.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Show a dialog to offer the user to pick a virtual directory
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
enum VirtualDirectoryTargetType { USER }
|
||||
|
||||
enum _CheckStatus { EMPTY, PENDING, VALID, INVALID }
|
||||
|
||||
Future<String> showVirtualDirectoryDialog({
|
||||
@required BuildContext context,
|
||||
@required String initialDirectory,
|
||||
@required int id,
|
||||
@required VirtualDirectoryTargetType type,
|
||||
}) async {
|
||||
assert(context != null);
|
||||
assert(initialDirectory != null);
|
||||
assert(id != null);
|
||||
assert(type != null);
|
||||
|
||||
return await showDialog<String>(
|
||||
context: context,
|
||||
builder: (c) => _VirtualDirectoryPicker(
|
||||
type: type, id: id, initialDirectory: initialDirectory));
|
||||
}
|
||||
|
||||
class _VirtualDirectoryPicker extends StatefulWidget {
|
||||
final String initialDirectory;
|
||||
final int id;
|
||||
final VirtualDirectoryTargetType type;
|
||||
|
||||
const _VirtualDirectoryPicker({
|
||||
Key key,
|
||||
@required this.initialDirectory,
|
||||
@required this.id,
|
||||
@required this.type,
|
||||
}) : assert(initialDirectory != null),
|
||||
assert(id != null),
|
||||
assert(type != null),
|
||||
super(key: key);
|
||||
|
||||
@override
|
||||
__VirtualDirectoryPickerState createState() =>
|
||||
__VirtualDirectoryPickerState();
|
||||
}
|
||||
|
||||
class __VirtualDirectoryPickerState extends SafeState<_VirtualDirectoryPicker> {
|
||||
TextEditingController _controller;
|
||||
var _status = _CheckStatus.VALID;
|
||||
|
||||
String get _currentValue => _controller.text;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = TextEditingController(text: widget.initialDirectory);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(tr("Choose a virtual directory")),
|
||||
|
||||
// Dialog content
|
||||
content: TextField(
|
||||
controller: _controller,
|
||||
onChanged: (s) => _checkValue(),
|
||||
decoration: InputDecoration(
|
||||
alignLabelWithHint: true,
|
||||
labelText: tr("Virtual directory"),
|
||||
helperText: _status == _CheckStatus.PENDING
|
||||
? tr("Checking availability...")
|
||||
: (_status == _CheckStatus.VALID
|
||||
? tr("You can use this virtual directory.")
|
||||
: null),
|
||||
errorText: _status == _CheckStatus.INVALID
|
||||
? tr("This virtual directory is invalid / unvailable !")
|
||||
: null,
|
||||
),
|
||||
),
|
||||
|
||||
// Dialog action
|
||||
actions: <Widget>[
|
||||
// Cancel
|
||||
MaterialButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text(tr("Cancel").toUpperCase()),
|
||||
),
|
||||
|
||||
// Confirm
|
||||
MaterialButton(
|
||||
onPressed:
|
||||
_status == _CheckStatus.VALID || _status == _CheckStatus.EMPTY
|
||||
? () => Navigator.of(context).pop(_currentValue)
|
||||
: null,
|
||||
child: Text(tr("Confirm").toUpperCase()),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _setStatus(_CheckStatus s) => setState(() => _status = s);
|
||||
|
||||
/// Check new given value
|
||||
void _checkValue() async {
|
||||
final value = _currentValue;
|
||||
try {
|
||||
_setStatus(_CheckStatus.PENDING);
|
||||
|
||||
if (value.length == 0) {
|
||||
_setStatus(_CheckStatus.EMPTY);
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate directory. This will throw in case of failure
|
||||
switch (widget.type) {
|
||||
case VirtualDirectoryTargetType.USER:
|
||||
await SettingsHelper.checkUserDirectoryAvailability(value);
|
||||
break;
|
||||
}
|
||||
|
||||
if (_currentValue == value) {
|
||||
_setStatus(_CheckStatus.VALID);
|
||||
}
|
||||
} catch (e, stack) {
|
||||
if (_currentValue == value) {
|
||||
print("Could not validate given directory! $e\n$stack");
|
||||
_setStatus(_CheckStatus.INVALID);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ import 'package:comunic/helpers/database/users_database_helper.dart';
|
||||
import 'package:comunic/helpers/settings_helper.dart';
|
||||
import 'package:comunic/models/general_settings.dart';
|
||||
import 'package:comunic/ui/dialogs/multi_choices_dialog.dart';
|
||||
import 'package:comunic/ui/dialogs/virtual_directory_dialog.dart';
|
||||
import 'package:comunic/ui/widgets/async_screen_widget.dart';
|
||||
import 'package:comunic/ui/widgets/settings/text_settings_edit_tile.dart';
|
||||
import 'package:comunic/utils/account_utils.dart';
|
||||
@ -214,6 +215,24 @@ class __GeneralAccountSettingsBodyState
|
||||
maxLength: 255,
|
||||
maxLines: 3,
|
||||
),
|
||||
|
||||
// Virtual directory
|
||||
SettingsTile(
|
||||
title: tr("Virtual directory (optional)"),
|
||||
subtitle: _settings.virtualDirectory,
|
||||
onTap: () async {
|
||||
final dir = await showVirtualDirectoryDialog(
|
||||
context: context,
|
||||
initialDirectory: _settings.virtualDirectory,
|
||||
type: VirtualDirectoryTargetType.USER,
|
||||
id: userID());
|
||||
|
||||
if (dir == null) return;
|
||||
|
||||
_settings.virtualDirectory = dir;
|
||||
_updateSettings();
|
||||
},
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user