Skip to content

Commit

Permalink
drawer now works for all, today, tomorrow, next 7 days
Browse files Browse the repository at this point in the history
  • Loading branch information
csarevalo committed Sep 23, 2024
1 parent a72e2c2 commit 1416d7e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
34 changes: 33 additions & 1 deletion lib/src/providers/task_provider.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';

import '../models/task.dart';
Expand All @@ -13,13 +14,24 @@ class TaskProvider with ChangeNotifier {
}

late List<Task> _todoList = [];
late List<Task> _activeTasks = [];
late bool Function(Task task) activeTaskTest;

List<Task> get todoList => _todoList; //not immutable
/// Get a mutable copy of app todo list
List<Task> get todoList => List.from(_todoList);

/// Get mutable active tasks list
List<Task> get activeTasks => _activeTasks;

Future<void> init() async {
_todoList = await _taskProviderService.loadTasks();
_todoList.sort((a, b) => a.id.compareTo(b.id)); // Sort tasks by id (asc)
_internalIdCounter = _todoList.last.id; // Init task id counter

//NEW STUFF
activeTaskTest = (Task t) => true; //FIXME: retrieve from somewhere
_activeTasks = List.from(_todoList); //FIXME: need filter here
_activeTasks.retainWhere(activeTaskTest);
notifyListeners();
}

Expand All @@ -34,10 +46,30 @@ class TaskProvider with ChangeNotifier {
}

void addTask(Task newTask) {
addToActiveTasks(newTask); //First try adding to active tasks
_todoList.add(newTask);
notifyListeners();
}

void addToActiveTasks(Task newTask) {
if (activeTaskTest(newTask)) _activeTasks.add(newTask);
}

void updateActiveTaskTest(bool Function(Task t) f) {
activeTaskTest = f; //no need to notify listeners
List<Task> newActiveTasks = List.from(_todoList);
newActiveTasks.retainWhere(f);
updateActiveTasks(newActiveTasks);
}

void updateActiveTasks(List<Task> newActiveTasks) {
if (!const DeepCollectionEquality().equals(_activeTasks, newActiveTasks)) {
_activeTasks = newActiveTasks;
debugPrint("Updated active tasks\n\n");
notifyListeners();
}
}

Task createTask({
required String title,
bool isDone = false,
Expand Down
37 changes: 33 additions & 4 deletions lib/src/screens/home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../models/task.dart';
import '../providers/task_provider.dart';
import '../screens/settings_view.dart';
import '../utils/filter_tasks.dart';
import '../widgets/dialogs/sort_tasks_dialog.dart';
import '../widgets/task_sections_builder.dart';
import '../widgets/dialogs/small_task_dialog.dart';
Expand Down Expand Up @@ -54,6 +58,7 @@ class SampleDrawerItems extends StatelessWidget {
@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
final taskProvider = context.read<TaskProvider>();
return ListView(
children: [
DrawerHeader(
Expand Down Expand Up @@ -102,7 +107,10 @@ class SampleDrawerItems extends StatelessWidget {
ListTile(
leading: const Icon(Icons.all_inbox),
title: const Text("All"),
onTap: () {},
onTap: () {
taskProvider.updateActiveTaskTest((Task t) => true);
taskProvider.updateActiveTasks(taskProvider.todoList);
},
),
ListTile(
leading: const Icon(Icons.inbox_rounded),
Expand All @@ -112,17 +120,38 @@ class SampleDrawerItems extends StatelessWidget {
ListTile(
leading: const Icon(Icons.today_rounded),
title: const Text("Today"),
onTap: () {},
onTap: () {
taskProvider.updateActiveTaskTest((Task task) {
if (task.dateDue == null) return false;
final DateTime today = DateUtils.dateOnly(DateTime.now());
final DateTime due = DateUtils.dateOnly(task.dateDue!);
return due.difference(today).inDays <= 0; //due on or before today
});
},
),
ListTile(
leading: const Icon(Icons.signpost_rounded),
title: const Text("Tomorrow"),
onTap: () {},
onTap: () {
taskProvider.updateActiveTaskTest((Task task) {
if (task.dateDue == null) return false;
final DateTime today = DateUtils.dateOnly(DateTime.now());
final DateTime due = DateUtils.dateOnly(task.dateDue!);
return due.difference(today).inDays == 1; //due tomorrow
});
},
),
ListTile(
leading: const Icon(Icons.calendar_month_rounded),
title: const Text("Next 7 Days"),
onTap: () {},
onTap: () {
taskProvider.updateActiveTaskTest((Task task) {
if (task.dateDue == null) return false;
final DateTime today = DateUtils.dateOnly(DateTime.now());
final DateTime due = DateUtils.dateOnly(task.dateDue!);
return due.difference(today).inDays <= 7; //on or b/f next 7 days
});
},
),
ExpansionTile(
leading: const Icon(Icons.style_rounded),
Expand Down
7 changes: 3 additions & 4 deletions lib/src/utils/filter_tasks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,10 @@ class ContinousTaskFilter {
}
}

typedef RetainTaskWhere = bool Function(Task task);

/// Retain Tasks where
///
/// datePeriod: 'overdue', 'today', 'tomorrow', 'next', 'later', 'no' ..date
RetainTaskWhere retainTasksByDate({
bool Function(Task task) retainTasksByDate({
required final TaskDateField dateField,
required final String datePeriod,
final bool isCompleted = false, //filter uncompleted tasks by default
Expand Down Expand Up @@ -201,7 +199,8 @@ DateTime? getTaskDate({
}
}

/// Get date only from datetime
/// Returns a nullable [DateTime] with the date of the original,
/// but time set to midnight.
DateTime? dateOnly(final DateTime? date) {
if (date == null) return null;
return DateTime(date.year, date.month, date.day);
Expand Down
6 changes: 5 additions & 1 deletion lib/src/widgets/task_section.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TaskSection extends StatelessWidget {
return Selector<TaskProvider, List<ImmutableTask>>(
selector: (ctx, tp) {
final filterTasks = SortAndFilterTasks(
tasks: tp.todoList,
tasks: tp.activeTasks,
taskSortOptions: taskSortOptions,
);
late List<Task> filteredTasks;
Expand Down Expand Up @@ -61,6 +61,10 @@ class TaskSection extends StatelessWidget {
);
},
shouldRebuild: (previous, next) {
//TODO: only rebuild task section when the order changes, or new tasks
//are added... so maybe do not compare immutable tasks
//instead make tasktile embed a selector to listen to changes to a
//specific task for each tasktile
return !const DeepCollectionEquality().equals(previous, next);
},
// shouldRebuild: (previous, next) {
Expand Down

0 comments on commit 1416d7e

Please sign in to comment.