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