Skip to content

Commit

Permalink
refactor(HomePage): extracted several widgets into methods
Browse files Browse the repository at this point in the history
refactor(HomePage): moved _updateLastLogin into UserDataUtil

chore: added additional TODOs
chore: updated README.md
  • Loading branch information
SethCohen committed Apr 11, 2023
1 parent 4b2a617 commit 60f8328
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 76 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ FIREBASE_TOKEN="YOUR_FIREBASE_TOKEN_HERE" # Generated from $ firebase login:ci
```

2. `$ docker compose build`
3. Go to `http://localhost:7357/` in browser.
3. Run the container with `$ docker compose up -d`
4. Go to `http://localhost:7357/` in browser.

### Contributing

1. Make your changes.
2. Submit a pull request.
2. Follow [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) standard.
3. Submit a pull request.
32 changes: 32 additions & 0 deletions src/lib/common/utils/user_data_util.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:spaced_repetition/sm.dart';
import '../../features/flashcard/flashcard_model.dart';

final currentUser = FirebaseAuth.instance.currentUser!;

class UserDataUtil {
static Future<void> updateLastLogin() async {
FirebaseFirestore.instance
.collection('users')
.doc(currentUser.uid)
.get()
.then((user) {
final userData = user.data() as Map<String, dynamic>;
final lastLogin = userData['lastLogin'] as Timestamp;
final lastLearnt = userData['lastLearnt'] as Timestamp;
final now = DateTime.now();
final startOfDay = DateTime(now.year, now.month, now.day);
final loginTimeDifference = startOfDay.difference(lastLogin.toDate());
final lastLearntTimeDifference =
startOfDay.difference(lastLearnt.toDate());

if (loginTimeDifference.inHours >= 24 ||
lastLearntTimeDifference.inHours >= 24) {
FirebaseFirestore.instance.collection('users').doc(user.id).set({
'streak': 0,
'lastLogin': now,
}, SetOptions(merge: true));
} else {
FirebaseFirestore.instance.collection('users').doc(user.id).set({
'lastLogin': now,
}, SetOptions(merge: true));
}
}).catchError((error) {
debugPrint(error);
});
}

static Future<void> updateLastLearnt() async {
final user = await FirebaseFirestore.instance
.collection('users')
Expand Down
1 change: 1 addition & 0 deletions src/lib/features/flashcard/flashcard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class _FlashcardState extends State<Flashcard> {
if (loadingProgress == null) {
return child;
}
// TODO fix loading box size to be a square box equal to the allowed image width
return Center(
child: CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null
Expand Down
128 changes: 54 additions & 74 deletions src/lib/features/home/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../common/utils/user_data_util.dart';
import '../../common/widgets/custom_tab.dart';
import '../authentication/google_provider.dart';
import '../creator/creator_page.dart';
import '../dictionary/dictionary_page.dart';
import '../lesson/lessons_list_page.dart';
Expand All @@ -20,9 +18,11 @@ class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
_updateLastLogin();
UserDataUtil.updateLastLogin();
}

// TODO add custom animations between tab views, e.g. fade in/out between lessons and review

@override
Widget build(BuildContext context) {
return SafeArea(
Expand All @@ -32,82 +32,62 @@ class _HomePageState extends State<HomePage> {
padding: EdgeInsets.symmetric(
horizontal: MediaQuery.of(context).size.width * 0.25),
child: Scaffold(
appBar: AppBar(
title: const Text('ASLearner'),
automaticallyImplyLeading: false,
actions: const <Widget>[
TabBar(
dividerColor: Colors.transparent,
isScrollable: true,
tabs: <Widget>[
CustomTab(
message: 'Lessons',
icon: Icons.school,
),
CustomTab(
message: 'Review',
icon: Icons.history,
),
CustomTab(
message: 'Dictionary',
icon: Icons.find_in_page,
),
CustomTab(
message: 'Creator',
icon: Icons.design_services,
),
CustomTab(
message: 'Profile',
icon: Icons.account_circle,
),
],
),
],
),
body: const TabBarView(
children: <Widget>[
LessonsPage(),
ReviewPage(),
DictionaryPage(),
CreatorPage(),
ProfilePage(),
],
)),
appBar: _buildAppBar(),
body: _buildTabBarBody(),
),
),
),
);
}

void _updateLastLogin() async {
final user = context.read<GoogleSignInProvider>().user;
Widget _buildTabBarBody() {
return const TabBarView(
children: <Widget>[
LessonsPage(),
ReviewPage(),
DictionaryPage(),
CreatorPage(),
ProfilePage(),
],
);
}

FirebaseFirestore.instance
.collection('users')
.doc(user!.uid)
.get()
.then((user) {
final userData = user.data() as Map<String, dynamic>;
final lastLogin = userData['lastLogin'] as Timestamp;
final lastLearnt = userData['lastLearnt'] as Timestamp;
final now = DateTime.now();
final startOfDay = DateTime(now.year, now.month, now.day);
final loginTimeDifference = startOfDay.difference(lastLogin.toDate());
final lastLearntTimeDifference =
startOfDay.difference(lastLearnt.toDate());
AppBar _buildAppBar() {
return AppBar(
title: const Text('ASLearner'),
automaticallyImplyLeading: false,
actions: <Widget>[
_buildTabBar(),
],
);
}

if (loginTimeDifference.inHours >= 24 ||
lastLearntTimeDifference.inHours >= 24) {
FirebaseFirestore.instance.collection('users').doc(user.id).set({
'streak': 0,
'lastLogin': now,
}, SetOptions(merge: true));
} else {
FirebaseFirestore.instance.collection('users').doc(user.id).set({
'lastLogin': now,
}, SetOptions(merge: true));
}
}).catchError((error) {
debugPrint(error);
});
Widget _buildTabBar() {
return const TabBar(
dividerColor: Colors.transparent,
isScrollable: true,
tabs: <Widget>[
CustomTab(
message: 'Lessons',
icon: Icons.school,
),
CustomTab(
message: 'Review',
icon: Icons.history,
),
CustomTab(
message: 'Dictionary',
icon: Icons.find_in_page,
),
CustomTab(
message: 'Creator',
icon: Icons.design_services,
),
CustomTab(
message: 'Profile',
icon: Icons.account_circle,
),
],
);
}
}

0 comments on commit 60f8328

Please sign in to comment.