Skip to content

Commit

Permalink
Merge pull request #21 from mapbox/1ec5-osx-15
Browse files Browse the repository at this point in the history
Support OS X
  • Loading branch information
1ec5 committed May 12, 2016
2 parents 5e6ab11 + 66535c7 commit a56459f
Show file tree
Hide file tree
Showing 11 changed files with 2,048 additions and 635 deletions.
3 changes: 2 additions & 1 deletion MapboxStatic.swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Pod::Spec.new do |s|
s.summary = "Classic Mapbox Static API wrapper for Objective-C and Swift."

s.description = <<-DESC
MapboxStatic.swift makes it easy to connect your iOS, tvOS, or watchOS application to the classic Mapbox Static API. Quickly generate a static map image with overlays, asynchronous imagery fetching, and first-class Swift data types.
MapboxStatic.swift makes it easy to connect your iOS, OS X, tvOS, or watchOS application to the classic Mapbox Static API. Quickly generate a static map image with overlays, asynchronous imagery fetching, and first-class Swift data types.
DESC

s.homepage = "https://www.mapbox.com/api-documentation/#static-classic"
Expand All @@ -25,6 +25,7 @@ Pod::Spec.new do |s|

# When using multiple platforms
s.ios.deployment_target = "8.0"
s.osx.deployment_target = "10.10"
s.watchos.deployment_target = "2.0"
s.tvos.deployment_target = "9.0"

Expand Down
2,394 changes: 1,797 additions & 597 deletions MapboxStatic.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

41 changes: 26 additions & 15 deletions MapboxStatic/UIColor.swift → MapboxStatic/Color.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import UIKit

