Skip to content

Commit

Permalink
feat: todos done!
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMuuo committed Jun 24, 2024
1 parent fa6a622 commit 344928c
Show file tree
Hide file tree
Showing 12 changed files with 580 additions and 101 deletions.
2 changes: 1 addition & 1 deletion lib/constants/tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ final List<Map<String, dynamic>> allTools = [
"action": "Manage your tasks",
"image": "assets/images/tasks_manager.png",
"ontap": () {
Get.to(const TodoPage());
Get.to(const TodoHomeScreen());
},
"description":
"Having trouble keeping track of your Assignments? We've got you covered",
Expand Down
28 changes: 24 additions & 4 deletions lib/tools/todo/controllers/todo_controller.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
import 'package:academia/exports/barrel.dart';
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import '../todo.dart';

class TodoController extends GetxController {
final RxList<Todo> allTodos = <Todo>[].obs;

@override
void onInit() {
super.onInit();
getAllTodos().then((value) {
debugPrint("[+] Todos Loaded");
});
}

Future<bool> addTask(Todo t) async {
int value = await TodoModelHelper().create(t.toJson());
return value == 0 ? false : true;
}

Future<bool> updateTodo(Todo t) async {
t.complete = !t.complete;
int value = await TodoModelHelper().update(t.toJson());
return value == 0 ? false : true;
}

Future<List<Todo>> getAllTodos() async {
TodoModelHelper().queryAll().then((value) {
allTodos.clear();
for (final val in value) {
allTodos.add(Todo.fromJson(val));
debugPrint("[+]Todo loaded");
}
});
return allTodos;
}

Future<bool> addTask(Todo t) async {
int value = await TodoModelHelper().create(t.toJson());
Future<bool> deleteTodo(Todo t) async {
int value = await TodoModelHelper().delete(t.toJson());
getAllTodos();
return value == 0 ? false : true;
}
}
15 changes: 15 additions & 0 deletions lib/tools/todo/todo.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
export 'todo_home_screen.dart';
export 'models/models.dart';
export 'controllers/todo_controller.dart';
export 'widgets/widgets.dart';
import "package:intl/intl.dart";

// Helper functions
String trimAndEllipsis(String input, int maxLength) {
if (input.length <= maxLength) {
return input;
} else {
return "${input.substring(0, maxLength)}...";
}
}

String formatDateTime(DateTime dateTime) {
return DateFormat('EEEE, d MMM y • H:mm a').format(dateTime);
}
98 changes: 10 additions & 88 deletions lib/tools/todo/todo_home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,102 +1,24 @@
import 'package:academia/exports/barrel.dart';
import 'package:academia/tools/todo/todo_view_page.dart';
import 'package:academia/tools/todo/widgets/filled_todo_homescreen.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';

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

@override
State<TodoPage> createState() => _TodoPageState();
}

class _TodoPageState extends State<TodoPage> {
final todoController = Get.find<TodoController>();
@override
void initState() {
super.initState();
}
class TodoHomeScreen extends StatelessWidget {
const TodoHomeScreen({super.key});

@override
Widget build(BuildContext context) {
final todoController = Get.find<TodoController>();
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
title: const Text("Tasker"),
actions: [
IconButton(
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Clarification"),
content: const Text(
"Keep track of your assignments and todo items in a secure way, tasks you add here never leave your device",
),
actions: [
FilledButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Wow, cool feature"),
),
],
),
);
},
icon: const Icon(Ionicons.information_circle_outline),
),
],
),
Obx(
() => todoController.allTodos.isEmpty
? SliverPadding(
padding: const EdgeInsets.all(16),
sliver: SliverFillRemaining(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Lottie.asset("assets/lotties/study.json"),
const SizedBox(height: 22),
Text(
"Get a clear view of the day ahead.",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.titleLarge,
),
Text(
"All tasks due today will show up here tap the + to add a task",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.titleSmall,
),
],
),
),
)
: SliverPadding(
padding: const EdgeInsets.symmetric(horizontal: 8),
sliver: SliverFillRemaining(
child: ListView.separated(
itemBuilder: (context, index) {
return ListTile(
title:
Text("${todoController.allTodos[index].name}"),
);
},
separatorBuilder: (context, index) {
return const SizedBox(height: 12);
},
itemCount: todoController.allTodos.length,
),
),
),
),
],
body: Obx(
() => todoController.allTodos.isEmpty
? const EmptyTodoHomeScreen()
: const FilledTodoHomeScreen(),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const TodoViewPage(),
),
Expand Down
57 changes: 49 additions & 8 deletions lib/tools/todo/todo_view_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:academia/exports/barrel.dart';
import 'todo.dart';
import 'package:flutter/material.dart';
import "package:ionicons/ionicons.dart";
import 'package:flex_color_picker/flex_color_picker.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

enum Mode {
create,
Expand Down Expand Up @@ -29,11 +30,6 @@ class _TodoViewPageState extends State<TodoViewPage> {
final TextEditingController dueDateController = TextEditingController();
final TextEditingController subTaskController = TextEditingController();
DateTime dueDate = DateTime.now().add(const Duration(days: 1));

String formatDateTime(DateTime dateTime) {
return DateFormat('EEEE, d MMM y • H:mm a').format(dateTime);
}

Future<void> promptDueDate() async {
showDatePicker(
context: context,
Expand Down Expand Up @@ -63,6 +59,19 @@ class _TodoViewPageState extends State<TodoViewPage> {
});
}

@override
void initState() {
super.initState();
if (widget.todo != null) {
taskColor = widget.todo!.color;
todoNameController.text =
widget.todo!.name + (widget.todo!.complete ? "(Completed)" : "");
subTaskController.text = widget.todo!.description;
dueDateController.text = formatDateTime(widget.todo!.due);
setState(() {});
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -203,6 +212,7 @@ class _TodoViewPageState extends State<TodoViewPage> {
description: subTaskController.text,
))
.then((value) {
todoController.getAllTodos();
if (value) {
showDialog(
context: context,
Expand All @@ -226,7 +236,38 @@ class _TodoViewPageState extends State<TodoViewPage> {
});
}
}
: () {},
: () {
// delete the darn task
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Confirmation"),
content: const Text(
"Are you sure you want tp delete the task, doing this will affect your graph",
),
actions: [
FilledButton(
onPressed: () {
todoController
.deleteTodo(widget.todo!)
.then((value) {
if (value) {
Navigator.pop(context);
Navigator.pop(context);
}
});
},
child: const Text("Delete it"),
),
FilledButton.tonal(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Cancel"),
),
],
));
},
child: Icon(
widget.mode == Mode.create ? Ionicons.add : Ionicons.trash,
),
Expand Down
35 changes: 35 additions & 0 deletions lib/tools/todo/widgets/empty_todo_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import 'package:ionicons/ionicons.dart';
import '../todo_view_page.dart';
import 'package:lottie/lottie.dart';

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

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Lottie.asset(
"assets/lotties/study.json",
),
Text(
"Nothing here yet? Create a new task to get started with productivity",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 12),
TextButton.icon(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => const TodoViewPage(),
));
},
icon: const Icon(Ionicons.add),
label: const Text("Create todo item"),
),
],
);
}
}
Loading

0 comments on commit 344928c

Please sign in to comment.