vendredi 4 octobre 2019

Restoring NSUserActivity with coordinator pattern + UITabBarController

Who is responsible for restoreUserActivityState in case I use coordinator pattern? In my subclass of UITabBarController I can directly access the view controller but I am not sure if I should wire this using responsible coordinator. With coordinator I need to pass NSUserActivity first to coordinator and later from coordinator to responsible view controller. What is the clean approach?

AppDelegate.m

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<UIUserActivityRestoring>> * __nullable restorableObjects))restorationHandler
{
    if ([userActivity.activityType isEqualToString:CSSearchableItemActionType]) {
        UITabBarController *tabBarController = (__kindof UITabBarController *)self.window.rootViewController;
        restorationHandler(@[tabBarController]);
    }
    return YES;
}

UITabBarController.m (subclass)

// ------------  ------------  ------------  ------------  ------------  ------------
#pragma mark - UIUserActivityRestoring

- (void)restoreUserActivityState:(NSUserActivity *)activity
{
    [self setSelectedViewController:self.mapCoordinator.presenter];
    UIViewController *viewController = [self.mapCoordinator.presenter.childViewControllers firstObject];
    [viewController restoreUserActivityState:activity];
}

Responding View Controller

// ------------  ------------  ------------  ------------  ------------  ------------
#pragma mark - UIUserActivityRestoring

- (void)restoreUserActivityState:(NSUserActivity *)userActivity
{
    NSString *stationObjectIDURI = userActivity.userInfo[CSSearchableItemActivityIdentifier];
    NSURL *URL = [NSURL URLWithString:stationObjectIDURI];
    Station *station = [Station stationWithURIRepresentation:URL inMoc:[self context]];
    if (station) {
        [self.mapView updateRegionForCoord:station.coordinate];
        [self.mapView setRegionChangeLock:YES];
        [self.mapView selectAnnotation:station animated:YES];
    }
}

Coordinator

- (void)start
{
    StationMapVC *stationMapVC = [StationMapVC instantiate];
    stationMapVC.context = self.context;
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Map", nil)
                                                       image:[UIImage
                                                              imageNamed:@"map_marker"]
                                                         tag:0];
    stationMapVC.tabBarItem = item;
    stationMapVC.coordinator = self;
    self.presenter.navigationBarHidden = YES;
    [self.presenter pushViewController:stationMapVC animated:NO];
    self.stationMapVC = stationMapVC;
}

Aucun commentaire:

Enregistrer un commentaire