Skip to content
This repository was archived by the owner on Sep 3, 2018. It is now read-only.

Commit c1a5f21

Browse files
committedApr 9, 2015
Break out Contentful fetching into its own class
1 parent 12b2ff6 commit c1a5f21

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
 

‎Main App/BridgingHeader.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//
2+
// BridgingHeader.h
3+
// WatchKitExample
4+
//
5+
// Created by Boris Bügling on 09/04/15.
6+
// Copyright (c) 2015 Contentful GmbH. All rights reserved.
7+
//
8+
9+
#import <ContentfulDeliveryAPI/ContentfulDeliveryAPI.h>

‎Main App/Locations.swift

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Locations.swift
3+
// WatchKitExample
4+
//
5+
// Created by Boris Bügling on 09/04/15.
6+
// Copyright (c) 2015 Contentful GmbH. All rights reserved.
7+
//
8+
9+
import CoreLocation
10+
11+
struct Location {
12+
let distance: Double
13+
let entry: CDAEntry
14+
}
15+
16+
struct Locations {
17+
let defaultCoordinate = CLLocationCoordinate2D(latitude: 52.52191, longitude: 13.413215)
18+
let client: CDAClient
19+
20+
/* Valid locations:
21+
37.33170, -122 for SF
22+
40.75889, -73.98513 for NY
23+
52.52191, 13.413215 for Berlin
24+
*/
25+
26+
init() {
27+
client = CDAClient(spaceKey: "exembnlnz9oo", accessToken: "2ec43b32ffdda511b09abfd6a5b8ff65125cd19a4f6377d6a1e9540d34120052")
28+
}
29+
30+
func fetchEntries(location: CLLocationCoordinate2D, handler: ([Location]) -> Void) {
31+
let userLocation = CLLocation(latitude: location.latitude, longitude: location.longitude)
32+
33+
client.fetchEntriesMatching(["content_type": "6LmYY0rGhOaUyweiwSm4m", "order": "-sys.createdAt", "fields.visible": true, "fields.location[within]": [ location.latitude, location.longitude, 1000 ] ],
34+
success: { (response, array) -> Void in
35+
let entries = array.items as [CDAEntry]
36+
37+
if entries.count == 0 {
38+
self.fetchEntries(self.defaultCoordinate, handler)
39+
return
40+
}
41+
42+
handler(sorted(entries.map { (entry) in
43+
let placeLocation = entry.CLLocationCoordinate2DFromFieldWithIdentifier("location")
44+
let distance = userLocation.distanceFromLocation(CLLocation(latitude: placeLocation.latitude, longitude: placeLocation.longitude))
45+
return Location(distance: distance, entry: entry)
46+
}) { $0.distance < $1.distance })
47+
}) { (reponse, error) -> Void in
48+
NSLog("@Error: %@", error)
49+
}
50+
}
51+
}

‎Podfile

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
inhibit_all_warnings!
2+
#use_frameworks!
3+
4+
target 'WatchKitExample' do
5+
6+
pod 'ContentfulDeliveryAPI'
7+
8+
end
29

310
target 'WatchKitExample WatchKit Extension' do
411

‎WatchKitExample.xcodeproj/project.pbxproj

