-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from FabianVarela/navigator2
Implement Navigator 2.0
- Loading branch information
Showing
14 changed files
with
1,205 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import 'package:equatable/equatable.dart'; | ||
|
||
class CrudTodoConfig extends Equatable { | ||
const CrudTodoConfig.categoryList() | ||
: currentCategoryId = null, | ||
currentTodoId = null, | ||
isTodoNew = false, | ||
isTodoUpdate = false, | ||
isUnknown = false; | ||
|
||
const CrudTodoConfig.todoList({String? categoryId}) | ||
: currentCategoryId = categoryId, | ||
currentTodoId = null, | ||
isTodoNew = false, | ||
isTodoUpdate = false, | ||
isUnknown = false; | ||
|
||
const CrudTodoConfig.addTodo({String? categoryId}) | ||
: currentCategoryId = categoryId, | ||
currentTodoId = null, | ||
isTodoNew = true, | ||
isTodoUpdate = false, | ||
isUnknown = false; | ||
|
||
const CrudTodoConfig.updateTodo({String? categoryId, String? todoId}) | ||
: currentCategoryId = categoryId, | ||
currentTodoId = todoId, | ||
isTodoNew = false, | ||
isTodoUpdate = true, | ||
isUnknown = false; | ||
|
||
const CrudTodoConfig.unknown() | ||
: currentCategoryId = null, | ||
currentTodoId = null, | ||
isTodoNew = false, | ||
isTodoUpdate = false, | ||
isUnknown = true; | ||
|
||
final String? currentCategoryId; | ||
final String? currentTodoId; | ||
final bool isTodoNew; | ||
final bool isTodoUpdate; | ||
final bool isUnknown; | ||
|
||
bool get isPageUnknown => isUnknown; | ||
|
||
bool get isCategoryListPage => | ||
currentCategoryId == null && | ||
currentTodoId == null && | ||
!isTodoNew && | ||
!isTodoUpdate && | ||
!isUnknown; | ||
|
||
bool get isTodoListPage => | ||
currentCategoryId != null && | ||
currentTodoId == null && | ||
!isTodoNew && | ||
!isTodoUpdate && | ||
!isUnknown; | ||
|
||
bool get isAddTodoPage => | ||
currentCategoryId != null && | ||
currentTodoId == null && | ||
isTodoNew && | ||
!isTodoUpdate && | ||
!isUnknown; | ||
|
||
bool get isUpdateTodoPage => | ||
currentCategoryId != null && | ||
currentTodoId != null && | ||
!isTodoNew && | ||
isTodoUpdate && | ||
!isUnknown; | ||
|
||
@override | ||
List<Object?> get props => [ | ||
currentCategoryId, | ||
currentTodoId, | ||
isTodoNew, | ||
isTodoUpdate, | ||
isUnknown, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import 'package:crud_todo_app/navigator/config/crud_todo_config.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class CrudTodoPath { | ||
static const category = 'categories'; | ||
static const todo = 'todo'; | ||
static const unknown = '404'; | ||
} | ||
|
||
class CrudTodoInformationParser extends RouteInformationParser<CrudTodoConfig> { | ||
@override | ||
Future<CrudTodoConfig> parseRouteInformation( | ||
RouteInformation routeInformation, | ||
) async { | ||
final uri = Uri.parse(routeInformation.location ?? ''); | ||
|
||
if (uri.pathSegments.isEmpty) { | ||
// Home '/' | ||
return const CrudTodoConfig.categoryList(); | ||
} else if (uri.pathSegments.length == 1) { | ||
// Home '/categories' | ||
final firstSegment = uri.pathSegments[0].toLowerCase(); | ||
if (firstSegment == CrudTodoPath.category) { | ||
return const CrudTodoConfig.categoryList(); | ||
} | ||
} else if (uri.pathSegments.length == 2) { | ||
// Category detail '/categories/{id}' | ||
final firstSegment = uri.pathSegments[0].toLowerCase(); | ||
final secondSegment = uri.pathSegments[1]; | ||
|
||
if (firstSegment == CrudTodoPath.category) { | ||
if (secondSegment.isNotEmpty) { | ||
return CrudTodoConfig.todoList(categoryId: secondSegment); | ||
} | ||
} | ||
} else if (uri.pathSegments.length == 3) { | ||
// Todos new '/categories/{id}/todos/ | ||
final firstSegment = uri.pathSegments[0].toLowerCase(); | ||
final secondSegment = uri.pathSegments[1]; | ||
final thirdSegment = uri.pathSegments[2].toLowerCase(); | ||
|
||
if (firstSegment == CrudTodoPath.category) { | ||
if (secondSegment.isNotEmpty) { | ||
if (thirdSegment == CrudTodoPath.todo) { | ||
return CrudTodoConfig.addTodo(categoryId: secondSegment); | ||
} | ||
} | ||
} | ||
} else if (uri.pathSegments.length == 4) { | ||
// Todos update '/categories/{catId}/todos/{todoId} | ||
final firstSegment = uri.pathSegments[0].toLowerCase(); | ||
final secondSegment = uri.pathSegments[1]; | ||
final thirdSegment = uri.pathSegments[2].toLowerCase(); | ||
final lastSegment = uri.pathSegments[3]; | ||
|
||
if (firstSegment == CrudTodoPath.category) { | ||
if (secondSegment.isNotEmpty) { | ||
if (thirdSegment == CrudTodoPath.todo) { | ||
if (lastSegment.isNotEmpty) { | ||
return CrudTodoConfig.updateTodo( | ||
categoryId: secondSegment, | ||
todoId: lastSegment, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return const CrudTodoConfig.unknown(); | ||
} | ||
|
||
@override | ||
RouteInformation? restoreRouteInformation(CrudTodoConfig configuration) { | ||
if (configuration.isUnknown) { | ||
return const RouteInformation(location: '/${CrudTodoPath.unknown}'); | ||
} else if (configuration.isCategoryListPage) { | ||
const categoryPath = CrudTodoPath.category; | ||
|
||
return const RouteInformation(location: '/$categoryPath'); | ||
} else if (configuration.isTodoListPage) { | ||
const categoryPath = CrudTodoPath.category; | ||
final currentCategoryId = configuration.currentCategoryId; | ||
|
||
return RouteInformation(location: '/$categoryPath/$currentCategoryId'); | ||
} else if (configuration.isAddTodoPage) { | ||
const categoryPath = CrudTodoPath.category; | ||
final currentCategoryId = configuration.currentCategoryId; | ||
const todoPath = CrudTodoPath.todo; | ||
|
||
return RouteInformation( | ||
location: '/$categoryPath/$currentCategoryId/$todoPath/', | ||
); | ||
} else if (configuration.isUpdateTodoPage) { | ||
const categoryPath = CrudTodoPath.category; | ||
final currentCategoryId = configuration.currentCategoryId; | ||
const todoPath = CrudTodoPath.todo; | ||
final currentTodoId = configuration.currentTodoId; | ||
|
||
return RouteInformation( | ||
location: '/$categoryPath/$currentCategoryId/$todoPath/$currentTodoId', | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.