123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:flutter_hbb/common/widgets/setting_widgets.dart';
- import 'package:flutter_hbb/common/widgets/toolbar.dart';
- import 'package:get/get.dart';
- import '../../common.dart';
- import '../../models/platform_model.dart';
- void _showSuccess() {
- showToast(translate("Successful"));
- }
- void _showError() {
- showToast(translate("Error"));
- }
- void setPermanentPasswordDialog(OverlayDialogManager dialogManager) async {
- final pw = await bind.mainGetPermanentPassword();
- final p0 = TextEditingController(text: pw);
- final p1 = TextEditingController(text: pw);
- var validateLength = false;
- var validateSame = false;
- dialogManager.show((setState, close, context) {
- submit() async {
- close();
- dialogManager.showLoading(translate("Waiting"));
- if (await gFFI.serverModel.setPermanentPassword(p0.text)) {
- dialogManager.dismissAll();
- _showSuccess();
- } else {
- dialogManager.dismissAll();
- _showError();
- }
- }
- return CustomAlertDialog(
- title: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Icon(Icons.password_rounded, color: MyTheme.accent),
- Text(translate('Set your own password')).paddingOnly(left: 10),
- ],
- ),
- content: Form(
- autovalidateMode: AutovalidateMode.onUserInteraction,
- child: Column(mainAxisSize: MainAxisSize.min, children: [
- TextFormField(
- autofocus: true,
- obscureText: true,
- keyboardType: TextInputType.visiblePassword,
- decoration: InputDecoration(
- labelText: translate('Password'),
- ),
- controller: p0,
- validator: (v) {
- if (v == null) return null;
- final val = v.trim().length > 5;
- if (validateLength != val) {
- // use delay to make setState success
- Future.delayed(Duration(microseconds: 1),
- () => setState(() => validateLength = val));
- }
- return val
- ? null
- : translate('Too short, at least 6 characters.');
- },
- ),
- TextFormField(
- obscureText: true,
- keyboardType: TextInputType.visiblePassword,
- decoration: InputDecoration(
- labelText: translate('Confirmation'),
- ),
- controller: p1,
- validator: (v) {
- if (v == null) return null;
- final val = p0.text == v;
- if (validateSame != val) {
- Future.delayed(Duration(microseconds: 1),
- () => setState(() => validateSame = val));
- }
- return val
- ? null
- : translate('The confirmation is not identical.');
- },
- ),
- ])),
- onCancel: close,
- onSubmit: (validateLength && validateSame) ? submit : null,
- actions: [
- dialogButton(
- 'Cancel',
- icon: Icon(Icons.close_rounded),
- onPressed: close,
- isOutline: true,
- ),
- dialogButton(
- 'OK',
- icon: Icon(Icons.done_rounded),
- onPressed: (validateLength && validateSame) ? submit : null,
- ),
- ],
- );
- });
- }
- void setTemporaryPasswordLengthDialog(
- OverlayDialogManager dialogManager) async {
- List<String> lengths = ['6', '8', '10'];
- String length = await bind.mainGetOption(key: "temporary-password-length");
- var index = lengths.indexOf(length);
- if (index < 0) index = 0;
- length = lengths[index];
- dialogManager.show((setState, close, context) {
- setLength(newValue) {
- final oldValue = length;
- if (oldValue == newValue) return;
- setState(() {
- length = newValue;
- });
- bind.mainSetOption(key: "temporary-password-length", value: newValue);
- bind.mainUpdateTemporaryPassword();
- Future.delayed(Duration(milliseconds: 200), () {
- close();
- _showSuccess();
- });
- }
- return CustomAlertDialog(
- title: Text(translate("Set one-time password length")),
- content: Row(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: lengths
- .map(
- (value) => Row(
- children: [
- Text(value),
- Radio(
- value: value, groupValue: length, onChanged: setLength),
- ],
- ),
- )
- .toList()),
- );
- }, backDismiss: true, clickMaskDismiss: true);
- }
- void showServerSettingsWithValue(
- ServerConfig serverConfig, OverlayDialogManager dialogManager) async {
- var isInProgress = false;
- final idCtrl = TextEditingController(text: serverConfig.idServer);
- final relayCtrl = TextEditingController(text: serverConfig.relayServer);
- final apiCtrl = TextEditingController(text: serverConfig.apiServer);
- final keyCtrl = TextEditingController(text: serverConfig.key);
- RxString idServerMsg = ''.obs;
- RxString relayServerMsg = ''.obs;
- RxString apiServerMsg = ''.obs;
- final controllers = [idCtrl, relayCtrl, apiCtrl, keyCtrl];
- final errMsgs = [
- idServerMsg,
- relayServerMsg,
- apiServerMsg,
- ];
- dialogManager.show((setState, close, context) {
- Future<bool> submit() async {
- setState(() {
- isInProgress = true;
- });
- bool ret = await setServerConfig(
- null,
- errMsgs,
- ServerConfig(
- idServer: idCtrl.text.trim(),
- relayServer: relayCtrl.text.trim(),
- apiServer: apiCtrl.text.trim(),
- key: keyCtrl.text.trim()));
- setState(() {
- isInProgress = false;
- });
- return ret;
- }
- return CustomAlertDialog(
- title: Row(
- children: [
- Expanded(child: Text(translate('ID/Relay Server'))),
- ...ServerConfigImportExportWidgets(controllers, errMsgs),
- ],
- ),
- content: Form(
- child: Obx(() => Column(
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- TextFormField(
- controller: idCtrl,
- decoration: InputDecoration(
- labelText: translate('ID Server'),
- errorText: idServerMsg.value.isEmpty
- ? null
- : idServerMsg.value),
- )
- ] +
- [
- if (isAndroid)
- TextFormField(
- controller: relayCtrl,
- decoration: InputDecoration(
- labelText: translate('Relay Server'),
- errorText: relayServerMsg.value.isEmpty
- ? null
- : relayServerMsg.value),
- )
- ] +
- [
- TextFormField(
- controller: apiCtrl,
- decoration: InputDecoration(
- labelText: translate('API Server'),
- ),
- autovalidateMode: AutovalidateMode.onUserInteraction,
- validator: (v) {
- if (v != null && v.isNotEmpty) {
- if (!(v.startsWith('http://') ||
- v.startsWith("https://"))) {
- return translate("invalid_http");
- }
- }
- return null;
- },
- ),
- TextFormField(
- controller: keyCtrl,
- decoration: InputDecoration(
- labelText: 'Key',
- ),
- ),
- // NOT use Offstage to wrap LinearProgressIndicator
- if (isInProgress) const LinearProgressIndicator(),
- ]))),
- actions: [
- dialogButton('Cancel', onPressed: () {
- close();
- }, isOutline: true),
- dialogButton(
- 'OK',
- onPressed: () async {
- if (await submit()) {
- close();
- showToast(translate('Successful'));
- } else {
- showToast(translate('Failed'));
- }
- },
- ),
- ],
- );
- });
- }
- void setPrivacyModeDialog(
- OverlayDialogManager dialogManager,
- List<TToggleMenu> privacyModeList,
- RxString privacyModeState,
- ) async {
- dialogManager.dismissAll();
- dialogManager.show((setState, close, context) {
- return CustomAlertDialog(
- title: Text(translate('Privacy mode')),
- content: Column(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: privacyModeList
- .map((value) => CheckboxListTile(
- contentPadding: EdgeInsets.zero,
- visualDensity: VisualDensity.compact,
- title: value.child,
- value: value.value,
- onChanged: value.onChanged,
- ))
- .toList()),
- );
- }, backDismiss: true, clickMaskDismiss: true);
- }
|