added adding instances

This commit is contained in:
shilangyu 2020-09-08 00:34:09 +02:00
parent ed6c00172c
commit 1dee2e21f5
3 changed files with 65 additions and 14 deletions

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:provider/provider.dart';
@ -77,12 +78,16 @@ class _AppearanceConfig extends StatelessWidget {
}
}
class _AccountsConfig extends StatelessWidget {
class _AccountsConfig extends HookWidget {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var textFieldController = useTextEditingController();
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
backgroundColor: theme.scaffoldBackgroundColor,
shadowColor: Colors.transparent,
@ -90,6 +95,48 @@ class _AccountsConfig extends StatelessWidget {
title: Text('Accounts', style: theme.textTheme.headline6),
centerTitle: true,
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
await showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text('Add instance'),
content: TextField(
controller: textFieldController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Instance url',
),
),
actions: <Widget>[
FlatButton(
child: Text('Add'),
onPressed: () async {
try {
await context
.read<AccountsStore>()
.addInstance(textFieldController.text);
} on Exception catch (err) {
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(err.toString()),
));
}
Navigator.of(context).pop();
},
),
FlatButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
)
],
),
);
textFieldController.clear();
},
child: Icon(Icons.add),
),
body: Observer(
builder: (ctx) {
var accountsStore = ctx.watch<AccountsStore>();

View File

@ -139,13 +139,21 @@ abstract class _AccountsStore with Store {
tokens[instanceUrl][userData.name] = token;
}
/// adds a new instance with no accounts associated with it
/// adds a new instance with no accounts associated with it.
/// Additionally makes a test GET /site request to check if the instance exists
@action
void addInstance(String instanceUrl) {
Future<void> addInstance(String instanceUrl) async {
if (users.containsKey(instanceUrl)) {
throw Exception('This instance has already been added');
}
try {
await LemmyApi(instanceUrl).v1.getSite();
// ignore: avoid_catches_without_on_clauses
} catch (_) {
throw Exception('This instance seems to not exist');
}
users[instanceUrl] = ObservableMap();
tokens[instanceUrl] = ObservableMap();
}

View File

@ -93,6 +93,13 @@ mixin _$AccountsStore on _AccountsStore, Store {
.run(() => super.addAccount(instanceUrl, usernameOrEmail, password));
}
final _$addInstanceAsyncAction = AsyncAction('_AccountsStore.addInstance');
@override
Future<void> addInstance(String instanceUrl) {
return _$addInstanceAsyncAction.run(() => super.addInstance(instanceUrl));
}
final _$_AccountsStoreActionController =
ActionController(name: '_AccountsStore');
@ -118,17 +125,6 @@ mixin _$AccountsStore on _AccountsStore, Store {
}
}
@override
void addInstance(String instanceUrl) {
final _$actionInfo = _$_AccountsStoreActionController.startAction(
name: '_AccountsStore.addInstance');
try {
return super.addInstance(instanceUrl);
} finally {
_$_AccountsStoreActionController.endAction(_$actionInfo);
}
}
@override
String toString() {
return '''