Skip to content

Commit c827f09

Browse files
committed
split huge functions into small ones
1 parent c5a4299 commit c827f09

File tree

5 files changed

+98
-63
lines changed

5 files changed

+98
-63
lines changed

NestedCloudKitCodable.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
639C8E7721A6E16E006148E4 /* Array+CKRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639C8E6521A6E16E006148E4 /* Array+CKRecord.swift */; };
3434
639C8E7821A6E16E006148E4 /* CKRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639C8E6621A6E16E006148E4 /* CKRecord.swift */; };
3535
639C8E7921A6E16E006148E4 /* CKCodableError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 639C8E6821A6E16E006148E4 /* CKCodableError.swift */; };
36+
63FC021221A71AF4006D5A8D /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63FC021121A71AF4006D5A8D /* Constants.swift */; };
3637
/* End PBXBuildFile section */
3738

3839
/* Begin PBXContainerItemProxy section */
@@ -89,6 +90,7 @@
8990
639C8E6521A6E16E006148E4 /* Array+CKRecord.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array+CKRecord.swift"; sourceTree = "<group>"; };
9091
639C8E6621A6E16E006148E4 /* CKRecord.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CKRecord.swift; sourceTree = "<group>"; };
9192
639C8E6821A6E16E006148E4 /* CKCodableError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CKCodableError.swift; sourceTree = "<group>"; };
93+
63FC021121A71AF4006D5A8D /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
9294
/* End PBXFileReference section */
9395

