Skip to content

Commit

Permalink
Merge pull request #8 from olmps/feature/spm
Browse files Browse the repository at this point in the history
Swift Package Manager support
  • Loading branch information
ggirotto authored Feb 4, 2021
2 parents 8982651 + 5f2f6ab commit 43f603f
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 43 deletions.
2 changes: 2 additions & 0 deletions NestedCloudKitCodable.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
630DDFD824F5C13300537AD5 /* NestedCloudKitCodableMacOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = NestedCloudKitCodableMacOS.entitlements; sourceTree = "<group>"; };
635A0E8B24F562C4002BC7AD /* NestedCloudKitCodableTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = NestedCloudKitCodableTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
635A0EF824F56516002BC7AD /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
63658A6525CC31FE0000C747 /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
6375386324F5347D0093936D /* NestedCloudKitCodableWatch.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NestedCloudKitCodableWatch.app; sourceTree = BUILT_PRODUCTS_DIR; };
6375386624F5347D0093936D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Interface.storyboard; sourceTree = "<group>"; };
6375386824F5347E0093936D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -288,6 +289,7 @@
607FACC71AFB9204008FA782 = {
isa = PBXGroup;
children = (
63658A6525CC31FE0000C747 /* Package.swift */,
63A249B324F567E9008C8C56 /* Source */,
63CDFCF524F5477900535338 /* NestedCloudKitCodable_Example.entitlements */,
63CDFCE424F53D3D00535338 /* Shared */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,16 @@
allowLocationSimulation = "YES"
launchAutomaticallySubstyle = "8"
notificationPayloadFile = "NestedCloudKitCodableWatch Extension/PushNotificationPayload.apns">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/NestedCloudKitCodable_Example">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6375386224F5347D0093936D"
BuildableName = "NestedCloudKitCodableWatch.app"
BlueprintName = "NestedCloudKitCodableWatch"
ReferencedContainer = "container:NestedCloudKitCodable.xcodeproj">
</BuildableReference>
</RemoteRunnable>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand All @@ -77,27 +75,16 @@
debugDocumentVersioning = "YES"
launchAutomaticallySubstyle = "8"
notificationPayloadFile = "NestedCloudKitCodableWatch Extension/PushNotificationPayload.apns">
<RemoteRunnable
runnableDebuggingMode = "2"
BundleIdentifier = "com.apple.Carousel"
RemotePath = "/NestedCloudKitCodable_Example">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6375386224F5347D0093936D"
BuildableName = "NestedCloudKitCodableWatch.app"
BlueprintName = "NestedCloudKitCodableWatch"
ReferencedContainer = "container:NestedCloudKitCodable.xcodeproj">
</BuildableReference>
</RemoteRunnable>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6375386224F5347D0093936D"
BuildableName = "NestedCloudKitCodableWatch.app"
BlueprintName = "NestedCloudKitCodableWatch"
ReferencedContainer = "container:NestedCloudKitCodable.xcodeproj">
</BuildableReference>
</MacroExpansion>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
Expand Down
51 changes: 46 additions & 5 deletions NestedCloudKitCodable/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -20,11 +18,54 @@
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="46" translatesAutoresizingMaskIntoConstraints="NO" id="V4e-g8-jiG">
<rect key="frame" x="135.5" y="280.5" width="104" height="106"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="AXo-LC-48C">
<rect key="frame" x="0.0" y="0.0" width="104" height="30"/>
<state key="normal" title="Encode School"/>
<connections>
<action selector="encodeTapped:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="jlj-KU-QRw"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="JiC-5R-2CI">
<rect key="frame" x="0.0" y="76" width="104" height="30"/>
<state key="normal" title="Decode School"/>
<connections>
<action selector="decodeTapped:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="Dij-fo-U3D"/>
</connections>
</button>
</subviews>
</stackView>
<activityIndicatorView hidden="YES" opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" hidesWhenStopped="YES" style="large" translatesAutoresizingMaskIntoConstraints="NO" id="Eww-Al-f3S">
<rect key="frame" x="177" y="202" width="20" height="20"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</activityIndicatorView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Result:" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="7dX-sG-QHO">
<rect key="frame" x="36" y="406.5" width="303" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="7dX-sG-QHO" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" constant="20" id="3bc-Ql-8bV"/>
<constraint firstItem="V4e-g8-jiG" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" id="BrR-68-BGl"/>
<constraint firstItem="V4e-g8-jiG" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="Gi6-5A-yM4"/>
<constraint firstAttribute="trailingMargin" secondItem="7dX-sG-QHO" secondAttribute="trailing" constant="20" id="YKG-Gd-pCz"/>
<constraint firstItem="7dX-sG-QHO" firstAttribute="top" secondItem="V4e-g8-jiG" secondAttribute="bottom" constant="20" id="fpC-hI-TzX"/>
</constraints>
</view>
<connections>
<outlet property="activityIndicator" destination="Eww-Al-f3S" id="KPu-yj-ogP"/>
<outlet property="resultLabel" destination="7dX-sG-QHO" id="oIP-0h-4KP"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-295" y="134"/>
</scene>
</scenes>
</document>
68 changes: 50 additions & 18 deletions NestedCloudKitCodable/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import NestedCloudKitCodable

