Skip to content

Commit

Permalink
Fix #68.
Browse files Browse the repository at this point in the history
The Client object is passed as Map.context instead of using objc_setAssociatedObject. Also the locale resolution has been fixed.
  • Loading branch information
sebastianludwig committed Jun 1, 2017
1 parent 33afda4 commit 9c4e37c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 33 deletions.
5 changes: 3 additions & 2 deletions Sources/Asset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class Asset: Resource, LocalizedResource {
let defaultLocale: String

/// Currently selected locale
public var locale: String
public var locale: String?

/// The URL for the underlying media file
public func url() throws -> URL {
Expand All @@ -72,7 +72,8 @@ public class Asset: Resource, LocalizedResource {
public required init(map: Map) throws {
let (locale, localizedFields) = try parseLocalizedFields(map.JSON)
self.locale = locale
self.defaultLocale = determineDefaultLocale(map.JSON)
let client = map.context as? Client
self.defaultLocale = determineDefaultLocale(client: client)
self.localizedFields = localizedFields

// Optional properties
Expand Down
10 changes: 7 additions & 3 deletions Sources/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ open class Client {
fileprivate func handleJSON<MappableType: ImmutableMappable>(_ data: Data, _ completion: ResultsHandler<MappableType>) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
if let json = json as? NSDictionary { json.client = self }

let map = Map(mappingType: .fromJSON, JSON: json as! [String : Any])
let map = Map(mappingType: .fromJSON, JSON: json as! [String : Any], context: self)

// Handle error thrown by CDA.
if let error = try? ContentfulError(map: map) {
Expand All @@ -179,6 +178,8 @@ open class Client {
}
}

extension Client: MapContext { }

// MARK: - Query

extension Client {
Expand Down Expand Up @@ -474,7 +475,10 @@ extension Client {
completion(.success(space))
return nil
}
return fetch(url: self.URL(), then: completion)
return fetch(url: self.URL()) { (result: Result<Space>) in
self.space = result.value
completion(result)
}
}

/**
Expand Down
21 changes: 4 additions & 17 deletions Sources/Decoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,11 @@

import Foundation
import ObjectMapper
import ObjectiveC.runtime

private var key = "ContentfulClientKey"

extension NSDictionary {
var client: Client? {
get {
return objc_getAssociatedObject(self, &key) as? Client
}
set {
objc_setAssociatedObject(self, &key, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}


internal func determineDefaultLocale(_ json: Any) -> String {
if let json = json as? NSDictionary, let space = json.client?.space {
internal func determineDefaultLocale(client: Client?) -> String {
if let space = client?.space {
if let locale = (space.locales.filter { $0.isDefault }).first {
return locale.code
}
Expand All @@ -34,7 +21,7 @@ internal func determineDefaultLocale(_ json: Any) -> String {
return Defaults.locale
}

internal func parseLocalizedFields(_ json: [String: Any]) throws -> (String, [String: [String: Any]]) {
internal func parseLocalizedFields(_ json: [String: Any]) throws -> (String?, [String: [String: Any]]) {
let map = Map(mappingType: .fromJSON, JSON: json)
var fields: [String: Any]!
fields <- map["fields"]
Expand Down Expand Up @@ -64,5 +51,5 @@ internal func parseLocalizedFields(_ json: [String: Any]) throws -> (String, [St
}
}

return (locale ?? Defaults.locale, localizedFields)
return (locale, localizedFields)
}
5 changes: 3 additions & 2 deletions Sources/Entry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Entry: Resource, LocalizedResource {
let defaultLocale: String

/// Currently selected locale
public var locale: String
public var locale: String?

// MARK: Internal

Expand Down Expand Up @@ -83,7 +83,8 @@ public class Entry: Resource, LocalizedResource {
public required init(map: Map) throws {
let (locale, localizedFields) = try parseLocalizedFields(map.JSON)
self.locale = locale
self.defaultLocale = determineDefaultLocale(map.JSON)
let client = map.context as? Client
self.defaultLocale = determineDefaultLocale(client: client)
self.localizedFields = localizedFields

try super.init(map: map)
Expand Down
14 changes: 7 additions & 7 deletions Sources/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protocol LocalizedResource {
// Should this be moved to Resource?
var fields: [String: Any]! { get }

var locale: String { get set }
var locale: String? { get set }
var localizedFields: [String: [String: Any]] { get }
var defaultLocale: String { get }
}
Expand Down Expand Up @@ -69,14 +69,14 @@ func +<K: Hashable, V> (left: [K: V], right: [K: V]) -> [K: V] {
return left += right
}

func fields(_ localizedFields: [String: [String: Any]], forLocale locale: String, defaultLocale: String) -> [String: Any] {
if let fields = localizedFields[locale], locale != defaultLocale {
let defaultLocaleFields = localizedFields[defaultLocale] ?? [String: Any]()
func fields(_ localizedFields: [String: [String: Any]], forLocale locale: String?, defaultLocale: String) -> [String: Any] {
let localeFields = localizedFields[defaultLocale] ?? [String: Any]()

return defaultLocaleFields + fields
if let locale = locale, let fields = localizedFields[locale], locale != defaultLocale {
return localeFields + fields
} else {
return localeFields
}

return localizedFields[locale] ?? [String: Any]()
}

public extension Dictionary where Key: ExpressibleByStringLiteral {
Expand Down
1 change: 0 additions & 1 deletion Sources/SignalUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Foundation
import Interstellar
import ObjectiveC.runtime


typealias SignalBang<U> = (@escaping (Result<U>) -> Void) -> URLSessionDataTask?
Expand Down
2 changes: 1 addition & 1 deletion Sources/SyncSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public final class SyncSpace: ImmutableMappable {
items <- map["items"]

let resources: [Resource] = items.flatMap { itemJSON in
let map = Map(mappingType: .fromJSON, JSON: itemJSON)
let map = Map(mappingType: .fromJSON, JSON: itemJSON, context: map.context)

var type: String!
type <- map["sys.type"]
Expand Down

0 comments on commit 9c4e37c

Please sign in to comment.