internal extension UIColor {

#if os(iOS)
import UIKit
typealias Color = UIColor
#elseif os(OSX)
import Cocoa
typealias Color = NSColor
#endif

internal extension Color {
internal func toHexString() -> String {

var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0

self.getRed(&r, green: &g, blue: &b, alpha: &a)
let color: Color
#if os(iOS)
color = self
#elseif os(OSX)
color = colorUsingColorSpaceName(NSCalibratedRGBColorSpace)!
#endif
color.getRed(&r, green: &g, blue: &b, alpha: &a)

r *= 255
g *= 255
Expand All @@ -18,8 +28,7 @@ internal extension UIColor {
return NSString(format: "%02x%02x%02x", Int(r), Int(g), Int(b)) as String
}

internal class func colorWithHexString(hexString: String) -> UIColor {

convenience init(hexString: String) {
var hexString = hexString.stringByReplacingOccurrencesOfString("#", withString: "")

if hexString.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 3 {
Expand All @@ -30,22 +39,24 @@ internal extension UIColor {
hexString = r + r + g + g + b + b
}

var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0

if hexString.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 6 {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0

var hexInt: UInt32 = 0

if NSScanner(string: hexString).scanHexInt(&hexInt) {
r = CGFloat((hexInt >> 16) & 0xff) / 255
g = CGFloat((hexInt >> 8) & 0xff) / 255
b = CGFloat(hexInt & 0xff) / 255

return UIColor(red: r, green: g, blue: b, alpha: 1)
}
}

return UIColor.blackColor()
#if os(iOS)
self.init(red: r, green: g, blue: b, alpha: 1)
#elseif os(OSX)
self.init(calibratedRed: r, green: g, blue: b, alpha: 1)
#endif
}
}
2 changes: 1 addition & 1 deletion MapboxStatic/MapboxStatic.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import UIKit;
#import <Foundation/Foundation.h>

//! Project version number for MapboxStatic.
FOUNDATION_EXPORT double MapboxStaticVersionNumber;
Expand Down
20 changes: 14 additions & 6 deletions MapboxStatic/Overlay.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import CoreLocation
import UIKit
#if os(iOS)
import UIKit
#elseif os(OSX)
import Cocoa
#endif

let allowedCharacterSet: NSCharacterSet = {
let characterSet = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() as! NSMutableCharacterSet
Expand All @@ -14,7 +18,12 @@ public enum MarkerSize: String {
}

public class Overlay: CustomStringConvertible {

#if os(iOS)
public typealias Color = UIColor
#elseif os(OSX)
public typealias Color = NSColor
#endif

internal var requestString: String = ""

public var description: String {
Expand All @@ -24,11 +33,10 @@ public class Overlay: CustomStringConvertible {
}

public class Marker: Overlay {

public init(coordinate: CLLocationCoordinate2D,
size: MarkerSize = .Small,
label: String = "",
color: UIColor = UIColor.redColor()) {
color: Color = .redColor()) {

super.init()

Expand Down Expand Up @@ -116,9 +124,9 @@ public class Path: Overlay {

public init(pathCoordinates: [CLLocationCoordinate2D],
strokeWidth: Int = 1,
strokeColor: UIColor = UIColor.colorWithHexString("555"),
strokeColor: Color = Color(hexString: "555"),
strokeOpacity: Double = 1.0,
fillColor: UIColor = UIColor.colorWithHexString("555"),
fillColor: Color = Color(hexString: "555"),
fillOpacity: Double = 0) {

super.init()
Expand Down
27 changes: 20 additions & 7 deletions MapboxStatic/Snapshot.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import CoreLocation
import UIKit
#if os(iOS)
import UIKit
#elseif os(OSX)
import Cocoa
#endif

public enum SnapshotFormat: String {
case PNG = "png"
Expand Down Expand Up @@ -27,7 +31,11 @@ public struct SnapshotOptions {

public var format: SnapshotFormat = .PNG
public var size: CGSize
#if os(iOS)
public var scale: CGFloat = UIScreen.mainScreen().scale
#elseif os(OSX)
public var scale: CGFloat = NSScreen.mainScreen()?.backingScaleFactor ?? 1
#endif
public var showsAttribution: Bool = true
public var showsLogo: Bool = true

Expand Down Expand Up @@ -69,9 +77,14 @@ public struct SnapshotOptions {
}
}

public typealias SnapshotCompletionHandler = (UIImage?, NSError?) -> Void

public struct Snapshot {
#if os(iOS)
public typealias Image = UIImage
#elseif os(OSX)
public typealias Image = NSImage
#endif
public typealias CompletionHandler = (Image?, NSError?) -> Void

private var apiEndpoint: String = "https://api.mapbox.com"
private let accessToken: String

Expand Down Expand Up @@ -131,22 +144,22 @@ public struct Snapshot {
]
}

public var image: UIImage? {
public var image: Image? {
if let data = NSData(contentsOfURL: requestURL) {
return UIImage(data: data)
return Image(data: data)
} else {
return nil
}
}

public func image(completionHandler handler: SnapshotCompletionHandler) -> NSURLSessionDataTask {
public func image(completionHandler handler: CompletionHandler) -> NSURLSessionDataTask {
let task = NSURLSession.sharedSession().dataTaskWithURL(requestURL) { (data, response, error) in
if let error = error {
dispatch_async(dispatch_get_main_queue()) {
handler(nil, error)
}
} else {
let image = UIImage(data: data!)
let image = Image(data: data!)
dispatch_async(dispatch_get_main_queue()) {
handler(image, nil)
}
Expand Down
25 changes: 19 additions & 6 deletions MapboxStaticTests/MapboxStaticTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import CoreLocation
import Foundation
import MapboxStatic

#if os(iOS)
typealias Color = UIColor
#elseif os(OSX)
typealias Color = NSColor
#endif

class MapboxStaticTests: XCTestCase {
let mapID = "justin.o0mbikn2"
let accessToken = "pk.eyJ1IjoianVzdGluIiwiYSI6IlpDbUJLSUEifQ.4mG8vhelFMju6HpIY-Hi5A"
Expand Down Expand Up @@ -38,6 +44,13 @@ class MapboxStaticTests: XCTestCase {
let autoFitExp = expectationWithDescription("auto-fit should default to enabled")

let options = SnapshotOptions(mapIdentifier: mapID, size: CGSize(width: 200, height: 200))

let scale: CGFloat
#if os(iOS)
scale = UIScreen.mainScreen().scale
#elseif os(OSX)
scale = NSScreen.mainScreen()?.backingScaleFactor ?? 1
#endif

stub(isHost(serviceHost)) { [unowned self] request in
if let p = request.URL?.pathComponents {
Expand All @@ -51,11 +64,11 @@ class MapboxStaticTests: XCTestCase {
overlaysExp.fulfill()
autoFitExp.fulfill()
}
if p[4].componentsSeparatedByString(".").first == "200x200" && UIScreen.mainScreen().scale == 1 {
if p[4].componentsSeparatedByString(".").first == "200x200" && scale == 1 {
retinaExp.fulfill()
sizeExp.fulfill()
}
else if p[4].componentsSeparatedByString(".").first == "200x200@2x" && UIScreen.mainScreen().scale > 1 {
else if p[4].componentsSeparatedByString(".").first == "200x200@2x" && scale > 1 {
retinaExp.fulfill()
sizeExp.fulfill()
}
Expand Down Expand Up @@ -220,7 +233,7 @@ class MapboxStaticTests: XCTestCase {
let lon = -122.681944
let size = MarkerSize.Medium
let label = "cafe"
let color = UIColor.brownColor()
let color = Color.brownColor()
let colorRaw = "996633"

let markerExp = expectationWithDescription("builtin marker argument should format Maki request properly")
Expand Down Expand Up @@ -324,10 +337,10 @@ class MapboxStaticTests: XCTestCase {

func testOverlayPath() {
let strokeWidth = 2
let strokeColor = UIColor.blackColor()
let strokeColor = Color.blackColor()
let strokeColorRaw = "000000"
let strokeOpacity = 0.75
let fillColor = UIColor.redColor()
let fillColor = Color.redColor()
let fillColorRaw = "ff0000"
let fillOpacity = 0.25
let encodedPolyline = "(upztG%60jxkVn@al@bo@nFWzuAaTcAyZen@)"
Expand Down Expand Up @@ -392,7 +405,7 @@ class MapboxStaticTests: XCTestCase {
coordinate: CLLocationCoordinate2D(latitude: 45.52, longitude: -122.681944),
size: .Medium,
label: "cafe",
color: UIColor.brownColor())
color: .brownColor())

var options = SnapshotOptions(
mapIdentifier: mapID,
Expand Down
Loading

0 comments on commit a56459f

Please sign in to comment.