Skip to content

Commit

Permalink
Use UIViewController as Entry #40 - Infrastructure.
Browse files Browse the repository at this point in the history
  • Loading branch information
huri000 committed Jun 6, 2018
1 parent 5eb8537 commit 763c7ce
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 31 deletions.
14 changes: 13 additions & 1 deletion Example/SwiftEntryKit/Presets/PresetsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,20 @@ class PresetsViewController: UIViewController {
SwiftEntryKit.dismiss()
}

let ok1Button = EKProperty.ButtonContent(label: okButtonLabel, backgroundColor: .clear, highlightedBackgroundColor: EKColor.Teal.a600.withAlphaComponent(0.05)) {
SwiftEntryKit.dismiss()
}

let ok2Button = EKProperty.ButtonContent(label: okButtonLabel, backgroundColor: .clear, highlightedBackgroundColor: EKColor.Teal.a600.withAlphaComponent(0.05)) {
SwiftEntryKit.dismiss()
}

let ok3Button = EKProperty.ButtonContent(label: okButtonLabel, backgroundColor: .clear, highlightedBackgroundColor: EKColor.Teal.a600.withAlphaComponent(0.05)) {
SwiftEntryKit.dismiss()
}

// Generate the content
let buttonsBarContent = EKProperty.ButtonBarContent(with: okButton, laterButton, closeButton, separatorColor: EKColor.Gray.light, expandAnimatedly: true)
let buttonsBarContent = EKProperty.ButtonBarContent(with: okButton, laterButton, closeButton, ok1Button, ok3Button, ok2Button, separatorColor: EKColor.Gray.light, expandAnimatedly: true)

let alertMessage = EKAlertMessage(simpleMessage: simpleMessage, buttonBarContent: buttonsBarContent)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ source 'https://github.com/cocoapods/specs.git'
platform :ios, '9.0'
use_frameworks!

