Skip to content

Commit

Permalink
fix navigate to same route
Browse files Browse the repository at this point in the history
  • Loading branch information
jonataslaw committed Aug 9, 2021
2 parents ed9ddcf + ee6e490 commit 2ca4670
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 81 deletions.
119 changes: 59 additions & 60 deletions README-vi.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/get_connect/http/src/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class GetHttpClient {
if (authenticate) await _modifier.authenticator!(request);
final newRequest = await _modifier.modifyRequest<T>(request);

_httpClient.timeout = timeout;
var response = await _httpClient.send<T>(newRequest);

final newResponse =
Expand Down
4 changes: 4 additions & 0 deletions lib/get_connect/http/src/http/html/http_request_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ class HttpRequestImpl implements HttpRequestBase {
///on different sites. The default is false
final bool withCredentials;

@override
Duration? timeout;

/// Sends an HTTP request and asynchronously returns the response.
@override
Future<Response<T>> send<T>(Request<T> request) async {
var bytes = await request.bodyBytes.toBytes();
html.HttpRequest xhr;

xhr = html.HttpRequest()
..timeout = timeout?.inMilliseconds
..open(request.method, '${request.url}', async: true); // check this

_xhrs.add(xhr);
Expand Down
8 changes: 8 additions & 0 deletions lib/get_connect/http/src/http/interface/request_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ abstract class HttpRequestBase {

/// Closes the [Request] and cleans up any resources associated with it.
void close();

/// Gets and sets the timeout.
///
/// For mobile, this value will be applied for both connection and request
/// timeout.
///
/// For web, this value will be the request timeout.
Duration? timeout;
}
13 changes: 10 additions & 3 deletions lib/get_connect/http/src/http/io/http_request_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ class HttpRequestImpl extends HttpRequestBase {
@override
Future<Response<T>> send<T>(Request<T> request) async {
var stream = request.bodyBytes.asBroadcastStream();

io.HttpClientRequest? ioRequest;
try {
var ioRequest = (await _httpClient!.openUrl(request.method, request.url))
_httpClient!.connectionTimeout = timeout;
ioRequest = (await _httpClient!.openUrl(request.method, request.url))
..followRedirects = request.followRedirects
..persistentConnection = request.persistentConnection
..maxRedirects = request.maxRedirects
..contentLength = request.contentLength ?? -1;
request.headers.forEach(ioRequest.headers.set);

var response = await stream.pipe(ioRequest) as io.HttpClientResponse;
var response = timeout == null
? await stream.pipe(ioRequest) as io.HttpClientResponse
: await stream.pipe(ioRequest).timeout(timeout!)
as io.HttpClientResponse;

var headers = <String, String>{};
response.headers.forEach((key, values) {
Expand All @@ -68,6 +72,9 @@ class HttpRequestImpl extends HttpRequestBase {
body: body,
bodyString: stringBody,
);
} on TimeoutException catch (_) {
ioRequest?.abort();
rethrow;
} on io.HttpException catch (error) {
throw GetHttpException(error.message, error.uri);
}
Expand Down
1 change: 0 additions & 1 deletion lib/get_instance/src/get_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ class GetInstance {
}

if (builder.fenix) {
//TODO: Remove if is late remove
builder.dependency = null;
builder.isInit = false;
return true;
Expand Down
50 changes: 38 additions & 12 deletions lib/get_navigation/src/extension_navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -539,25 +539,28 @@ extension GetNavigation on GetInterface {
Curve? curve,
Duration? duration,
int? id,
String? routeName,
bool fullscreenDialog = false,
dynamic arguments,
Bindings? binding,
bool preventDuplicates = true,
bool? popGesture,
double Function(BuildContext context)? gestureWidth,
}) {
var routeName = "/${page.runtimeType.toString()}";
// var routeName = "/${page.runtimeType}";
routeName ??= "/${page.runtimeType}";
routeName = _cleanRouteName(routeName);
if (preventDuplicates && routeName == currentRoute) {
return null;
}
return global(id).currentState?.push<T>(
GetPageRoute<T>(
opaque: opaque ?? true,
page: _resolve(page, 'to'),
page: _resolvePage(page, 'to'),
routeName: routeName,
gestureWidth: gestureWidth,
settings: RouteSettings(
// name: forceRouteName ? '${a.runtimeType}' : '',
name: routeName,
arguments: arguments,
),
popGesture: popGesture ?? defaultPopGesture,
Expand All @@ -570,7 +573,7 @@ extension GetNavigation on GetInterface {
);
}

GetPageBuilder _resolve(dynamic page, String method) {
GetPageBuilder _resolvePage(dynamic page, String method) {
if (page is GetPageBuilder) {
return page;
} else if (page is Widget) {
Expand Down Expand Up @@ -909,23 +912,28 @@ you can only use widgets and widget functions here''';
Curve? curve,
bool? popGesture,
int? id,
String? routeName,
dynamic arguments,
Bindings? binding,
bool fullscreenDialog = false,
bool preventDuplicates = true,
Duration? duration,
double Function(BuildContext context)? gestureWidth,
}) {
var routeName = "/${page.runtimeType.toString()}";
routeName ??= "/${page.runtimeType.toString()}";
routeName = _cleanRouteName(routeName);
if (preventDuplicates && routeName == currentRoute) {
return null;
}
return global(id).currentState?.pushReplacement(GetPageRoute(
opaque: opaque,
gestureWidth: gestureWidth,
page: _resolve(page, 'off'),
page: _resolvePage(page, 'off'),
binding: binding,
settings: RouteSettings(arguments: arguments),
settings: RouteSettings(
arguments: arguments,
name: routeName,
),
routeName: routeName,
fullscreenDialog: fullscreenDialog,
popGesture: popGesture ?? defaultPopGesture,
Expand All @@ -934,7 +942,6 @@ you can only use widgets and widget functions here''';
transitionDuration: duration ?? defaultTransitionDuration));
}

/// **Navigation.pushAndRemoveUntil()** shortcut .<br><br>
///
/// Push a `page` and pop several pages in the stack
/// until [predicate] returns true. [predicate] is optional
Expand Down Expand Up @@ -971,6 +978,7 @@ you can only use widgets and widget functions here''';
bool opaque = false,
bool? popGesture,
int? id,
String? routeName,
dynamic arguments,
Bindings? binding,
bool fullscreenDialog = false,
Expand All @@ -979,16 +987,19 @@ you can only use widgets and widget functions here''';
Duration? duration,
double Function(BuildContext context)? gestureWidth,
}) {
var routeName = "/${page.runtimeType.toString()}";

routeName ??= "/${page.runtimeType.toString()}";
routeName = _cleanRouteName(routeName);
return global(id).currentState?.pushAndRemoveUntil<T>(
GetPageRoute<T>(
opaque: opaque,
popGesture: popGesture ?? defaultPopGesture,
page: _resolve(page, 'offAll'),
page: _resolvePage(page, 'offAll'),
binding: binding,
gestureWidth: gestureWidth,
settings: RouteSettings(arguments: arguments),
settings: RouteSettings(
name: routeName,
arguments: arguments,
),
fullscreenDialog: fullscreenDialog,
routeName: routeName,
transition: transition ?? defaultTransition,
Expand All @@ -998,6 +1009,21 @@ you can only use widgets and widget functions here''';
predicate ?? (route) => false);
}

/// Takes a route [name] String generated by [to], [off], [offAll]
/// (and similar context navigation methods), cleans the extra chars and
/// accommodates the format.
/// TODO: check for a more "appealing" URL naming convention.
/// `() => MyHomeScreenView` becomes `/my-home-screen-view`.
String _cleanRouteName(String name) {
name = name.replaceAll('() => ', '');
/// uncommonent for URL styling.
// name = name.paramCase!;
if (!name.startsWith('/')) {
name = '/$name';
}
return Uri.tryParse(name)?.toString() ?? name;
}

/// change default config of Get
void config(
{bool? enableLog,
Expand Down
6 changes: 3 additions & 3 deletions lib/get_navigation/src/router_report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class RouterReportManager<T> {
static void reportRouteDispose(Route disposed) {
if (Get.smartManagement != SmartManagement.onlyBuilder) {
WidgetsBinding.instance!.addPostFrameCallback((_) {
///TODO: Is necessary this comparator?
//if (_current != disposed) {
///TODO: Check if it's necessary to compare _current != disposed
///Adding it breaks the context Navigation logic,
///as it resolves by Route name.
_removeDependencyByRoute(disposed);
// }
});
}
}
Expand Down
2 changes: 0 additions & 2 deletions lib/get_navigation/src/routes/get_route.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down
2 changes: 2 additions & 0 deletions lib/get_utils/src/extensions/string_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ extension GetStringUtils on String {

String? get camelCase => GetUtils.camelCase(this);

String? get paramCase => GetUtils.paramCase(this);

String numericOnly({bool firstWordOnly = false}) =>
GetUtils.numericOnly(this, firstWordOnly: firstWordOnly);

Expand Down
38 changes: 38 additions & 0 deletions lib/get_utils/src/get_utils/get_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,44 @@ class GetUtils {
return newString[0].toLowerCase() + newString.substring(1);
}

/// credits to "ReCase" package.
static final RegExp _upperAlphaRegex = RegExp(r'[A-Z]');
static final _symbolSet = {' ', '.', '/', '_', '\\', '-'};
static List<String> _groupIntoWords(String text) {
var sb = StringBuffer();
var words = <String>[];
var isAllCaps = text.toUpperCase() == text;

for (var i = 0; i < text.length; i++) {
var char = text[i];
var nextChar = i + 1 == text.length ? null : text[i + 1];
if (_symbolSet.contains(char)) {
continue;
}
sb.write(char);
var isEndOfWord = nextChar == null ||
(_upperAlphaRegex.hasMatch(nextChar) && !isAllCaps) ||
_symbolSet.contains(nextChar);
if (isEndOfWord) {
words.add('$sb');
sb.clear();
}
}
return words;
}

/// snake_case
static String? snakeCase(String? text, {String separator = '_'}) {
if (isNullOrBlank(text)!) {
return null;
}
return _groupIntoWords(text!)
.map((word) => word.toLowerCase()).join(separator);
}

/// param-case
static String? paramCase(String? text) => snakeCase(text, separator: '-');

/// Extract numeric value of string
/// Example: OTP 12312 27/04/2020 => 1231227042020ß
/// If firstword only is true, then the example return is "12312"
Expand Down

0 comments on commit 2ca4670

Please sign in to comment.