Skip to content

Commit

Permalink
PotLoc: Version 1.1, 2016-03-21
Browse files Browse the repository at this point in the history
Adopt Swift 2.2 language changes.
  • Loading branch information
Lax committed May 15, 2016
1 parent df4e0b9 commit 5f1884e
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 130 deletions.
2 changes: 1 addition & 1 deletion PotLoc/Common/PotlocConstants.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information

Abstract:
Expand Down
4 changes: 2 additions & 2 deletions PotLoc/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Sample code project: PotLoc: CoreLocation with iPhone and Apple Watch
Version: 1.0
Version: 1.1

IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
Expand Down Expand Up @@ -39,4 +39,4 @@ AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Copyright (C) 2015 Apple Inc. All Rights Reserved.
Copyright (C) 2016 Apple Inc. All Rights Reserved.
8 changes: 3 additions & 5 deletions PotLoc/Potloc WatchKit App/Base.lproj/Interface.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="8121.17" systemVersion="15A178t" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="AgC-eL-Hgc">
<document type="com.apple.InterfaceBuilder.WatchKit.Storyboard" version="3.0" toolsVersion="8152.3" systemVersion="15A214" targetRuntime="watchKit" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="AgC-eL-Hgc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8101.14"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="8066.14"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8124.4"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBWatchKitPlugin" version="8077.2"/>
</dependencies>
<scenes>
<!--Potloc-->
Expand Down Expand Up @@ -43,15 +43,13 @@
<action selector="requestLocation:" destination="vKN-QW-QSX" id="Sub-nD-iv4"/>
</connections>
</button>
<timer alignment="left" id="qpm-Zg-mhf"/>
<separator alignment="left" id="vK1-9E-kfS"/>
<label alignment="left" text="Label" id="jpF-sQ-sjD"/>
<label alignment="left" text="Label" id="Xry-1x-JDh"/>
<separator alignment="left" id="x6Z-ra-Hm7"/>
<label alignment="left" text="Label" numberOfLines="0" id="WTm-mB-7Kd"/>
</items>
<connections>
<outlet property="displayTimer" destination="qpm-Zg-mhf" id="Xzb-eh-U9S"/>
<outlet property="errorLabel" destination="WTm-mB-7Kd" id="RXk-5s-CYR"/>
<outlet property="latitudeLabel" destination="jpF-sQ-sjD" id="IZU-CZ-aIh"/>
<outlet property="longitudeLabel" destination="Xry-1x-JDh" id="5TX-s5-9RD"/>
Expand Down
2 changes: 1 addition & 1 deletion PotLoc/Potloc WatchKit Extension/ExtensionDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information

Abstract:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information

Abstract:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information

*/
Expand All @@ -20,12 +20,6 @@ import Foundation
*/
class RequestLocationInterfaceController: WKInterfaceController, CLLocationManagerDelegate {
// MARK: Properties

/**
When this timer times out, the labels in the interface reset to a default
state that does not resemble a requestLocation result.
*/
var interfaceResetTimer = NSTimer()

/// Location manager to request authorization and location updates.
let manager = CLLocationManager()
Expand All @@ -36,9 +30,6 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
/// Button to request location. Also allows cancelling the location request.
@IBOutlet var requestLocationButton: WKInterfaceButton!

/// Timer to count down 5 seconds as a visual cue that the interface will reset.
@IBOutlet var displayTimer: WKInterfaceTimer!

/// Label to display the most recent location's latitude.
@IBOutlet var latitudeLabel: WKInterfaceLabel!

Expand Down Expand Up @@ -70,29 +61,19 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
return NSLocalizedString("Unexpected authorization status.", comment: "Text to indicate authorization status is an unexpected value")
}

var latitudeResetText: String {
return NSLocalizedString("<latitude reset>", comment: "String indicating that no latitude is shown to the user due to a timer reset")
}

var longitudeResetText: String {
return NSLocalizedString("<longitude reset>", comment: "String indicating that no longitude is shown to the user due to a timer reset")
}

var errorResetText: String {
return NSLocalizedString("<no error>", comment: "String indicating that no error is shown to the user")
}

// MARK: Interface Controller

override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)

self.setTitle(interfaceTitle)
setTitle(interfaceTitle)

// Remember to set the location manager's delegate.
manager.delegate = self

resetInterface()

latitudeLabel.setAlpha(0)
longitudeLabel.setAlpha(0)
errorLabel.setAlpha(0)
}

/// MARK - Button Actions
Expand Down Expand Up @@ -127,12 +108,14 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
manager.requestLocation()

case .Denied:
errorLabel.setAlpha(1)
errorLabel.setText(deniedText)
restartTimers()
simulateFadeOut(errorLabel)

default:
errorLabel.setAlpha(1)
errorLabel.setText(unexpectedText)
restartTimers()
simulateFadeOut(errorLabel)
}
}

Expand All @@ -142,21 +125,27 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
When the location manager receives new locations, display the latitude and
longitude of the latest location and restart the timers.
*/
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard !locations.isEmpty else { return }

dispatch_async(dispatch_get_main_queue()) {
let lastLocationCoordinate = locations.last!.coordinate!

let lastLocationCoordinate = locations.last!.coordinate
self.latitudeLabel.setText(String(lastLocationCoordinate.latitude))

self.longitudeLabel.setText(String(lastLocationCoordinate.longitude))

self.latitudeLabel.setAlpha(1)

self.longitudeLabel.setAlpha(1)

self.isRequestingLocation = false

self.requestLocationButton.setTitle(self.requestLocationTitle)

self.restartTimers()
self.simulateFadeOut(self.latitudeLabel)

self.simulateFadeOut(self.longitudeLabel)
}
}

Expand All @@ -166,13 +155,15 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
*/
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
dispatch_async(dispatch_get_main_queue()) {
self.errorLabel.setAlpha(1)

self.errorLabel.setText(String(error.localizedDescription))

self.isRequestingLocation = false

self.requestLocationButton.setTitle(self.requestLocationTitle)

self.restartTimers()
self.simulateFadeOut(self.errorLabel)
}
}

Expand All @@ -189,63 +180,39 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
manager.requestLocation()

case .Denied:
self.errorLabel.setAlpha(1)
self.errorLabel.setText(self.deniedText)
self.isRequestingLocation = false
self.requestLocationButton.setTitle(self.requestLocationTitle)
self.restartTimers()
self.simulateFadeOut(self.errorLabel)

default:
self.errorLabel.setAlpha(1)
self.errorLabel.setText(self.unexpectedText)
self.isRequestingLocation = false
self.requestLocationButton.setTitle(self.requestLocationTitle)
self.restartTimers()
self.simulateFadeOut(self.errorLabel)
}
}
}

/// MARK - Resetting

/**
Resets the text labels in the interface to empty labels.

This method is useful for cleaning the interface to ensure that data
displayed to the user is not stale.
Simulates fading out animation by setting the alpha of the given label to
progressively smaller numbers.
*/
func resetInterface() {
dispatch_async(dispatch_get_main_queue()) {
self.stopDisplayTimer()

self.latitudeLabel.setText(self.latitudeResetText)

self.longitudeLabel.setText(self.longitudeResetText)

self.errorLabel.setText(self.errorResetText)
}
}

/**
Restarts the NSTimer and the WKInterface timer by stopping / invalidating
them, then starting them with a 5 second timeout.
*/
func restartTimers() {
stopDisplayTimer()

interfaceResetTimer.invalidate()

interfaceResetTimer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "resetInterface", userInfo: [:], repeats: false)

let fiveSecondDelay = NSDate(timeIntervalSinceNow: 5)
func simulateFadeOut(label: WKInterfaceLabel) {
let mainQueue = dispatch_get_main_queue()

displayTimer.setDate(fiveSecondDelay)

displayTimer.start()
}

/// Stops the display timer.
func stopDisplayTimer() {
let now = NSDate()
displayTimer.setDate(now)
for index in 1...10 {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(index) / 10.0 * Double(NSEC_PER_SEC)))

dispatch_after(time, mainQueue) {
let alphaAmount = CGFloat(1 - (0.1 * Float(index)))

displayTimer.stop()
label.setAlpha(alphaAmount)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information

*/
Expand Down Expand Up @@ -83,7 +83,7 @@ class StreamLocationInterfaceController: WKInterfaceController, WCSessionDelegat
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)

self.setTitle(interfaceTitle)
setTitle(interfaceTitle)
locationsReeivedOnPhoneCountTitleLabel.setText(locationsReceivedText)

// Initialize the `WCSession`.
Expand Down
6 changes: 6 additions & 0 deletions PotLoc/Potloc.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,14 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Developer";
EMBEDDED_CONTENT_CONTAINS_SWIFT = YES;
IBSC_MODULE = Potloc_WatchKit_Extension;
INFOPLIST_FILE = "Potloc WatchKit App/Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "com.example.apple-samplecode.Potloc.nativewatchkitapp";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SDKROOT = watchos;
SKIP_INSTALL = YES;
TARGETED_DEVICE_FAMILY = 4;
Expand All @@ -511,12 +513,14 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "iPhone Developer";
EMBEDDED_CONTENT_CONTAINS_SWIFT = YES;
IBSC_MODULE = Potloc_WatchKit_Extension;
INFOPLIST_FILE = "Potloc WatchKit App/Info.plist";
PRODUCT_BUNDLE_IDENTIFIER = "com.example.apple-samplecode.Potloc.nativewatchkitapp";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SDKROOT = watchos;
SKIP_INSTALL = YES;
TARGETED_DEVICE_FAMILY = 4;
Expand All @@ -528,6 +532,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
INFOPLIST_FILE = Potloc/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.example.apple-samplecode.Potloc";
Expand All @@ -539,6 +544,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
INFOPLIST_FILE = Potloc/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.example.apple-samplecode.Potloc";
Expand Down
2 changes: 1 addition & 1 deletion PotLoc/Potloc/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information

Abstract:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
"idiom" : "ipad",
"filename" : "Potloc76x2.png",
"scale" : "2x"
},
{
"size" : "83.5x83.5",
"idiom" : "ipad",
"filename" : "Potloc83.5x2.png",
"scale" : "2x"
}
],
"info" : {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions PotLoc/Potloc/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8121.17" systemVersion="15A177" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8121.20" systemVersion="15A210a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8101.13"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8101.16"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand Down
Loading

0 comments on commit 5f1884e

Please sign in to comment.