Fixed namespaces loading every time a widget was loaded (#34)
continuous-integration/drone/push Build is passing Details

This commit is contained in:
konrad 2019-03-18 15:30:54 +00:00 committed by Gitea
parent 75f6608863
commit 9848c462f8
5 changed files with 68 additions and 58 deletions

View File

@ -12,7 +12,6 @@ class VikunjaApp extends StatelessWidget {
final Widget home; final Widget home;
const VikunjaApp({Key key, this.home}) : super(key: key); const VikunjaApp({Key key, this.home}) : super(key: key);
// This widget is the root of your application.
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new MaterialApp( return new MaterialApp(

View File

@ -1,6 +1,8 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:after_layout/after_layout.dart';
import 'package:vikunja_app/components/AddDialog.dart'; import 'package:vikunja_app/components/AddDialog.dart';
import 'package:vikunja_app/components/GravatarImage.dart'; import 'package:vikunja_app/components/GravatarImage.dart';
import 'package:vikunja_app/pages/namespace/namespace.dart'; import 'package:vikunja_app/pages/namespace/namespace.dart';
@ -13,7 +15,7 @@ class HomePage extends StatefulWidget {
State<StatefulWidget> createState() => new HomePageState(); State<StatefulWidget> createState() => new HomePageState();
} }
class HomePageState extends State<HomePage> { class HomePageState extends State<HomePage> with AfterLayoutMixin<HomePage> {
List<Namespace> _namespaces = []; List<Namespace> _namespaces = [];
Namespace get _currentNamespace => Namespace get _currentNamespace =>
_selectedDrawerIndex >= 0 && _selectedDrawerIndex < _namespaces.length _selectedDrawerIndex >= 0 && _selectedDrawerIndex < _namespaces.length
@ -22,52 +24,8 @@ class HomePageState extends State<HomePage> {
int _selectedDrawerIndex = -1; int _selectedDrawerIndex = -1;
bool _loading = true; bool _loading = true;
_getDrawerItemWidget(int pos) {
if (pos == -1) {
return new PlaceholderPage();
}
return new NamespacePage(namespace: _namespaces[pos]);
}
_onSelectItem(int index) {
setState(() => _selectedDrawerIndex = index);
Navigator.of(context).pop();
}
_addNamespaceDialog(BuildContext context) {
showDialog(
context: context,
builder: (_) => AddDialog(
onAdd: (name) => _addNamespace(name, context),
decoration: new InputDecoration(
labelText: 'Namespace', hintText: 'eg. Personal Namespace'),
));
}
_addNamespace(String name, BuildContext context) {
VikunjaGlobal.of(context)
.namespaceService
.create(Namespace(id: null, name: name))
.then((_) {
_loadNamespaces();
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('The namespace was created successfully!'),
));
});
}
Future<void> _loadNamespaces() {
return VikunjaGlobal.of(context).namespaceService.getAll().then((result) {
setState(() {
_loading = false;
_namespaces = result;
});
});
}
@override @override
void didChangeDependencies() { void afterFirstLayout(BuildContext context) {
super.didChangeDependencies();
_loadNamespaces(); _loadNamespaces();
} }
@ -128,4 +86,47 @@ class HomePageState extends State<HomePage> {
body: _getDrawerItemWidget(_selectedDrawerIndex), body: _getDrawerItemWidget(_selectedDrawerIndex),
); );
} }
_getDrawerItemWidget(int pos) {
if (pos == -1) {
return new PlaceholderPage();
}
return new NamespacePage(namespace: _namespaces[pos]);
}
_onSelectItem(int index) {
setState(() => _selectedDrawerIndex = index);
Navigator.of(context).pop();
}
_addNamespaceDialog(BuildContext context) {
showDialog(
context: context,
builder: (_) => AddDialog(
onAdd: (name) => _addNamespace(name, context),
decoration: new InputDecoration(
labelText: 'Namespace', hintText: 'eg. Personal Namespace'),
));
}
_addNamespace(String name, BuildContext context) {
VikunjaGlobal.of(context)
.namespaceService
.create(Namespace(id: null, name: name))
.then((_) {
_loadNamespaces();
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('The namespace was created successfully!'),
));
});
}
Future<void> _loadNamespaces() {
return VikunjaGlobal.of(context).namespaceService.getAll().then((result) {
setState(() {
_loading = false;
_namespaces = result;
});
});
}
} }

View File

@ -2,6 +2,8 @@ import 'dart:async';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:after_layout/after_layout.dart';
import 'package:vikunja_app/components/AddDialog.dart'; import 'package:vikunja_app/components/AddDialog.dart';
import 'package:vikunja_app/global.dart'; import 'package:vikunja_app/global.dart';
import 'package:vikunja_app/models/list.dart'; import 'package:vikunja_app/models/list.dart';
@ -17,10 +19,16 @@ class NamespacePage extends StatefulWidget {
_NamespacePageState createState() => new _NamespacePageState(); _NamespacePageState createState() => new _NamespacePageState();
} }
class _NamespacePageState extends State<NamespacePage> { class _NamespacePageState extends State<NamespacePage>
with AfterLayoutMixin<NamespacePage> {
List<TaskList> _lists = []; List<TaskList> _lists = [];
bool _loading = true; bool _loading = true;
@override
void afterFirstLayout(BuildContext context) {
_loadLists();
}
///// /////
// This essentially shows the lists. // This essentially shows the lists.
@override @override
@ -53,7 +61,7 @@ class _NamespacePageState extends State<NamespacePage> {
}, },
))).toList(), ))).toList(),
), ),
onRefresh: _updateLists, onRefresh: _loadLists,
) )
: Center(child: CircularProgressIndicator()), : Center(child: CircularProgressIndicator()),
floatingActionButton: Builder( floatingActionButton: Builder(
@ -63,20 +71,14 @@ class _NamespacePageState extends State<NamespacePage> {
); );
} }
@override
void didChangeDependencies() {
super.didChangeDependencies();
_updateLists();
}
Future _removeList(TaskList list) { Future _removeList(TaskList list) {
return VikunjaGlobal.of(context) return VikunjaGlobal.of(context)
.listService .listService
.delete(list.id) .delete(list.id)
.then((_) => _updateLists()); .then((_) => _loadLists());
} }
Future<void> _updateLists() { Future<void> _loadLists() {
return VikunjaGlobal.of(context) return VikunjaGlobal.of(context)
.listService .listService
.getByNamespace(widget.namespace.id) .getByNamespace(widget.namespace.id)
@ -107,7 +109,7 @@ class _NamespacePageState extends State<NamespacePage> {
.create(widget.namespace.id, TaskList(id: null, title: name, tasks: [])) .create(widget.namespace.id, TaskList(id: null, title: name, tasks: []))
.then((_) { .then((_) {
setState(() {}); setState(() {});
_updateLists(); _loadLists();
Scaffold.of(context).showSnackBar( Scaffold.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text('The list was successfully created!'), content: Text('The list was successfully created!'),

View File

@ -1,6 +1,13 @@
# Generated by pub # Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile # See https://www.dartlang.org/tools/pub/glossary#lockfile
packages: packages:
after_layout:
dependency: "direct main"
description:
name: after_layout
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.7"
archive: archive:
dependency: transitive dependency: transitive
description: description:

View File

@ -15,6 +15,7 @@ dependencies:
cupertino_icons: ^0.1.2 cupertino_icons: ^0.1.2
flutter_secure_storage: 3.1.1 flutter_secure_storage: 3.1.1
http: 0.12.0 http: 0.12.0
after_layout: ^1.0.7
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: