From e9b7a397598587957dd99057b365502ea73f63a7 Mon Sep 17 00:00:00 2001 From: foxsofter Date: Wed, 4 Nov 2020 13:56:59 +0800 Subject: [PATCH] feat: refactor get route methods --- .../flutter/thrio/module/ThrioModule.kt | 19 +- .../flutter/thrio/navigator/PageRoutes.kt | 2 +- .../thrio/navigator/RouteReceiveChannel.kt | 6 +- .../flutter/thrio/navigator/ThrioNavigator.kt | 15 +- .../Navigator/NavigatorRouteReceiveChannel.m | 401 ++++++++++-------- ios/Classes/Navigator/ThrioNavigator.h | 16 +- ios/Classes/Navigator/ThrioNavigator.m | 38 +- .../UINavigationController+Navigator.h | 6 +- .../UINavigationController+Navigator.m | 24 +- .../Navigator/UIViewController+Navigator.h | 4 +- .../Navigator/UIViewController+Navigator.m | 12 +- .../navigator_route_send_channel.dart | 4 +- lib/src/navigator/thrio_navigator.dart | 4 +- .../navigator/thrio_navigator_implement.dart | 4 +- 14 files changed, 303 insertions(+), 252 deletions(-) diff --git a/android/src/main/kotlin/com/hellobike/flutter/thrio/module/ThrioModule.kt b/android/src/main/kotlin/com/hellobike/flutter/thrio/module/ThrioModule.kt index 92f02c14..f385985b 100644 --- a/android/src/main/kotlin/com/hellobike/flutter/thrio/module/ThrioModule.kt +++ b/android/src/main/kotlin/com/hellobike/flutter/thrio/module/ThrioModule.kt @@ -25,10 +25,9 @@ package com.hellobike.flutter.thrio.module import android.app.Application import android.content.Context +import com.hellobike.flutter.thrio.navigator.ActivityDelegate import com.hellobike.flutter.thrio.navigator.FlutterEngineFactory -import com.hellobike.flutter.thrio.navigator.IntentBuilders import com.hellobike.flutter.thrio.navigator.Log -import com.hellobike.flutter.thrio.navigator.ThrioNavigator open class ThrioModule { private val modules by lazy { mutableMapOf, ThrioModule>() } @@ -38,18 +37,19 @@ open class ThrioModule { @JvmStatic fun init(context: Application, module: ThrioModule) { + context.registerActivityLifecycleCallbacks(ActivityDelegate) + root.registerModule(context, module) root.initModule(context) - ThrioNavigator.init(context) } @JvmStatic fun init(context: Application, module: ThrioModule, multiEngineEnabled: Boolean) { FlutterEngineFactory.isMultiEngineEnabled = multiEngineEnabled + context.registerActivityLifecycleCallbacks(ActivityDelegate) root.registerModule(context, module) root.initModule(context) - ThrioNavigator.init(context) } } @@ -68,6 +68,9 @@ open class ThrioModule { modules.values.forEach { it.onPageRegister(context) } + if (!FlutterEngineFactory.isMultiEngineEnabled) { + FlutterEngineFactory.startup(context) + } } protected open fun onModuleRegister(context: Context) {} @@ -76,9 +79,15 @@ open class ThrioModule { protected open fun onModuleInit(context: Context) {} - protected open var navigatorLogEnabled + protected var navigatorLogEnabled get() = Log.navigatorLogging set(enabled) { Log.navigatorLogging = enabled } + + protected fun startupFlutterEngine(context: Context, entrypoint: String) { + if (!FlutterEngineFactory.isMultiEngineEnabled) { + FlutterEngineFactory.startup(context, entrypoint) + } + } } \ No newline at end of file diff --git a/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/PageRoutes.kt b/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/PageRoutes.kt index 1983a941..8842273f 100644 --- a/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/PageRoutes.kt +++ b/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/PageRoutes.kt @@ -78,7 +78,7 @@ internal object PageRoutes : Application.ActivityLifecycleCallbacks { return holder?.lastRoute() } - fun allRoute(url: String): List { + fun allRoutes(url: String): List { val allRoutes = mutableListOf() for (i in routeHolders.size - 1 downTo 0) { val holder = routeHolders[i] diff --git a/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/RouteReceiveChannel.kt b/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/RouteReceiveChannel.kt index bf3ae4ce..929218d1 100644 --- a/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/RouteReceiveChannel.kt +++ b/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/RouteReceiveChannel.kt @@ -35,7 +35,7 @@ internal class RouteReceiveChannel(private val channel: ThrioChannel, onPopTo() onRemove() onLastIndex() - onGetAllIndex() + onGetAllIndexs() onSetPopDisabled() onHotRestart() onRegisterUrls() @@ -103,8 +103,8 @@ internal class RouteReceiveChannel(private val channel: ThrioChannel, channel.registryMethod("lastIndex") { _, _ -> } } - private fun onGetAllIndex() { - channel.registryMethod("getAllIndex") { _, _ -> } + private fun onGetAllIndexs() { + channel.registryMethod("getAllIndexs") { _, _ -> } } private fun onSetPopDisabled() { diff --git a/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/ThrioNavigator.kt b/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/ThrioNavigator.kt index 50a1fe34..b7fe88e8 100644 --- a/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/ThrioNavigator.kt +++ b/android/src/main/kotlin/com/hellobike/flutter/thrio/navigator/ThrioNavigator.kt @@ -41,7 +41,6 @@ object ThrioNavigator { NAVIGATION_NATIVE_ENTRYPOINT, poppedResult, result) } - @JvmStatic @JvmOverloads fun pop(params: Any? = null, @@ -79,12 +78,10 @@ object ThrioNavigator { NavigationController.Notify.notify(url, index, name, params, result) } - internal fun init(context: Application) { - if (!FlutterEngineFactory.isMultiEngineEnabled) { - FlutterEngineFactory.startup(context) - } - context.registerActivityLifecycleCallbacks(ActivityDelegate) - } -} - + @JvmStatic + @JvmOverloads + fun lastRoute(url: String? = null): PageRoute? = PageRoutes.lastRoute(url) + @JvmStatic + fun allRoutes(url: String): List = PageRoutes.allRoutes(url); +} \ No newline at end of file diff --git a/ios/Classes/Navigator/NavigatorRouteReceiveChannel.m b/ios/Classes/Navigator/NavigatorRouteReceiveChannel.m index 985940ab..cccdf208 100644 --- a/ios/Classes/Navigator/NavigatorRouteReceiveChannel.m +++ b/ios/Classes/Navigator/NavigatorRouteReceiveChannel.m @@ -20,246 +20,289 @@ // IN THE SOFTWARE. #import "NavigatorRouteReceiveChannel.h" +#import "NavigatorFlutterEngineFactory.h" +#import "NavigatorLogger.h" #import "ThrioNavigator+Internal.h" #import "ThrioNavigator+PageBuilders.h" -#import "UINavigationController+Navigator.h" #import "UINavigationController+HotRestart.h" +#import "UINavigationController+Navigator.h" #import "UINavigationController+PopDisabled.h" -#import "NavigatorFlutterEngineFactory.h" -#import "NavigatorLogger.h" NS_ASSUME_NONNULL_BEGIN @interface NavigatorRouteReceiveChannel () -@property (nonatomic, strong) ThrioChannel *channel; +@property(nonatomic, strong) ThrioChannel *channel; -@property (nonatomic, copy, nullable) ThrioIdCallback readyBlock; +@property(nonatomic, copy, nullable) ThrioIdCallback readyBlock; @end @implementation NavigatorRouteReceiveChannel - (instancetype)initWithChannel:(ThrioChannel *)channel { - self = [super init]; - if (self) { - _channel = channel; - [self _onReady]; - [self _onPush]; - [self _onNotify]; - [self _onPop]; - [self _onPopTo]; - [self _onRemove]; - [self _onLastIndex]; - [self _onGetAllIndex]; - [self _onSetPopDisabled]; - [self _onHotRestart]; - [self _onRegisterUrls]; - [self _onUnregisterUrls]; - } - return self; + self = [super init]; + if (self) { + _channel = channel; + [self _onReady]; + [self _onPush]; + [self _onNotify]; + [self _onPop]; + [self _onPopTo]; + [self _onRemove]; + [self _onLastIndex]; + [self _onGetAllIndexs]; + [self _onSetPopDisabled]; + [self _onHotRestart]; + [self _onRegisterUrls]; + [self _onUnregisterUrls]; + } + return self; } - (void)setReadyBlock:(ThrioIdCallback _Nullable)block { - _readyBlock = block; + _readyBlock = block; } #pragma mark - on channel methods - (void)_onReady { - __weak typeof(self) weakself = self; - [_channel registryMethod:@"ready" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - __strong typeof(weakself) strongSelf = weakself; - if (strongSelf.readyBlock) { - NavigatorVerbose(@"on ready: %@", strongSelf.channel.entrypoint); - strongSelf.readyBlock(strongSelf.channel.entrypoint); - strongSelf.readyBlock = nil; - } - }]; + __weak typeof(self) weakself = self; + [_channel registryMethod:@"ready" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + __strong typeof(weakself) strongSelf = weakself; + if (strongSelf.readyBlock) { + NavigatorVerbose(@"on ready: %@", + strongSelf.channel.entrypoint); + strongSelf.readyBlock(strongSelf.channel.entrypoint); + strongSelf.readyBlock = nil; + } + }]; } - (void)_onPush { - __weak typeof(self) weakself = self; - [_channel registryMethod:@"push" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - NSString *url = arguments[@"url"]; - if (url.length < 1) { - if (result) { - result(nil); - } - return; - } - id params = [arguments[@"params"] isKindOfClass:NSNull.class] ? nil : arguments[@"params"]; - BOOL animated = [arguments[@"animated"] boolValue]; - NavigatorVerbose(@"on push: %@", url); - __strong typeof(weakself) strongSelf = weakself; - [ThrioNavigator _pushUrl:url - params:params - animated:animated - fromEntrypoint:strongSelf.channel.entrypoint - result:^(NSNumber *idx) { result(idx); } - poppedResult:nil]; - }]; + __weak typeof(self) weakself = self; + [_channel registryMethod:@"push" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + NSString *url = arguments[@"url"]; + if (url.length < 1) { + if (result) { + result(nil); + } + return; + } + id params = + [arguments[@"params"] isKindOfClass:NSNull.class] + ? nil + : arguments[@"params"]; + BOOL animated = [arguments[@"animated"] boolValue]; + NavigatorVerbose(@"on push: %@", url); + __strong typeof(weakself) strongSelf = weakself; + [ThrioNavigator _pushUrl:url + params:params + animated:animated + fromEntrypoint:strongSelf.channel.entrypoint + result:^(NSNumber *idx) { + result(idx); + } + poppedResult:nil]; + }]; } - (void)_onNotify { - [_channel registryMethod:@"notify" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - NSString *name = arguments[@"name"]; - if (name.length < 1) { - if (result) { - result(@NO); - } - return; - } - NSString *url = arguments[@"url"]; - if (url.length < 1) { - if (result) { - result(@NO); - } - return; - } - NSNumber *index = [arguments[@"index"] isKindOfClass:NSNull.class] ? nil : arguments[@"index"]; - id params = [arguments[@"params"] isKindOfClass:NSNull.class] ? nil : arguments[@"params"]; - [ThrioNavigator _notifyUrl:url - index:index - name:name - params:params result:^(BOOL r) { - if (result) { - result(@(r)); - } - }]; - }]; + [_channel registryMethod:@"notify" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + NSString *name = arguments[@"name"]; + if (name.length < 1) { + if (result) { + result(@NO); + } + return; + } + NSString *url = arguments[@"url"]; + if (url.length < 1) { + if (result) { + result(@NO); + } + return; + } + NSNumber *index = + [arguments[@"index"] isKindOfClass:NSNull.class] + ? nil + : arguments[@"index"]; + id params = + [arguments[@"params"] isKindOfClass:NSNull.class] + ? nil + : arguments[@"params"]; + [ThrioNavigator _notifyUrl:url + index:index + name:name + params:params + result:^(BOOL r) { + if (result) { + result(@(r)); + } + }]; + }]; } - (void)_onPop { - [_channel registryMethod:@"pop" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - id params = [arguments[@"params"] isKindOfClass:NSNull.class] ? nil : arguments[@"params"]; - BOOL animated = [arguments[@"animated"] boolValue]; + [_channel registryMethod:@"pop" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + id params = + [arguments[@"params"] isKindOfClass:NSNull.class] + ? nil + : arguments[@"params"]; + BOOL animated = [arguments[@"animated"] boolValue]; - NavigatorVerbose(@"on pop"); - [ThrioNavigator _popParams:params animated:animated result:^(BOOL r) { - if (result) { - result(@(r)); - } - }]; - }]; + NavigatorVerbose(@"on pop"); + [ThrioNavigator _popParams:params + animated:animated + result:^(BOOL r) { + if (result) { + result(@(r)); + } + }]; + }]; } - (void)_onPopTo { - [_channel registryMethod:@"popTo" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - NSString *url = arguments[@"url"]; - if (url.length < 1) { - if (result) { - result(@NO); - } - return; - } - NSNumber *index = [arguments[@"index"] isKindOfClass:NSNull.class] ? nil : arguments[@"index"]; - BOOL animated = [arguments[@"animated"] boolValue]; + [_channel registryMethod:@"popTo" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + NSString *url = arguments[@"url"]; + if (url.length < 1) { + if (result) { + result(@NO); + } + return; + } + NSNumber *index = + [arguments[@"index"] isKindOfClass:NSNull.class] + ? nil + : arguments[@"index"]; + BOOL animated = [arguments[@"animated"] boolValue]; - NavigatorVerbose(@"on popTo: %@.%@", url, index); + NavigatorVerbose(@"on popTo: %@.%@", url, index); - [ThrioNavigator _popToUrl:url index:index animated:animated result:^(BOOL r) { - if (result) { - result(@(r)); - } - }]; - }]; + [ThrioNavigator _popToUrl:url + index:index + animated:animated + result:^(BOOL r) { + if (result) { + result(@(r)); + } + }]; + }]; } - (void)_onRemove { - [_channel registryMethod:@"remove" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - NSString *url = arguments[@"url"]; - NSNumber *index = [arguments[@"index"] isKindOfClass:NSNull.class] ? nil : arguments[@"index"]; - BOOL animated = [arguments[@"animated"] boolValue]; + [_channel registryMethod:@"remove" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + NSString *url = arguments[@"url"]; + NSNumber *index = + [arguments[@"index"] isKindOfClass:NSNull.class] + ? nil + : arguments[@"index"]; + BOOL animated = [arguments[@"animated"] boolValue]; - NavigatorVerbose(@"on remove: %@.%@", url, index); + NavigatorVerbose(@"on remove: %@.%@", url, index); - [ThrioNavigator _removeUrl:url index:index animated:animated result:^(BOOL r) { - if (result) { - result(@(r)); - } - }]; - }]; + [ThrioNavigator _removeUrl:url + index:index + animated:animated + result:^(BOOL r) { + if (result) { + result(@(r)); + } + }]; + }]; } - (void)_onLastIndex { - [_channel registryMethod:@"lastIndex" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - if (result) { - NSString *url = arguments[@"url"]; - if (url.length < 1) { - result([ThrioNavigator lastIndex]); - } else { - result([ThrioNavigator getLastIndexByUrl:url]); - } - } - }]; + [_channel registryMethod:@"lastRoute" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + if (result) { + NSString *url = arguments[@"url"]; + NavigatorPageRoute *lastRoute = nil; + if (url.length < 1) { + lastRoute = [ThrioNavigator lastRoute]; + } else { + lastRoute = [ThrioNavigator getLastRouteByUrl:url]; + } + result(lastRoute.settings.index); + } + }]; } -- (void)_onGetAllIndex { - [_channel registryMethod:@"allIndex" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - NSString *url = arguments[@"url"]; - if (result) { - result([ThrioNavigator getAllIndexByUrl:url]); - } - }]; +- (void)_onGetAllIndexs { + [_channel registryMethod:@"allIndexs" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + NSString *url = arguments[@"url"]; + NSArray *allRoutes = + [ThrioNavigator getAllRoutesByUrl:url]; + NSMutableArray *allIndexs = [NSMutableArray array]; + for (NavigatorPageRoute *route in allRoutes) { + [allIndexs addObject:route.settings.index]; + } + if (result) { + result(allIndexs); + } + }]; } - (void)_onSetPopDisabled { - [_channel registryMethod:@"setPopDisabled" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - NSString *url = arguments[@"url"]; - NSNumber *index = arguments[@"index"]; - BOOL disabled = [arguments[@"disabled"] boolValue]; - NavigatorVerbose(@"setPopDisabled: %@.%@ %@", url, index, @(disabled)); - [ThrioNavigator _setPopDisabledUrl:url index:index disabled:disabled]; - }]; + [_channel registryMethod:@"setPopDisabled" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + NSString *url = arguments[@"url"]; + NSNumber *index = arguments[@"index"]; + BOOL disabled = [arguments[@"disabled"] boolValue]; + NavigatorVerbose(@"setPopDisabled: %@.%@ %@", url, index, + @(disabled)); + [ThrioNavigator _setPopDisabledUrl:url + index:index + disabled:disabled]; + }]; } - (void)_onHotRestart { - [_channel registryMethod:@"hotRestart" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - if (!ThrioNavigator.isMultiEngineEnabled) { - [ThrioNavigator _hotRestart:^(BOOL r) { - result(@(r)); - }]; - } - }]; + [_channel registryMethod:@"hotRestart" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + if (!ThrioNavigator.isMultiEngineEnabled) { + [ThrioNavigator _hotRestart:^(BOOL r) { + result(@(r)); + }]; + } + }]; } - (void)_onRegisterUrls { - [_channel registryMethod:@"registerUrls" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - NSArray *urls = arguments[@"urls"]; - [NavigatorFlutterEngineFactory.shared registerFlutterUrls:urls]; - }]; + [_channel + registryMethod:@"registerUrls" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + NSArray *urls = arguments[@"urls"]; + [NavigatorFlutterEngineFactory.shared registerFlutterUrls:urls]; + }]; } - (void)_onUnregisterUrls { - [_channel registryMethod:@"unregisterUrls" - handler:^void (NSDictionary *arguments, - ThrioIdCallback _Nullable result) { - NSArray *urls = arguments[@"urls"]; - [NavigatorFlutterEngineFactory.shared unregisterFlutterUrls:urls]; - }]; + [_channel registryMethod:@"unregisterUrls" + handler:^void(NSDictionary *arguments, + ThrioIdCallback _Nullable result) { + NSArray *urls = arguments[@"urls"]; + [NavigatorFlutterEngineFactory.shared + unregisterFlutterUrls:urls]; + }]; } @end diff --git a/ios/Classes/Navigator/ThrioNavigator.h b/ios/Classes/Navigator/ThrioNavigator.h index be28c04b..7a8d501e 100644 --- a/ios/Classes/Navigator/ThrioNavigator.h +++ b/ios/Classes/Navigator/ThrioNavigator.h @@ -23,6 +23,7 @@ #import #import "ThrioTypes.h" #import "ThrioChannel.h" +#import "NavigatorPageRoute.h" NS_ASSUME_NONNULL_BEGIN @@ -364,22 +365,21 @@ NS_ASSUME_NONNULL_BEGIN animated:(BOOL)animated result:(ThrioBoolCallback)result; -#pragma mark - get index methods +#pragma mark - get route methods -/// Returns the index of the page that was last pushed to the navigation +/// Returns the route of the page that was last pushed to the navigation /// stack. /// -+ (NSNumber *_Nullable)lastIndex; ++ (NavigatorPageRoute *_Nullable)lastRoute; -/// Returns the index of the page that was last pushed to the navigation +/// Returns the route of the page that was last pushed to the navigation /// stack. /// -+ (NSNumber *_Nullable)getLastIndexByUrl:(NSString *)url; ++ (NavigatorPageRoute *_Nullable)getLastRouteByUrl:(NSString *)url; -/// Returns all index of the page with `url` in the navigation stack. +/// Returns all route of the page with `url` in the navigation stack. /// -+ (NSArray *)getAllIndexByUrl:(NSString *)url; - ++ (NSArray *)getAllRoutesByUrl:(NSString *)url; #pragma mark - engine methods diff --git a/ios/Classes/Navigator/ThrioNavigator.m b/ios/Classes/Navigator/ThrioNavigator.m index dffd33a0..0ad73fdf 100644 --- a/ios/Classes/Navigator/ThrioNavigator.m +++ b/ios/Classes/Navigator/ThrioNavigator.m @@ -302,45 +302,45 @@ + (void)removeUrl:(NSString *)url #pragma mark - get index methods -+ (NSNumber *_Nullable)lastIndex { ++ (NavigatorPageRoute *_Nullable)lastRoute { NSEnumerator *allNvcs = self.navigationControllers.allObjects.reverseObjectEnumerator; for (UINavigationController *nvc in allNvcs) { - NSNumber *index = [nvc thrio_lastIndex]; - if (index && ![index isEqualToNumber:@0]) { - return index; + NavigatorPageRoute *route = [nvc thrio_lastRoute]; + if (route && ![route.settings.index isEqualToNumber:@0]) { + return route; } } return nil; } -+ (NSNumber *_Nullable)getLastIndexByUrl:(NSString *)url { - NSNumber *lastIndex = nil; ++ (NavigatorPageRoute *_Nullable)getLastRouteByUrl:(NSString *)url { + NavigatorPageRoute *lastRoute = nil; NSEnumerator *allNvcs = self.navigationControllers.allObjects.reverseObjectEnumerator; for (UINavigationController *nvc in allNvcs) { - NSNumber *index = [nvc thrio_getLastIndexByUrl:url]; - if (index && ![index isEqualToNumber:@0]) { - if (!lastIndex) { - lastIndex = index; + NavigatorPageRoute *route = [nvc thrio_getLastRouteByUrl:url]; + if (route && ![route.settings.index isEqualToNumber:@0]) { + if (!lastRoute) { + lastRoute = route; } else { - if (lastIndex.integerValue < index.integerValue) { - lastIndex = index; + if (lastRoute.settings.index.integerValue < route.settings.index.integerValue) { + lastRoute = route; } } } } - return lastIndex; + return lastRoute; } -+ (NSArray *)getAllIndexByUrl:(NSString *)url { - NSMutableArray *allIndexs = [NSMutableArray array]; ++ (NSArray *)getAllRoutesByUrl:(NSString *)url { + NSMutableArray *allRoutes = [NSMutableArray array]; NSEnumerator *allNvcs = self.navigationControllers.allObjects.reverseObjectEnumerator; for (UINavigationController *nvc in allNvcs) { - NSArray *indexs = [nvc thrio_getAllIndexByUrl:url]; - if (indexs.count > 0) { - [allIndexs addObjectsFromArray:indexs]; + NSArray *routes = [nvc thrio_getAllRoutesByUrl:url]; + if (routes.count > 0) { + [allRoutes addObjectsFromArray:routes]; } } - return allIndexs; + return allRoutes; } #pragma mark - engine methods diff --git a/ios/Classes/Navigator/UINavigationController+Navigator.h b/ios/Classes/Navigator/UINavigationController+Navigator.h index 4a95c240..1542898d 100644 --- a/ios/Classes/Navigator/UINavigationController+Navigator.h +++ b/ios/Classes/Navigator/UINavigationController+Navigator.h @@ -61,11 +61,11 @@ NS_ASSUME_NONNULL_BEGIN - (void)thrio_didRemoveUrl:(NSString *)url index:(NSNumber *)index; -- (NSNumber *_Nullable)thrio_lastIndex; +- (NavigatorPageRoute *_Nullable)thrio_lastRoute; -- (NSNumber *_Nullable)thrio_getLastIndexByUrl:(NSString *)url; +- (NavigatorPageRoute *_Nullable)thrio_getLastRouteByUrl:(NSString *)url; -- (NSArray *)thrio_getAllIndexByUrl:(NSString *)url; +- (NSArray *)thrio_getAllRoutesByUrl:(NSString *)url; - (NavigatorPageRoute *)thrio_getLastRouteByEntrypoint:(NSString *)entrypoint; diff --git a/ios/Classes/Navigator/UINavigationController+Navigator.m b/ios/Classes/Navigator/UINavigationController+Navigator.m index 1b74e1f2..01e49bee 100644 --- a/ios/Classes/Navigator/UINavigationController+Navigator.m +++ b/ios/Classes/Navigator/UINavigationController+Navigator.m @@ -87,7 +87,8 @@ - (void)thrio_pushUrl:(NSString *)url __strong typeof(weakself) strongSelf = weakself; if ([strongSelf.topViewController isKindOfClass:NavigatorFlutterViewController.class] && [[(NavigatorFlutterViewController *)strongSelf.topViewController entrypoint] isEqualToString:entrypoint]) { - NSNumber *index = @([ThrioNavigator getLastIndexByUrl:url].integerValue + 1); + NavigatorPageRoute *lastRoute = [ThrioNavigator getLastRouteByUrl:url]; + NSNumber *index = @(lastRoute.settings.index.integerValue + 1); [strongSelf.topViewController thrio_pushUrl:url index:index params:params @@ -100,7 +101,7 @@ - (void)thrio_pushUrl:(NSString *)url if (result) { result(idx); } - } poppedResult:poppedResult]; + } poppedResult:poppedResult]; } else { UIViewController *viewController = [strongSelf thrio_createFlutterViewControllerWithEntrypoint:entrypoint]; [strongSelf thrio_pushViewController:viewController @@ -321,22 +322,22 @@ - (void)thrio_didRemoveUrl:(NSString *)url index:(NSNumber *)index { } } -- (NSNumber *_Nullable)thrio_lastIndex { - return self.topViewController.thrio_lastRoute.settings.index; +- (NavigatorPageRoute *_Nullable)thrio_lastRoute { + return self.topViewController.thrio_lastRoute; } -- (NSNumber *_Nullable)thrio_getLastIndexByUrl:(NSString *)url { +- (NavigatorPageRoute *_Nullable)thrio_getLastRouteByUrl:(NSString *)url { UIViewController *vc = [self getViewControllerByUrl:url index:nil]; - return [vc thrio_getLastIndexByUrl:url]; + return [vc thrio_getLastRouteByUrl:url]; } -- (NSArray *)thrio_getAllIndexByUrl:(NSString *)url { +- (NSArray *)thrio_getAllRoutesByUrl:(NSString *)url { NSArray *vcs = self.viewControllers; - NSMutableArray *indexs = [NSMutableArray array]; + NSMutableArray *routes = [NSMutableArray array]; for (UIViewController *vc in vcs) { - [indexs addObjectsFromArray:[vc thrio_getAllIndexByUrl:url]]; + [routes addObjectsFromArray:[vc thrio_getAllRoutesByUrl:url]]; } - return indexs; + return routes; } - (NavigatorPageRoute *)thrio_getLastRouteByEntrypoint:(NSString *)entrypoint { @@ -621,7 +622,8 @@ - (void)thrio_pushViewController:(UIViewController *)viewController result:(ThrioNumberCallback _Nullable)result poppedResult:(ThrioIdCallback _Nullable)poppedResult { if (viewController) { - NSNumber *index = @([self thrio_getLastIndexByUrl:url].integerValue + 1); + NavigatorPageRoute *lastRoute = [ThrioNavigator getLastRouteByUrl:url]; + NSNumber *index = @(lastRoute.settings.index.integerValue + 1); __weak typeof(self) weakself = self; [viewController thrio_pushUrl:url index:index diff --git a/ios/Classes/Navigator/UIViewController+Navigator.h b/ios/Classes/Navigator/UIViewController+Navigator.h index 34d34438..08eb30df 100644 --- a/ios/Classes/Navigator/UIViewController+Navigator.h +++ b/ios/Classes/Navigator/UIViewController+Navigator.h @@ -70,9 +70,9 @@ NS_ASSUME_NONNULL_BEGIN - (NavigatorPageRoute *_Nullable)thrio_getRouteByUrl:(NSString *)url index:(NSNumber *_Nullable)index; -- (NSNumber *_Nullable)thrio_getLastIndexByUrl:(NSString *)url; +- (NavigatorPageRoute *_Nullable)thrio_getLastRouteByUrl:(NSString *)url; -- (NSArray *)thrio_getAllIndexByUrl:(NSString *)url; +- (NSArray *)thrio_getAllRoutesByUrl:(NSString *)url; @end diff --git a/ios/Classes/Navigator/UIViewController+Navigator.m b/ios/Classes/Navigator/UIViewController+Navigator.m index ea930918..8541ec2d 100644 --- a/ios/Classes/Navigator/UIViewController+Navigator.m +++ b/ios/Classes/Navigator/UIViewController+Navigator.m @@ -341,20 +341,20 @@ - (NavigatorPageRoute *_Nullable)thrio_getRouteByUrl:(NSString *)url return nil; } -- (NSNumber *_Nullable)thrio_getLastIndexByUrl:(NSString *)url { +- (NavigatorPageRoute *_Nullable)thrio_getLastRouteByUrl:(NSString *)url { NavigatorPageRoute *route = [self thrio_getRouteByUrl:url index:nil]; - return route.settings.index; + return route; } -- (NSArray *)thrio_getAllIndexByUrl:(NSString *)url { - NSMutableArray *indexs = [NSMutableArray array]; +- (NSArray *)thrio_getAllRoutesByUrl:(NSString *)url { + NSMutableArray *routes = [NSMutableArray array]; NavigatorPageRoute *first = self.thrio_firstRoute; do { if ([first.settings.url isEqualToString:url]) { - [indexs addObject:first.settings.index]; + [routes addObject:first]; } } while ((first = first.next)); - return [indexs copy]; + return [routes copy]; } #pragma mark - method swizzling diff --git a/lib/src/navigator/navigator_route_send_channel.dart b/lib/src/navigator/navigator_route_send_channel.dart index 49a4c467..19b5cb85 100644 --- a/lib/src/navigator/navigator_route_send_channel.dart +++ b/lib/src/navigator/navigator_route_send_channel.dart @@ -100,8 +100,8 @@ class NavigatorRouteSendChannel { return _channel.invokeMethod('lastIndex', arguments); } - Future> allIndex({@required String url}) => - _channel.invokeListMethod('allIndex', {'url': url}); + Future> allIndexs({@required String url}) => + _channel.invokeListMethod('allIndexs', {'url': url}); Future setPopDisabled({ @required String url, diff --git a/lib/src/navigator/thrio_navigator.dart b/lib/src/navigator/thrio_navigator.dart index ec642ec1..d39c3c90 100644 --- a/lib/src/navigator/thrio_navigator.dart +++ b/lib/src/navigator/thrio_navigator.dart @@ -103,6 +103,6 @@ abstract class ThrioNavigator { /// Returns all index of the page with `url` in the navigation stack. /// - static Future> allIndex({@required String url}) => - ThrioNavigatorImplement.allIndex(url: url); + static Future> allIndexs({@required String url}) => + ThrioNavigatorImplement.allIndexs(url: url); } diff --git a/lib/src/navigator/thrio_navigator_implement.dart b/lib/src/navigator/thrio_navigator_implement.dart index 33918824..82954a3e 100644 --- a/lib/src/navigator/thrio_navigator_implement.dart +++ b/lib/src/navigator/thrio_navigator_implement.dart @@ -198,11 +198,11 @@ class ThrioNavigatorImplement { return _default._sendChannel.lastIndex(url: url); } - static Future> allIndex({@required String url}) { + static Future> allIndexs({@required String url}) { if (_default == null) { throw ThrioException('Must call the `builder` method first'); } - return _default._sendChannel.allIndex(url: url); + return _default._sendChannel.allIndexs(url: url); } static Future setPopDisabled({