2020-09-20 20:09:41 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2020-09-19 01:34:04 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2020-09-20 20:09:41 +02:00
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
2020-09-19 01:34:04 +02:00
|
|
|
|
2020-09-20 20:09:41 +02:00
|
|
|
import '../hooks/debounce.dart';
|
|
|
|
import '../hooks/delayed_loading.dart';
|
2020-09-19 01:34:04 +02:00
|
|
|
import '../hooks/stores.dart';
|
2020-09-20 20:09:41 +02:00
|
|
|
import '../widgets/fullscreenable_image.dart';
|
2020-09-19 01:34:04 +02:00
|
|
|
|
|
|
|
class AddInstancePage extends HookWidget {
|
2020-09-21 23:15:45 +02:00
|
|
|
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
|
|
|
|
|
|
|
|
AddInstancePage();
|
|
|
|
|
2020-09-19 01:34:04 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
final instanceController = useTextEditingController();
|
|
|
|
useValueListenable(instanceController);
|
|
|
|
final accountsStore = useAccountsStore();
|
2020-09-21 01:00:38 +02:00
|
|
|
final delayedLoading = useDelayedLoading(Duration(milliseconds: 500));
|
2020-09-19 01:34:04 +02:00
|
|
|
|
2020-09-20 20:09:41 +02:00
|
|
|
final isSite = useState<bool>(null);
|
2020-09-19 01:34:04 +02:00
|
|
|
final loading = useState(false);
|
2020-09-20 20:09:41 +02:00
|
|
|
final icon = useState<String>(null);
|
2020-09-21 23:49:20 +02:00
|
|
|
final prevInput = usePrevious<String>(instanceController.text);
|
2020-09-21 01:00:38 +02:00
|
|
|
final debounce = useDebounce(() async {
|
2020-09-21 23:49:20 +02:00
|
|
|
if (prevInput == instanceController.text) return;
|
2020-09-21 01:00:38 +02:00
|
|
|
if (instanceController.text.isEmpty) {
|
|
|
|
isSite.value = null;
|
|
|
|
return;
|
2020-09-20 20:09:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-21 01:00:38 +02:00
|
|
|
try {
|
|
|
|
icon.value =
|
|
|
|
(await LemmyApi(instanceController.text).v1.getSite()).site.icon;
|
|
|
|
isSite.value = true;
|
|
|
|
} catch (e) {
|
|
|
|
isSite.value = false;
|
2020-09-20 20:09:41 +02:00
|
|
|
}
|
2020-09-21 01:00:38 +02:00
|
|
|
});
|
2020-09-20 20:09:41 +02:00
|
|
|
|
2020-09-21 01:00:38 +02:00
|
|
|
instanceController.addListener(debounce);
|
2020-09-19 01:34:04 +02:00
|
|
|
|
|
|
|
handleOnAdd() async {
|
2020-09-20 20:09:41 +02:00
|
|
|
delayedLoading.start();
|
2020-09-19 01:34:04 +02:00
|
|
|
try {
|
|
|
|
loading.value = true;
|
|
|
|
await accountsStore.addInstance(instanceController.text);
|
|
|
|
Navigator.of(context).pop(instanceController.text);
|
|
|
|
} 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-20 20:09:41 +02:00
|
|
|
} catch (err) {
|
2020-09-21 23:15:45 +02:00
|
|
|
scaffoldKey.currentState.showSnackBar(SnackBar(
|
2020-09-20 20:09:41 +02:00
|
|
|
content: Text('UNKNOWN ERROR ${err.toString()}'),
|
|
|
|
));
|
2020-09-19 01:34:04 +02:00
|
|
|
}
|
2020-09-20 20:09:41 +02:00
|
|
|
delayedLoading.cancel();
|
2020-09-19 01:34:04 +02:00
|
|
|
loading.value = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Scaffold(
|
2020-09-21 23:15:45 +02:00
|
|
|
key: scaffoldKey,
|
2020-09-19 01:34:04 +02:00
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: theme.scaffoldBackgroundColor,
|
2020-09-19 23:19:49 +02:00
|
|
|
brightness: theme.brightness,
|
2020-09-19 01:34:04 +02:00
|
|
|
shadowColor: Colors.transparent,
|
|
|
|
iconTheme: theme.iconTheme,
|
2020-09-19 23:19:49 +02:00
|
|
|
centerTitle: true,
|
2020-09-19 01:34:04 +02:00
|
|
|
leading: CloseButton(),
|
|
|
|
actionsIconTheme: theme.iconTheme,
|
|
|
|
textTheme: theme.textTheme,
|
|
|
|
title: Text('Add instance'),
|
|
|
|
),
|
2020-09-19 23:19:49 +02:00
|
|
|
bottomNavigationBar: BottomAppBar(
|
|
|
|
color: Colors.transparent,
|
|
|
|
elevation: 0,
|
|
|
|
child: SafeArea(
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(15),
|
|
|
|
child: FlatButton(
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
|
|
|
),
|
2020-09-19 01:34:04 +02:00
|
|
|
child: Text('Cancel'),
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
),
|
2020-09-19 23:19:49 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2020-09-19 01:34:04 +02:00
|
|
|
),
|
|
|
|
),
|
2020-09-19 23:19:49 +02:00
|
|
|
body: ListView(
|
|
|
|
children: [
|
2020-09-20 20:09:41 +02:00
|
|
|
if (isSite.value == true)
|
|
|
|
SizedBox(
|
|
|
|
height: 150,
|
|
|
|
child: FullscreenableImage(
|
2020-09-21 23:49:46 +02:00
|
|
|
child: CachedNetworkImage(
|
|
|
|
imageUrl: icon.value,
|
|
|
|
errorWidget: (_, __, ___) => Container(),
|
|
|
|
),
|
2020-09-20 20:09:41 +02:00
|
|
|
url: icon.value,
|
|
|
|
))
|
|
|
|
else if (isSite.value == false)
|
|
|
|
SizedBox(
|
|
|
|
height: 150,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Icon(Icons.close, color: Colors.red),
|
|
|
|
Text('instance not found')
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
else
|
|
|
|
SizedBox(height: 150),
|
|
|
|
SizedBox(height: 15),
|
2020-09-19 23:19:49 +02:00
|
|
|
SizedBox(
|
|
|
|
height: 40,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 15),
|
|
|
|
child: TextField(
|
|
|
|
autofocus: true,
|
|
|
|
controller: instanceController,
|
2020-09-20 20:09:41 +02:00
|
|
|
autocorrect: false,
|
2020-09-19 23:19:49 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
isDense: true,
|
|
|
|
border: OutlineInputBorder(
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
|
|
|
),
|
|
|
|
labelText: 'instance url',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
SizedBox(height: 5),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 15),
|
|
|
|
child: SizedBox(
|
|
|
|
height: 40,
|
|
|
|
child: RaisedButton(
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
|
|
|
),
|
2020-09-20 20:09:41 +02:00
|
|
|
color: theme.accentColor,
|
|
|
|
child: !debounce.loading
|
2020-09-20 01:09:09 +02:00
|
|
|
? Text('Add')
|
|
|
|
: SizedBox(
|
|
|
|
height: 20,
|
|
|
|
width: 20,
|
2020-09-20 20:09:41 +02:00
|
|
|
child: CircularProgressIndicator(
|
|
|
|
valueColor:
|
|
|
|
AlwaysStoppedAnimation<Color>(theme.canvasColor),
|
|
|
|
),
|
2020-09-20 01:09:09 +02:00
|
|
|
),
|
2020-09-20 20:09:41 +02:00
|
|
|
onPressed: isSite.value == true ? handleOnAdd : null,
|
2020-09-19 23:19:49 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2020-09-19 01:34:04 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|