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

Added Dialogs #1021

Merged
merged 8 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
20 changes: 20 additions & 0 deletions Material.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@
96E3C39A1D3A1CC20086A024 /* ErrorTextField.swift in Headers */ = {isa = PBXBuildFile; fileRef = 961F18E71CD93E3E008927C5 /* ErrorTextField.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96E3C39C1D3A1CC20086A024 /* Offset.swift in Headers */ = {isa = PBXBuildFile; fileRef = 968C99461D377849000074FF /* Offset.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96F1A5531F24F17A001D8CAF /* TabsController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96E09DC71F2287E50000B121 /* TabsController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
9D13671A2006A8170004DE2D /* DialogView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D1367192006A8170004DE2D /* DialogView.swift */; };
9D13671C2006A8D80004DE2D /* DialogController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D13671B2006A8D80004DE2D /* DialogController.swift */; };
9D13671E2006A9450004DE2D /* DialogBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D13671D2006A9450004DE2D /* DialogBuilder.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -285,6 +288,9 @@
96E09DC71F2287E50000B121 /* TabsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TabsController.swift; sourceTree = "<group>"; };
96E3C3931D397AE90086A024 /* Material+UIView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Material+UIView.swift"; sourceTree = "<group>"; };
96F1DC871D654FDF0025F925 /* Material+CALayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Material+CALayer.swift"; sourceTree = "<group>"; };
9D1367192006A8170004DE2D /* DialogView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DialogView.swift; sourceTree = "<group>"; };
9D13671B2006A8D80004DE2D /* DialogController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DialogController.swift; sourceTree = "<group>"; };
9D13671D2006A9450004DE2D /* DialogBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DialogBuilder.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXGroup section */
Expand Down Expand Up @@ -523,6 +529,7 @@
96BCB8001CB40F0300C806FE /* Color */,
96328B9A1E05C135009A4C90 /* Data */,
96BCB80B1CB410CC00C806FE /* Device */,
9D1367172006A5730004DE2D /* Dialogs */,
96230AB61D6A51FD00AF47DC /* Divider */,
96BCB80A1CB410A100C806FE /* Extension */,
963FBF021D6696D0008F8512 /* FABMenu */,
Expand Down Expand Up @@ -729,6 +736,16 @@
name = Animation;
sourceTree = "<group>";
};
9D1367172006A5730004DE2D /* Dialogs */ = {
isa = PBXGroup;
children = (
9D1367192006A8170004DE2D /* DialogView.swift */,
9D13671B2006A8D80004DE2D /* DialogController.swift */,
9D13671D2006A9450004DE2D /* DialogBuilder.swift */,
);
name = Dialogs;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -936,12 +953,14 @@
965E81171DD4D5C800D61E4B /* TransitionController.swift in Sources */,
965E81181DD4D5C800D61E4B /* Snackbar.swift in Sources */,
965E81191DD4D5C800D61E4B /* SnackbarController.swift in Sources */,
9D13671C2006A8D80004DE2D /* DialogController.swift in Sources */,
9618006D1F4D384200CD77A1 /* Material+UIViewController.swift in Sources */,
965E811A1DD4D5C800D61E4B /* StatusBarController.swift in Sources */,
965E811B1DD4D5C800D61E4B /* Switch.swift in Sources */,
965E811C1DD4D5C800D61E4B /* TabBar.swift in Sources */,
965E811D1DD4D5C800D61E4B /* TableViewCell.swift in Sources */,
965E811E1DD4D5C800D61E4B /* TextField.swift in Sources */,
9D13671E2006A9450004DE2D /* DialogBuilder.swift in Sources */,
965E811F1DD4D5C800D61E4B /* ErrorTextField.swift in Sources */,
965E81211DD4D5C800D61E4B /* TextStorage.swift in Sources */,
965E81221DD4D5C800D61E4B /* TextView.swift in Sources */,
Expand Down Expand Up @@ -982,6 +1001,7 @@
961E6BDF1DDA2A95004E6C93 /* Application.swift in Sources */,
965E80D71DD4C50600D61E4B /* Icon.swift in Sources */,
965E80FC1DD4D59500D61E4B /* SearchBarController.swift in Sources */,
9D13671A2006A8170004DE2D /* DialogView.swift in Sources */,
965E80D81DD4C50600D61E4B /* Layer.swift in Sources */,
965E80D91DD4C50600D61E4B /* Layout.swift in Sources */,
965E80DA1DD4C50600D61E4B /* Border.swift in Sources */,
Expand Down
67 changes: 67 additions & 0 deletions Sources/iOS/DialogBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// DialogBuilder.swift
// Material
//
// Created by Orkhan Alikhanov on 1/11/18.
// Copyright © 2018 CosmicMind, Inc. All rights reserved.
//

import UIKit

public typealias Dialog = DialogBuilder<DialogView>
open class DialogBuilder<T: DialogView> {

public init() {}
open let controller = DialogController<T>()

open func title(_ text: String?) -> DialogBuilder {
dialogView.titleLabel.text = text
return self
}

open func details(_ text: String?) -> DialogBuilder {
dialogView.detailsLabel.text = text
return self
}

open func isCancelable(_ value: Bool, handler: (() -> Void)? = nil) -> DialogBuilder {
controller.isCancelable = value
controller.didCancelHandler = handler
return self
}

open func shouldDismiss(handler: ((T, Button?) -> Bool)?) -> DialogBuilder {
controller.shouldDismissHandler = handler
return self
}

open func positive(_ title: String?, handler: (() -> Void)?) -> DialogBuilder {
dialogView.positiveButton.title = title
controller.didTapPositiveButtonHandler = handler
return self
}

open func negative(_ title: String?, handler: (() -> Void)?) -> DialogBuilder {
dialogView.negativeButton.title = title
controller.didTapNegativeButtonHandler = handler
return self
}

open func neutral(_ title: String?, handler: (() -> Void)?) -> DialogBuilder {
dialogView.neutralButton.title = title
controller.didTapNeutralButtonHandler = handler
return self
}

@discardableResult
open func show(_ vc: UIViewController) -> DialogBuilder {
vc.present(controller, animated: true, completion: nil)
return self
}
}

extension DialogBuilder {
private var dialogView: T {
return controller.dialogView
}
}
83 changes: 83 additions & 0 deletions Sources/iOS/DialogController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// DialogController.swift
// Material
//
// Created by Orkhan Alikhanov on 1/11/18.
// Copyright © 2018 CosmicMind, Inc. All rights reserved.
//

import UIKit

open class DialogController<T: DialogView>: UIViewController {
open let dialogView = T()
open var isCancelable = false

open func prepare() {
isMotionEnabled = true
motionTransitionType = .fade
modalPresentationStyle = .overFullScreen
}

open override func viewDidLoad() {
super.viewDidLoad()
view = UIControl()
view.backgroundColor = UIColor.black.withAlphaComponent(0.33)
view.layout(dialogView)
.center()

(view as? UIControl)?.addTarget(self, action: #selector(didTapView), for: .touchUpInside)
dialogView.buttonArea.subviews.forEach {
($0 as? Button)?.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
}
}

open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
dialogView.maxSize = CGSize(width: Screen.width * 0.8, height: Screen.height * 0.9)
}

open var didCancelHandler: (() -> Void)?
open var shouldDismissHandler: ((T, Button?) -> Bool)?

open var didTapPositiveButtonHandler: (() -> Void)?
open var didTapNegativeButtonHandler: (() -> Void)?
open var didTapNeutralButtonHandler: (() -> Void)?

@objc
private func didTapView() {
guard isCancelable else { return }
dismiss(nil)
didCancelHandler?()
}

@objc
private func didTapButton(_ sender: Button) {
switch sender {
case dialogView.positiveButton:
didTapPositiveButtonHandler?()
case dialogView.negativeButton:
didTapNegativeButtonHandler?()
case dialogView.neutralButton:
didTapNeutralButtonHandler?()
default:
break
}
dismiss(sender)
}

open func dismiss(_ button: Button?) {
if shouldDismissHandler?(dialogView, button) ?? true {
presentingViewController?.dismiss(animated: true, completion: nil)
}
}

public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
prepare()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepare()
}
}
Loading