pod 'SwiftEntryKit', '0.3.1'
pod 'SwiftEntryKit', '0.4.0'
```

Then, run the following command:
Expand All @@ -150,7 +150,7 @@ $ brew install carthage
To integrate SwiftEntryKit into your Xcode project using Carthage, specify the following in your `Cartfile`:

```ogdl
github "huri000/SwiftEntryKit" == 0.3.1
github "huri000/SwiftEntryKit" == 0.4.0
```

## Usage
Expand Down
37 changes: 22 additions & 15 deletions Source/Infra/EKEntryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ import QuickLayout
class EKEntryView: EKStyleView {

struct Content {
var view: UIView
var viewController: UIViewController!
var view: UIView!
var attributes: EKAttributes

init(viewController: UIViewController, attributes: EKAttributes) {
self.viewController = viewController
self.view = viewController.view
self.attributes = attributes
}

init(view: UIView, attributes: EKAttributes) {
self.view = view
self.attributes = attributes
}
}

// MARK: Props
private var backgroundView: EKBackgroundView!
private var content: Content!
private var content: Content
private lazy var contentView: UIView = {
return UIView()
}()
Expand All @@ -37,8 +49,13 @@ class EKEntryView: EKStyleView {
}()

// MARK: Setup
init() {
init(newEntry content: Content) {
self.content = content
super.init(frame: UIScreen.main.bounds)
setupContentView()
applyDropShadow()
applyBackgroundToContentView()
applyFrameStyle()
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -50,16 +67,6 @@ class EKEntryView: EKStyleView {
applyFrameStyle()
}

func setup(newEntry content: Content) {

self.content = content

setupContentView()
applyDropShadow()
applyBackgroundToContentView()
applyFrameStyle()
}

func transform(to view: UIView) {

let previousView = content.view
Expand All @@ -76,15 +83,15 @@ class EKEntryView: EKStyleView {
previousHeight.priority = .defaultLow
nextHeight.priority = .must

previousView.alpha = 0
previousView!.alpha = 0

SwiftEntryKit.layoutIfNeeded()

}, completion: { (finished) in

view.alpha = 0

previousView.removeFromSuperview()
previousView!.removeFromSuperview()
self.removeConstraints([previousHeight, nextHeight])

self.setupContentView()
Expand Down
37 changes: 26 additions & 11 deletions Source/Infra/EKWindowProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class EKWindowProvider {
public enum State {
case transform(to: UIView)
case entry(view: UIView, attributes: EKAttributes)
case entryController(viewController: UIViewController, attributes: EKAttributes)
case main

var isMain: Bool {
Expand Down Expand Up @@ -46,6 +47,8 @@ public class EKWindowProvider {
clean()
case .entry(view: let view, attributes: let attributes):
setup(with: view, attributes: attributes)
case .entryController(viewController: let vc, attributes: let attributes):
setup(with: vc, attributes: attributes)
case .transform(to: let view):
transform(to: view)
}
Expand All @@ -66,23 +69,35 @@ public class EKWindowProvider {

// MARK: Setup and Teardown methods

// Setup new entry
// Setup new view controller
private func setup(with viewController: UIViewController, attributes: EKAttributes) {
guard let entryVC = prepare(for: attributes) else {
return
}
let entryView = EKEntryView(newEntry: .init(viewController: viewController, attributes: attributes))
entryVC.configure(newEntryView: entryView, attributes: attributes)
self.entryView = entryView
}

// Setup new view
private func setup(with view: UIView, attributes: EKAttributes) {

let entryVC = setupWindowAndRootVC()

guard entryVC.canDisplay(attributes: attributes) else {
guard let entryVC = prepare(for: attributes) else {
return
}
let entryView = EKEntryView(newEntry: .init(view: view, attributes: attributes))
entryVC.configure(newEntryView: entryView, attributes: attributes)
self.entryView = entryView
}

// Prepare the window and the host view controller
private func prepare(for attributes: EKAttributes) -> EKRootViewController? {
let entryVC = setupWindowAndRootVC()
guard entryVC.canDisplay(attributes: attributes) else {
return nil
}
entryWindow.windowLevel = attributes.windowLevel.value

entryVC.setStatusBarStyle(for: attributes)

let entryView = EKEntryView()
entryView.setup(newEntry: .init(view: view, attributes: attributes))
entryVC.configure(newEntryView: entryView, attributes: attributes)
self.entryView = entryView
return entryVC
}

// Transform current entry
Expand Down
15 changes: 14 additions & 1 deletion Source/SwiftEntryKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class SwiftEntryKit {
}

/**
Displays a given entry view using a given attributes struct.
Displays a given view entry view using a given attributes struct.
- A thread-safe method - Can be invokes from anywhere.
- A class method - Should be called on the class.
- parameter view: Custom view that is to be displayed
Expand All @@ -33,6 +33,19 @@ public final class SwiftEntryKit {
}
}

/**
Displays a given view controller entry view using a given attributes struct.
- A thread-safe method - Can be invokes from anywhere.
- A class method - Should be called on the class.
- parameter view: Custom view that is to be displayed
- parameter attributes: Attributes (The display properties)
*/
public class func display(entry viewController: UIViewController, attributes: EKAttributes) {
execute {
EKWindowProvider.shared.state = .entryController(viewController: viewController, attributes: attributes)
}
}

/**
ALPHA FEATURE: Transform the previous entry to the current one using the previous attributes struct.
- A thread-safe method - Can be invokes from anywhere.
Expand Down
2 changes: 1 addition & 1 deletion SwiftEntryKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SwiftEntryKit'
s.version = '0.3.1'
s.version = '0.4.0'
s.summary = 'A simple banner and pop-up displayer for iOS. Written in Swift.'
s.platform = :ios
s.ios.deployment_target = '9.0'
Expand Down

0 comments on commit 763c7ce

Please sign in to comment.