Last active
January 23, 2023 18:35
-
-
Save ulusoyca/4ef9a0de88cae28dd6e42e769e745327 to your computer and use it in GitHub Desktop.
RoouterDelegate 002_04
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
class MyAppRouterDelegate extends RouterDelegate<MyAppConfiguration> | |
with ChangeNotifier, PopNavigatorRouterDelegateMixin<MyAppConfiguration> { | |
final GlobalKey<NavigatorState> _navigatorKey; | |
final AuthRepository authRepository; | |
final ColorsRepository colorsRepository; | |
bool _show404; | |
bool get show404 => _show404; | |
set show404(bool value) { | |
_show404 = value; | |
if (value == true) { | |
selectedColorCode = null; | |
selectedShapeBorderType = null; | |
} | |
notifyListeners(); | |
} | |
bool _loggedIn; | |
bool get loggedIn => _loggedIn; | |
set loggedIn(value) { | |
if (_loggedIn == true && value == false) { | |
// It is a logout! | |
_clear(); | |
} | |
_loggedIn = value; | |
notifyListeners(); | |
} | |
List<Color> _colors; | |
List<Color> get colors => _colors; | |
set colors(List<Color> value) { | |
_colors = value; | |
if (value != null && selectedColorCode != null) { | |
show404 = !_isValidColor(selectedColorCode); | |
} | |
notifyListeners(); | |
} | |
String _selectedColorCode; | |
String get selectedColorCode => _selectedColorCode; | |
set selectedColorCode(String value) { | |
if (colors != null && value != null) { | |
show404 = !_isValidColor(value); | |
} | |
_selectedColorCode = value; | |
notifyListeners(); | |
} | |
ShapeBorderType _selectedShapeBorderType; | |
ShapeBorderType get selectedShapeBorderType => _selectedShapeBorderType; | |
set selectedShapeBorderType(ShapeBorderType value) { | |
_selectedShapeBorderType = value; | |
notifyListeners(); | |
} | |
@override | |
GlobalKey<NavigatorState> get navigatorKey => _navigatorKey; | |
MyAppRouterDelegate(this.authRepository, this.colorsRepository) : _navigatorKey = GlobalKey<NavigatorState>() { | |
_init(); | |
} | |
_init() async { | |
loggedIn = await authRepository.isUserLoggedIn(); | |
if (loggedIn == true) { | |
colors = await colorsRepository.fetchColors(); | |
} | |
} | |
@override | |
MyAppConfiguration get currentConfiguration { | |
if (loggedIn == false) { | |
return MyAppConfiguration.login(); | |
} else if (loggedIn == null) { | |
return MyAppConfiguration.splash(); | |
} else if (show404 == true) { | |
return MyAppConfiguration.unknown(); | |
} else if (selectedColorCode == null) { | |
return MyAppConfiguration.home(); | |
} else if (selectedShapeBorderType == null) { | |
return MyAppConfiguration.color(selectedColorCode); | |
} else if (selectedShapeBorderType != null) { | |
return MyAppConfiguration.shapeBorder(selectedColorCode, selectedShapeBorderType); | |
} else { | |
return null; | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
List<Page> stack; | |
if (show404 == true) { | |
stack = _unknownStack; | |
}else if (loggedIn == null || (loggedIn && colors == null)) { | |
stack = _splashStack; | |
} else if (loggedIn) { | |
stack = _loggedInStack; | |
} else { | |
stack = _loggedOutStack; | |
} | |
return Navigator( | |
key: navigatorKey, | |
pages: stack, | |
onPopPage: (route, result) { | |
if (!route.didPop(result)) return false; | |
if (selectedShapeBorderType == null) selectedColorCode = null; | |
selectedShapeBorderType = null; | |
return true; | |
}, | |
); | |
} | |
List<Page> get _splashStack { | |
String process; | |
if (loggedIn == null) { | |
process = 'Checking login state...'; | |
} else if (colors == null) { | |
process = 'Fetching colors...'; | |
} | |
return [ | |
SplashPage(process: process), | |
]; | |
} | |
List<Page> get _unknownStack => [UnknownPage()]; | |
List<Page> get _loggedOutStack => [ | |
LoginPage(onLogin: () async { | |
loggedIn = true; | |
colors = await colorsRepository.fetchColors(); | |
}) | |
]; | |
List<Page> get _loggedInStack { | |
final onLogout = () async { | |
loggedIn = false; | |
}; | |
return [ | |
HomePage( | |
onColorTap: (String colorCode) { | |
selectedColorCode = colorCode; | |
}, | |
colors: colors, | |
onLogout: onLogout, | |
), | |
if (selectedColorCode != null) | |
ColorPage( | |
selectedColorCode: selectedColorCode, | |
onShapeTap: (ShapeBorderType shapeBorderType) { | |
selectedShapeBorderType = shapeBorderType; | |
}, | |
onLogout: onLogout, | |
), | |
if (selectedShapeBorderType != null) | |
ShapePage( | |
colorCode: selectedColorCode, | |
shapeBorderType: selectedShapeBorderType, | |
onLogout: onLogout, | |
) | |
]; | |
} | |
@override | |
Future<void> setNewRoutePath(MyAppConfiguration configuration) async { | |
if (configuration.unknown) { | |
show404 = true; | |
} else if (configuration.isHomePage || configuration.isLoginPage || configuration.isSplashPage) { | |
show404 = false; | |
selectedColorCode = null; | |
selectedShapeBorderType = null; | |
} else if (configuration.isColorPage) { | |
show404 = false; | |
selectedColorCode = configuration.colorCode; | |
selectedShapeBorderType = null; | |
} else if (configuration.isShapePage) { | |
show404 = false; | |
selectedColorCode = configuration.colorCode; | |
selectedShapeBorderType = configuration.shapeBorderType; | |
} else { | |
print(' Could not set new route'); | |
} | |
} | |
_clear() { | |
selectedColorCode = null; | |
selectedShapeBorderType = null; | |
colors = null; | |
show404 = null; | |
} | |
bool _isValidColor(String colorCode) { | |
final List<String> colorCodes = colors.map((e) { | |
return e.toHex(); | |
}).toList(); | |
return colorCodes.contains("$colorCode"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not to store
MyAppConfiguration
directly inMyAppRouterDelegate
like this:and just use
_configuration
in#build
method directly?