class ViewController: UIViewController {

@IBOutlet private var resultLabel: UILabel!
@IBOutlet private var activityIndicator: UIActivityIndicatorView!

private var schoolRecord: CKRecord?

private var director: Person {
return Person(name: "Director", birthDate: Date())
}
Expand All @@ -37,47 +42,74 @@ class ViewController: UIViewController {
return [book1, book2]
}

private var database: CKDatabase {
return CKContainer(identifier: "iCloud.com.nestedCloudKitCodable.Container").publicCloudDatabase
}

override func viewDidLoad() {
super.viewDidLoad()

private var school: School {
var schoolObject = School()
schoolObject.name = "School Name"
schoolObject.location = CLLocation(latitude: 37.331274, longitude: -122.030397)
schoolObject.students = students
schoolObject.director = director
schoolObject.books = books

return schoolObject
}

private var database: CKDatabase {
return CKContainer(identifier: "iCloud.com.nestedCloudKitCodable.Container").publicCloudDatabase
}

@IBAction private func encodeTapped(_ sender: UIButton) {
activityIndicator.isHidden = false
activityIndicator.startAnimating()
resultLabel.text = "Loading..."

do {
let encodedRecords = try CKRecordEncoder().encode(schoolObject)
let encodedRecords = try CKRecordEncoder().encode(school)
schoolRecord = encodedRecords.last

let saveOperator = CKModifyRecordsOperation(recordsToSave: encodedRecords)
saveOperator.modifyRecordsCompletionBlock = { (records, recordsIDs, error) in
if let error = error {
print(error)
} else {
self.decodeSchool(encodedRecords.last!)
DispatchQueue.main.async {
self.activityIndicator.stopAnimating()

if let error = error {
print(error)
self.resultLabel.text = "Error: \(error.localizedDescription)"
} else {
self.resultLabel.text = "Successfully encoded school object!"
}
}
}

database.add(saveOperator)
} catch let error {
let formattedError = error as! CKCodableError //swiftlint:disable:this force_cast
print(formattedError)
resultLabel.text = "Error: \(formattedError.localizedDescription)"
}
}

private func decodeSchool(_ schoolRecord: CKRecord) {
@IBAction private func decodeTapped(_ sender: UIButton) {
guard let schoolRecord = schoolRecord else {
resultLabel.text = "Please encode the object first"
return
}

activityIndicator.isHidden = false
activityIndicator.startAnimating()
resultLabel.text = "Loading..."

CKRecordDecoder().decode(School.self,
from: schoolRecord,
referenceDatabase: database) { (decodedSchool, error) in
if let error = error {
print(error)
} else if let decodedSchool = decodedSchool {
print(decodedSchool)
DispatchQueue.main.async {
self.activityIndicator.stopAnimating()

if let error = error {
print(error)
self.resultLabel.text = "Error: \(error.localizedDescription)"
} else if let decodedSchool = decodedSchool {
self.resultLabel.text = "Successfully encoded school object!"
print(decodedSchool)
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// swift-tools-version:5.0
import PackageDescription

let package = Package(
name: "NestedCloudKitCodable",
platforms: [
.macOS(.v10_13),
.iOS(.v10),
.tvOS(.v11),
.watchOS(.v3),
],
products: [
.library(name: "NestedCloudKitCodable", targets: ["NestedCKCodable"])
],
targets: [
.target(name: "NestedCKCodable", path: "./Source")
]
)
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 2ffe4c6d25cbfb6b22eb4b0a78b0a2359a4bb4f2

COCOAPODS: 1.9.3
COCOAPODS: 1.10.0

0 comments on commit 43f603f

Please sign in to comment.