Skip to content

Commit

Permalink
Merge pull request #37 in MA/avito-ios-navigation from feature/AI-735…
Browse files Browse the repository at this point in the history
…8-marshrout-asserts to master

* commit 'f02213b113505107b32a8be989e6ddf5b99d8eed':
  AI-7358: Move customization docs to another place
  AI-7358: Refactored asserts using ssertion failure
  AI-7358: Implement new assertion method in demo assertion plugn
  AI-7358: Add assertionFailure method to assertion plugin
  AI-7358: Add plugins documantation
  AI-7358: Fix assertionManager usage syntax
  AI-7358: Update pods xcodeproj
  AI-7358: Inject demoAssertionPlugin in navigationDemo project
  AI-7358: Add demo assertion plugin
  AI-7358: Refactor print manager
  AI-7358: Change asserts to marshrouteAsserts
  AI-7358: Add marshrouteAssert and assertionManager
  • Loading branch information
Vladimir Ignatov committed Oct 5, 2017
2 parents cdcc0ff + f02213b commit 1f6c383
Show file tree
Hide file tree
Showing 15 changed files with 1,132 additions and 922 deletions.
4 changes: 4 additions & 0 deletions Example/NavigationDemo.xcodeproj/project.pbxproj
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
0CBCF8311F865F5A00E2F0E6 /* PrintPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CBCF8301F865F5A00E2F0E6 /* PrintPlugin.swift */; };
0CBCF83A1F8679E500E2F0E6 /* AssertionPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CBCF8391F8679E500E2F0E6 /* AssertionPlugin.swift */; };
1313F20C96058FEF50F595A6 /* Pods_NavigationDemo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F85A938D3DCD5CE101DCA321 /* Pods_NavigationDemo.framework */; };
BF69B5A3AE24511680DB2ED1 /* Pods_MarshrouteTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FE239487B05BB3C04361F501 /* Pods_MarshrouteTests.framework */; };
CC0B1D441CC20687006707B6 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC0B1D431CC20687006707B6 /* AppDelegate.swift */; };
Expand Down Expand Up @@ -224,6 +225,7 @@

