From be266bc18fca51f4e4606de2a294249c06ef97dc Mon Sep 17 00:00:00 2001 From: star Date: Tue, 8 Aug 2023 03:07:35 +0900 Subject: [PATCH] fix: use enum OTLNavigatorTransition instead of String --- lib/pages/course_detail_page.dart | 7 +- lib/pages/lecture_detail_page.dart | 24 +++--- lib/pages/liked_review_page.dart | 3 +- lib/pages/main_page.dart | 12 +-- lib/utils/navigator.dart | 89 +++++++++++---------- lib/widgets/lecture_group_simple_block.dart | 11 +-- lib/widgets/otl_scaffold.dart | 4 +- 7 files changed, 74 insertions(+), 76 deletions(-) diff --git a/lib/pages/course_detail_page.dart b/lib/pages/course_detail_page.dart index 4c392d49..a4136721 100644 --- a/lib/pages/course_detail_page.dart +++ b/lib/pages/course_detail_page.dart @@ -19,11 +19,9 @@ import 'package:otlplus/widgets/review_block.dart'; import 'package:otlplus/widgets/review_write_block.dart'; class CourseDetailPage extends StatelessWidget { - CourseDetailPage({Key? key, this.fromLectureDetailPage = false}) - : super(key: key); + CourseDetailPage({Key? key}) : super(key: key); static String route = 'course_detail_page'; - final bool fromLectureDetailPage; final _scrollController = ScrollController(); @override @@ -326,8 +324,7 @@ class CourseDetailPage extends StatelessWidget { return LectureGroupSimpleBlock( lectures: filteredLectures, semester: semester, - filter: selectedFilter, - fromLectureDetailPage: fromLectureDetailPage); + filter: selectedFilter); }, ).toList(), ), diff --git a/lib/pages/lecture_detail_page.dart b/lib/pages/lecture_detail_page.dart index a7dae8c2..6c3487e0 100644 --- a/lib/pages/lecture_detail_page.dart +++ b/lib/pages/lecture_detail_page.dart @@ -156,19 +156,19 @@ class LectureDetailPage extends StatelessWidget { return Row( mainAxisAlignment: MainAxisAlignment.end, children: [ - Visibility( - visible: fromCourseDetailPage == false, - child: Padding( - padding: EdgeInsets.only(right: 8.0), - child: IconTextButton( - onTap: () { + Padding( + padding: EdgeInsets.only(right: 8.0), + child: IconTextButton( + onTap: () { + if (fromCourseDetailPage) { + OTLNavigator.pop(context); + } else { context.read().loadCourse(lecture.course); - OTLNavigator.push( - context, CourseDetailPage(fromLectureDetailPage: false)); - }, - text: "dictionary.dictionary".tr(), - textStyle: bodyRegular.copyWith(color: OTLColor.pinksMain), - ), + OTLNavigator.push(context, CourseDetailPage()); + } + }, + text: "dictionary.dictionary".tr(), + textStyle: bodyRegular.copyWith(color: OTLColor.pinksMain), ), ), IconTextButton( diff --git a/lib/pages/liked_review_page.dart b/lib/pages/liked_review_page.dart index cccb29bd..10309a42 100644 --- a/lib/pages/liked_review_page.dart +++ b/lib/pages/liked_review_page.dart @@ -68,7 +68,8 @@ class LikedReviewPage extends StatelessWidget { reviews[index].course.id); OTLNavigator.push( context, CourseDetailPage(), - transition: 'right-left'); + transition: OTLNavigatorTransition + .rightLeft); }, ); }, diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 687560da..88eee7ab 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -53,8 +53,8 @@ class _MainPageState extends State { trailing: Row( children: [ IconTextButton( - onTap: () => - OTLNavigator.push(context, UserPage(), transition: 'down-up'), + onTap: () => OTLNavigator.push(context, UserPage(), + transition: OTLNavigatorTransition.downUp), icon: 'assets/icons/person.svg', iconSize: 24, color: OTLColor.pinksMain, @@ -63,7 +63,7 @@ class _MainPageState extends State { ), IconTextButton( onTap: () => OTLNavigator.push(context, SettingsPage(), - transition: 'down-up'), + transition: OTLNavigatorTransition.downUp), icon: 'assets/icons/gear.svg', iconSize: 24, color: OTLColor.pinksMain, @@ -241,7 +241,8 @@ class _MainPageState extends State { children: [ IconTextButton( onTap: () { - OTLNavigator.push(context, PrivacyPage(), transition: 'down-up'); + OTLNavigator.push(context, PrivacyPage(), + transition: OTLNavigatorTransition.downUp); }, text: 'title.privacy'.tr(), textStyle: labelRegular.copyWith(color: OTLColor.gray75), @@ -250,7 +251,8 @@ class _MainPageState extends State { ), IconTextButton( onTap: () { - OTLNavigator.push(context, PeoplePage(), transition: 'down-up'); + OTLNavigator.push(context, PeoplePage(), + transition: OTLNavigatorTransition.downUp); }, text: 'title.credit'.tr(), textStyle: labelRegular.copyWith(color: OTLColor.gray75), diff --git a/lib/utils/navigator.dart b/lib/utils/navigator.dart index 020bd5f4..6940caed 100644 --- a/lib/utils/navigator.dart +++ b/lib/utils/navigator.dart @@ -1,26 +1,30 @@ import 'package:flutter/material.dart'; +enum OTLNavigatorTransition { rightLeft, downUp, immediate } + +enum _TransitionHistory { rightLeft, downUp, immediate, dialog } + class OTLNavigator { static List _rightleftTransitionHistory = []; static List _downupTransitionHistory = []; - //0 for right-left, 1 for down-up, 2 for immediate, 3 for dialog - static List _history = []; + static List<_TransitionHistory> _history = []; static void _removeLastHistory() { switch (_history.removeLast()) { - case 0: + case _TransitionHistory.rightLeft: _rightleftTransitionHistory.removeLast(); break; - case 1: - case 2: + case _TransitionHistory.downUp: + case _TransitionHistory.immediate: _downupTransitionHistory.removeLast(); break; - case 3: + case _TransitionHistory.dialog: break; } } - static void _removeLastUntil(bool Function(int mode) predicate) { + static void _removeLastUntil( + bool Function(_TransitionHistory mode) predicate) { while (!predicate(_history.last)) { _removeLastHistory(); } @@ -28,70 +32,69 @@ class OTLNavigator { } static Future push(BuildContext context, Widget page, - {String transition = "right-left"}) { - assert(transition == "down-up" || - transition == "right-left" || - transition == "immediate"); + {OTLNavigatorTransition transition = OTLNavigatorTransition.rightLeft}) { Route _route; - if (transition == "right-left") { - _route = buildRightLeftPageRoute(page); - _rightleftTransitionHistory.add(_route); - _history.add(0); - } else if (transition == "down-up") { - _route = buildDownUpPageRoute(page); - _downupTransitionHistory.add(_route); - _history.add(1); - } else { - _route = buildImmediatePageRoute(page); - _downupTransitionHistory.add(_route); - _history.add(2); + switch (transition) { + case OTLNavigatorTransition.rightLeft: + _route = buildRightLeftPageRoute(page); + _rightleftTransitionHistory.add(_route); + _history.add(_TransitionHistory.rightLeft); + break; + case OTLNavigatorTransition.downUp: + _route = buildDownUpPageRoute(page); + _downupTransitionHistory.add(_route); + _history.add(_TransitionHistory.downUp); + break; + case OTLNavigatorTransition.immediate: + _route = buildImmediatePageRoute(page); + _downupTransitionHistory.add(_route); + _history.add(_TransitionHistory.immediate); + break; } return Navigator.of(context).push(_route); } static Future pushRoot( BuildContext context, Widget page, - {String transition = "immediate"}) { - assert(transition == "down-up" || - transition == "right-left" || - transition == "immediate"); + {OTLNavigatorTransition transition = OTLNavigatorTransition.immediate}) { _rightleftTransitionHistory.clear(); _downupTransitionHistory.clear(); _history.clear(); Route _route; - if (transition == "right-left") { - _route = buildRightLeftPageRoute(page); - } else if (transition == "down-up") { - _route = buildDownUpPageRoute(page); - } else { - _route = buildImmediatePageRoute(page); + switch (transition) { + case OTLNavigatorTransition.rightLeft: + _route = buildRightLeftPageRoute(page); + break; + case OTLNavigatorTransition.downUp: + _route = buildDownUpPageRoute(page); + break; + case OTLNavigatorTransition.immediate: + _route = buildImmediatePageRoute(page); + break; } return Navigator.of(context) .pushAndRemoveUntil(_route, (Route route) => false); } static void pop(BuildContext context, - {String transition = "none", T? result}) { - assert(transition == "down-up" || - transition == "right-left" || - transition == "immediate" || - transition == "none"); + {OTLNavigatorTransition? until, T? result}) { NavigatorState navigator = Navigator.of(context); assert(_history.isNotEmpty); - if (transition == "none") { + if (until == null) { _removeLastHistory(); return navigator.pop(result); - } else if (transition == "right-left") { + } else if (until == OTLNavigatorTransition.rightLeft) { assert(_rightleftTransitionHistory.isNotEmpty); navigator.popUntil((Route route) => _rightleftTransitionHistory.last == route || route.isFirst); - _removeLastUntil((mode) => mode == 0); + _removeLastUntil((mode) => mode == _TransitionHistory.rightLeft); return navigator.pop(result); } else { assert(_downupTransitionHistory.isNotEmpty); navigator.popUntil((Route route) => _downupTransitionHistory.last == route || route.isFirst); - _removeLastUntil((mode) => (mode == 1 || mode == 2)); + _removeLastUntil((mode) => (mode == _TransitionHistory.downUp || + mode == _TransitionHistory.immediate)); return navigator.pop(result); } } @@ -113,7 +116,7 @@ class OTLNavigator { Offset? anchorPoint, TraversalEdgeBehavior? traversalEdgeBehavior, }) { - _history.add(3); + _history.add(_TransitionHistory.dialog); return showDialog( context: context, builder: builder, diff --git a/lib/widgets/lecture_group_simple_block.dart b/lib/widgets/lecture_group_simple_block.dart index 98e40f45..2165efa9 100644 --- a/lib/widgets/lecture_group_simple_block.dart +++ b/lib/widgets/lecture_group_simple_block.dart @@ -14,13 +14,9 @@ class LectureGroupSimpleBlock extends StatelessWidget { final List lectures; final int semester; final String? filter; - final bool fromLectureDetailPage; LectureGroupSimpleBlock( - {required this.lectures, - required this.semester, - this.filter, - this.fromLectureDetailPage = false}); + {required this.lectures, required this.semester, this.filter}); @override Widget build(BuildContext context) { @@ -65,9 +61,8 @@ class LectureGroupSimpleBlock extends StatelessWidget { context .read() .loadLecture(lecture.id, false); - if (fromLectureDetailPage == false) - OTLNavigator.push(context, - LectureDetailPage(fromCourseDetailPage: true)); + OTLNavigator.push(context, + LectureDetailPage(fromCourseDetailPage: true)); }, child: Padding( padding: const EdgeInsets.symmetric( diff --git a/lib/widgets/otl_scaffold.dart b/lib/widgets/otl_scaffold.dart index 34f6ab21..0996c9c1 100644 --- a/lib/widgets/otl_scaffold.dart +++ b/lib/widgets/otl_scaffold.dart @@ -165,7 +165,7 @@ class _OTLLayoutState extends State { IconTextButton( icon: Icons.navigate_before, onTap: () => OTLNavigator.pop(context, - transition: 'right-left'), + until: OTLNavigatorTransition.rightLeft), padding: EdgeInsets.all(16)), if (hasDrawer) IconTextButton( @@ -193,7 +193,7 @@ class _OTLLayoutState extends State { IconTextButton( icon: Icons.close, onTap: () => OTLNavigator.pop(context, - transition: 'down-up'), + until: OTLNavigatorTransition.downUp), padding: EdgeInsets.all(16)), ], )