lemmur-app-android/lib/pages/add_account.dart

207 lines
6.7 KiB
Dart
Raw Normal View History

2020-09-20 00:15:36 +02:00
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2021-01-24 20:01:55 +01:00
import 'package:lemmy_api_client/v2.dart';
import 'package:url_launcher/url_launcher.dart' as ul;
import '../hooks/delayed_loading.dart';
import '../hooks/stores.dart';
import '../widgets/bottom_modal.dart';
2020-09-20 00:15:36 +02:00
import '../widgets/fullscreenable_image.dart';
import 'add_instance.dart';
2020-09-30 19:05:00 +02:00
/// A modal where an account can be added for a given instance
class AddAccountPage extends HookWidget {
final String instanceHost;
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
AddAccountPage({@required this.instanceHost}) : assert(instanceHost != null);
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final usernameController = useTextEditingController();
final passwordController = useTextEditingController();
useValueListenable(usernameController);
useValueListenable(passwordController);
final accountsStore = useAccountsStore();
2020-09-30 19:05:00 +02:00
final loading = useDelayedLoading();
final selectedInstance = useState(instanceHost);
2020-09-20 00:15:36 +02:00
final icon = useState<String>(null);
useEffect(() {
2021-01-24 20:01:55 +01:00
LemmyApiV2(selectedInstance.value)
.run(GetSite())
.then((site) => icon.value = site.siteView.site.icon);
return null;
}, [selectedInstance.value]);
2020-09-30 20:10:13 +02:00
/// show a modal with a list of instance checkboxes
selectInstance() async {
final val = await showModalBottomSheet<String>(
backgroundColor: Colors.transparent,
isScrollControlled: true,
context: context,
builder: (context) => BottomModal(
title: 'select instance',
child: Column(children: [
for (final i in accountsStore.instances)
RadioListTile<String>(
value: i,
groupValue: selectedInstance.value,
onChanged: (val) {
Navigator.of(context).pop(val);
},
title: Text(i),
),
ListTile(
2021-01-03 19:43:39 +01:00
leading: const Padding(
padding: EdgeInsets.all(8),
child: Icon(Icons.add),
),
2021-01-03 19:43:39 +01:00
title: const Text('Add instance'),
onTap: () async {
final val = await showCupertinoModalPopup<String>(
context: context,
builder: (context) => AddInstancePage(),
);
Navigator.of(context).pop(val);
},
),
]),
),
);
if (val != null) {
selectedInstance.value = val;
}
}
handleOnAdd() async {
try {
loading.start();
await accountsStore.addAccount(
selectedInstance.value,
usernameController.text,
passwordController.text,
);
Navigator.of(context).pop();
} on Exception catch (err) {
scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(err.toString()),
));
}
loading.cancel();
}
return Scaffold(
key: scaffoldKey,
appBar: AppBar(
2021-01-03 19:43:39 +01:00
leading: const CloseButton(),
actionsIconTheme: theme.iconTheme,
iconTheme: theme.iconTheme,
textTheme: theme.textTheme,
brightness: theme.brightness,
centerTitle: true,
2021-01-03 19:43:39 +01:00
title: const Text('Add account'),
backgroundColor: theme.canvasColor,
shadowColor: Colors.transparent,
),
body: Padding(
padding: const EdgeInsets.all(15),
child: ListView(
children: [
2020-09-20 00:15:36 +02:00
if (icon.value == null)
2021-01-03 19:43:39 +01:00
const SizedBox(height: 150)
2020-09-20 00:15:36 +02:00
else
SizedBox(
height: 150,
child: FullscreenableImage(
url: icon.value,
child: CachedNetworkImage(
imageUrl: icon.value,
2021-01-03 19:43:39 +01:00
errorWidget: (_, __, ___) => const SizedBox.shrink(),
2020-09-20 00:15:36 +02:00
),
),
),
FlatButton(
onPressed: selectInstance,
2021-01-03 18:13:25 +01:00
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(selectedInstance.value),
2021-01-03 19:43:39 +01:00
const Icon(Icons.arrow_drop_down),
],
),
),
2020-09-22 01:21:03 +02:00
// TODO: add support for password managers
TextField(
autofocus: true,
controller: usernameController,
decoration: InputDecoration(
isDense: true,
contentPadding:
2021-01-03 19:43:39 +01:00
const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
labelText: 'Username or email',
),
),
const SizedBox(height: 5),
TextField(
controller: passwordController,
obscureText: true,
decoration: InputDecoration(
isDense: true,
contentPadding:
2021-01-03 19:43:39 +01:00
const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
labelText: 'Password',
),
),
RaisedButton(
color: theme.accentColor,
2021-01-03 19:43:39 +01:00
padding: const EdgeInsets.all(0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
2021-01-03 18:13:25 +01:00
onPressed: usernameController.text.isEmpty ||
passwordController.text.isEmpty
? null
: loading.pending
? () {}
: handleOnAdd,
child: !loading.loading
2021-01-03 19:43:39 +01:00
? const Text('Sign in')
: SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(theme.canvasColor),
),
),
),
FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
onPressed: () {
2020-09-23 23:27:57 +02:00
ul.launch('https://${selectedInstance.value}/login');
},
2021-01-03 19:43:39 +01:00
child: const Text('Register'),
),
],
),
),
);
}
}