Skip to content

Commit

Permalink
Updated to Swift 4 and dispatched adding SMToast to main queue
Browse files Browse the repository at this point in the history
  • Loading branch information
mandrusiaks committed Aug 22, 2017
1 parent 726ae35 commit aa0bf8c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 39 deletions.
22 changes: 18 additions & 4 deletions Example/SMToast.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0820;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = CocoaPods;
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = FJ9965JL6C;
LastSwiftMigration = 0820;
LastSwiftMigration = 0900;
};
};
};
Expand Down Expand Up @@ -292,14 +292,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -339,14 +345,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -384,7 +396,8 @@
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -401,7 +414,8 @@
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = On;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
LastUpgradeVersion = "0900"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -40,6 +40,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -69,6 +70,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
2 changes: 1 addition & 1 deletion SMToast.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SMToast'
s.version = '0.5.0'
s.version = '0.5.1'
s.summary = 'SMToast is a simple way to display a message. SMToast\'s are draggable and can be stacked on the screen.'

# This description is used to generate tags and improve search results.
Expand Down
68 changes: 35 additions & 33 deletions SMToast/Classes/SMToast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ import UIKit
public class SMToast: UIView {

//MARK: - Queue
fileprivate static var activeQueue = SMQueue()
fileprivate static var onHoldQueue = SMQueue()
private static var activeQueue = SMQueue()
private static var onHoldQueue = SMQueue()

//MARK: - Configuration Variables
static public var isOnHoldQueueEnabled = false
static public var isActiveQueueEnabled = false
fileprivate let cornerRadius: CGFloat = 15
fileprivate var title: String = ""
fileprivate var message: String = ""
fileprivate var toastColor: UIColor = .black
fileprivate var fontColor: UIColor = .white
fileprivate var duration: TimeInterval = 2
fileprivate var fadeDuration: TimeInterval = 1
private let cornerRadius: CGFloat = 15
private var title: String = ""
private var message: String = ""
private var toastColor: UIColor = .black
private var fontColor: UIColor = .white
private var duration: TimeInterval = 2
private var fadeDuration: TimeInterval = 1
let id: TimeInterval = Date().timeIntervalSince1970

//MARK: - Variables
fileprivate var initialCenter: CGPoint!
fileprivate var animator: UIDynamicAnimator?
private var initialCenter: CGPoint!
private var animator: UIDynamicAnimator?

//MARK: - View Components
fileprivate var view: UIView!
fileprivate var titleLabel: UILabel!
fileprivate var messageLabel: UILabel!
private var view: UIView!
private var titleLabel: UILabel!
private var messageLabel: UILabel!

public override var center: CGPoint {
didSet {
Expand Down Expand Up @@ -303,7 +303,7 @@ public class SMToast: UIView {

//MARK: - Setup
extension SMToast {
fileprivate func initialSetup() {
private func initialSetup() {
setup()
formatTitleLabel()
formatMessageLabel()
Expand Down Expand Up @@ -338,7 +338,7 @@ extension SMToast {
}
private func formatTitleLabel() {
titleLabel = UILabel(frame: .zero)
titleLabel.font = UIFont.systemFont(ofSize: 15, weight: UIFontWeightSemibold)
titleLabel.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
titleLabel.textAlignment = .center
titleLabel.textColor = fontColor
titleLabel.numberOfLines = 2
Expand All @@ -349,7 +349,7 @@ extension SMToast {
private func formatMessageLabel() {
let frame = CGRect(x: 0, y: titleLabel!.frame.height, width: 0, height: 0)
messageLabel = UILabel(frame: frame)
messageLabel.font = UIFont.systemFont(ofSize: 13, weight: UIFontWeightLight)
messageLabel.font = UIFont.systemFont(ofSize: 13, weight: .light)
messageLabel.textAlignment = .center
messageLabel.textColor = fontColor
messageLabel.numberOfLines = 4
Expand All @@ -367,7 +367,7 @@ extension SMToast {

//MARK: - Action
extension SMToast {
func panned(_ sender: UIPanGestureRecognizer) {
@objc func panned(_ sender: UIPanGestureRecognizer) {
guard let topView = UIApplication.shared.windows.first else { return }
switch sender.state {
case .began:
Expand Down Expand Up @@ -439,18 +439,20 @@ extension SMToast {
is met, the toast is simply displayed on the screen.

*/
fileprivate func present(fromHold: Bool = false) {
if fromHold || shouldShowToast() {
SMToast.activeQueue.add(toast: self)
UIApplication.shared.windows.first?.addSubview(self)
UIView.animate(withDuration: self.duration, delay: 0, options: .allowUserInteraction, animations: {
self.alpha = 0.75
}, completion: { (_) in
self.fadeOut()
})
}else {
if SMToast.isOnHoldQueueEnabled {
SMToast.onHoldQueue.add(toast: self)
private func present(fromHold: Bool = false) {
DispatchQueue.main.async {
if fromHold || self.shouldShowToast() {
SMToast.activeQueue.add(toast: self)
UIApplication.shared.windows.first?.addSubview(self)
UIView.animate(withDuration: self.duration, delay: 0, options: .allowUserInteraction, animations: {
self.alpha = 0.75
}, completion: { (_) in
self.fadeOut()
})
}else {
if SMToast.isOnHoldQueueEnabled {
SMToast.onHoldQueue.add(toast: self)
}
}
}
}
Expand Down Expand Up @@ -501,7 +503,7 @@ extension SMToast {

//MARK: - Additional
extension SMToast {
fileprivate func sizeComponents() -> (CGFloat, CGFloat) {
private func sizeComponents() -> (CGFloat, CGFloat) {
let titleWidth = titleLabel.frame.width
let messageWidth = messageLabel.frame.width
let maxLabelWidth = messageWidth > titleWidth ? messageWidth : titleWidth
Expand Down Expand Up @@ -540,7 +542,7 @@ extension SMToast {
the same center as the most recently expired SMToast.
*/

fileprivate func checkHold() {
private func checkHold() {
guard let toast = SMToast.onHoldQueue.first else { return }
toast.center = self.initialCenter
present(fromHold: true)
Expand All @@ -553,7 +555,7 @@ extension SMToast {
- Author:
Spencer Mandrusiak
*/
fileprivate func shouldShowToast() -> Bool {
private func shouldShowToast() -> Bool {
guard let lastToast = SMToast.activeQueue.last else { return true }
let newY = (lastToast.initialCenter!.y - 8) - (frame.height)
guard newY - (frame.height/2) > 0 else { return false }
Expand Down

0 comments on commit aa0bf8c

Please sign in to comment.