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 {
|
|
|
|
final String instanceUrl;
|
2020-09-21 23:15:45 +02:00
|
|
|
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
|
2020-09-19 01:34:04 +02:00
|
|
|
|
2020-09-21 23:15:45 +02:00
|
|
|
AddAccountPage({@required this.instanceUrl}) : assert(instanceUrl != 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-09-19 01:34:04 +02:00
|
|
|
final selectedInstance = useState(instanceUrl);
|
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 19:05:00 +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(
|
|
|
|
leading: Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: Icon(Icons.add),
|
|
|
|
),
|
|
|
|
title: 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 {
|
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(
|
|
|
|
leading: CloseButton(),
|
|
|
|
actionsIconTheme: theme.iconTheme,
|
|
|
|
iconTheme: theme.iconTheme,
|
|
|
|
textTheme: theme.textTheme,
|
2020-09-19 23:19:49 +02:00
|
|
|
brightness: theme.brightness,
|
|
|
|
centerTitle: true,
|
2020-09-19 01:34:04 +02:00
|
|
|
title: 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)
|
|
|
|
SizedBox(height: 150)
|
|
|
|
else
|
|
|
|
SizedBox(
|
|
|
|
height: 150,
|
|
|
|
child: FullscreenableImage(
|
|
|
|
url: icon.value,
|
|
|
|
child: CachedNetworkImage(
|
|
|
|
imageUrl: icon.value,
|
2020-09-29 21:45:18 +02:00
|
|
|
errorWidget: (_, __, ___) => SizedBox.shrink(),
|
2020-09-20 00:15:36 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2020-09-19 01:34:04 +02:00
|
|
|
FlatButton(
|
|
|
|
onPressed: selectInstance,
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Text(selectedInstance.value),
|
|
|
|
Icon(Icons.arrow_drop_down),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
|
|
|
),
|
|
|
|
),
|
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:
|
|
|
|
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:
|
|
|
|
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,
|
|
|
|
padding: EdgeInsets.all(0),
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
|
|
|
),
|
2020-09-22 00:58:52 +02:00
|
|
|
child: !loading.loading
|
2020-09-19 23:19:49 +02:00
|
|
|
? 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
|
|
|
onPressed: usernameController.text.isEmpty ||
|
|
|
|
passwordController.text.isEmpty
|
|
|
|
? null
|
2020-09-22 01:00:31 +02:00
|
|
|
: loading.pending ? () {} : handleOnAdd,
|
2020-09-19 23:19:49 +02:00
|
|
|
),
|
|
|
|
FlatButton(
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
|
|
|
),
|
|
|
|
child: Text('Register'),
|
|
|
|
onPressed: () {
|
2020-09-23 23:27:57 +02:00
|
|
|
ul.launch('https://${selectedInstance.value}/login');
|
2020-09-19 23:19:49 +02:00
|
|
|
},
|
|
|
|
),
|
2020-09-19 01:34:04 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|