9496
/* Begin PBXFrameworksBuildPhase section */
@@ -178,6 +180,7 @@
178180
children = (
179181
639C8E5721A6E16E006148E4 /* BoxedArray.swift */,
180182
639C8E5E21A6E16E006148E4 /* CKRecordRepresentable.swift */,
183+
63FC021121A71AF4006D5A8D /* Constants.swift */,
181184
639C8E5821A6E16E006148E4 /* Encoder */,
182185
639C8E5F21A6E16E006148E4 /* Decoder */,
183186
639C8E6421A6E16E006148E4 /* Extensions */,
@@ -364,6 +367,7 @@
364367
639C8E7221A6E16E006148E4 /* CKRecordRepresentable.swift in Sources */,
365368
639C8E7121A6E16E006148E4 /* CKRecordEncoder.swift in Sources */,
366369
639C8E6D21A6E16E006148E4 /* CKEncoderUnkeyedContainer.swift in Sources */,
370+
63FC021221A71AF4006D5A8D /* Constants.swift in Sources */,
367371
639C8E7821A6E16E006148E4 /* CKRecord.swift in Sources */,
368372
639C8E7421A6E16E006148E4 /* CKDecoderKeyedContainer.swift in Sources */,
369373
639C8E6E21A6E16E006148E4 /* CKEncoderKeyedContainer.swift in Sources */,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Constants.swift
3+
// NestedCloudKitCodable
4+
//
5+
// Created by Guilherme Girotto on 22/11/18.
6+
// Copyright © 2018 Guilherme Girotto. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
internal struct Constants {
12+
static let locationSeparator: Character = ";"
13+
}

NestedCloudKitCodable/Sources/Decoder/CKDecoderKeyedContainer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ extension CKDecoderKeyedContainer {
9898
}
9999

100100
if let locationValue = value as? CLLocation {
101-
let locationStringValue = "\(locationValue.coordinate.latitude);\(locationValue.coordinate.longitude)"
101+
let locationStringValue = "\(locationValue.coordinate.latitude)\(Constants.locationSeparator)\(locationValue.coordinate.longitude)"
102102
guard let locationValue = locationStringValue as? T else {
103103
throw CKCodableError(.typeMismatch, context: ["Error:": "Couldn't convert String value to \(String(describing: T.self))"])
104104
}
@@ -108,7 +108,7 @@ extension CKDecoderKeyedContainer {
108108
if let locationsValues = value as? [CLLocation] {
109109
var locations = [String]()
110110
locationsValues.forEach {
111-
let value = "\($0.coordinate.latitude);\($0.coordinate.longitude)"
111+
let value = "\($0.coordinate.latitude)\(Constants.locationSeparator)\($0.coordinate.longitude)"
112112
locations.append(value)
113113
}
114114
guard let castedLocations = locations as? T else {

NestedCloudKitCodable/Sources/Decoder/CKDecoderSingleValueContainer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension CKDecoderSingleValueContainer {
3030

3131
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
3232
let decoder = _CKRecordDecoder(records: records,
33-
recordBeingAnalyzed: recordBeingAnalyzed)
33+
recordBeingAnalyzed: recordBeingAnalyzed)
3434
return try T(from: decoder)
3535
}
3636

NestedCloudKitCodable/Sources/Encoder/CKEncoderKeyedContainer.swift

Lines changed: 78 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ internal class CKEncoderKeyedContainer<Key>: CKKeyedEncoder where Key: CodingKey
3232
extension CKEncoderKeyedContainer {
3333

3434
var recordID: CKRecord.ID {
35-
let zid = zoneID ?? CKRecordZone.ID(zoneName: CKRecordZone.ID.defaultZoneName, ownerName: CKCurrentUserDefaultName)
36-
return CKRecord.ID(recordName: object.cloudKitIdentifier, zoneID: zid)
35+
let normalizedZone = zoneID ?? CKRecordZone.ID(zoneName: CKRecordZone.ID.defaultZoneName, ownerName: CKCurrentUserDefaultName)
36+
return CKRecord.ID(recordName: object.cloudKitIdentifier, zoneID: normalizedZone)
3737
}
3838

3939
var generatedRecord: CKRecord {
@@ -106,46 +106,19 @@ extension CKEncoderKeyedContainer: KeyedEncodingContainerProtocol {
106106
private func encodeCKRecordValue(_ value: CKRecordValue, forKey key: Key) throws {
107107

108108
if let data = value as? Data {
109-
let tempStr = ProcessInfo.processInfo.globallyUniqueString
110-
let filename = "\(tempStr)_file.bin"
111-
let baseURL = URL(fileURLWithPath: NSTemporaryDirectory())
112-
let fileURL = baseURL.appendingPathComponent(filename, isDirectory: false)
113-
try data.write(to: fileURL, options: .atomic)
114-
let asset = CKAsset(fileURL: fileURL)
115-
storage[key.stringValue] = asset
116-
return
109+
try encodeData(data, forKey: key)
117110
}
118111

119112
if let datas = value as? [Data] {
120-
var assets = [CKAsset]()
121-
for i in 0..<datas.count {
122-
let data = datas[i]
123-
let tempStr = ProcessInfo.processInfo.globallyUniqueString
124-
let filename = "\(tempStr)_file.bin"
125-
let baseURL = URL(fileURLWithPath: NSTemporaryDirectory())
126-
let fileURL = baseURL.appendingPathComponent(filename, isDirectory: false)
127-
try data.write(to: fileURL, options: .atomic)
128-
let asset = CKAsset(fileURL: fileURL)
129-
assets.append(asset)
130-
}
131-
storage[key.stringValue] = assets as CKRecordValue
113+
try encodeDataArray(datas, forKey: key)
132114
}
133115

134116
if let url = value as? URL {
135-
let asset = CKAsset(fileURL: url)
136-
storage[key.stringValue] = asset
137-
return
117+
encodeURL(url, forKey: key)
138118
}
139119

140120
if let urls = value as? [URL] {
141-
var assets = [CKAsset]()
142-
for i in 0..<urls.count {
143-
let url = urls[i]
144-
let asset = CKAsset(fileURL: url)
145-
assets.append(asset)
146-
}
147-
storage[key.stringValue] = assets as CKRecordValue
148-
return
121+
encodeURLArray(urls, forKey: key)
149122
}
150123

151124
/**
@@ -154,42 +127,87 @@ extension CKEncoderKeyedContainer: KeyedEncodingContainerProtocol {
154127
this values is "lat;long".
155128
*/
156129
if let locationString = value as? String,
157-
locationString.contains(";") {
158-
159-
let split = locationString.split(separator: ";")
160-
161-
guard let latitude = Double(split[0]),
162-
let longitude = Double(split[1]) else {
163-
storage[key.stringValue] = nil
164-
return
165-
}
166-
167-
storage[key.stringValue] = CLLocation(latitude: latitude, longitude: longitude)
168-
return
130+
locationString.contains(Constants.locationSeparator) {
131+
encodeLocation(fromString: locationString, forKey: key)
169132
}
170133

171134
if let locationsStrings = value as? [String],
172135
let firstString = locationsStrings.first,
173-
firstString.contains(";") {
174-
175-
var locations = [CLLocation]()
176-
locationsStrings.forEach {
177-
let split = $0.split(separator: ";")
178-
guard let latitude = Double(split[0]),
179-
let longitude = Double(split[1]) else {
180-
storage[key.stringValue] = nil
181-
return
182-
}
183-
let location = CLLocation(latitude: latitude, longitude: longitude)
184-
locations.append(location)
185-
}
186-
187-
storage[key.stringValue] = locations as CKRecordValue
136+
firstString.contains(Constants.locationSeparator) {
137+
encodeLocations(fromStrings: locationsStrings, forKey: key)
188138
}
189139

190140
storage[key.stringValue] = value
191141
}
192142

143+
private func encodeData(_ value: Data, forKey key: Key) throws {
144+
let tempStr = ProcessInfo.processInfo.globallyUniqueString
145+
let filename = "\(tempStr)_file.bin"
146+
let baseURL = URL(fileURLWithPath: NSTemporaryDirectory())
147+
let fileURL = baseURL.appendingPathComponent(filename, isDirectory: false)
148+
try value.write(to: fileURL, options: .atomic)
149+
let asset = CKAsset(fileURL: fileURL)
150+
storage[key.stringValue] = asset
151+
}
152+
153+
private func encodeDataArray(_ values: [Data], forKey key: Key) throws {
154+
var assets = [CKAsset]()
155+
for i in 0..<values.count {
156+
let data = values[i]
157+
let tempStr = ProcessInfo.processInfo.globallyUniqueString
158+
let filename = "\(tempStr)_file.bin"
159+
let baseURL = URL(fileURLWithPath: NSTemporaryDirectory())
160+
let fileURL = baseURL.appendingPathComponent(filename, isDirectory: false)
161+
try data.write(to: fileURL, options: .atomic)
162+
let asset = CKAsset(fileURL: fileURL)
163+
assets.append(asset)
164+
}
165+
storage[key.stringValue] = assets as CKRecordValue
166+
}
167+
168+
private func encodeURL(_ value: URL, forKey key: Key) {
169+
let asset = CKAsset(fileURL: value)
170+
storage[key.stringValue] = asset
171+
}
172+
173+
private func encodeURLArray(_ values: [URL], forKey key: Key) {
174+
var assets = [CKAsset]()
175+
for i in 0..<values.count {
176+
let url = values[i]
177+
let asset = CKAsset(fileURL: url)
178+
assets.append(asset)
179+
}
180+
storage[key.stringValue] = assets as CKRecordValue
181+
}
182+
183+
private func encodeLocation(fromString value: String, forKey key: Key) {
184+
let split = value.split(separator: Constants.locationSeparator)
185+
186+
guard let latitude = Double(split[0]),
187+
let longitude = Double(split[1]) else {
188+
storage[key.stringValue] = nil
189+
return
190+
}
191+
192+
storage[key.stringValue] = CLLocation(latitude: latitude, longitude: longitude)
193+
}
194+
195+
private func encodeLocations(fromStrings values: [String], forKey key: Key) {
196+
var locations = [CLLocation]()
197+
values.forEach {
198+
let split = $0.split(separator: Constants.locationSeparator)
199+
guard let latitude = Double(split[0]),
200+
let longitude = Double(split[1]) else {
201+
storage[key.stringValue] = nil
202+
return
203+
}
204+
let location = CLLocation(latitude: latitude, longitude: longitude)
205+
locations.append(location)
206+
}
207+
208+
storage[key.stringValue] = locations as CKRecordValue
209+
}
210+
193211
private func produceReference(for value: CKEncodable) throws -> CKRecord.Reference {
194212
let recordID = CKRecord.ID(recordName: value.cloudKitIdentifier)
195213
return CKRecord.Reference(recordID: recordID, action: .deleteSelf)

0 commit comments

Comments
 (0)