 extension SplitViewCoordinator: UISplitViewControllerDelegate {
     
     func splitViewController(_ splitViewController: UISplitViewController, separateSecondaryFrom primaryViewController: UIViewController) -> UIViewController? {
-                
-        // Return the top view controller of the master navigation controller, if it is a navigation controller itself.
-        if let topViewController = masterNavigationController.topViewController as? UINavigationController {
-            // Keep the detail scene
-            return topViewController
+        
+        if let detailViewController = self.masterPresentable?.secondViewControllerWhenSeparatedFromPrimary() {
+            return detailViewController
         }
 
         // Else return the default empty details view controller from the storyboard.

        }

        // Release the current selected item (room/contact/group...).
        masterTabBarController.releaseSelectedItem()
    }
    
    func popToHome(animated: Bool, completion: (() -> Void)?) {
        if let secondNavController = self.secondaryNavigationController() {
            secondNavController.popToRootViewController(animated: animated)
        }

        // Force back to the main screen if this is not the one that is displayed
        if masterTabBarController != masterNavigationController.visibleViewController {
            // Listen to the masterNavigationController changes
            // We need to be sure that masterTabBarController is back to the screen
            popToHomeViewControllerCompletion = completion
            masterNavigationController.delegate = self

            masterNavigationController.popToViewController(masterTabBarController, animated: animated)
        } else {
            // Select the Home tab
            masterTabBarController.selectedIndex = Int(TABBAR_HOME_INDEX)
            completion?()
        }
    }
    
    // MARK: - Private methods
    
    private func createPlaceholderDetailsViewController() -> UIViewController {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let emptyDetailsViewController = storyboard.instantiateViewController(withIdentifier: "EmptyDetailsViewControllerStoryboardId")
        return emptyDetailsViewController
    }
    
    func secondaryNavigationController() -> UINavigationController? {
        guard splitViewController.viewControllers.count == 2, let secondViewController = splitViewController.viewControllers.last as? UINavigationController else {
            return nil
        }
        return secondViewController
    }
}


    }
    
    func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {
        return self.masterPresentable?.collapseDetailViewController ?? false
    }
}

/// MARK: - UINavigationControllerDelegate
extension SplitViewCoordinator: TabBarCoordinatorDelegate {
    
}

/// MARK: - SplitViewMasterPresentableDelegate
extension SplitViewCoordinator: SplitViewMasterPresentableDelegate {
    func splitViewMasterPresentable(_ presentable: Presentable, wantsToDisplay detailPresentable: Presentable) {
        guard let detailNavigationController = self.detailNavigationController else {
            return
        }
        
        detailNavigationController.viewControllers = [detailPresentable.toPresentable()]
        
        self.detailNavigationController?.viewControllers = [detailPresentable.toPresentable()]
        self.splitViewController.showDetailViewController(detailNavigationController, sender: nil)
    }
}
