-
Notifications
You must be signed in to change notification settings - Fork 4
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 #77 from Onix-Systems/dev
1.1.2+27
- Loading branch information
Showing
43 changed files
with
492 additions
and
213 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
28 changes: 28 additions & 0 deletions
28
.../{{project_name.snakeCase()}}/lib/app/router/{{#isGoRouter}}app_route.dart{{/isGoRouter}}
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,28 @@ | ||
import 'package:{{project_name}}/app/router/app_route_location.dart'; | ||
|
||
enum AppRoute { | ||
|
||
//{routes declaration end} | ||
|
||
final String routePath; | ||
final AppRouteLocation location; | ||
|
||
const AppRoute( | ||
this.routePath, { | ||
this.location = AppRouteLocation.securedApp, | ||
}); | ||
|
||
static AppRoute? fromName(String? value) { | ||
if (value == null || value.isEmpty) { | ||
return null; | ||
} | ||
return AppRoute.values.firstWhere((e) => e.name == value); | ||
} | ||
|
||
static AppRoute? fromRoutePath(String? value) { | ||
if (value == null || value.isEmpty) { | ||
return null; | ||
} | ||
return AppRoute.values.firstWhere((e) => e.routePath == value); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...t_name.snakeCase()}}/lib/app/router/{{#isGoRouter}}app_route_location.dart{{/isGoRouter}}
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,6 @@ | ||
|
||
enum AppRouteLocation { | ||
auth, | ||
securedApp, | ||
} | ||
|
150 changes: 150 additions & 0 deletions
150
...{{#isGoRouter}}flow{{/isGoRouter}}/{{#isGoRouter}}reusable_route_flow.dart{{/isGoRouter}}
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,150 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import 'package:{{project_name}}/app/router/app_route.dart'; | ||
|
||
|
||
///[ReusableRouteFlow] designed to build nested flow with few screens | ||
///with functionality to return to flow begin point. | ||
///Usage: Extend yor Flow class with [ReusableRouteFlow] | ||
///Example | ||
///class KycFlow extends ReusableRouteFlow { | ||
/// | ||
/// KycFlow(super.rootNavigatorKey); | ||
/// | ||
/// @override | ||
/// List<GoRoute> routes({ | ||
/// bool useRootNavigator = true, | ||
/// }) => | ||
/// [ | ||
/// //flow routes here | ||
/// ]; | ||
/// } | ||
/// | ||
/// Declare Flow in AppRouter and | ||
/// add to GoRouter top level routes from Flow also | ||
/// ...yourFlow.routes() | ||
/// | ||
abstract class ReusableRouteFlow { | ||
final GlobalKey<NavigatorState> rootNavigatorKey; | ||
String? _flowRedirect; | ||
Map<String, String>? _redirectParameters; | ||
Object? _redirectExtra; | ||
int _flowIndex = 0; | ||
|
||
ReusableRouteFlow(this.rootNavigatorKey); | ||
|
||
///Starts Flow rom first screen in flow | ||
///AppRouter.myFlow.startFlow(context) | ||
void startFlow( | ||
BuildContext context, { | ||
AppRoute? flowRedirect, | ||
Map<String, String>? redirectParameters, | ||
Object? redirectExtra, | ||
}) { | ||
if (flowRedirect != null) { | ||
_flowRedirect = flowRedirect.routePath; | ||
_redirectParameters = redirectParameters; | ||
_redirectExtra = redirectExtra; | ||
} else { | ||
_flowRedirect = _getRedirectToCurrentPath(context); | ||
_redirectParameters = _getRedirectToCurrentParameters(context); | ||
_redirectExtra = _getRedirectToCurrentExtra(context); | ||
} | ||
_flowIndex = 0; | ||
final flowEntry = routes().first.name; | ||
if (flowEntry == null) { | ||
throw Exception('Flow entry invalid'); | ||
} | ||
context.pushNamed(flowEntry); | ||
} | ||
|
||
///Navigates to next screen in Flow | ||
void next( | ||
BuildContext context, { | ||
Map<String, String>? parameters, | ||
Object? extra, | ||
}) { | ||
_flowIndex++; | ||
if (_flowIndex >= routes().length) { | ||
finishFlow(context); | ||
return; | ||
} | ||
final flowStep = routes()[_flowIndex].name; | ||
if (flowStep == null) { | ||
throw Exception('Next flow step invalid'); | ||
} | ||
context.pushNamed( | ||
flowStep, | ||
pathParameters: parameters ?? {}, | ||
extra: extra, | ||
); | ||
} | ||
|
||
///Navigates to Previous screen in flow | ||
void previous( | ||
BuildContext context, { | ||
Object? result, | ||
}) { | ||
_flowIndex--; | ||
if (_flowIndex <= 0) { | ||
finishFlow(context); | ||
return; | ||
} | ||
context.pop(result); | ||
} | ||
|
||
///Finish flow and go back to declared redirect location | ||
///AppRouter.myFlow.finishFlow(context) | ||
/// | ||
void finishFlow(BuildContext context) { | ||
final flowRedirect = _flowRedirect; | ||
if (flowRedirect == null) { | ||
throw Exception('Flow entry invalid'); | ||
} | ||
if (flowRedirect.contains('/')) { | ||
context.go( | ||
flowRedirect, | ||
extra: _redirectExtra, | ||
); | ||
} else { | ||
context.goNamed( | ||
flowRedirect, | ||
pathParameters: _redirectParameters ?? {}, | ||
extra: _redirectExtra, | ||
); | ||
} | ||
_flowRedirect = null; | ||
_redirectParameters = null; | ||
_redirectExtra = null; | ||
_flowIndex = 0; | ||
} | ||
|
||
@protected | ||
List<GoRoute> routes({ | ||
bool useRootNavigator = true, | ||
}); | ||
|
||
String _getRedirectToCurrentPath(BuildContext context) { | ||
final router = GoRouter.maybeOf(context); | ||
if (router == null) { | ||
throw Exception('GoRouter not found'); | ||
} | ||
return router.routerDelegate.currentConfiguration.fullPath; | ||
} | ||
|
||
Map<String, String> _getRedirectToCurrentParameters(BuildContext context) { | ||
final router = GoRouter.maybeOf(context); | ||
if (router == null) { | ||
throw Exception('GoRouter not found'); | ||
} | ||
return router.routerDelegate.currentConfiguration.pathParameters; | ||
} | ||
|
||
Object? _getRedirectToCurrentExtra(BuildContext context) { | ||
final router = GoRouter.maybeOf(context); | ||
if (router == null) { | ||
throw Exception('GoRouter not found'); | ||
} | ||
return router.routerDelegate.currentConfiguration.extra; | ||
} | ||
} |
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
40 changes: 36 additions & 4 deletions
40
...brick__/{{project_name.snakeCase()}}/lib/app/service/session_service/session_service.dart
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 |
---|---|---|
@@ -1,29 +1,61 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:{{project_name}}/core/di/remote.dart'; | ||
{{#isGoRouter}}import 'package:flutter/foundation.dart';{{/isGoRouter}} | ||
import 'package:{{project_name}}/core/di/repository.dart'; | ||
import 'package:{{project_name}}/domain/entity/authentication/authentication.dart'; | ||
import 'package:{{project_name}}/app/service/session_service/session_status.dart'; | ||
|
||
class SessionService { | ||
class SessionService {{#isGoRouter}}extends ChangeNotifier{{/isGoRouter}} { | ||
|
||
{{^isGoRouter}} | ||
final StreamController<SessionStatus> _sessionObserver = | ||
StreamController<SessionStatus>(); | ||
StreamController<SessionStatus>(); | ||
{{/isGoRouter}} | ||
|
||
SessionStatus _sessionStatus = SessionStatus.closed; | ||
|
||
{{^isGoRouter}} | ||
Stream<SessionStatus> get sessionObserver => _sessionObserver.stream; | ||
{{/isGoRouter}} | ||
|
||
SessionStatus get sessionStatus => _sessionStatus; | ||
|
||
Future<void> renewSession() async { | ||
final authData = await tokenRepository.getAuthData(); | ||
if (authData == null) { | ||
return; | ||
} | ||
if (authData.accessToken.isEmpty) { | ||
return; | ||
} | ||
_sessionStatus = SessionStatus.open; | ||
{{#isGoRouter}} | ||
notifyListeners(); | ||
{{/isGoRouter}} | ||
{{^isGoRouter}} | ||
_sessionObserver.add(_sessionStatus); | ||
{{/isGoRouter}} | ||
} | ||
|
||
Future<void> openSession(Authentication authEntity) async { | ||
await tokenRepository.update(authEntity); | ||
_sessionStatus = SessionStatus.open; | ||
{{#isGoRouter}} | ||
notifyListeners(); | ||
{{/isGoRouter}} | ||
{{^isGoRouter}} | ||
_sessionObserver.add(_sessionStatus); | ||
{{/isGoRouter}} | ||
} | ||
|
||
Future<void> closeSession() async { | ||
await tokenRepository.clear(); | ||
_sessionStatus = SessionStatus.closed; | ||
_sessionObserver.add(_sessionStatus); | ||
{{#isGoRouter}} | ||
notifyListeners(); | ||
{{/isGoRouter}} | ||
{{^isGoRouter}} | ||
_sessionObserver.add(_sessionStatus); | ||
{{/isGoRouter}} | ||
} | ||
} |
Oops, something went wrong.