+52
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
7BBF7B8973B4CC7CB8C35CB0 /* libPods-WatchKitExample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9E9EFF9E5138A3E864A83B20 /* libPods-WatchKitExample.a */; };
1011
A16DED7E1A31D63B007F09E8 /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A16DED7D1A31D63B007F09E8 /* MapKit.framework */; };
1112
A171BC8C1A24AF4D0078E938 /* ImageInterfaceController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A171BC8A1A24AED50078E938 /* ImageInterfaceController.swift */; };
1213
A171BC8D1A24AF530078E938 /* MapInterfaceController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A171BC881A24AE2D0078E938 /* MapInterfaceController.swift */; };
@@ -25,6 +26,8 @@
2526
A1992C981A1CA20F005A48B8 /* WatchKitExample WatchKit Extension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = A1992C7E1A1CA20F005A48B8 /* WatchKitExample WatchKit Extension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
2627
A1CA0B0B1A1CE70600451409 /* DetailInterfaceController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1CA0B091A1CE6FE00451409 /* DetailInterfaceController.swift */; };
2728
A1DCEFFA1A1CC9FC003FCAA7 /* NewsTableRowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1DCEFF81A1CC90E003FCAA7 /* NewsTableRowController.swift */; };
29+
A1EA03961AD685D300AC6C9A /* Locations.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1EA03951AD685D300AC6C9A /* Locations.swift */; };
30+
A1EA03971AD685D300AC6C9A /* Locations.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1EA03951AD685D300AC6C9A /* Locations.swift */; };
2831
D245F3A01D42786A986E9C4A /* libPods-WatchKitExample WatchKit Extension.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F02DD82A59598445A1FBF192 /* libPods-WatchKitExample WatchKit Extension.a */; };
2932
/* End PBXBuildFile section */
3033

@@ -61,7 +64,10 @@
6164

6265
/* Begin PBXFileReference section */
6366
17263925E928AB8FC5D80922 /* Pods-WatchKitExample WatchKit Extension.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WatchKitExample WatchKit Extension.debug.xcconfig"; path = "Pods/Target Support Files/Pods-WatchKitExample WatchKit Extension/Pods-WatchKitExample WatchKit Extension.debug.xcconfig"; sourceTree = "<group>"; };
67+
338C55B3DEE699ED0DF018DF /* Pods-WatchKitExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WatchKitExample.release.xcconfig"; path = "Pods/Target Support Files/Pods-WatchKitExample/Pods-WatchKitExample.release.xcconfig"; sourceTree = "<group>"; };
6468
419CD544A8700BC4196F241F /* Pods-WatchKitExample WatchKit Extension.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WatchKitExample WatchKit Extension.release.xcconfig"; path = "Pods/Target Support Files/Pods-WatchKitExample WatchKit Extension/Pods-WatchKitExample WatchKit Extension.release.xcconfig"; sourceTree = "<group>"; };
69+
4E0E4C65D310517A201D49D3 /* Pods-WatchKitExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WatchKitExample.debug.xcconfig"; path = "Pods/Target Support Files/Pods-WatchKitExample/Pods-WatchKitExample.debug.xcconfig"; sourceTree = "<group>"; };
70+
9E9EFF9E5138A3E864A83B20 /* libPods-WatchKitExample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-WatchKitExample.a"; sourceTree = BUILT_PRODUCTS_DIR; };
6571
A16DED7D1A31D63B007F09E8 /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
6672
A171BC881A24AE2D0078E938 /* MapInterfaceController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapInterfaceController.swift; sourceTree = "<group>"; };
6773
A171BC8A1A24AED50078E938 /* ImageInterfaceController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageInterfaceController.swift; sourceTree = "<group>"; };
@@ -87,6 +93,8 @@
8793
A1CA0B091A1CE6FE00451409 /* DetailInterfaceController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DetailInterfaceController.swift; sourceTree = "<group>"; };
8894
A1DCEFF81A1CC90E003FCAA7 /* NewsTableRowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NewsTableRowController.swift; sourceTree = "<group>"; };
8995
A1DCEFFB1A1CCADF003FCAA7 /* BridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BridgingHeader.h; sourceTree = "<group>"; };
96+
A1EA03941AD67EC000AC6C9A /* BridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BridgingHeader.h; sourceTree = "<group>"; };
97+
A1EA03951AD685D300AC6C9A /* Locations.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Locations.swift; sourceTree = "<group>"; };
9098
F02DD82A59598445A1FBF192 /* libPods-WatchKitExample WatchKit Extension.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-WatchKitExample WatchKit Extension.a"; sourceTree = BUILT_PRODUCTS_DIR; };
9199
/* End PBXFileReference section */
92100

