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';
|
2021-01-24 20:01:55 +01:00
|
|
|
import 'package:lemmy_api_client/v2.dart';
|
2020-09-19 01:34:04 +02:00
|
|
|
|
2020-09-20 20:09:41 +02:00
|
|
|
import '../hooks/debounce.dart';
|
2020-09-19 01:34:04 +02:00
|
|
|
import '../hooks/stores.dart';
|
2020-10-06 16:34:03 +02:00
|
|
|
import '../util/cleanup_url.dart';
|
2020-09-20 20:09:41 +02:00
|
|
|
import '../widgets/fullscreenable_image.dart';
|
2020-09-19 01:34:04 +02:00
|
|
|
|
2020-09-24 15:50:52 +02:00
|
|
|
/// A page that let's user add a new instance. Pops a url of the added instance
|
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-20 20:09:41 +02:00
|
|
|
final isSite = useState<bool>(null);
|
|
|
|
final icon = useState<String>(null);
|
2020-09-23 23:23:38 +02:00
|
|
|
final prevInput = usePrevious(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-24 15:38:54 +02:00
|
|
|
|
2020-10-06 16:19:03 +02:00
|
|
|
final inst = cleanUpUrl(instanceController.text);
|
2020-09-24 15:38:54 +02:00
|
|
|
if (inst.isEmpty) {
|
2020-09-21 01:00:38 +02:00
|
|
|
isSite.value = null;
|
|
|
|
return;
|
2020-09-20 20:09:41 +02:00
|
|
|
}
|
2020-09-21 01:00:38 +02:00
|
|
|
try {
|
2021-01-24 20:01:55 +01:00
|
|
|
icon.value = (await LemmyApiV2(inst).run(GetSite())).siteView.site.icon;
|
2020-09-21 01:00:38 +02:00
|
|
|
isSite.value = true;
|
2020-09-23 18:03:47 +02:00
|
|
|
// ignore: avoid_catches_without_on_clauses
|
2020-09-21 01:00:38 +02:00
|
|
|
} 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-23 23:31:34 +02:00
|
|
|
useEffect(() {
|
|
|
|
instanceController.addListener(debounce);
|
|
|
|
|
2020-09-24 15:01:20 +02:00
|
|
|
return () {
|
|
|
|
instanceController.removeListener(debounce);
|
|
|
|
};
|
2020-09-23 23:31:34 +02:00
|
|
|
}, []);
|
2020-10-06 16:19:03 +02:00
|
|
|
final inst = cleanUpUrl(instanceController.text);
|
2020-09-19 01:34:04 +02:00
|
|
|
handleOnAdd() async {
|
|
|
|
try {
|
2020-09-24 15:50:52 +02:00
|
|
|
await accountsStore.addInstance(inst, assumeValid: true);
|
|
|
|
Navigator.of(context).pop(inst);
|
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()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-01-03 19:43:39 +01:00
|
|
|
leading: const CloseButton(),
|
2020-09-19 01:34:04 +02:00
|
|
|
actionsIconTheme: theme.iconTheme,
|
|
|
|
textTheme: theme.textTheme,
|
2021-01-03 19:43:39 +01:00
|
|
|
title: const Text('Add instance'),
|
2020-09-19 01:34:04 +02:00
|
|
|
),
|
2020-09-19 23:19:49 +02:00
|
|
|
body: ListView(
|
|
|
|
children: [
|
2021-01-24 20:01:55 +01:00
|
|
|
if (isSite.value == true && icon.value != null)
|
2020-09-20 20:09:41 +02:00
|
|
|
SizedBox(
|
|
|
|
height: 150,
|
|
|
|
child: FullscreenableImage(
|
2021-01-03 18:13:25 +01:00
|
|
|
url: icon.value,
|
2020-09-21 23:49:46 +02:00
|
|
|
child: CachedNetworkImage(
|
|
|
|
imageUrl: icon.value,
|
2021-01-03 19:43:39 +01:00
|
|
|
errorWidget: (_, __, ___) => const SizedBox.shrink(),
|
2020-09-21 23:49:46 +02:00
|
|
|
),
|
2020-09-20 20:09:41 +02:00
|
|
|
))
|
|
|
|
else if (isSite.value == false)
|
|
|
|
SizedBox(
|
|
|
|
height: 150,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
2021-01-03 19:43:39 +01:00
|
|
|
children: const [
|
2020-09-20 20:09:41 +02:00
|
|
|
Icon(Icons.close, color: Colors.red),
|
|
|
|
Text('instance not found')
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
else
|
2021-01-03 19:43:39 +01:00
|
|
|
const SizedBox(height: 150),
|
|
|
|
const 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',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-01-03 19:43:39 +01:00
|
|
|
const SizedBox(height: 5),
|
2020-09-19 23:19:49 +02:00
|
|
|
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,
|
2021-01-03 18:13:25 +01:00
|
|
|
onPressed: isSite.value == true ? handleOnAdd : null,
|
2020-09-20 20:09:41 +02:00
|
|
|
child: !debounce.loading
|
2021-01-03 19:43:39 +01:00
|
|
|
? const Text('Add')
|
2020-09-20 01:09:09 +02:00
|
|
|
: 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-19 23:19:49 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2020-09-19 01:34:04 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|