Skip to content

Commit

Permalink
Update filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
eshfield committed Jun 24, 2024
1 parent 6f79df0 commit af77608
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 65 deletions.
2 changes: 1 addition & 1 deletion lib/data/tasks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final mockTasks = [
'Купить что-то, где-то, зачем-то, но зачем не очень понятно, но точно чтобы показать как обрезается текст в этом поле',
priority: Priority.high,
deadline: DateTime(2024, 09, 23),
isDone: true,
isDone: false,
),
Task(
text: 'Купить что-то, где-то, зачем-то, но зачем не очень понятно',
Expand Down
2 changes: 1 addition & 1 deletion lib/l10n/app_ru.arb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
"addTaskPriorityHigh": "Высокий",
"addTaskDeadline": "Сделать до",
"noTasks": "Вы ещё не добавили ни одной задачи",
"noDoneTasks": "Нет ни одной выполненной задачи\nДля просмотра всех дел отключите фильтр"
"onlyDoneTasksLeft": "Все задачи выполнены!\nДля просмотра завершённых дел отключите фильтр"
}
86 changes: 43 additions & 43 deletions lib/presentation/home_screen/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:app/data/tasks.dart';
import 'package:app/domain/models/task.dart';
import 'package:app/l10n/l10n_extension.dart';
import 'package:app/presentation/add_task_screen/add_task_screen.dart';
import 'package:app/presentation/home_screen/widgets/done_tasks_visibility_button.dart';
import 'package:app/presentation/home_screen/widgets/header.dart';
import 'package:app/presentation/home_screen/widgets/task_list.dart';
import 'package:app/presentation/home_screen/widgets/top_bar.dart';
Expand All @@ -27,17 +28,23 @@ class HomeScreen extends StatefulWidget {
aspect: _Aspects.tasksToDisplay,
).tasksToDisplay;

static int doneTaskCountOf(BuildContext context) =>
_HomeScreenInheritedModel.of(
context,
aspect: _Aspects.doneTaskCount,
).doneTaskCount;

static bool showAppBarOf(BuildContext context) =>
_HomeScreenInheritedModel.of(
context,
aspect: _Aspects.showAppBar,
).showAppBar;

static bool showOnlyDoneTasksOf(BuildContext context) =>
static bool showDoneTasksOf(BuildContext context) =>
_HomeScreenInheritedModel.of(
context,
aspect: _Aspects.showOnlyDoneTasks,
).showOnlyDoneTasks;
aspect: _Aspects.showDoneTasks,
).showDoneTasks;

@override
State<HomeScreen> createState() => HomeScreenState();
Expand All @@ -47,10 +54,13 @@ class HomeScreenState extends State<HomeScreen> {
final scrollController = ScrollController();
late List<Task> tasks;
var showAppBar = false;
var showOnlyDoneTasks = false;
var showDoneTasks = false;

List<Task> get tasksToDisplay =>
showOnlyDoneTasks ? tasks.where((task) => task.isDone).toList() : tasks;
showDoneTasks ? tasks : tasks.where((task) => !task.isDone).toList();

int get doneTaskCount =>
tasks.fold(0, (acc, task) => acc + (task.isDone ? 1 : 0));

@override
void initState() {
Expand Down Expand Up @@ -94,12 +104,10 @@ class HomeScreenState extends State<HomeScreen> {
});
}

