Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mono0926 committed Apr 23, 2017
1 parent cdd1fd2 commit 0ef32dc
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 49 deletions.
18 changes: 9 additions & 9 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ class ViewController: UIViewController {
}

@IBAction func customDidTap(_ sender: UIButton) {
NativePopup.show(info: NativePopup.Info(image: NativePopup.Image.custom(image: UIImage(named: "love")!),
title: "参考になった",
message: "フィードバックをありがとう\nございました。"))
NativePopup.show(image: UIImage(named: "love")!,
title: "参考になった",
message: "フィードバックをありがとう\nございました。")
}
@IBAction func goodDidTap(_ sender: UIButton) {
NativePopup.show(info: NativePopup.Info(image: NativePopup.Image.good,
title: "Helpful",
message: "Thanks for your feedback."))
NativePopup.show(image: NativePopup.FeedbackImage.good,
title: "Helpful",
message: "Thanks for your feedback.")
}
@IBAction func badDidTap(_ sender: UIButton) {
NativePopup.show(info: NativePopup.Info(image: NativePopup.Image.bad,
title: "Not Helpful",
message: "Thanks for your feedback."))
NativePopup.show(image: NativePopup.FeedbackImage.bad,
title: "Not Helpful",
message: "Thanks for your feedback.")
}
}

4 changes: 4 additions & 0 deletions NativePopup.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
FECA87DA1EAC87B700D07CB1 /* NativePopup.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = FE85338B1EAC7C92004BB756 /* NativePopup.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
FECA87DF1EAC94B300D07CB1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FECA87DE1EAC94B300D07CB1 /* Assets.xcassets */; };
FECA87E11EACC77800D07CB1 /* NativePopup.extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FECA87E01EACC77800D07CB1 /* NativePopup.extension.swift */; };
FECA87E31EACC7F000D07CB1 /* UIImageConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = FECA87E21EACC7F000D07CB1 /* UIImageConvertible.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -58,6 +59,7 @@
FECA87D71EAC7D9E00D07CB1 /* NativePopup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NativePopup.swift; sourceTree = "<group>"; };
FECA87DE1EAC94B300D07CB1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
FECA87E01EACC77800D07CB1 /* NativePopup.extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NativePopup.extension.swift; sourceTree = "<group>"; };
FECA87E21EACC7F000D07CB1 /* UIImageConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageConvertible.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -104,6 +106,7 @@
FE85338F1EAC7C92004BB756 /* Info.plist */,
FECA87D71EAC7D9E00D07CB1 /* NativePopup.swift */,
FECA87E01EACC77800D07CB1 /* NativePopup.extension.swift */,
FECA87E21EACC7F000D07CB1 /* UIImageConvertible.swift */,
FECA87DE1EAC94B300D07CB1 /* Assets.xcassets */,
);
path = NativePopup;
Expand Down Expand Up @@ -240,6 +243,7 @@
buildActionMask = 2147483647;
files = (
FECA87E11EACC77800D07CB1 /* NativePopup.extension.swift in Sources */,
FECA87E31EACC7F000D07CB1 /* UIImageConvertible.swift in Sources */,
FECA87D81EAC7D9E00D07CB1 /* NativePopup.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 6 additions & 0 deletions NativePopup/Assets.xcassets/feedback/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
30 changes: 4 additions & 26 deletions NativePopup/NativePopup.extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,11 @@
import Foundation

extension NativePopup {
public enum Image {
public enum FeedbackImage: String, HavingUIImageName {
case
good,
bad,
custom(image: UIImage)

var image: UIImage {
switch self {
case .good: return type(of: self).createImage(named: "good")
case .bad: return type(of: self).createImage(named: "bad")
case .custom(let image): return image
}
}

private static func createImage(named: String) -> UIImage {
return UIImage.init(named: named, in: Bundle(for: NativePopup.self), compatibleWith: nil)!
}
}

public struct Info {
public let image: Image
public let title: String
public let message: String?
public init(image: Image, title: String, message: String?) {
self.image = image
self.title = title
self.message = message
}
bad

public var imageName: String { return "feedback_\(rawValue)" }
}
}
26 changes: 12 additions & 14 deletions NativePopup/NativePopup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import Foundation
import UIKit

public class NativePopup: UIView {

private static let keyWindow = UIApplication.shared.keyWindow!
private weak static var currentView: NativePopup?
fileprivate weak static var currentView: NativePopup?

public static func show(info: Info) {
let view = NativePopup(info: info)
public static func show(image: UIImageConvertible, title: String, message: String?) {
let view = NativePopup(image: image, title: title, message: message)
view.show()
currentView = view
}
Expand All @@ -24,30 +23,29 @@ public class NativePopup: UIView {
fatalError("should not be called")
}

private init(info: Info) {
private init(image: UIImageConvertible, title: String, message: String?) {
super.init(frame: CGRect.zero)

layer.cornerRadius = 8
clipsToBounds = true

isUserInteractionEnabled = false

let image = info.image.image
let image = image.image
let imageView = UIImageView(image: image)
assert(image.size.width == image.size.height, "Aspect ratio should be 1:1.")
imageView.contentMode = .scaleAspectFit
if case .custom = info.image {
imageView.layer.cornerRadius = 6
imageView.clipsToBounds = true
}

imageView.layer.cornerRadius = 6
imageView.clipsToBounds = true

let titleLabel = UILabel()
titleLabel.text = info.title
titleLabel.text = title
// not dynamic size
titleLabel.font = UIFont.boldSystemFont(ofSize: 20)

let messageLabel = UILabel()
messageLabel.text = info.message
messageLabel.text = message
messageLabel.font = UIFont.systemFont(ofSize: 15)

[titleLabel, messageLabel].forEach {
Expand Down Expand Up @@ -80,7 +78,7 @@ public class NativePopup: UIView {
titleLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: sideSpace).isActive = true

let bottomSpace: CGFloat = 28
if info.message?.isEmpty ?? true {
if message?.isEmpty ?? true {
titleLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -bottomSpace).isActive = true
} else {
addSubview(messageLabel)
Expand All @@ -96,7 +94,7 @@ public class NativePopup: UIView {
private func show() {
let window = type(of: self).keyWindow
// TODO: need to remove?
for v in window.subviews where v is NativePopup { v.removeFromSuperview() }
type(of: self).currentView?.removeFromSuperview()
window.addSubview(self)
widthAnchor.constraint(equalToConstant: 250).isActive = true
centerXAnchor.constraint(equalTo: window.centerXAnchor).isActive = true
Expand Down
26 changes: 26 additions & 0 deletions NativePopup/UIImageConvertible.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// UIImageConvertible.swift
// NativePopup
//
// Created by mono on 2017/04/23.
// Copyright © 2017 mono. All rights reserved.
//

import Foundation
import UIKit

public protocol UIImageConvertible {
var image: UIImage { get }
}

extension UIImage: UIImageConvertible {
public var image: UIImage { return self }
}

public protocol HavingUIImageName: UIImageConvertible {
var imageName: String { get }
}

extension HavingUIImageName {
public var image: UIImage { return UIImage.init(named: imageName, in: Bundle(for: NativePopup.self), compatibleWith: nil)! }
}

0 comments on commit 0ef32dc

Please sign in to comment.