Skip to content

Commit

Permalink
Merge pull request #99 from CodexRodney/anki
Browse files Browse the repository at this point in the history
FIXES AND ADDITIONS
  • Loading branch information
IamMuuo authored Oct 4, 2024
2 parents 915583f + 606ecc9 commit e159492
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 152 deletions.
18 changes: 2 additions & 16 deletions lib/tools/anki/controllers/ankicard_controller.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import 'package:academia/exports/barrel.dart';
import 'package:academia/tools/anki/models/models.dart';
import 'package:get/get.dart';

class AnkiCardController extends GetxController {
AnkiCardController({
required this.topicId,
});

final RxList<AnkiCard> allCards = <AnkiCard>[].obs;
final int topicId;

@override
void onInit() {
super.onInit();
getAllTopicCards().then((value) {
debugPrint("[+] Anki Topic Cards Loaded");
});
}

// returns the number of topics
int numAnkiCards() {
Expand All @@ -33,7 +19,7 @@ class AnkiCardController extends GetxController {
return value == 0 ? false : true;
}

Future<List<AnkiCard>> getAllTopicCards() async {
Future<List<AnkiCard>> getAllTopicCards(int topicId) async {
AnkiCardModelHelper().queryAnkiCardsByTopic(topicId).then((values) {
allCards.clear();
values = values.reversed.toList();
Expand All @@ -46,7 +32,7 @@ class AnkiCardController extends GetxController {

Future<bool> deleteCard(AnkiCard ankiCard) async {
int value = await AnkiCardModelHelper().delete(ankiCard.toJson());
getAllTopicCards();
getAllTopicCards(ankiCard.topicId);
return value == 0 ? false : true;
}
}
15 changes: 15 additions & 0 deletions lib/tools/anki/pages/anki_home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class AnkiHomePage extends StatelessWidget {
Widget build(BuildContext context) {
// topic controller
final TopicController topicController = Get.put(TopicController());
// putting ankicard controller
Get.put(AnkiCardController());

return Scaffold(
resizeToAvoidBottomInset: false,
Expand All @@ -26,6 +28,19 @@ class AnkiHomePage extends StatelessWidget {
flexibleSpace: const FlexibleSpaceBar(
title: Text("Anki"),
),
actions: [
IconButton(
onPressed: () {
Get.defaultDialog(
title: "Academia Help",
content: const Text(
"Mark a topic as your favorite for easy access by tapping the star icon.\nTap on the cards to view their corresponding Anki cards.",
),
);
},
icon: const Icon(Ionicons.help),
),
],
),
SliverFillRemaining(
child: Obx(
Expand Down
14 changes: 10 additions & 4 deletions lib/tools/anki/pages/anki_swap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,18 @@ class _AnkiSwapState extends State<AnkiSwap> {
height: MediaQuery.of(context).size.height * 0.45,
decoration: BoxDecoration(
color: colorDet == 0
? const Color(0xff8999aa)
? Theme.of(context).colorScheme.primaryContainer
: colorDet == 1
? const Color(0xffffcdfe)
? Theme.of(context)
.colorScheme
.secondaryContainer
: colorDet == 2
? const Color(0xffffe7cd)
: const Color(0xffcdffce),
? Theme.of(context)
.colorScheme
.tertiaryContainer
: Theme.of(context)
.colorScheme
.errorContainer,
borderRadius: const BorderRadius.all(
Radius.circular(12),
),
Expand Down
66 changes: 43 additions & 23 deletions lib/tools/anki/pages/create_ankicard.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import 'package:academia/exports/barrel.dart';
import 'package:academia/tools/anki/controllers/controllers.dart';
import 'package:academia/tools/anki/models/models.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class CreateAnkicard extends StatelessWidget {
const CreateAnkicard({super.key});
const CreateAnkicard({
super.key,
required this.topicId,
});

final int topicId;

@override
Widget build(BuildContext context) {
Expand All @@ -15,29 +20,44 @@ class CreateAnkicard extends StatelessWidget {

return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
title: const Text("Create Card"),
centerTitle: true,
actions: [
IconButton(
onPressed: () {
Get.defaultDialog(
title: "Academia Help",
content: const Text(
"To create an Anki card, you can either type a question and its answer, then tap \"Create Card\" to save it, or type a statement, highlight the portion you want to hide, tap outside the text box to preview the hidden answer, and then tap \"Create Card\" to save. Both options let you quickly generate flashcards for efficient studying.",
),
);
},
icon: const Icon(Ionicons.help),
),
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: cardInfo,
maxLines: 5,
decoration: InputDecoration(
hintText: "Some question you want to remember",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
child: Obx(
() => TextField(
controller: cardInfo,
maxLines: 5,
decoration: InputDecoration(
hintText: ansCardController.ansSwitch.value
? "Some question you want to remember"
: "A stetement to highlight a section that you want to remember",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
),
onTapOutside: (event) {
ansCardController.ansCard.value =
cardInfo.selection.textInside(cardInfo.text);
cardAns.text = ansCardController.ansCard.value;
},
),
onTapOutside: (event) {
ansCardController.ansCard.value =
cardInfo.selection.textInside(cardInfo.text);
cardAns.text = ansCardController.ansCard.value;
},
),
),
Obx(
Expand All @@ -59,7 +79,7 @@ class CreateAnkicard extends StatelessWidget {
),
),
Obx(
() => ansCardController.ansSwitch.value
() => !ansCardController.ansSwitch.value
// TextField for Showing Highlighted Answer
? Padding(
padding: const EdgeInsets.all(8.0),
Expand Down Expand Up @@ -104,7 +124,7 @@ class CreateAnkicard extends StatelessWidget {
},
child: Obx(
() => Text(
"Switch To ${ansCardController.ansSwitch.value ? "Writing" : "Highlight"}",
"Switch To ${!ansCardController.ansSwitch.value ? "Writing" : "Highlight"}",
),
),
),
Expand All @@ -123,9 +143,9 @@ class CreateAnkicard extends StatelessWidget {
);
}
// user chose to write the answer
else if (!ansCardController.ansSwitch.value) {
else if (ansCardController.ansSwitch.value) {
AnkiCard ankiCard = AnkiCard(
topicId: ankiCardController.topicId,
topicId: topicId,
question: cardInfo.text.trim(),
answer: cardAns.text.trim());
await ankiCardController.addAnkiCard(ankiCard);
Expand All @@ -134,7 +154,7 @@ class CreateAnkicard extends StatelessWidget {
cardAns.clear();
ansCardController.ansCard.value = "";
// reload the anki cards
await ankiCardController.getAllTopicCards();
await ankiCardController.getAllTopicCards(topicId);
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
Expand All @@ -147,7 +167,7 @@ class CreateAnkicard extends StatelessWidget {
String question = cardInfo.text.trim();
question = question.replaceAll(cardAns.text.trim(), "_");
AnkiCard ankiCard = AnkiCard(
topicId: ankiCardController.topicId,
topicId: topicId,
question: question,
answer: cardAns.text.trim());
await ankiCardController.addAnkiCard(ankiCard);
Expand All @@ -156,7 +176,7 @@ class CreateAnkicard extends StatelessWidget {
cardAns.clear();
ansCardController.ansCard.value = "";
// reload the anki cards
await ankiCardController.getAllTopicCards();
await ankiCardController.getAllTopicCards(topicId);

// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
12 changes: 10 additions & 2 deletions lib/tools/anki/pages/create_topic_form.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:academia/exports/barrel.dart';
import 'package:academia/tools/anki/controllers/controllers.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../models/models.dart';

Expand Down Expand Up @@ -36,6 +36,7 @@ class CreateTopicForm extends StatelessWidget {
const SizedBox(height: 8),
TextFormField(
controller: descController,
maxLength: 50,
maxLines: 10,
decoration: InputDecoration(
hintText: "Write a description for your topic",
Expand All @@ -55,7 +56,7 @@ class CreateTopicForm extends StatelessWidget {
duration: Duration(seconds: 1),
),
);
} else if (titleController.text.trim().length <= 3) {
} else if (titleController.text.trim().length < 3) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
Expand Down Expand Up @@ -90,6 +91,13 @@ class CreateTopicForm extends StatelessWidget {
// updating topic list and favorites
topicController.getAllTopics();
topicController.getAllFavourites();
// informing user successful creation
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Topic successfully created!!"),
duration: Duration(seconds: 1),
),
);
Navigator.of(context).pop();
}
},
Expand Down
3 changes: 2 additions & 1 deletion lib/tools/anki/pages/edit_anki_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ class _EditAnkiCardState extends State<EditAnkiCard> {
onPressed: () async {
// updating anki card
await ankiCardController.updateAnkiCard(widget.ankiCard);
await ankiCardController.getAllTopicCards();
await ankiCardController
.getAllTopicCards(widget.ankiCard.topicId);
if (!mounted) return;
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
26 changes: 19 additions & 7 deletions lib/tools/anki/pages/flashcards.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,26 @@ class TopicFlashCards extends StatelessWidget {

@override
Widget build(BuildContext context) {
// adding AnkiCardController
AnkiCardController ankiCardController = Get.put(
AnkiCardController(
topicId: topicId,
),
);
// finding AnkiCardController
AnkiCardController ankiCardController = Get.find<AnkiCardController>();

return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: const Text("Topic flash cards"),
actions: [
IconButton(
onPressed: () {
Get.defaultDialog(
title: "Academia Help",
content: const Text(
"Double Tap On AnkiCard To Delete\nTap On AnkiCard To Preview",
),
);
},
icon: const Icon(Ionicons.help),
),
],
),
floatingActionButton: SizedBox(
height: MediaQuery.of(context).size.height * 0.18,
Expand All @@ -39,7 +49,9 @@ class TopicFlashCards extends StatelessWidget {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const CreateAnkicard(),
builder: (context) => CreateAnkicard(
topicId: topicId,
),
),
);
},
Expand Down
44 changes: 23 additions & 21 deletions lib/tools/anki/pages/populated_anki_home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,35 @@ class PopulatedAnkiHomeScreen extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Visibility(
visible: topicController.allFavourites.isNotEmpty,
child: Align(
alignment: Alignment.center,
child: Container(
padding: const EdgeInsets.symmetric(vertical: 8),
height: MediaQuery.of(context).size.height * 0.27,
width: MediaQuery.of(context).size.width * 0.87,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemBuilder: (context, idx) {
return StarredTopics(
idx: topicController.allFavourites[idx].id!,
topic: topicController.allFavourites[idx].name,
desc: topicController.allFavourites[idx].desc,
topicController: topicController,
);
},
itemCount: topicController.allFavourites.length,
// displays favourite topics
Obx(
() => Visibility(
visible: topicController.allFavourites.isNotEmpty,
child: Align(
alignment: Alignment.center,
child: Container(
padding: const EdgeInsets.symmetric(vertical: 8),
height: MediaQuery.of(context).size.height * 0.27,
width: MediaQuery.of(context).size.width * 0.87,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemBuilder: (context, idx) {
return StarredTopics(
topicId: topicController.allFavourites[idx].id!,
topic: topicController.allFavourites[idx].name,
desc: topicController.allFavourites[idx].desc,
);
},
itemCount: topicController.allFavourites.length,
),
),
),
),
),
// displays topicics
Container(
height: MediaQuery.of(context).size.height * 0.6,
height: MediaQuery.of(context).size.height *
(topicController.allFavourites.isNotEmpty ? 0.6 : 1),
padding: const EdgeInsets.all(12),
child: Obx(
() => GridView.builder(
Expand All @@ -57,7 +60,6 @@ class PopulatedAnkiHomeScreen extends StatelessWidget {
topic: topicController.allTopics[idx].name,
topicDesc: topicController.allTopics[idx].desc,
isFavourite: topicController.allTopics[idx].isFavourite,
controller: topicController,
);
},
itemCount: topicController.allTopics.length,
Expand Down
Loading

0 comments on commit e159492

Please sign in to comment.