void toggleShowOnlyDoneTasks() {
void toggleShowDoneTasks() {
setState(() {
showOnlyDoneTasks = !showOnlyDoneTasks;
showDoneTasks = !showDoneTasks;
});
scrollController.animateTo(0,
duration: appTopBarAnimationDuration, curve: curve);
}

Future<void> createTask() async {
Expand All @@ -112,9 +120,6 @@ class HomeScreenState extends State<HomeScreen> {
}
}

int countDoneTasks() =>
tasks.fold(0, (acc, task) => acc + (task.isDone ? 1 : 0));

@override
Widget build(BuildContext context) {
return _HomeScreenInheritedModel(
Expand Down Expand Up @@ -143,22 +148,25 @@ class HomeScreenState extends State<HomeScreen> {

enum _Aspects {
tasksToDisplay,
doneTaskCount,
showAppBar,
showOnlyDoneTasks,
showDoneTasks,
}

class _HomeScreenInheritedModel extends InheritedModel<_Aspects> {
final HomeScreenState state;
final List<Task> tasksToDisplay;
final int doneTaskCount;
final bool showAppBar;
final bool showOnlyDoneTasks;
final bool showDoneTasks;

_HomeScreenInheritedModel({
required this.state,
required super.child,
}) : tasksToDisplay = state.tasksToDisplay,
doneTaskCount = state.doneTaskCount,
showAppBar = state.showAppBar,
showOnlyDoneTasks = state.showOnlyDoneTasks;
showDoneTasks = state.showDoneTasks;

static _HomeScreenInheritedModel of(
BuildContext context, {
Expand Down Expand Up @@ -189,10 +197,12 @@ class _HomeScreenInheritedModel extends InheritedModel<_Aspects> {
) =>
(dependencies.contains(_Aspects.tasksToDisplay) &&
tasksToDisplay != oldWidget.tasksToDisplay) ||
(dependencies.contains(_Aspects.doneTaskCount) &&
doneTaskCount != oldWidget.doneTaskCount) ||
(dependencies.contains(_Aspects.showAppBar) &&
showAppBar != oldWidget.showAppBar) ||
(dependencies.contains(_Aspects.showOnlyDoneTasks) &&
showOnlyDoneTasks != oldWidget.showOnlyDoneTasks);
(dependencies.contains(_Aspects.showDoneTasks) &&
showDoneTasks != oldWidget.showDoneTasks);
}

class _Content extends StatelessWidget {
Expand All @@ -204,13 +214,8 @@ class _Content extends StatelessWidget {
return CustomScrollView(
controller: HomeScreen.of(context).scrollController,
slivers: [
SliverToBoxAdapter(
child: Header(
doneTaskCount: HomeScreen.of(context).countDoneTasks(),
shouldShowOnlyDoneTasks: HomeScreen.showOnlyDoneTasksOf(context),
toggleShowOnlyDoneTasks:
HomeScreen.of(context).toggleShowOnlyDoneTasks,
),
const SliverToBoxAdapter(
child: Header(),
),
tasksToDisplay.isEmpty
? const SliverFillRemaining(
Expand Down Expand Up @@ -241,17 +246,9 @@ class _TopBar extends StatelessWidget {
duration: appTopBarAnimationDuration,
child: TopBar(
title: context.l10n.appTitle,
trailing: Padding(
padding: const EdgeInsets.only(right: 16),
child: IconButton(
onPressed: HomeScreen.of(context).toggleShowOnlyDoneTasks,
icon: Icon(
HomeScreen.showOnlyDoneTasksOf(context)
? Icons.visibility_off
: Icons.visibility,
color: context.appColors.blue,
),
),
trailing: const Padding(
padding: EdgeInsets.only(right: 16),
child: DoneTasksVisibilityButton(),
),
),
),
Expand All @@ -264,15 +261,18 @@ class _Empty extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Center(
child: Text(
HomeScreen.showOnlyDoneTasksOf(context)
? context.l10n.noDoneTasks
: context.l10n.noTasks,
style: context.appTextStyles.subhead.copyWith(
color: context.appColors.labelSecondary,
return Padding(
padding: const EdgeInsets.all(32),
child: Center(
child: Text(
HomeScreen.showDoneTasksOf(context)
? context.l10n.noTasks
: context.l10n.onlyDoneTasksLeft,
style: context.appTextStyles.subhead.copyWith(
color: context.appColors.labelSecondary,
),
textAlign: TextAlign.center,
),
textAlign: TextAlign.center,
),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:app/presentation/home_screen/home_screen.dart';
import 'package:app/presentation/theme/app_theme_extensions.dart';
import 'package:flutter/material.dart';

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

@override
Widget build(BuildContext context) {
return IconButton(
onPressed: HomeScreen.of(context).toggleShowDoneTasks,
icon: Icon(
HomeScreen.showDoneTasksOf(context)
? Icons.visibility_off
: Icons.visibility,
color: context.appColors.blue,
),
);
}
}
25 changes: 5 additions & 20 deletions lib/presentation/home_screen/widgets/header.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import 'package:app/l10n/l10n_extension.dart';
import 'package:app/presentation/home_screen/home_screen.dart';
import 'package:app/presentation/home_screen/widgets/done_tasks_visibility_button.dart';
import 'package:app/presentation/theme/app_theme_extensions.dart';
import 'package:flutter/material.dart';

class Header extends StatelessWidget {
final int doneTaskCount;
final bool shouldShowOnlyDoneTasks;
final VoidCallback toggleShowOnlyDoneTasks;

const Header({
super.key,
required this.doneTaskCount,
required this.shouldShowOnlyDoneTasks,
required this.toggleShowOnlyDoneTasks,
});
const Header({super.key});

@override
Widget build(BuildContext context) {
Expand All @@ -35,23 +28,15 @@ class Header extends StatelessWidget {
children: [
Expanded(
child: Text(
context.l10n.tasksDone(doneTaskCount),
context.l10n.tasksDone(HomeScreen.doneTaskCountOf(context)),
style: context.appTextStyles.body.copyWith(
color: context.appColors.labelTertiary,
),
overflow: TextOverflow.ellipsis,
),
),
const SizedBox(width: 16),
IconButton(
onPressed: toggleShowOnlyDoneTasks,
icon: Icon(
shouldShowOnlyDoneTasks
? Icons.visibility_off
: Icons.visibility,
color: context.appColors.blue,
),
),
const DoneTasksVisibilityButton(),
],
),
],
Expand Down

0 comments on commit af77608

Please sign in to comment.