app-mirror-github/lib/pages/home.dart

226 lines
7.3 KiB
Dart
Raw Normal View History

2018-09-27 15:55:56 +00:00
import 'dart:async';
import 'package:flutter/cupertino.dart';
2018-09-15 15:01:45 +00:00
import 'package:flutter/material.dart';
import 'package:after_layout/after_layout.dart';
import 'package:provider/provider.dart';
2018-09-27 15:55:56 +00:00
import 'package:vikunja_app/components/AddDialog.dart';
import 'package:vikunja_app/components/ErrorDialog.dart';
2019-03-16 13:29:00 +00:00
import 'package:vikunja_app/pages/namespace/namespace.dart';
2019-03-18 17:00:34 +00:00
import 'package:vikunja_app/pages/namespace/namespace_edit.dart';
import 'package:vikunja_app/pages/landing_page.dart';
2018-09-22 20:56:16 +00:00
import 'package:vikunja_app/global.dart';
import 'package:vikunja_app/models/namespace.dart';
2022-04-12 22:32:21 +00:00
import 'package:vikunja_app/pages/settings.dart';
import 'package:vikunja_app/stores/list_store.dart';
2018-09-15 15:01:45 +00:00
class HomePage extends StatefulWidget {
@override
2021-03-15 07:31:38 +00:00
State<StatefulWidget> createState() => HomePageState();
2018-09-15 15:01:45 +00:00
}
class HomePageState extends State<HomePage> with AfterLayoutMixin<HomePage> {
List<Namespace> _namespaces = [];
2019-03-18 16:56:15 +00:00
2022-08-27 21:04:43 +00:00
Namespace? get _currentNamespace =>
_selectedDrawerIndex >= 0 && _selectedDrawerIndex < _namespaces.length
? _namespaces[_selectedDrawerIndex]
: null;
int _selectedDrawerIndex = -1, _previousDrawerIndex = -1;
2018-09-27 15:55:56 +00:00
bool _loading = true;
2019-03-18 16:56:15 +00:00
bool _showUserDetails = false;
2022-08-27 21:04:43 +00:00
Widget? drawerItem;
2018-09-15 15:01:45 +00:00
@override
void afterFirstLayout(BuildContext context) {
2019-03-16 13:29:00 +00:00
_loadNamespaces();
2018-09-15 15:01:45 +00:00
}
2019-03-18 16:56:15 +00:00
Widget _namespacesWidget() {
List<Widget> namespacesList = <Widget>[];
_namespaces
.asMap()
2019-03-18 16:56:15 +00:00
.forEach((i, namespace) => namespacesList.add(new ListTile(
leading: const Icon(Icons.folder),
2022-09-03 15:43:16 +00:00
title: new Text(namespace.title),
selected: i == _selectedDrawerIndex,
onTap: () => _onSelectItem(i),
)));
2019-03-18 16:56:15 +00:00
return this._loading
? Center(child: CircularProgressIndicator())
: RefreshIndicator(
child: ListView(
padding: EdgeInsets.zero,
children: ListTile.divideTiles(
context: context, tiles: namespacesList)
.toList()),
onRefresh: _loadNamespaces,
);
}
Widget _userDetailsWidget(BuildContext context) {
return ListView(padding: EdgeInsets.zero, children: <Widget>[
ListTile(
title: Text('Logout'),
leading: Icon(Icons.exit_to_app),
onTap: () {
VikunjaGlobal.of(context).logoutUser(context);
},
),
2022-04-12 22:32:21 +00:00
ListTile(
title: Text('Settings'),
leading: Icon(Icons.settings),
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => SettingsPage()))
.whenComplete(() => setState(() {
//returning from settings, this needs to be force-refreshed
drawerItem = _getDrawerItemWidget(_selectedDrawerIndex,
forceReload: true);
}));
2022-04-12 22:32:21 +00:00
},
)
2019-03-18 16:56:15 +00:00
]);
}
@override
Widget build(BuildContext context) {
2022-09-03 15:43:16 +00:00
final currentUser = VikunjaGlobal.of(context).currentUser;
if (_selectedDrawerIndex != _previousDrawerIndex || drawerItem == null)
drawerItem = _getDrawerItemWidget(_selectedDrawerIndex);
2019-03-18 16:56:15 +00:00
2018-09-15 15:01:45 +00:00
return new Scaffold(
2019-03-18 17:00:34 +00:00
appBar: AppBar(
title: new Text(_currentNamespace?.title ?? 'Vikunja'),
2019-03-18 17:00:34 +00:00
actions: _currentNamespace == null
? null
: <Widget>[
IconButton(
icon: Icon(Icons.edit),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NamespaceEditPage(
2022-08-27 21:04:43 +00:00
namespace: _currentNamespace!,
))).whenComplete(() => _loadNamespaces()))
2019-03-18 17:00:34 +00:00
],
),
2022-09-03 15:43:16 +00:00
drawer: Drawer(
child: Column(children: <Widget>[
UserAccountsDrawerHeader(
accountName: currentUser != null
? Text(currentUser.username)
: null,
accountEmail: currentUser != null
? Text(currentUser.name)
: null,
2019-03-18 16:56:15 +00:00
onDetailsPressed: () {
setState(() {
_showUserDetails = !_showUserDetails;
});
},
2018-09-16 19:47:53 +00:00
currentAccountPicture: currentUser == null
? null
: CircleAvatar(
2022-04-10 13:31:56 +00:00
//backgroundImage: NetworkImage(currentUser.avatarUrl(context)),
),
2018-09-15 17:40:59 +00:00
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/graphics/hypnotize.png"),
repeat: ImageRepeat.repeat,
colorFilter: ColorFilter.mode(
Theme.of(context).primaryColor, BlendMode.multiply)),
2018-09-15 17:40:59 +00:00
),
),
2022-09-03 15:43:16 +00:00
Builder(
2019-03-18 16:56:15 +00:00
builder: (BuildContext context) => Expanded(
child: _showUserDetails
? _userDetailsWidget(context)
: _namespacesWidget())),
2022-09-03 15:43:16 +00:00
Align(
alignment: FractionalOffset.bottomLeft,
child: Builder(
builder: (context) => ListTile(
leading: Icon(Icons.house),
onTap: () {
Navigator.of(context).pop();
setState(() => _selectedDrawerIndex = -1);
},
),
),
),
2022-09-03 15:43:16 +00:00
Align(
alignment: FractionalOffset.bottomCenter,
2019-03-15 06:52:50 +00:00
child: Builder(
builder: (context) => ListTile(
leading: const Icon(Icons.add),
title: const Text('Add namespace...'),
onTap: () => _addNamespaceDialog(context),
),
),
),
])),
body: drawerItem,
2018-09-15 15:01:45 +00:00
);
}
_getDrawerItemWidget(int pos, {bool forceReload = false}) {
_previousDrawerIndex = pos;
if (pos == -1) {
//return forceReload
// ? new LandingPage(key: UniqueKey())
// : new LandingPage();
return ChangeNotifierProvider<ListProvider>(
create: (_) => new ListProvider(),
child: forceReload ? LandingPage(key: UniqueKey()) : LandingPage(),
);
}
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) {
2022-09-03 15:43:16 +00:00
final currentUser = VikunjaGlobal.of(context).currentUser;
if (currentUser == null) {
return;
}
VikunjaGlobal.of(context)
.namespaceService
2022-09-03 15:43:16 +00:00
.create(Namespace(title: name, owner: currentUser))
.then((_) {
_loadNamespaces();
2022-04-10 13:31:56 +00:00
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('The namespace was created successfully!'),
));
}).catchError((error) => showDialog(
context: context, builder: (context) => ErrorDialog(error: error)));
}
Future<void> _loadNamespaces() {
return VikunjaGlobal.of(context).namespaceService.getAll().then((result) {
setState(() {
_loading = false;
_namespaces = result;
});
});
}
2018-09-15 15:10:34 +00:00
}