Skip to content

Commit

Permalink
feat: restructured and refactored entire project (#19)
Browse files Browse the repository at this point in the history
feat: added pagination using Provider

feat: added dynamic routing

refactor: updated project to use relative imports

refactor: review.dart -> review_page.dart

refactor: lesson.dart -> lesson_page.dart

refactor: manage_page -> profile_page.dart
  • Loading branch information
SethCohen authored Mar 24, 2023
1 parent 6064aa4 commit b87b4a3
Show file tree
Hide file tree
Showing 21 changed files with 716 additions and 868 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ google-services.json
GoogleService-Info.plist
.vscode/*
src/lib/tests/*
scripts/
29 changes: 10 additions & 19 deletions src/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import 'package:asl/pages/manage_page.dart';
import 'package:asl/providers/data_provider.dart';
import 'package:asl/themes/comfy.dart';
import 'package:asl/widgets/lesson.dart';
import 'package:asl/widgets/review.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:asl/widgets/page_manager.dart';
import 'package:asl/providers/google_provider.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:url_strategy/url_strategy.dart';
import 'providers/data_provider.dart';
import 'providers/google_provider.dart';
import 'firebase_options.dart';
import 'routes.dart';
import 'themes/comfy.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

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

await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await FirebaseFirestore.instance
.enablePersistence(const PersistenceSettings(synchronizeTabs: true));
setPathUrlStrategy();

runApp(const MyApp());
}

Expand All @@ -37,12 +32,8 @@ class MyApp extends StatelessWidget {
debugShowCheckedModeBanner: false,
title: 'ASL Learner',
theme: comfyTheme,
home: const PageManager(),
routes: <String, WidgetBuilder>{
'/manageAccount': (BuildContext context) => const ManagePage(),
'/lesson': (BuildContext context) => const Lesson(),
'/review': (BuildContext context) => const Review(),
},
initialRoute: '/',
onGenerateRoute: generateRoute,
));
}
}
28 changes: 28 additions & 0 deletions src/lib/models/flashcard_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class FlashcardModel {
String cardId;
String deckId;
String title;
String instructions;
String image;

FlashcardModel(
{required this.cardId,
required this.deckId,
required this.title,
required this.instructions,
required this.image});

factory FlashcardModel.fromMap(
Map<String, dynamic> data,
String cardId,
String deckId,
) {
return FlashcardModel(
cardId: cardId,
deckId: deckId,
title: data['title'] as String,
instructions: data['instructions'] as String,
image: data['image'] as String,
);
}
}
49 changes: 49 additions & 0 deletions src/lib/models/lesson_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:flutter/material.dart';

class LessonModel {
final String title;
final String description;
final String id;
final int cardsCompleted;
final int cardsTotal;

LessonModel({
required this.cardsCompleted,
required this.cardsTotal,
required this.title,
required this.description,
required this.id,
});

factory LessonModel.fromMap(id, cardsCompleted, Map<String, dynamic> data) =>
LessonModel(
cardsCompleted: cardsCompleted,
cardsTotal: data['cardCount'],
title: data['title'],
description: data['description'],
id: id,
);

LessonModel copyWith({
int? cardsCompleted,
int? cardsTotal,
String? title,
String? description,
String? id,
}) =>
LessonModel(
cardsCompleted: cardsCompleted ?? this.cardsCompleted,
cardsTotal: cardsTotal ?? this.cardsTotal,
title: title ?? this.title,
description: description ?? this.description,
id: id ?? this.id,
);

void navigateToLesson(BuildContext context) => Navigator.pushNamed(
context,
'/${title.toLowerCase().replaceAll(' ', '_')}',
arguments: {
'lesson': this,
},
);
}
29 changes: 29 additions & 0 deletions src/lib/models/review_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ReviewModel {
final String deckId;
final String deckTitle;
final String deckDescription;
final String cardId;
final String cardTitle;
final String cardInstructions;
final String cardImage;

ReviewModel({
required this.cardTitle,
required this.cardInstructions,
required this.cardImage,
required this.deckTitle,
required this.deckDescription,
required this.cardId,
required this.deckId,
});

factory ReviewModel.fromMap(cardId, Map<String, dynamic> data) => ReviewModel(
cardId: cardId,
cardTitle: data['title'],
cardInstructions: data['instructions'],
cardImage: data['image'],
deckTitle: data['deckTitle'],
deckDescription: data['deckDescription'],
deckId: data['deckId'],
);
}
90 changes: 45 additions & 45 deletions src/lib/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'package:asl/pages/creator_page.dart';
import 'package:asl/pages/dictionary_page.dart';
import 'package:asl/pages/lessons_page.dart';
import 'package:asl/pages/manage_page.dart';
import 'package:asl/pages/review_page.dart';
import 'package:asl/providers/data_provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/data_provider.dart';
import 'creator_page.dart';
import 'dictionary_page.dart';
import 'lessons_page.dart';
import 'profile_page.dart';
import 'reviews_page.dart';

class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
const HomePage({super.key});

@override
State<HomePage> createState() => _HomePageState();
Expand All @@ -18,7 +18,8 @@ class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
context.read<DataProvider>().loadData();

context.read<DataProvider>().loadLessons();
}

@override
Expand All @@ -30,43 +31,42 @@ 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>[
Tooltip(
message: 'Lessons',
child: Tab(icon: Icon(Icons.school))),
Tooltip(
message: 'Review',
child: Tab(icon: Icon(Icons.history))),
Tooltip(
message: 'Dictionary',
child: Tab(icon: Icon(Icons.find_in_page))),
Tooltip(
message: 'Creator',
child: Tab(icon: Icon(Icons.design_services))),
Tooltip(
message: 'Profile',
child: Tab(icon: Icon(Icons.account_circle))),
],
),
],
),
body: const TabBarView(
children: <Widget>[
LessonsPage(),
ReviewPage(),
DictionaryPage(),
CreatorPage(),
ManagePage(),
],
),
),
appBar: AppBar(
title: const Text('ASLearner'),
automaticallyImplyLeading: false,
actions: const <Widget>[
TabBar(
dividerColor: Colors.transparent,
isScrollable: true,
tabs: <Widget>[
Tooltip(
message: 'Lessons',
child: Tab(icon: Icon(Icons.school))),
Tooltip(
message: 'Review',
child: Tab(icon: Icon(Icons.history))),
Tooltip(
message: 'Dictionary',
child: Tab(icon: Icon(Icons.find_in_page))),
Tooltip(
message: 'Creator',
child: Tab(icon: Icon(Icons.design_services))),
Tooltip(
message: 'Profile',
child: Tab(icon: Icon(Icons.account_circle))),
],
),
],
),
body: const TabBarView(
children: <Widget>[
LessonsPage(),
ReviewPage(),
DictionaryPage(),
CreatorPage(),
ProfilePage(),
],
)),
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/pages/landing_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import 'package:asl/providers/google_provider.dart';
import '../providers/google_provider.dart';

class LandingPage extends StatelessWidget {
const LandingPage({Key? key}) : super(key: key);
Expand Down
95 changes: 95 additions & 0 deletions src/lib/pages/lesson_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import '../models/lesson_model.dart';
import '../models/flashcard_model.dart';
import '../widgets/flashcard.dart';

class Lesson extends StatefulWidget {
const Lesson({super.key, required this.lesson});
final LessonModel lesson;

@override
State<Lesson> createState() => _LessonState();
}

class _LessonState extends State<Lesson> {
int _currentCardIndex = 0;

@override
Widget build(BuildContext context) {
// TODO optional: quiz minigame at end of lesson or loop

return WillPopScope(
onWillPop: () async {
return true;
},
child: Scaffold(
appBar: AppBar(
title: Text(widget.lesson.title),
),
body: FutureBuilder<QuerySnapshot>(
future: _getCardsFuture(),
builder: (context, snapshot) => snapshot.hasData
? Column(
mainAxisSize: MainAxisSize.max,
children: [
_buildFlashcards(snapshot.data!.docs),
// TODO set prograss indicator location fixed to bottom of screen
_buildProgressTextIndicator(),
_buildProgressBarIndicator(),
],
)
: const Center(child: CircularProgressIndicator()))),
);
}

Future<QuerySnapshot> _getCardsFuture() => FirebaseFirestore.instance
.collection("decks")
.doc(widget.lesson.id)
.collection('cards')
.get();

void _handleIndex() => setState(() {
bool isCompleted = _currentCardIndex == widget.lesson.cardsTotal - 1;
if (isCompleted) {
Navigator.pop(context);
} else {
_currentCardIndex++;
}
});

List<Widget> _getFlashcards(List<QueryDocumentSnapshot> cards) => cards
.map((card) => Flashcard(
card: FlashcardModel.fromMap(
card.data() as Map<String, dynamic>, card.id, widget.lesson.id),
handleIndex: _handleIndex,
isReview: false,
))
.toList();

Widget _buildFlashcards(List<QueryDocumentSnapshot<Object?>> cards) =>
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: IndexedStack(
index: _currentCardIndex,
children: _getFlashcards(cards),
),
);

Widget _buildProgressTextIndicator() {
return Container(
padding: const EdgeInsets.all(8.0),
alignment: Alignment.centerRight,
child: Text('${_currentCardIndex + 1} / ${widget.lesson.cardsTotal}'));
}

Widget _buildProgressBarIndicator() {
return TweenAnimationBuilder(
tween: Tween<double>(
begin: _currentCardIndex / widget.lesson.cardsTotal,
end: (_currentCardIndex + 1) / widget.lesson.cardsTotal),
duration: const Duration(milliseconds: 1000),
builder: (context, double value, child) =>
LinearProgressIndicator(value: value));
}
}
Loading

0 comments on commit b87b4a3

Please sign in to comment.