From fe0337d0018fd11f6685c4448d0cccaeac817574 Mon Sep 17 00:00:00 2001 From: Luke Knoble <35696371+Desync-o-tron@users.noreply.github.com> Date: Tue, 17 Sep 2024 22:52:16 -0400 Subject: [PATCH] does this work for my web? --- lib/cloud_io/firestore_sync.dart | 11 ++-- lib/community/community_page.dart | 2 +- lib/history/history_page.dart | 81 ++++++------------------- lib/history/import_training_dialog.dart | 2 - lib/main.dart | 45 +------------- 5 files changed, 26 insertions(+), 115 deletions(-) diff --git a/lib/cloud_io/firestore_sync.dart b/lib/cloud_io/firestore_sync.dart index 68f7c4b..4923bc0 100644 --- a/lib/cloud_io/firestore_sync.dart +++ b/lib/cloud_io/firestore_sync.dart @@ -13,13 +13,9 @@ class MyStorage { final FirebaseFirestore firestore = FirebaseFirestore.instance; Future addTrainingSessionToHistory(TrainingSession session) async { - try { - CollectionReference users = firestore.collection('users'); - DocumentReference userDoc = users.doc(FirebaseAuth.instance.currentUser!.uid); - await userDoc.collection(historyKey).add(session.toJson()); - } catch (e) { - print("Error adding session to history: $e"); - } + CollectionReference users = firestore.collection('users'); + DocumentReference userDoc = users.doc(FirebaseAuth.instance.currentUser!.uid); + userDoc.collection(historyKey).add(session.toJson()); } Stream> getUserTrainingHistoryStream({ @@ -45,6 +41,7 @@ class MyStorage { return query.snapshots().map((snapshot) { return snapshot.docs.map((doc) { + // print(doc.data()); return TrainingSession.fromJson(doc.data() as Map)..id = doc.id; }).toList(); }); diff --git a/lib/community/community_page.dart b/lib/community/community_page.dart index 2b5c24c..7872b87 100644 --- a/lib/community/community_page.dart +++ b/lib/community/community_page.dart @@ -29,7 +29,7 @@ class _SignInOrProfileWidgetState extends State { @override Widget build(BuildContext context) { FirebaseAuth.instance.authStateChanges().listen((User? user) { - setState(() {}); + if (mounted) setState(() {}); }); if (FirebaseAuth.instance.currentUser == null) { return Column( diff --git a/lib/history/history_page.dart b/lib/history/history_page.dart index c649dcb..f72d886 100644 --- a/lib/history/history_page.dart +++ b/lib/history/history_page.dart @@ -33,7 +33,7 @@ class _HistoryPageState extends State { _loadMoreData(); _scrollController.addListener(() { - if (_scrollController.position.pixels >= _scrollController.position.maxScrollExtent - 200 && + if (_scrollController.position.pixels >= _scrollController.position.maxScrollExtent - 400 && !_isLoading && _hasMore) { _loadMoreData(); @@ -75,10 +75,13 @@ class _HistoryPageState extends State { setState(() { _isLoading = false; }); - print('Error loading data: $error'); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('Error loading data: $error')), - ); + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('Error loading data: $error')), + ); + } else { + //todo error handling? + } }); } @@ -111,7 +114,17 @@ class _HistoryPageState extends State { } else if (_hasMore) { return const CircularProgressIndicator(); } else { - return Container(); + return Container( + decoration: BoxDecoration( + border: Border.all( + color: Colors.grey, + width: 2.0, + ), + borderRadius: BorderRadius.circular(8.0), + ), + padding: const EdgeInsets.all(16.0), + child: const Text("that's everything!"), + ); } }, ); @@ -143,67 +156,11 @@ class _HistoryPageState extends State { }, child: const Text("dont click me"), )), - // PopupMenuItem( - // value: 'resync history w/ server', - // child: ElevatedButton( - // onPressed: () { - // // have this do a pull down? - // // getUserTrainingHistory(); - // }, - // child: const Text("sync history?"), - // )), ], ); } } -/* -class HistoryPageOld extends StatelessWidget { - const HistoryPageOld({super.key}); - - @override - Widget build(BuildContext context) { - return FutureBuilder>( - future: myStorage.getUserTrainingHistory(), - builder: (BuildContext context, AsyncSnapshot> snapshot) { - String sessionCountText = ""; - if (snapshot.data != null) { - sessionCountText = " (${snapshot.data!.length} Sessions)"; - } - return Scaffold( - appBar: AppBar( - title: Text('History$sessionCountText'), - actions: [ - _hamburgerMenuActions(context), - ], - ), - body: _buildBody(snapshot), - ); - }, - ); - } - - Widget _buildBody(AsyncSnapshot> snapshot) { - if (snapshot.connectionState == ConnectionState.waiting) { - return const Center(child: CircularProgressIndicator()); - } else if (snapshot.hasError) { - return Center(child: Text('Error: ${snapshot.error}')); - } else if (!snapshot.hasData || snapshot.data!.isEmpty) { - return const Center(child: Text('No History')); - } else { - return ListView.builder( - itemCount: snapshot.data!.length, - itemBuilder: (context, index) { - return TrainingSessionHistoryCard(session: snapshot.data![index]); - }, - ); - } - } - - -} -*/ - class TrainingSessionHistoryCard extends StatelessWidget { final TrainingSession session; const TrainingSessionHistoryCard({required this.session, super.key}); diff --git a/lib/history/import_training_dialog.dart b/lib/history/import_training_dialog.dart index b9cee61..8a1f7cb 100644 --- a/lib/history/import_training_dialog.dart +++ b/lib/history/import_training_dialog.dart @@ -61,8 +61,6 @@ class ExternalAppTrainingImportDialog extends StatelessWidget { // https://github.com/miguelpruivo/flutter_file_picker/wiki/FAQ for (var session in sessions) { - print("test"); - print(session.toJson()); myStorage.addTrainingSessionToHistory(session); } diff --git a/lib/main.dart b/lib/main.dart index 35f1bf6..88fcfc4 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -18,7 +17,7 @@ Future main() async { WidgetsFlutterBinding.ensureInitialized(); await ExDB.init(); - FirebaseApp firebase = await Firebase.initializeApp( + await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); @@ -32,49 +31,9 @@ Future main() async { Timer? trainingDurationTimer; -class MyApp extends StatefulWidget { +class MyApp extends StatelessWidget { const MyApp({super.key}); - @override - State createState() => _MyAppState(); -} - -class _MyAppState extends State { - // _myAsyncMethod() async { - // var ff = FirebaseFirestore.instance; - // final user = {"first": "hmm!", "last": "Lovelace", "born": 1815}; - // // await ff.collection("users").get().then((event) { - // // for (var doc in event.docs) { - // // print("${doc.id} => ${doc.data()}"); - // // } - // // }); - // // Add a new document with a generated ID - // ff - // .collection("users") - // .add(user) - // .then((DocumentReference doc) => print('DocumentSnapshot added with ID: ${doc.id}')); - - // var usrCollection = ff.collection('users'); - // // var athing = await usrCollection.get(); - - // // await usrCollection.add({"aoeu": "sth"}); - // var usrdoc = usrCollection.doc("0qYo6ihSKsh8s6dsLksQ2N6WLek2"); - // await usrdoc.set({"buttttX new": "stuff"}); //, SetOptions()); - // try { - // var newcol = usrdoc.collection("newcol"); - // await newcol.add({"akey": "aval"}); - // } catch (e) { - // print('crap'); - // } - // print("object"); - // } - - // @override - // void initState() { - // super.initState(); - // _myAsyncMethod(); - // } - @override Widget build(BuildContext context) { return MultiBlocProvider(