Skip to content

Commit

Permalink
Improve iOS only code
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Nov 6, 2017
1 parent 483f244 commit 5ffaa48
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 155 deletions.
52 changes: 24 additions & 28 deletions Demo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

57 changes: 0 additions & 57 deletions Sources/OpenInSafariActivity.swift

This file was deleted.

1 change: 1 addition & 0 deletions Sources/SearchBarContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ open class SearchBarContainerView: UIView {
fatalError()
}
}

65 changes: 0 additions & 65 deletions Sources/UILocalNotification+Sweet.swift

This file was deleted.

55 changes: 55 additions & 0 deletions Sources/iOS/OpenInSafariActivity.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import UIKit

public class OpenInSafariActivity: UIActivity {
fileprivate var url: URL?

public override var activityType: UIActivityType? {
return UIActivityType(String(describing: classForCoder))
}

public override var activityTitle: String? {
let defaultTitle = Bundle(for: classForCoder).localizedString(forKey: "Open in Safari", value: "Open in Safari Default", table: "OpenInSafariActivity")
let title = Bundle.main.localizedString(forKey: "Open in Safari", value: defaultTitle, table: nil)

return title
}

public override var activityImage: UIImage? {
return UIImage(named: "Safari", in: Bundle(for: classForCoder), compatibleWith: nil)
}

public override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
for item in activityItems {
if let item = item as? URL {
return UIApplication.shared.canOpenURL(item)
}
}

return false
}

public override func prepare(withActivityItems activityItems: [Any]) {
for item in activityItems {
if let item = item as? URL {
url = item
break
}
}
}

public override func perform() {
if let url = self.url {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:]) { completed in
self.activityDidFinish(completed)
}
} else {
let completed = UIApplication.shared.openURL(url)
activityDidFinish(completed)
}
} else {
activityDidFinish(false)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ open class SearchableCollectionController: SweetCollectionController {
open lazy var searchController: UISearchController = {
let controller = UISearchController(searchResultsController: nil)

#if os(iOS)
controller.dimsBackgroundDuringPresentation = false
#endif

controller.dimsBackgroundDuringPresentation = false
controller.delegate = self

if #available(iOS 11.0, *) {
Expand Down Expand Up @@ -71,7 +68,6 @@ open class SearchableCollectionController: SweetCollectionController {
searchBarContainerView.set(height: searchBar.frame.height)
definesPresentationContext = true
}

searchBar.sizeToFit()

collectionView.delegate = self
Expand Down Expand Up @@ -166,3 +162,4 @@ extension SearchableCollectionController: UISearchControllerDelegate {
}
}
}

64 changes: 64 additions & 0 deletions Sources/iOS/UILocalNotification+Sweet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import UIKit

public extension UILocalNotification {
static let idKey = "idKey"
static let fireDateKey = "fireDateKey"

/// Schedules a local notification.
///
/// - Parameters:
/// - id: The id used to represent a local notification in your app.
/// - date: The date when the notification will trigger
/// - soundName: The name of the sound to be used when the notification is received.
/// - message: The message that the app will use for your local notification.
/// - actionTitle: The title of the action to be displayed by your notification, it's optional, though, since Apple provides a good default for this.
public static func schedule(_ id: String, at date: Date, soundName: String? = nil, message: String, actionTitle: String? = nil) {
let notification = UILocalNotification()
notification.soundName = soundName

notification.fireDate = date
notification.timeZone = TimeZone.current
notification.alertBody = message
notification.alertAction = actionTitle
notification.hasAction = actionTitle != nil

var userInfo = [AnyHashable: Any]()
userInfo[idKey] = id
userInfo[fireDateKey] = date
notification.userInfo = userInfo

UIApplication.shared.scheduleLocalNotification(notification)
}

/// Finds a local notification for certain id.
///
/// - Parameter id: The id used to store the notification.
/// - Returns: A local notification that matches the provided id.
public static func find(_ id: String) -> UILocalNotification? {
let notifications = UIApplication.shared.scheduledLocalNotifications ?? [UILocalNotification]()
let filteredNotifications = notifications.filter { notification in
let filteredNotificationID = notification.userInfo?[idKey] as? String
return filteredNotificationID == id
}

return filteredNotifications.first
}

/// Cancels a local notification.
///
/// - Parameter id: The id used to store the notification.
public static func cancel(_ id: String) {
if let notification = self.find(id) {
UIApplication.shared.cancelLocalNotification(notification)
}
}

/// Cancels all the local notifications for the current app.
public static func cancelAll() {
let notifications = UIApplication.shared.scheduledLocalNotifications ?? [UILocalNotification]()
notifications.forEach { notification in
UIApplication.shared.cancelLocalNotification(notification)
}
}
}

1 change: 1 addition & 0 deletions SweetUIKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Pod::Spec.new do |s|
s.tvos.deployment_target = '9.0'
s.requires_arc = true
s.source_files = 'Sources/**/*.swift'
s.tvos.exclude_files = 'Sources/**/iOS/*.swift'
s.frameworks = 'UIKit'
s.resources = 'Resources/**/*'
s.resource_bundles = { 'SweetUIKitResources' => ['Resources/**/*'] }
Expand Down

0 comments on commit 5ffaa48

Please sign in to comment.