nc-photos/app/lib/widget/sign_in.dart

299 lines
9.3 KiB
Dart
Raw Normal View History

2022-07-28 18:59:26 +02:00
import 'dart:async';
import 'package:flutter/foundation.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:kiwi/kiwi.dart';
2021-04-10 06:28:12 +02:00
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';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/sqlite_table_extension.dart' as sql;
2021-11-19 14:47:22 +01:00
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/legacy/sign_in.dart' as legacy;
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';
2021-12-05 13:02:22 +01:00
import 'package:nc_photos/pref_util.dart' as pref_util;
2021-04-10 06:28:12 +02:00
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-09-15 08:58:06 +02:00
const 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) {
2022-11-12 10:55:33 +01:00
return Scaffold(
body: Builder(builder: (context) => _buildContent(context)),
2021-04-10 06:28:12 +02:00
);
}
Widget _buildContent(BuildContext context) {
2022-07-18 13:13:34 +02:00
if (_isConnecting) {
return Stack(
children: const [
Positioned(
left: 0,
right: 0,
bottom: 64,
child: Center(
child: SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(),
),
),
),
],
);
} else {
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,
style: Theme.of(context).textTheme.headline5,
textAlign: TextAlign.center,
),
2021-04-10 06:28:12 +02:00
),
2022-07-18 13:13:34 +02:00
Align(
alignment: Alignment.center,
child: Container(
2022-11-12 10:55:33 +01:00
constraints: BoxConstraints(
maxWidth:
Theme.of(context).widthLimitedContentMaxWidth,
),
2022-07-18 13:13:34 +02:00
padding: const EdgeInsets.symmetric(horizontal: 32),
child: _buildForm(context),
),
),
if (!platform_k.isWeb) Expanded(child: Container()),
Container(
2022-11-12 10:55:33 +01:00
constraints: BoxConstraints(
maxWidth:
Theme.of(context).widthLimitedContentMaxWidth,
),
2022-07-18 13:13:34 +02:00
padding: const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (!ModalRoute.of(context)!.isFirst)
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(MaterialLocalizations.of(context)
.cancelButtonLabel),
)
else
Container(),
ElevatedButton(
2021-04-10 06:28:12 +02:00
onPressed: () {
2022-07-18 13:13:34 +02:00
if (_formKey.currentState?.validate() ==
true) {
_connect();
}
2021-04-10 06:28:12 +02:00
},
2022-07-18 13:13:34 +02:00
child: Text(L10n.global().connectButtonLabel),
),
],
),
2021-04-10 06:28:12 +02:00
),
2022-07-18 13:13:34 +02:00
],
),
2021-04-10 06:28:12 +02:00
),
),
),
2022-07-18 13:13:34 +02:00
);
},
),
);
}
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: [
2021-09-15 12:50:51 +02:00
SizedBox(
2021-04-10 06:28:12 +02:00
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(
2021-09-15 08:58:06 +02:00
padding: EdgeInsets.symmetric(horizontal: 4),
child: Text("://"),
2021-04-10 06:28:12 +02:00
),
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
},
),
),
],
),
if (kDebugMode) ...[
const SizedBox(height: 8),
InkWell(
onTap: () {
Navigator.pushReplacementNamed(context, legacy.SignIn.routeName);
},
child: const Text(
"Legacy sign in",
style: TextStyle(decoration: TextDecoration.underline),
),
),
],
2021-04-10 06:28:12 +02:00
],
);
}
2021-12-05 13:02:22 +01:00
Future<void> _connect() async {
2021-07-23 22:05:57 +02:00
_formKey.currentState!.save();
Uri url = Uri.parse("${_formValue.scheme}://${_formValue.address}");
_log.info("[_connect] Try connecting with url: $url");
Account? account = await Navigator.pushNamed<Account>(
context, Connect.routeName,
arguments: ConnectArguments(url));
2021-12-05 13:02:22 +01:00
if (account == null) {
// connection failed
return;
}
account = await Navigator.pushNamed<Account>(context, RootPicker.routeName,
arguments: RootPickerArguments(account));
if (account == null) {
// ???
return;
}
// we've got a good account
2022-07-18 13:13:34 +02:00
setState(() {
_isConnecting = true;
});
try {
await _persistAccount(account);
2022-07-28 18:59:26 +02:00
unawaited(
Navigator.pushNamedAndRemoveUntil(
context, Home.routeName, (route) => false,
arguments: HomeArguments(account)),
);
2022-07-18 13:13:34 +02:00
} catch (_) {
setState(() {
_isConnecting = false;
});
rethrow;
}
}
Future<void> _persistAccount(Account account) async {
final c = KiwiContainer().resolve<DiContainer>();
await c.sqliteDb.use((db) async {
2022-07-18 13:13:34 +02:00
await db.insertAccountOf(account);
});
2021-12-05 13:02:22 +01:00
// only signing in with app password would trigger distinct
final accounts = (Pref().getAccounts3Or([])..add(account)).distinct();
try {
AccountPref.setGlobalInstance(
account, await pref_util.loadAccountPref(account));
} catch (e, stackTrace) {
_log.shout("[_connect] Failed reading pref for account: $account", e,
stackTrace);
}
2022-07-28 18:59:26 +02:00
unawaited(Pref().setAccounts3(accounts));
unawaited(Pref().setCurrentAccountIndex(accounts.indexOf(account)));
2021-04-10 06:28:12 +02:00
}
final _formKey = GlobalKey<FormState>();
var _scheme = _Scheme.https;
2022-07-18 13:13:34 +02:00
var _isConnecting = false;
2021-04-10 06:28:12 +02:00
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;
2021-04-10 06:28:12 +02:00
}