@@ -103,6 +111,7 @@
103111
buildActionMask = 2147483647;
104112
files = (
105113
A16DED7E1A31D63B007F09E8 /* MapKit.framework in Frameworks */,
114+
7BBF7B8973B4CC7CB8C35CB0 /* libPods-WatchKitExample.a in Frameworks */,
106115
);
107116
runOnlyForDeploymentPostprocessing = 0;
108117
};
@@ -123,6 +132,8 @@
123132
children = (
124133
17263925E928AB8FC5D80922 /* Pods-WatchKitExample WatchKit Extension.debug.xcconfig */,
125134
419CD544A8700BC4196F241F /* Pods-WatchKitExample WatchKit Extension.release.xcconfig */,
135+
4E0E4C65D310517A201D49D3 /* Pods-WatchKitExample.debug.xcconfig */,
136+
338C55B3DEE699ED0DF018DF /* Pods-WatchKitExample.release.xcconfig */,
126137
);
127138
name = Pods;
128139
sourceTree = "<group>";
@@ -153,9 +164,11 @@
153164
isa = PBXGroup;
154165
children = (
155166
A1992C5A1A1CA1F6005A48B8 /* AppDelegate.swift */,
167+
A1EA03941AD67EC000AC6C9A /* BridgingHeader.h */,
156168
A1992C611A1CA1F6005A48B8 /* Images.xcassets */,
157169
A1992C591A1CA1F6005A48B8 /* Info.plist */,
158170
A1992C631A1CA1F6005A48B8 /* LaunchScreen.xib */,
171+
A1EA03951AD685D300AC6C9A /* Locations.swift */,
159172
A1992C5E1A1CA1F6005A48B8 /* Main.storyboard */,
160173
A1992C5C1A1CA1F6005A48B8 /* ViewController.swift */,
161174
A179EA8A1A31C97F001155AE /* WatchKitExample.entitlements */,
@@ -169,6 +182,7 @@
169182
A16DED7D1A31D63B007F09E8 /* MapKit.framework */,
170183
A1992C801A1CA20F005A48B8 /* WatchKit.framework */,
171184
F02DD82A59598445A1FBF192 /* libPods-WatchKitExample WatchKit Extension.a */,
185+
9E9EFF9E5138A3E864A83B20 /* libPods-WatchKitExample.a */,
172186
);
173187
name = Frameworks;
174188
sourceTree = "<group>";
@@ -207,10 +221,12 @@
207221
isa = PBXNativeTarget;
208222
buildConfigurationList = A1992C741A1CA1F6005A48B8 /* Build configuration list for PBXNativeTarget "WatchKitExample" */;
209223
buildPhases = (
224+
CADE691FCD7B51B30B7C31A6 /* Check Pods Manifest.lock */,
210225
A1992C511A1CA1F6005A48B8 /* Sources */,
211226
A1992C521A1CA1F6005A48B8 /* Frameworks */,
212227
A1992C531A1CA1F6005A48B8 /* Resources */,
213228
A1992C9F1A1CA20F005A48B8 /* Embed App Extensions */,
229+
450E9D246791D92AC26C8F95 /* Copy Pods Resources */,
214230
);
215231
buildRules = (
216232
);
@@ -342,6 +358,21 @@
342358
/* End PBXResourcesBuildPhase section */
343359

