diff --git a/lib/pages/dashboard_page.dart b/lib/pages/dashboard_page.dart index f67c1f8..3e3d9a1 100644 --- a/lib/pages/dashboard_page.dart +++ b/lib/pages/dashboard_page.dart @@ -115,7 +115,7 @@ class _DashboardPageState extends State { } void _onClChanged(List> res) { - checklists = DbHelper.resToList(res); + checklists = DbHelper.resToChecklistList(res); } Widget _streamBuilder( @@ -123,7 +123,7 @@ class _DashboardPageState extends State { AsyncSnapshot>> snapshot, List checklists) { if (snapshot.hasData) { - checklists = DbHelper.resToList(snapshot.data!); + checklists = DbHelper.resToChecklistList(snapshot.data!); } return ListView.builder( diff --git a/lib/pages/detail_checklist_page.dart b/lib/pages/detail_checklist_page.dart index dd366e3..f212808 100644 --- a/lib/pages/detail_checklist_page.dart +++ b/lib/pages/detail_checklist_page.dart @@ -23,6 +23,7 @@ class _DetailChecklistPageState extends State { Checklist? _checklist; late Future> _checklistFutures; late final ChecklistProvider _checklistProvider; + TextEditingController titleController = TextEditingController(); List _items = []; int? _selectedItemId; bool _titleEditMode = false; @@ -52,23 +53,11 @@ class _DetailChecklistPageState extends State { if (snapshot.hasData) { _checklist = snapshot.data!.first as Checklist; _items = snapshot.data!.last as List; - String title = _checklist!.title; - if (pageTitle == null) { - WidgetsBinding.instance - .addPostFrameCallback((_) => setState(() => pageTitle = title)); - } - return Column( - children: [ - Text(_checklist!.description), - SizedBox( - width: 500, - height: 500, - child: ListView.builder( - itemCount: _items.length, - itemBuilder: _itemListBuilder, - ), - ), - ], + return StreamBuilder( + stream: DbHelper.itemsChangeEventStream, + builder: (BuildContext context, + AsyncSnapshot>> snapshot) => + _streamBuilder(context, snapshot, _checklist, _items), ); } else if (snapshot.hasError) { return Text('Ooooops, ${snapshot.error}'); @@ -172,35 +161,17 @@ class _DetailChecklistPageState extends State { } Widget get _pageTitleBuilder { - TextEditingController titleController = TextEditingController(); if (!_titleEditMode) { return GestureDetector( onTap: () => setState(() => _titleEditMode = true), child: Text(pageTitle ?? '')); } else { titleController.text = pageTitle ?? ''; - return Row( - crossAxisAlignment: CrossAxisAlignment.end, - children: [ - SizedBox( - width: 300, - height: Theme.of(context).appBarTheme.toolbarHeight, - child: TextField( - autofocus: true, - controller: titleController, - ), - ), - IconButton( - onPressed: () => _onTitleChanged( - _checklist!.id, - titleController.text, - ), - icon: const Icon(Icons.check), - ), - IconButton( - onPressed: () => setState(() => _titleEditMode = false), - icon: const Icon(Icons.cancel_outlined)) - ], + return Expanded( + child: TextField( + autofocus: true, + controller: titleController, + ), ); } } @@ -219,10 +190,22 @@ class _DetailChecklistPageState extends State { appBar: AppBar( title: _pageTitleBuilder, actions: [ - if (selectedItemIndexes.isNotEmpty) + if (selectedItemIndexes.isNotEmpty && !_titleEditMode) IconButton( onPressed: _onDeleteItemsPressed, - icon: const Icon(Icons.delete)) + icon: const Icon(Icons.delete)), + if (_titleEditMode) + IconButton( + onPressed: () => _onTitleChanged( + _checklist!.id, + titleController.text, + ), + icon: const Icon(Icons.check), + ), + if (_titleEditMode) + IconButton( + onPressed: () => setState(() => _titleEditMode = false), + icon: const Icon(Icons.cancel_outlined)), ], ), body: FutureBuilder( @@ -235,4 +218,31 @@ class _DetailChecklistPageState extends State { ), ); } + + _streamBuilder( + BuildContext context, + AsyncSnapshot>> snapshot, + Checklist? checklist, + List items) { + if (pageTitle != _checklist!.title) { + WidgetsBinding.instance.addPostFrameCallback( + (_) => setState(() => pageTitle = _checklist!.title)); + } + if (snapshot.hasData) { + _items = DbHelper.resToItemList(snapshot.data!); + } + return Column( + children: [ + Text(_checklist!.description), + SizedBox( + width: 500, + height: 500, + child: ListView.builder( + itemCount: _items.length, + itemBuilder: _itemListBuilder, + ), + ), + ], + ); + } } diff --git a/lib/services/dbhelper.dart b/lib/services/dbhelper.dart index e6ea0c5..896c0c9 100644 --- a/lib/services/dbhelper.dart +++ b/lib/services/dbhelper.dart @@ -179,7 +179,11 @@ class DbHelper { .from(checklistsTableName) .stream(primaryKey: ['id']).eq('id', clProvider?.selectedChecklistId); - static List resToList(List> res) { + static Stream>> get itemsChangeEventStream => + _client.from(itemsTableName).stream(primaryKey: ['id']).eq( + 'checklist_id', clProvider?.selectedChecklistId); + + static List resToChecklistList(List> res) { List checklists = []; for (final element in res) { Checklist cl = Checklist( @@ -193,4 +197,22 @@ class DbHelper { } return checklists; } + + static List resToItemList(List> res) { + final List items = []; + + for (final item in res) { + items.add( + Item( + item['id'], + item['owner_id'], + item['title'], + item['description'], + DateTime.parse(item['created_time']), + null, + ), + ); + } + return items; + } } diff --git a/lib/widgets/item_list_tile.dart b/lib/widgets/item_list_tile.dart index 7a69ee7..c5ddcc6 100644 --- a/lib/widgets/item_list_tile.dart +++ b/lib/widgets/item_list_tile.dart @@ -10,19 +10,28 @@ class ItemListTile extends StatefulWidget { required this.onTap, required this.itemSelectionChanged, required this.selectionMode, + this.isSelected = false, }); final String title; final String description; final bool selectionMode; final VoidCallback onTap; final BoolCallback itemSelectionChanged; + final bool isSelected; @override State createState() => _ItemListTileState(); } class _ItemListTileState extends State { - bool isSelected = false; + late bool isSelected; + + @override + void initState() { + super.initState(); + isSelected = widget.isSelected; + } + @override Widget build(BuildContext context) { return ListTile(