Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Example + add bottom sheet to 2.0.0 #23

Open
wants to merge 3 commits into
base: release/2.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,39 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
353B5E132B91FCB20085900F /* UIComponents */ = {
isa = PBXGroup;
children = (
E239A3E12916E38300A03EB6 /* ButtonView.swift */,
);
path = UIComponents;
sourceTree = "<group>";
};
353B5E142B91FCD20085900F /* FirstScreen */ = {
isa = PBXGroup;
children = (
E239A3BD2915756C00A03EB6 /* FirstContentView.swift */,
);
path = FirstScreen;
sourceTree = "<group>";
};
353B5E152B91FCE00085900F /* SecondScreen */ = {
isa = PBXGroup;
children = (
E239A3DF2916E14800A03EB6 /* SecondContentView.swift */,
);
path = SecondScreen;
sourceTree = "<group>";
};
353B5E162B91FECC0085900F /* ThirdScreen */ = {
isa = PBXGroup;
children = (
4F45EBC62AFB701C00A92472 /* ThirdContentViewController.swift */,
4F45EBC82AFB8AD000A92472 /* ThirdContentTableViewCell.swift */,
);
path = ThirdScreen;
sourceTree = "<group>";
};
E239A3AF2915756C00A03EB6 = {
isa = PBXGroup;
children = (
Expand All @@ -65,10 +98,11 @@
E239A3BA2915756C00A03EB6 /* Example */ = {
isa = PBXGroup;
children = (
E239A3DE2916DE4700A03EB6 /* Info.plist */,
353B5E132B91FCB20085900F /* UIComponents */,
E239A3CF291578FD00A03EB6 /* Screens */,
E239A3CE291578F100A03EB6 /* App */,
E239A3BF2915756D00A03EB6 /* Assets.xcassets */,
E239A3DE2916DE4700A03EB6 /* Info.plist */,
);
path = Example;
sourceTree = "<group>";
Expand All @@ -92,11 +126,9 @@
E239A3CF291578FD00A03EB6 /* Screens */ = {
isa = PBXGroup;
children = (
E239A3BD2915756C00A03EB6 /* FirstContentView.swift */,
E239A3DF2916E14800A03EB6 /* SecondContentView.swift */,
E239A3E12916E38300A03EB6 /* ButtonView.swift */,
4F45EBC62AFB701C00A92472 /* ThirdContentViewController.swift */,
4F45EBC82AFB8AD000A92472 /* ThirdContentTableViewCell.swift */,
353B5E142B91FCD20085900F /* FirstScreen */,
353B5E152B91FCE00085900F /* SecondScreen */,
353B5E162B91FECC0085900F /* ThirdScreen */,
);
path = Screens;
sourceTree = "<group>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ struct FirstContentView: View {
title: "Open fullscreen second screen"
)
.padding(.top, 40)
ButtonView(
action: { [weak navigation] in
navigation?.perform(action: .push(.second, as: .bottomSheet(.auto, completion: nil)))
},
title: "Open second as bottom sheet"
)
}
.padding()
.navigationBarBackButtonHidden()
Expand All @@ -25,12 +31,17 @@ struct FirstContentView: View {

struct FirstContentModule: Module {
func assembly(using coordinator: Coordinator) -> any NavigationScreen {
return HostingController(rootView: FirstContentView(navigation: coordinator))
HostingController(
title: "First",
isNeedBackButton: false,
coordinator: coordinator,
content: FirstContentView(navigation: coordinator)
)
}
}

extension Module where Self == FirstContentModule {
static var first: Self {
return FirstContentModule()
FirstContentModule()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ struct SecondContentView: View {
.padding(.top, 40)
ButtonView(
action: { [weak navigation] in
// Bottom Sheet
navigation?.perform(action: .push(.third, as: .bottomSheet(.auto, completion: nil)))
},
title: "Present bottom sheet - auto"
)
ButtonView(
action: { [weak navigation] in
// Bottom Sheet with fixed height
navigation?.perform(action: .push(.third, as: .bottomSheet(.fixed(120), completion: nil)))
},
title: "Present bottom sheet - fixed height"
)
ButtonView(
action: { [weak navigation] in
// Bottom Sheet with full screen
navigation?.perform(action: .push(.third, as: .bottomSheet(.fullScreen, completion: nil)))
},
title: "Present bottom sheet - full screen"
)
ButtonView(
action: { [weak navigation] in
// Bottom Sheet with half screen
navigation?.perform(action: .push(.third, as: .bottomSheet(.halfScreen, completion: nil)))
},
title: "Present bottom sheet - half screen"
)
Expand All @@ -49,12 +49,16 @@ struct SecondContentView: View {

struct SecondContentModule: Module {
func assembly(using coordinator: Coordinator) -> any NavigationScreen {
return HostingController(rootView: SecondContentView(navigation: coordinator))
HostingController(
title: "Second",
coordinator: coordinator,
content: SecondContentView(navigation: coordinator)
)
}
}

extension Module where Self == SecondContentModule {
static var second: Self {
return SecondContentModule()
SecondContentModule()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class ThirdContentTableViewCell: UITableViewCell {
}

func configureWith(image: UIImage, title: String) {
imageView?.image = image
iconImageView.image = image
label.text = title
}

Expand All @@ -43,16 +43,14 @@ final class ThirdContentTableViewCell: UITableViewCell {

NSLayoutConstraint.activate([
iconImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
iconImageView.topAnchor.constraint(equalTo: contentView.topAnchor),
iconImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
iconImageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
iconImageView.heightAnchor.constraint(equalToConstant: 32),
iconImageView.widthAnchor.constraint(equalToConstant: 32),

label.leadingAnchor.constraint(equalTo: iconImageView.trailingAnchor, constant: 8),
label.topAnchor.constraint(equalTo: contentView.topAnchor),
label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
label.heightAnchor.constraint(equalToConstant: 64)
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ final class ThirdContentViewController: ViewController {
return tableView
}()

init() {
super.init()
init(coordinator: Coordinator?) {
super.init(title: "Third", coordinator: coordinator)
}

required init?(coder: NSCoder) { nil }
Expand Down Expand Up @@ -100,12 +100,12 @@ extension ThirdContentViewController: UITableViewDelegate {

struct ThirdContent: Module {
func assembly(using coordinator: Coordinator) -> any NavigationScreen {
return ThirdContentViewController()
ThirdContentViewController(coordinator: coordinator)
}
}

extension Module where Self == ThirdContent {
static var third: Self {
return ThirdContent()
ThirdContent()
}
}
4 changes: 4 additions & 0 deletions Sources/Navidux/Core/Coordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import Foundation

public final class Coordinator {
let bottomSheetTransitioningDelegate = BSTransitioningDelegate()

public var navigationController: NavigationController
public var dependencyContainer: DependencyContainer
public var store: NavigationStore

public init(navigationController: NavigationController? = nil) {
self.navigationController = navigationController ?? BaseNavigationController()
self.dependencyContainer = DependencyContainer()
self.store = NavigationStore()
}

public func perform(action: NavigationAction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ public struct PopNavigationAction: NavigationAction {
let screenClass,
let screen = coordinator.navigationController.screens.last(where: { $0.isKind(of: screenClass) })
else {
coordinator.navigationController.popViewController(animated: self.animated)
if coordinator.store.hasOverlay {
coordinator.store.hasOverlay = false
coordinator.navigationController.dismiss(animated: self.animated, completion: nil)
} else {
coordinator.navigationController.popViewController(animated: self.animated)
}
return
}

Expand All @@ -31,14 +36,14 @@ public struct PopNavigationAction: NavigationAction {

public extension NavigationAction where Self == PopNavigationAction {
static var pop: Self {
return PopNavigationAction(screenClass: nil, animated: true)
PopNavigationAction(screenClass: nil, animated: true)
}

static func pop(animated: Bool) -> Self {
return PopNavigationAction(screenClass: nil, animated: animated)
PopNavigationAction(screenClass: nil, animated: animated)
}

static func popTo(_ screenClass: UIViewController.Type, animated: Bool = true) -> Self {
return PopNavigationAction(screenClass: screenClass, animated: animated)
PopNavigationAction(screenClass: screenClass, animated: animated)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public struct PushNavigationAction: NavigationAction {
public enum PresentationStyle {
case fullScreen
case modal(completion: (() -> Void)?)
case bottomSheet(BSSize, completion: (() -> Void)?)
}

private let module: Module
Expand All @@ -29,13 +30,21 @@ public struct PushNavigationAction: NavigationAction {
case .fullScreen:
coordinator.navigationController.pushViewController(screen, animated: self.animated)
case .modal(let completion):
coordinator.store.hasOverlay = true
coordinator.navigationController.present(screen, animated: self.animated, completion: completion)
case .bottomSheet(let size, let completion):
BSBuilder.buildBottomSheet(
coordinator: coordinator,
screen: screen,
size: size
)
coordinator.navigationController.present(screen, animated: self.animated, completion: completion)
}
}
}

public extension NavigationAction where Self == PushNavigationAction {
static func push(_ module: Module, as presentationStyle: Self.PresentationStyle = .fullScreen, animated: Bool = true) -> Self {
return PushNavigationAction(module: module, presentationStyle: presentationStyle, animated: animated)
PushNavigationAction(module: module, presentationStyle: presentationStyle, animated: animated)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public struct RestructNavigationAction: NavigationAction {

public extension NavigationAction where Self == RestructNavigationAction {
static func restruct(with modules: [Module], animation: Self.AnimationType) -> Self {
return RestructNavigationAction(modules: modules, animationType: animation)
RestructNavigationAction(modules: modules, animationType: animation)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// BSBuilder.swift
//
//
// Created by Петр Тартынских on 05.03.2024.
//

import Foundation

class BSBuilder {

static func buildBottomSheet(
coordinator: Coordinator,
screen: any NavigationScreen,
size: BSSize
) {
switch size {
case .auto:
coordinator.bottomSheetTransitioningDelegate.sheetSize = .auto
screen.transitioningDelegate = coordinator.bottomSheetTransitioningDelegate
screen.modalPresentationStyle = .custom
case .fixed(let height):
coordinator.bottomSheetTransitioningDelegate.sheetSize = .fixed(height)
screen.transitioningDelegate = coordinator.bottomSheetTransitioningDelegate
screen.modalPresentationStyle = .custom
case .halfScreen:
coordinator.bottomSheetTransitioningDelegate.sheetSize = .halfScreen
screen.transitioningDelegate = coordinator.bottomSheetTransitioningDelegate
screen.modalPresentationStyle = .custom
case .fullScreen:
screen.modalPresentationStyle = .formSheet
}
coordinator.store.hasOverlay = true
}
}
Comment on lines +10 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Может тогда лучше будем хранить делегат тут, чтобы не прокидывать координатор сюда, а то как-то слишком неочевидно получается. Давай еще переименуем функцию просто в build(screen:size:), а то c окончанием bottomSheet будто получается тавтология. Плюсом сюда думаю будет правильней возвращать уже сконфигурированный экран, чтобы не нарушать семантику, т.к. мы используем слово build

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import UIKit

final class BSPresentationController: UIPresentationController {

var sheetSize: BSSize = .auto

private lazy var dimmView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.black.withAlphaComponent(0.75)
Expand Down Expand Up @@ -52,6 +54,29 @@ final class BSPresentationController: UIPresentationController {
dimmView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
]

switch sheetSize {
case .fixed(let height):
constraints.append(
presentedView.heightAnchor.constraint(equalToConstant: height)
)
case .halfScreen:
constraints.append(
presentedView.heightAnchor.constraint(
lessThanOrEqualTo: containerView.heightAnchor,
constant: -(containerView.bounds.height / 2)
)
)
case .auto:
constraints.append(
presentedView.heightAnchor.constraint(
lessThanOrEqualTo: containerView.heightAnchor,
constant: -containerView.safeAreaInsets.top
)
)
case .fullScreen:
break
}

NSLayoutConstraint.activate(constraints)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// BSSize.swift
//
//
// Created by Петр Тартынских on 05.03.2024.
//

import Foundation

public enum BSSize {
case fixed(CGFloat)
case halfScreen
case fullScreen
case auto
}
Loading