/* Begin PBXFileReference section */
0CBCF8301F865F5A00E2F0E6 /* PrintPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrintPlugin.swift; sourceTree = "<group>"; };
0CBCF8391F8679E500E2F0E6 /* AssertionPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssertionPlugin.swift; sourceTree = "<group>"; };
616970D95027090C37F0EAAE /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6C1CE8A4F513C6013BD4CCAC /* Pods-MarshrouteTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MarshrouteTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-MarshrouteTests/Pods-MarshrouteTests.release.xcconfig"; sourceTree = "<group>"; };
77767704E4AC1FE96EDB2B9E /* Pods-MarshrouteTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MarshrouteTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-MarshrouteTests/Pods-MarshrouteTests.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -463,6 +465,7 @@
isa = PBXGroup;
children = (
0CBCF8301F865F5A00E2F0E6 /* PrintPlugin.swift */,
0CBCF8391F8679E500E2F0E6 /* AssertionPlugin.swift */,
);
path = Plugins;
sourceTree = "<group>";
Expand Down Expand Up @@ -1826,6 +1829,7 @@
CC8406851CD6693500644CA1 /* ShelfInteractor.swift in Sources */,
CCA7C8EC1CD9049C00B7ADBD /* CategoriesRouterIpad.swift in Sources */,
CC8406911CD6713800644CA1 /* RootModulesProviderImpl.swift in Sources */,
0CBCF83A1F8679E500E2F0E6 /* AssertionPlugin.swift in Sources */,
CC0B1E1A1CC2DF84006707B6 /* SearchResultsRouter.swift in Sources */,
CC0B1D811CC20999006707B6 /* CategoriesAssemblyImpl.swift in Sources */,
CC84068A1CD6693500644CA1 /* ShelfView.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion Example/NavigationDemo/AppDelegate.swift
Expand Up @@ -43,7 +43,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
// Init `Marshroute` stack
MarshroutePrintManager.setupPrintPlugin(DemoPrintPlugin())
MarshroutePrintManager.setUpPrintPlugin(DemoPrintPlugin())
MarshrouteAssertionManager.setUpAssertionPlugin(DemoAssertionPlugin())
let marshrouteSetupService = MarshrouteSetupServiceImpl()

let applicationModuleSeed = ApplicationModuleSeedProvider().applicationModuleSeed(
Expand Down
22 changes: 22 additions & 0 deletions Example/NavigationDemo/Common/Plugins/AssertionPlugin.swift
@@ -0,0 +1,22 @@
import Marshroute

final class DemoAssertionPlugin: MarshrouteAssertionPlugin {
private let demoPrefix = "NavigatonDemo "

func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String,
file: StaticString,
line: UInt) {
let demoMessage = demoPrefix + message()
Swift.assert(condition(), demoMessage, file: file, line: line)
}

func assertionFailure(
_ message: @autoclosure () -> String,
file: StaticString,
line: UInt) {
let demoMessage = demoPrefix + message()
Swift.assertionFailure(demoMessage, file: file, line: line)
}
}
1,888 changes: 993 additions & 895 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Marshroute/Sources/Routers/Routers/DetailRouter.swift
Expand Up @@ -34,7 +34,7 @@ extension DetailRouter where Self: DetailRouterTransitionable, Self: RouterIdent
animator: ResetNavigationTransitionsAnimator)
{
guard let animatingDetailTransitionsHandler = detailTransitionsHandlerBox.unboxAnimatingTransitionsHandler()
else { assert(false); return }
else { marshrouteAssertionFailure(); return }

let routerSeed = RouterSeed(
transitionsHandlerBox: detailTransitionsHandlerBox,
Expand Down
4 changes: 2 additions & 2 deletions Marshroute/Sources/Routers/Routers/MasterRouter.swift
Expand Up @@ -48,7 +48,7 @@ extension MasterRouter where Self: MasterRouterTransitionable, Self: DetailRoute
animator: ResetNavigationTransitionsAnimator)
{
guard let animatingMasterTransitionsHandler = masterTransitionsHandlerBox.unboxAnimatingTransitionsHandler()
else { assert(false); return }
else { marshrouteAssertionFailure(); return }

let masterDetailRouterSeed = MasterDetailRouterSeed(
masterTransitionsHandlerBox: masterTransitionsHandlerBox,
Expand Down Expand Up @@ -126,7 +126,7 @@ extension MasterRouter where Self: MasterRouterTransitionable, Self: DetailRoute
animator: ResetNavigationTransitionsAnimator)
{
guard let animatingDetailTransitionsHandler = detailTransitionsHandlerBox.unboxAnimatingTransitionsHandler()
else { assert(false); return }
else { marshrouteAssertionFailure(); return }

let detailRouterSeed = RouterSeed(
transitionsHandlerBox: detailTransitionsHandlerBox,
Expand Down
Expand Up @@ -38,7 +38,7 @@ public struct CompletedTransitionContext {
sourceTransitionsHandler: AnimatingTransitionsHandler)
{
guard !context.needsAnimatingTargetTransitionHandler else {
assert(false, "заполните `targetTransitionsHandlerBox` анимирующим обработчиком переходов раньше - до выполнения самого перехода")
marshrouteAssertionFailure("заполните `targetTransitionsHandlerBox` анимирующим обработчиком переходов раньше - до выполнения самого перехода")
return nil
}

Expand Down
Expand Up @@ -57,12 +57,12 @@ public extension PresentationTransitionContext {
animator: ModalTransitionsAnimator,
transitionId: TransitionId)
{
assert(
marshrouteAssert(
!(targetViewController is UISplitViewController) && !(targetViewController is UITabBarController),
"use presentingModalMasterDetailViewController:targetTransitionsHandler:animator:transitionId:"
)

assert(
marshrouteAssert(
!(targetViewController is UINavigationController),
"use presentingModalNavigationController:targetTransitionsHandler:animator:transitionId:"
)
Expand Down Expand Up @@ -117,7 +117,7 @@ public extension PresentationTransitionContext {
animator: ModalNavigationTransitionsAnimator,
transitionId: TransitionId)
{
assert(
marshrouteAssert(
!(targetViewController is UISplitViewController) && !(targetViewController is UITabBarController),
"use presentingModalMasterDetailViewController:targetTransitionsHandler:animator:transitionId:"
)
Expand Down Expand Up @@ -297,7 +297,7 @@ extension PresentationTransitionContext {
/// Проставляем непроставленного ранее обработчика переходов
mutating func setAnimatingTargetTransitionsHandler(_ transitionsHandler: AnimatingTransitionsHandler)
{
assert(needsAnimatingTargetTransitionHandler)
marshrouteAssert(needsAnimatingTargetTransitionHandler)
targetTransitionsHandlerBox = .init(animatingTransitionsHandler: transitionsHandler)
}
}
Expand Up @@ -379,13 +379,22 @@ private extension TransitionsCoordinator where
forTransitionsHandler animatingTransitionsHandler: AnimatingTransitionsHandler?)
{
guard let animatingTransitionsHandler = animatingTransitionsHandler
else { assert(false, "к этому моменту должен быть найден анимирующий обработчик"); return }
else {
marshrouteAssertionFailure("к этому моменту должен быть найден анимирующий обработчик")
return
}

guard let stackClient = stackClientProvider.stackClient(forTransitionsHandler: animatingTransitionsHandler)
else { assert(false, "сначала нужно было делать resetWithTransition, а не performTransition"); return }
else {
marshrouteAssertionFailure("сначала нужно было делать resetWithTransition, а не performTransition")
return
}

guard let lastTransition = stackClient.lastTransitionForTransitionsHandler(animatingTransitionsHandler)
else { assert(false, "сначала нужно было делать resetWithTransition, а не performTransition"); return }
else {
marshrouteAssertionFailure("сначала нужно было делать resetWithTransition, а не performTransition")
return
}

// спрашиваем делегата о разрешении выполнения анимаций `Resetting` перехода,
// если переход помечен пользовательским идентификатором
Expand Down Expand Up @@ -548,14 +557,14 @@ private extension TransitionsCoordinator where
transitionId: context.transitionId,
forTransitionsHandler: animatingTransitionsHandler
) else {
assert(false, "нужно вызвать `resetWithTransition:` вместо того, чтобы отменять самый первый переход")
marshrouteAssertionFailure("нужно вызвать `resetWithTransition:` вместо того, чтобы отменять самый первый переход")
return
}

guard let presentationAnimationLaunchingContextBox
= context.sourceAnimationLaunchingContextBox.unboxPresentationAnimationLaunchingContextBox() else {
assert(false, "невозможно сделать обратный переход для `reset`-перехода. только для `presentation`-перехода")
return
marshrouteAssertionFailure("невозможно сделать обратный переход для `reset`-перехода. только для `presentation`-перехода")
return
}

// готовим параметры запуска анимации обратного перехода
Expand Down Expand Up @@ -668,7 +677,7 @@ private extension TransitionsCoordinator where
// если дочерний - анимирующий
if let chainedAnimatingTransitionsHandler = chainedTransitionsHandlerBox!.unboxAnimatingTransitionsHandler() {
guard let chainedStackClient = stackClientProvider.stackClient(forTransitionsHandler: chainedAnimatingTransitionsHandler)
else { assert(false); break }
else { marshrouteAssertionFailure(); break }

// если какой-то дочерний анимирующий обработчик выполнял переход с переданным id, возвращаем его
if (chainedStackClient.transitionWith(transitionId: transitionId, forTransitionsHandler: chainedAnimatingTransitionsHandler) != nil) {
Expand Down Expand Up @@ -696,7 +705,10 @@ private extension TransitionsCoordinator where

// иначе обрываем цикл
chainedTransitionsHandlerBox = nil
} else { assert(false, "добавились новые виды обработчиков. нужно дописать код"); break }
} else {
marshrouteAssertionFailure("добавились новые виды обработчиков. нужно дописать код")
break
}
}
}
return nil
Expand Down Expand Up @@ -1015,7 +1027,7 @@ private extension TransitionsCoordinator where

// ищем последний переход, выполненный анимирующим обработчиком
guard stackClient.lastTransitionForTransitionsHandler(animatingTransitionsHandler) != nil else {
assert(false, "нужно было вызывать resetWithTransition(context:). а не performTransition(context:)")
marshrouteAssertionFailure("нужно было вызывать resetWithTransition(context:). а не performTransition(context:)")
return
}

Expand All @@ -1025,7 +1037,7 @@ private extension TransitionsCoordinator where
)

guard completedTransitionContext != nil
else { assert(false); return }
else { marshrouteAssertionFailure(); return }

// создаем новую запись о переходе
_ = stackClient.appendTransition(
Expand Down Expand Up @@ -1058,7 +1070,7 @@ private extension TransitionsCoordinator where
)

guard completedTransitionContext != nil
else { assert(false); return }
else { marshrouteAssertionFailure(); return }

// создаем новую запись о переходе
_ = stackClient.appendTransition(
Expand Down
Expand Up @@ -13,7 +13,7 @@ extension TransitionContextsStackClientProviderImpl: TransitionContextsStackClie

let matchingHistoryItems = historyItems.filter { $0.transitionsHandler === transitionsHandler }

assert(matchingHistoryItems.count <= 1)
marshrouteAssert(matchingHistoryItems.count <= 1)

return matchingHistoryItems.first?.stackClient
}
Expand Down
@@ -0,0 +1,14 @@
func marshrouteAssert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = "",
file: StaticString = #file,
line: UInt = #line) {
MarshrouteAssertionManager.assert(condition, message, file: file, line: line)
}

func marshrouteAssertionFailure(
_ message: @autoclosure () -> String = "",
file: StaticString = #file,
line: UInt = #line) {
MarshrouteAssertionManager.assertionFailure(message, file: file, line: line)
}
@@ -0,0 +1,22 @@
public final class MarshrouteAssertionManager {
private static var instance: MarshrouteAssertionPlugin = DefaultMarshrouteAssertionPlugin()

public static func setUpAssertionPlugin(_ plugin: MarshrouteAssertionPlugin) {
instance = plugin
}

static func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String,
file: StaticString,
line: UInt) {
instance.assert(condition, message, file: file, line: line)
}

static func assertionFailure(
_ message: @autoclosure () -> String,
file: StaticString,
line: UInt) {
instance.assertionFailure(message, file: file, line: line)
}
}
@@ -0,0 +1,29 @@
public protocol MarshrouteAssertionPlugin {
func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String,
file: StaticString,
line: UInt)

func assertionFailure(
_ message: @autoclosure () -> String,
file: StaticString,
line: UInt)
}

final class DefaultMarshrouteAssertionPlugin: MarshrouteAssertionPlugin {
func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String,
file: StaticString,
line: UInt) {
Swift.assert(condition, message, file: file, line: line)
}

func assertionFailure(
_ message: @autoclosure () -> String,
file: StaticString,
line: UInt) {
Swift.assertionFailure(message, file: file, line: line)
}
}
@@ -1,15 +1,15 @@
public final class MarshroutePrintManager {
private static var sharedInstance: MarshroutePrintPlugin = DefaultMarshroutePrintPlugin()
private static var instance: MarshroutePrintPlugin = DefaultMarshroutePrintPlugin()

public static func setupPrintPlugin(_ plugin: MarshroutePrintPlugin) {
sharedInstance = plugin
public static func setUpPrintPlugin(_ plugin: MarshroutePrintPlugin) {
instance = plugin
}

static func print(_ item: Any, separator: String, terminator: String) {
sharedInstance.print(item, separator: separator, terminator: terminator)
instance.print(item, separator: separator, terminator: terminator)
}

static func debugPrint(_ item: Any, separator: String, terminator: String) {
sharedInstance.print(item, separator: separator, terminator: terminator)
instance.print(item, separator: separator, terminator: terminator)
}
}
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -13,6 +13,7 @@
* [Installation](#installation)
* [Cocoapods](#cocoapods)
* [Carthage](#carthage)
* [Customization](#plugin-customization)
* [Licence](#licence)
* [Objective-c support](#objective-c-support)
* [Useful links](#useful-links)
Expand Down Expand Up @@ -89,7 +90,6 @@ The key line here is

So the syntax remains clean and it is super easy to switch back to the original animation style.


## <a name="3d-touch-support"/> 3d touch support

### <a name="peek-and-pop-utility"/> PeekAndPopUtility
Expand Down Expand Up @@ -226,6 +226,14 @@ github "avito-tech/Marshroute" ~> 0.4.2

Then run `carthage update --platform iOS` command. For details of the installation and usage of Carthage, visit [its repo website](https://github.com/Carthage/Carthage).

## <a name="plugin-customization"/> Customization

You can provide custom print and assert realization using `MarshroutePrintPlugin` and `MarshrouteAssertionPlugin`.
This is as easy as:
```
MarshroutePrintManager.setUpPrintPlugin(YourPrintPlugin())
MarshrouteAssertionManager.setUpAssertionPlugin(YourAssertionPlugin())
```

## <a name="licence"/> Licence
MIT
Expand Down

0 comments on commit 1f6c383

Please sign in to comment.