Skip to content

Commit

Permalink
does this work for my web?
Browse files Browse the repository at this point in the history
  • Loading branch information
Desync-o-tron committed Sep 18, 2024
1 parent 0a125dd commit fe0337d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 115 deletions.
11 changes: 4 additions & 7 deletions lib/cloud_io/firestore_sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@ class MyStorage {
final FirebaseFirestore firestore = FirebaseFirestore.instance;

Future<void> 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<List<TrainingSession>> getUserTrainingHistoryStream({
Expand All @@ -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<String, dynamic>)..id = doc.id;
}).toList();
});
Expand Down
2 changes: 1 addition & 1 deletion lib/community/community_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class _SignInOrProfileWidgetState extends State<SignInOrProfileWidget> {
@override
Widget build(BuildContext context) {
FirebaseAuth.instance.authStateChanges().listen((User? user) {
setState(() {});
if (mounted) setState(() {});
});
if (FirebaseAuth.instance.currentUser == null) {
return Column(
Expand Down
81 changes: 19 additions & 62 deletions lib/history/history_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class _HistoryPageState extends State<HistoryPage> {
_loadMoreData();

_scrollController.addListener(() {
if (_scrollController.position.pixels >= _scrollController.position.maxScrollExtent - 200 &&
if (_scrollController.position.pixels >= _scrollController.position.maxScrollExtent - 400 &&
!_isLoading &&
_hasMore) {
_loadMoreData();
Expand Down Expand Up @@ -75,10 +75,13 @@ class _HistoryPageState extends State<HistoryPage> {
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?
}
});
}

Expand Down Expand Up @@ -111,7 +114,17 @@ class _HistoryPageState extends State<HistoryPage> {
} 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!"),
);
}
},
);
Expand Down Expand Up @@ -143,67 +156,11 @@ class _HistoryPageState extends State<HistoryPage> {
},
child: const Text("dont click me"),
)),
// PopupMenuItem<String>(
// 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<List<TrainingSession>>(
future: myStorage.getUserTrainingHistory(),
builder: (BuildContext context, AsyncSnapshot<List<TrainingSession>> 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<List<TrainingSession>> 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});
Expand Down
2 changes: 0 additions & 2 deletions lib/history/import_training_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
45 changes: 2 additions & 43 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -18,7 +17,7 @@ Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await ExDB.init();

FirebaseApp firebase = await Firebase.initializeApp(
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);

Expand All @@ -32,49 +31,9 @@ Future<void> main() async {

Timer? trainingDurationTimer;

class MyApp extends StatefulWidget {
class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
// _myAsyncMethod() async {
// var ff = FirebaseFirestore.instance;
// final user = <String, dynamic>{"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(
Expand Down

0 comments on commit fe0337d

Please sign in to comment.