realtime updating dashboard
This commit is contained in:
@@ -16,6 +16,13 @@ void main() async {
|
||||
ChangeNotifierProvider(
|
||||
create: (_) => ChecklistProvider(),
|
||||
),
|
||||
StreamProvider.value(
|
||||
value: DbHelper.checklistChangeEventStream,
|
||||
initialData: null,
|
||||
),
|
||||
StreamProvider.value(
|
||||
value: DbHelper.selectedChecklistChangeEventStreamById,
|
||||
initialData: null),
|
||||
], child: const MyApp()));
|
||||
}
|
||||
|
||||
@@ -24,6 +31,8 @@ class MyApp extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
DbHelper.initStreams(context);
|
||||
|
||||
return MaterialApp(
|
||||
title: 'Briessenchecker',
|
||||
theme: ThemeData.dark(
|
||||
|
||||
@@ -15,9 +15,10 @@ class DashboardPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _DashboardPageState extends State<DashboardPage> {
|
||||
final Future<List<Checklist>> checklistFuture = DbHelper.fetchChecklist;
|
||||
Future<List<Checklist>> checklistFuture = DbHelper.fetchChecklist;
|
||||
late List<Checklist> checklists;
|
||||
late ChecklistProvider checklistProvider;
|
||||
late Stream<List<Map<String, dynamic>>> clChangeStream;
|
||||
|
||||
int? _selectedChecklistIndex;
|
||||
|
||||
@@ -39,11 +40,10 @@ class _DashboardPageState extends State<DashboardPage> {
|
||||
BuildContext context, AsyncSnapshot<List<Checklist>> snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
checklists = snapshot.data!;
|
||||
return ListView.builder(
|
||||
itemCount: snapshot.data!.length,
|
||||
itemBuilder: (context, index) =>
|
||||
_listBuilder(context, index, snapshot.data!),
|
||||
);
|
||||
return StreamBuilder(
|
||||
stream: DbHelper.checklistChangeEventStream,
|
||||
builder: (context, snapshot) =>
|
||||
_streamBuilder(context, snapshot, checklists));
|
||||
} else if (snapshot.hasError) {
|
||||
return Text(snapshot.error.toString());
|
||||
} else {
|
||||
@@ -89,11 +89,14 @@ class _DashboardPageState extends State<DashboardPage> {
|
||||
void _onDeleteTapped() {
|
||||
DbHelper.deleteChecklistByid(
|
||||
checklists.elementAt(_selectedChecklistIndex!).id);
|
||||
_selectedChecklistIndex = null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
checklistProvider = Provider.of<ChecklistProvider>(context, listen: true);
|
||||
clChangeStream = DbHelper.checklistChangeEventStream;
|
||||
clChangeStream.listen(_onClChanged);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Brießenchecker9000'),
|
||||
@@ -110,4 +113,22 @@ class _DashboardPageState extends State<DashboardPage> {
|
||||
),
|
||||
floatingActionButton: _fabBuilder);
|
||||
}
|
||||
|
||||
void _onClChanged(List<Map<String, dynamic>> res) {
|
||||
checklists = DbHelper.resToList(res);
|
||||
}
|
||||
|
||||
Widget _streamBuilder(
|
||||
BuildContext context,
|
||||
AsyncSnapshot<List<Map<String, dynamic>>> snapshot,
|
||||
List<Checklist> checklists) {
|
||||
if (snapshot.hasData) {
|
||||
checklists = DbHelper.resToList(snapshot.data!);
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
itemCount: checklists.length,
|
||||
itemBuilder: (context, index) => _listBuilder(context, index, checklists),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import 'package:briessenchecker/services/checklist_provider.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import '../models/checklist.dart';
|
||||
import '../models/listitem.dart';
|
||||
import 'package:provider/provider.dart' as provider;
|
||||
|
||||
class DbHelper {
|
||||
static const checkedItemsTableName = 'checkedItems';
|
||||
@@ -10,6 +13,7 @@ class DbHelper {
|
||||
static const itemsTableName = 'items';
|
||||
|
||||
static late final SupabaseClient _client;
|
||||
static ChecklistProvider? clProvider;
|
||||
|
||||
static Future<void> init() async {
|
||||
await Supabase.initialize(
|
||||
@@ -18,6 +22,11 @@ class DbHelper {
|
||||
_client = Supabase.instance.client;
|
||||
}
|
||||
|
||||
static void initStreams(BuildContext context) {
|
||||
clProvider =
|
||||
provider.Provider.of<ChecklistProvider>(context, listen: false);
|
||||
}
|
||||
|
||||
static Future<void> login(String email, String password) async {
|
||||
email = 'sites@skup.in';
|
||||
password = 'pass';
|
||||
@@ -161,4 +170,27 @@ class DbHelper {
|
||||
|
||||
static Stream<AuthState> get authChangeEventStream =>
|
||||
_client.auth.onAuthStateChange;
|
||||
|
||||
static Stream<List<Map<String, dynamic>>> get checklistChangeEventStream =>
|
||||
_client.from(checklistsTableName).stream(primaryKey: ['id']);
|
||||
|
||||
static Stream<List<Map<String, dynamic>>>
|
||||
get selectedChecklistChangeEventStreamById => _client
|
||||
.from(checklistsTableName)
|
||||
.stream(primaryKey: ['id']).eq('id', clProvider?.selectedChecklistId);
|
||||
|
||||
static List<Checklist> resToList(List<Map<String, dynamic>> res) {
|
||||
List<Checklist> checklists = [];
|
||||
for (final element in res) {
|
||||
Checklist cl = Checklist(
|
||||
element['id'],
|
||||
element['owner_id'],
|
||||
element['title'],
|
||||
element['description'],
|
||||
DateTime.parse(element['created_time']),
|
||||
);
|
||||
checklists.add(cl);
|
||||
}
|
||||
return checklists;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user