/
parser.dart
113 lines (102 loc) · 3.77 KB
/
parser.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:routers_service/src/models/index.dart';
import 'routes.dart';
class RouterConfiguration {
RouterConfiguration(this.path, this.browserState);
GalleryRoutePath? path;
BrowserState? browserState;
}
class GalleryRouteInformationParser
extends RouteInformationParser<RouterConfiguration> {
@override
Future<RouterConfiguration> parseRouteInformation(
RouteInformation routeInformation) {
final uri = Uri.parse(routeInformation.location!);
final state = routeInformation.state;
final browserState = state == null
? BrowserState()
: BrowserState.fromJson(state.toString());
// '/'
if (uri.pathSegments.isEmpty) {
final newState = browserState.galleriesHistory
.contains(RootPagePath.kRootPageLocation)
? browserState
: browserState.rebuild(
(b) => b..galleriesHistory.add(RootPagePath.kRootPageLocation));
return SynchronousFuture<RouterConfiguration>(
RouterConfiguration(const RootPagePath(), newState));
}
// '/${}/'
switch (uri.pathSegments.first) {
// '/login/'
case LoginPagePath.kLoginPageLocation:
return SynchronousFuture<RouterConfiguration>(
RouterConfiguration(const LoginPagePath(), browserState));
// '/album/${}'
case AlbumPagePath.kAlbumPageLocation:
if (uri.pathSegments.length > 1) {
final albumPath = uri.pathSegments[1];
final filePath =
uri.queryParameters[MediaFullscreenPath.kFullscreenPageLocation];
final newState = browserState.galleriesHistory.contains(albumPath)
? browserState
: browserState.rebuild((b) => b..galleriesHistory.add(albumPath));
// '/album/${}?view=${}'
if (filePath != null && filePath.isNotEmpty) {
return SynchronousFuture<RouterConfiguration>(RouterConfiguration(
MediaFullscreenPath(albumPath, filePath), newState));
}
// '/album/${}/'
return SynchronousFuture<RouterConfiguration>(
RouterConfiguration(AlbumPagePath(albumPath), newState));
}
}
// 404
return SynchronousFuture<RouterConfiguration>(
RouterConfiguration(const UnknownPagePath(), browserState));
}
@override
RouteInformation? restoreRouteInformation(RouterConfiguration configuration) {
if (configuration.path is UnknownPagePath) {
return RouteInformation(
location: '/' + UnknownPagePath.kUnknownPageLocation,
state: configuration.browserState!.toJson(),
);
}
if (configuration.path is RootPagePath) {
return RouteInformation(
location: '/' + RootPagePath.kRootPageLocation,
state: configuration.browserState!.toJson(),
);
}
if (configuration.path is LoginPagePath) {
return RouteInformation(
location: '/' + LoginPagePath.kLoginPageLocation,
state: configuration.browserState!.toJson(),
);
}
if (configuration.path is AlbumPagePath) {
final path = configuration.path as AlbumPagePath;
return RouteInformation(
location: '/' + AlbumPagePath.kAlbumPageLocation + '/' + path.albumPath,
state: configuration.browserState!.toJson(),
);
}
if (configuration.path is MediaFullscreenPath) {
final path = configuration.path as MediaFullscreenPath;
return RouteInformation(
location: '/' +
AlbumPagePath.kAlbumPageLocation +
'/' +
path.albumPath +
'?' +
MediaFullscreenPath.kFullscreenPageLocation +
'=' +
path.mediaPath,
state: configuration.browserState!.toJson(),
);
}
return null;
}
}