nc-photos/lib/widget/sign_in.dart

274 lines
8.9 KiB
Dart
Raw Normal View History

2021-04-20 12:12:33 +02:00
import 'package:flutter/foundation.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/list_extension.dart';
2021-04-29 17:42:44 +02:00
import 'package:nc_photos/platform/k.dart' as platform_k;
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/string_extension.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/widget/connect.dart';
import 'package:nc_photos/widget/home.dart';
import 'package:nc_photos/widget/root_picker.dart';
class SignIn extends StatefulWidget {
static const routeName = "/sign-in";
2021-07-23 22:05:57 +02:00
SignIn({Key? key}) : super(key: key);
2021-04-10 06:28:12 +02:00
@override
createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
@override
build(BuildContext context) {
return AppTheme(
child: Scaffold(
body: Builder(builder: (context) => _buildContent(context)),
),
);
}
Widget _buildContent(BuildContext context) {
return SafeArea(
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints viewportConstraints) {
return Form(
key: _formKey,
child: SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: viewportConstraints.maxHeight,
),
child: IntrinsicHeight(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(24),
child: Text(
L10n.global().signInHeaderText,
2021-04-10 06:28:12 +02:00
style: Theme.of(context).textTheme.headline5,
textAlign: TextAlign.center,
),
),
Align(
alignment: Alignment.center,
child: Container(
constraints: const BoxConstraints(
maxWidth: AppTheme.widthLimitedContentMaxWidth),
padding: const EdgeInsets.symmetric(horizontal: 32),
child: _buildForm(context),
),
),
2021-06-22 16:45:34 +02:00
Container(
alignment: AlignmentDirectional.centerStart,
constraints: const BoxConstraints(
maxWidth: AppTheme.widthLimitedContentMaxWidth),
padding: const EdgeInsets.symmetric(
horizontal: 32, vertical: 16),
child: Text(
L10n.global().signIn2faHintText,
2021-06-22 16:45:34 +02:00
style: TextStyle(fontStyle: FontStyle.italic),
),
),
2021-04-29 17:42:44 +02:00
if (!platform_k.isWeb) Expanded(child: Container()),
2021-04-10 06:28:12 +02:00
Container(
constraints: const BoxConstraints(
maxWidth: AppTheme.widthLimitedContentMaxWidth),
padding: const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2021-07-23 22:05:57 +02:00
if (!ModalRoute.of(context)!.isFirst)
2021-04-10 06:28:12 +02:00
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(MaterialLocalizations.of(context)
.cancelButtonLabel),
)
else
Container(),
ElevatedButton(
onPressed: () {
2021-07-23 22:05:57 +02:00
if (_formKey.currentState?.validate() == true) {
2021-04-10 06:28:12 +02:00
_connect();
}
},
child: Text(L10n.global().connectButtonLabel),
2021-04-10 06:28:12 +02:00
),
],
),
),
],
),
),
),
),
);
},
),
);
}
Widget _buildForm(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Icon(
Icons.cloud,
2021-07-13 18:05:25 +02:00
color: Theme.of(context).colorScheme.primary,
2021-04-10 06:28:12 +02:00
size: 72,
),
),
const SizedBox(height: 8),
Row(
children: [
Container(
width: 64,
child: DropdownButtonHideUnderline(
child: DropdownButtonFormField<_Scheme>(
value: _scheme,
items: [_Scheme.http, _Scheme.https]
.map((e) => DropdownMenuItem<_Scheme>(
value: e,
child: Text(e.toValueString()),
))
.toList(),
onChanged: (newValue) {
setState(() {
2021-07-23 22:05:57 +02:00
_scheme = newValue!;
2021-04-10 06:28:12 +02:00
});
},
onSaved: (value) {
2021-07-23 22:05:57 +02:00
_formValue.scheme = value!.toValueString();
2021-04-10 06:28:12 +02:00
},
),
),
),
const Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: const Text("://"),
),
Expanded(
child: TextFormField(
decoration: InputDecoration(
hintText: L10n.global().serverAddressInputHint,
2021-04-10 06:28:12 +02:00
),
keyboardType: TextInputType.url,
validator: (value) {
2021-07-23 22:05:57 +02:00
if (value!.trim().trimRightAny("/").isEmpty) {
return L10n.global().serverAddressInputInvalidEmpty;
2021-04-10 06:28:12 +02:00
}
return null;
},
onSaved: (value) {
2021-07-23 22:05:57 +02:00
_formValue.address = value!.trim().trimRightAny("/");
2021-04-10 06:28:12 +02:00
},
),
),
],
),
const SizedBox(height: 8),
TextFormField(
decoration: InputDecoration(
hintText: L10n.global().usernameInputHint,
2021-04-10 06:28:12 +02:00
),
validator: (value) {
2021-07-23 22:05:57 +02:00
if (value!.trim().isEmpty) {
return L10n.global().usernameInputInvalidEmpty;
2021-04-10 06:28:12 +02:00
}
return null;
},
onSaved: (value) {
2021-07-23 22:05:57 +02:00
_formValue.username = value!;
2021-04-10 06:28:12 +02:00
},
),
const SizedBox(height: 8),
TextFormField(
decoration: InputDecoration(
hintText: L10n.global().passwordInputHint,
2021-04-10 06:28:12 +02:00
),
obscureText: true,
validator: (value) {
2021-07-23 22:05:57 +02:00
if (value!.trim().isEmpty) {
return L10n.global().passwordInputInvalidEmpty;
2021-04-10 06:28:12 +02:00
}
return null;
},
onSaved: (value) {
2021-07-23 22:05:57 +02:00
_formValue.password = value!;
2021-04-10 06:28:12 +02:00
},
),
],
);
}
void _connect() {
2021-07-23 22:05:57 +02:00
_formKey.currentState!.save();
2021-04-10 06:28:12 +02:00
final account = Account(_formValue.scheme, _formValue.address,
_formValue.username, _formValue.password, [""]);
_log.info("[_connect] Try connecting with account: $account");
2021-07-23 22:05:57 +02:00
Navigator.pushNamed<Account>(context, Connect.routeName,
2021-04-10 06:28:12 +02:00
arguments: ConnectArguments(account))
2021-07-23 22:05:57 +02:00
.then<Account?>((result) {
2021-04-10 06:28:12 +02:00
return result != null
? Navigator.pushNamed(context, RootPicker.routeName,
arguments: RootPickerArguments(result))
2021-07-23 22:05:57 +02:00
: Future.value(null);
2021-04-10 06:28:12 +02:00
}).then((result) {
if (result != null) {
// we've got a good account
2021-07-02 21:45:35 +02:00
// only signing in with app password would trigger distinct
2021-07-23 22:05:57 +02:00
final accounts =
(Pref.inst().getAccountsOr([])..add(result)).distinct();
2021-04-10 06:28:12 +02:00
Pref.inst()
..setAccounts(accounts)
..setCurrentAccountIndex(accounts.indexOf(result));
Navigator.pushNamedAndRemoveUntil(
context, Home.routeName, (route) => false,
arguments: HomeArguments(result));
}
});
}
final _formKey = GlobalKey<FormState>();
var _scheme = _Scheme.https;
final _formValue = _FormValue();
static final _log = Logger("widget.sign_in._SignInState");
}
enum _Scheme {
http,
https,
}
extension on _Scheme {
String toValueString() {
switch (this) {
case _Scheme.http:
return "http";
case _Scheme.https:
return "https";
default:
throw StateError("Unknown value: $this");
}
}
}
class _FormValue {
2021-07-23 22:05:57 +02:00
late String scheme;
late String address;
late String username;
late String password;
2021-04-10 06:28:12 +02:00
}