2020-09-20 00:15:36 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2020-09-19 01:34:04 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2020-09-20 00:15:36 +02:00
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
2020-09-19 23:19:49 +02:00
|
|
|
import 'package:url_launcher/url_launcher.dart' as ul;
|
2020-09-19 01:34:04 +02:00
|
|
|
|
2020-09-22 00:58:52 +02:00
|
|
|
import '../hooks/delayed_loading.dart';
|
2020-09-19 23:19:49 +02:00
|
|
|
import '../hooks/stores.dart';
|
2020-09-19 01:34:04 +02:00
|
|
|
import '../widgets/bottom_modal.dart';
|
2020-09-20 00:15:36 +02:00
|
|
|
import '../widgets/fullscreenable_image.dart';
|
2020-09-19 23:19:49 +02:00
|
|
|
import 'add_instance.dart';
|
2020-09-19 01:34:04 +02:00
|
|
|
|
2020-09-30 19:05:00 +02:00
|
|
|
/// A modal where an account can be added for a given instance
|
2020-09-19 01:34:04 +02:00
|
|
|
class AddAccountPage extends HookWidget {
|
2020-12-31 14:58:23 +01:00
|
|
|
final String instanceHost;
|
2020-09-21 23:15:45 +02:00
|
|
|
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
|
2020-09-19 01:34:04 +02:00
|
|
|
|
2020-12-31 14:58:23 +01:00
|
|
|
AddAccountPage({@required this.instanceHost}) : assert(instanceHost != null);
|
2020-09-19 01:34:04 +02:00
|
|
|
|
|
|
|
@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();
|
2020-12-31 14:58:23 +01:00
|
|
|
final selectedInstance = useState(instanceHost);
|
2020-09-20 00:15:36 +02:00
|
|
|
final icon = useState<String>(null);
|
2020-09-23 23:20:16 +02:00
|
|
|
useEffect(() {
|
|
|
|
LemmyApi(selectedInstance.value)
|
|
|
|
.v1
|
|
|
|
.getSite()
|
|
|
|
.then((site) => icon.value = site.site.icon);
|
|
|
|
return null;
|
|
|
|
}, [selectedInstance.value]);
|
2020-09-19 01:34:04 +02:00
|
|
|
|
2020-09-30 20:10:13 +02:00
|
|
|
/// show a modal with a list of instance checkboxes
|
2020-09-19 01:34:04 +02:00
|
|
|
selectInstance() async {
|
|
|
|
final val = await showModalBottomSheet<String>(
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
isScrollControlled: true,
|
|
|
|
context: context,
|
|
|
|
builder: (context) => BottomModal(
|
|
|
|
title: 'select instance',
|
|
|
|
child: Column(children: [
|
2020-09-29 23:15:51 +02:00
|
|
|
for (final i in accountsStore.instances)
|
2020-09-19 01:34:04 +02:00
|
|
|
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),
|
2020-09-19 01:34:04 +02:00
|
|
|
child: Icon(Icons.add),
|
|
|
|
),
|
2021-01-03 19:43:39 +01:00
|
|
|
title: const Text('Add instance'),
|
2020-09-19 01:34:04 +02:00
|
|
|
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 {
|
2020-09-22 00:58:52 +02:00
|
|
|
loading.start();
|
2020-09-19 01:34:04 +02:00
|
|
|
await accountsStore.addAccount(
|
2020-09-22 00:58:52 +02:00
|
|
|
selectedInstance.value,
|
2020-09-19 01:34:04 +02:00
|
|
|
usernameController.text,
|
|
|
|
passwordController.text,
|
|
|
|
);
|
2020-09-22 00:58:52 +02:00
|
|
|
Navigator.of(context).pop();
|
2020-09-19 01:34:04 +02:00
|
|
|
} on Exception catch (err) {
|
2020-09-21 23:15:45 +02:00
|
|
|
scaffoldKey.currentState.showSnackBar(SnackBar(
|
2020-09-19 01:34:04 +02:00
|
|
|
content: Text(err.toString()),
|
|
|
|
));
|
|
|
|
}
|
2020-09-22 00:58:52 +02:00
|
|
|
loading.cancel();
|
2020-09-19 01:34:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Scaffold(
|
2020-09-21 23:15:45 +02:00
|
|
|
key: scaffoldKey,
|
2020-09-19 01:34:04 +02:00
|
|
|
appBar: AppBar(
|
2021-01-03 19:43:39 +01:00
|
|
|
leading: const CloseButton(),
|
2020-09-19 01:34:04 +02:00
|
|
|
actionsIconTheme: theme.iconTheme,
|
|
|
|
iconTheme: theme.iconTheme,
|
|
|
|
textTheme: theme.textTheme,
|
2020-09-19 23:19:49 +02:00
|
|
|
brightness: theme.brightness,
|
|
|
|
centerTitle: true,
|
2021-01-03 19:43:39 +01:00
|
|
|
title: const Text('Add account'),
|
2020-09-19 01:34:04 +02:00
|
|
|
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
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2020-09-19 01:34:04 +02:00
|
|
|
FlatButton(
|
|
|
|
onPressed: selectInstance,
|
2021-01-03 18:13:25 +01:00
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
|
|
|
),
|
2020-09-19 01:34:04 +02:00
|
|
|
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-19 01:34:04 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2020-09-22 01:21:03 +02:00
|
|
|
// TODO: add support for password managers
|
2020-09-19 01:34:04 +02:00
|
|
|
TextField(
|
|
|
|
autofocus: true,
|
|
|
|
controller: usernameController,
|
|
|
|
decoration: InputDecoration(
|
2020-09-19 23:19:49 +02:00
|
|
|
isDense: true,
|
|
|
|
contentPadding:
|
2021-01-03 19:43:39 +01:00
|
|
|
const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
|
2020-09-19 01:34:04 +02:00
|
|
|
border: OutlineInputBorder(
|
2020-09-19 23:19:49 +02:00
|
|
|
borderRadius: BorderRadius.circular(10),
|
2020-09-19 01:34:04 +02:00
|
|
|
),
|
|
|
|
labelText: 'Username or email',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 5),
|
|
|
|
TextField(
|
|
|
|
controller: passwordController,
|
|
|
|
obscureText: true,
|
|
|
|
decoration: InputDecoration(
|
2020-09-19 23:19:49 +02:00
|
|
|
isDense: true,
|
|
|
|
contentPadding:
|
2021-01-03 19:43:39 +01:00
|
|
|
const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
|
2020-09-19 01:34:04 +02:00
|
|
|
border: OutlineInputBorder(
|
2020-09-19 23:19:49 +02:00
|
|
|
borderRadius: BorderRadius.circular(10),
|
2020-09-19 01:34:04 +02:00
|
|
|
),
|
|
|
|
labelText: 'Password',
|
|
|
|
),
|
|
|
|
),
|
2020-09-19 23:19:49 +02:00
|
|
|
RaisedButton(
|
|
|
|
color: theme.accentColor,
|
2021-01-03 19:43:39 +01:00
|
|
|
padding: const EdgeInsets.all(0),
|
2020-09-19 23:19:49 +02:00
|
|
|
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,
|
2020-09-22 00:58:52 +02:00
|
|
|
child: !loading.loading
|
2021-01-03 19:43:39 +01:00
|
|
|
? const Text('Sign in')
|
2020-09-21 23:17:33 +02:00
|
|
|
: SizedBox(
|
|
|
|
width: 20,
|
|
|
|
height: 20,
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
valueColor:
|
|
|
|
AlwaysStoppedAnimation<Color>(theme.canvasColor),
|
|
|
|
),
|
|
|
|
),
|
2020-09-19 23:19:49 +02:00
|
|
|
),
|
|
|
|
FlatButton(
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
2020-09-23 23:27:57 +02:00
|
|
|
ul.launch('https://${selectedInstance.value}/login');
|
2020-09-19 23:19:49 +02:00
|
|
|
},
|
2021-01-03 19:43:39 +01:00
|
|
|
child: const Text('Register'),
|
2020-09-19 23:19:49 +02:00
|
|
|
),
|
2020-09-19 01:34:04 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|