344360
/* Begin PBXShellScriptBuildPhase section */
361+
450E9D246791D92AC26C8F95 /* Copy Pods Resources */ = {
362+
isa = PBXShellScriptBuildPhase;
363+
buildActionMask = 2147483647;
364+
files = (
365+
);
366+
inputPaths = (
367+
);
368+
name = "Copy Pods Resources";
369+
outputPaths = (
370+
);
371+
runOnlyForDeploymentPostprocessing = 0;
372+
shellPath = /bin/sh;
373+
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-WatchKitExample/Pods-WatchKitExample-resources.sh\"\n";
374+
showEnvVarsInLog = 0;
375+
};
345376
516619165BA4E455CC6C7B23 /* Check Pods Manifest.lock */ = {
346377
isa = PBXShellScriptBuildPhase;
347378
buildActionMask = 2147483647;
@@ -357,6 +388,21 @@
357388
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
358389
showEnvVarsInLog = 0;
359390
};
391+
CADE691FCD7B51B30B7C31A6 /* Check Pods Manifest.lock */ = {
392+
isa = PBXShellScriptBuildPhase;
393+
buildActionMask = 2147483647;
394+
files = (
395+
);
396+
inputPaths = (
397+
);
398+
name = "Check Pods Manifest.lock";
399+
outputPaths = (
400+
);
401+
runOnlyForDeploymentPostprocessing = 0;
402+
shellPath = /bin/sh;
403+
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
404+
showEnvVarsInLog = 0;
405+
};
360406
EADA10A0F53FDF82E47899A9 /* Copy Pods Resources */ = {
361407
isa = PBXShellScriptBuildPhase;
362408
buildActionMask = 2147483647;
@@ -379,6 +425,7 @@
379425
isa = PBXSourcesBuildPhase;
380426
buildActionMask = 2147483647;
381427
files = (
428+
A1EA03961AD685D300AC6C9A /* Locations.swift in Sources */,
382429
A1992C5D1A1CA1F6005A48B8 /* ViewController.swift in Sources */,
383430
A1992C5B1A1CA1F6005A48B8 /* AppDelegate.swift in Sources */,
384431
);
@@ -391,6 +438,7 @@
391438
A1CA0B0B1A1CE70600451409 /* DetailInterfaceController.swift in Sources */,
392439
A171BC8D1A24AF530078E938 /* MapInterfaceController.swift in Sources */,
393440
A171BC8C1A24AF4D0078E938 /* ImageInterfaceController.swift in Sources */,
441+
A1EA03971AD685D300AC6C9A /* Locations.swift in Sources */,
394442
A1DCEFFA1A1CC9FC003FCAA7 /* NewsTableRowController.swift in Sources */,
395443
A171BC901A24B1120078E938 /* BaseInterfaceController.swift in Sources */,
396444
A1992C861A1CA20F005A48B8 /* InterfaceController.swift in Sources */,
@@ -511,6 +559,7 @@
511559
};
512560
A1992C751A1CA1F6005A48B8 /* Debug */ = {
513561
isa = XCBuildConfiguration;
562+
baseConfigurationReference = 4E0E4C65D310517A201D49D3 /* Pods-WatchKitExample.debug.xcconfig */;
514563
buildSettings = {
515564
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
516565
CODE_SIGN_ENTITLEMENTS = "Main App/WatchKitExample.entitlements";
@@ -521,11 +570,13 @@
521570
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
522571
PRODUCT_NAME = "$(TARGET_NAME)";
523572
PROVISIONING_PROFILE = "";
573+
SWIFT_OBJC_BRIDGING_HEADER = "Main App/BridgingHeader.h";
524574
};
525575
name = Debug;
526576
};
527577
A1992C761A1CA1F6005A48B8 /* Release */ = {
528578
isa = XCBuildConfiguration;
579+
baseConfigurationReference = 338C55B3DEE699ED0DF018DF /* Pods-WatchKitExample.release.xcconfig */;
529580
buildSettings = {
530581
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
531582
CODE_SIGN_ENTITLEMENTS = "Main App/WatchKitExample.entitlements";
@@ -536,6 +587,7 @@
536587
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
537588
PRODUCT_NAME = "$(TARGET_NAME)";
538589
PROVISIONING_PROFILE = "";
590+
SWIFT_OBJC_BRIDGING_HEADER = "Main App/BridgingHeader.h";
539591
};
540592
name = Release;
541593
};

0 commit comments

Comments
 (0)