1
0
mirror of https://github.com/go-vikunja/app synced 2024-11-08 22:08:10 +00:00

Fixed namespaces loading every time a widget was loaded (#34)

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

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

@ -1,6 +1,8 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:after_layout/after_layout.dart';
import 'package:vikunja_app/components/AddDialog.dart';
import 'package:vikunja_app/components/GravatarImage.dart';
import 'package:vikunja_app/pages/namespace/namespace.dart';
@ -13,7 +15,7 @@ class HomePage extends StatefulWidget {
State<StatefulWidget> createState() => new HomePageState();
}
class HomePageState extends State<HomePage> {
class HomePageState extends State<HomePage> with AfterLayoutMixin<HomePage> {
List<Namespace> _namespaces = [];
Namespace get _currentNamespace =>
_selectedDrawerIndex >= 0 && _selectedDrawerIndex < _namespaces.length
@ -22,52 +24,8 @@ class HomePageState extends State<HomePage> {
int _selectedDrawerIndex = -1;
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
void didChangeDependencies() {
super.didChangeDependencies();
void afterFirstLayout(BuildContext context) {
_loadNamespaces();
}
@ -128,4 +86,47 @@ class HomePageState extends State<HomePage> {
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;
});
});
}
}

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

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

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