Skip to content

Commit

Permalink
Merge pull request #5 from carrot-ar/gn/example
Browse files Browse the repository at this point in the history
Example app
  • Loading branch information
gonzalonunez authored Nov 16, 2017
2 parents 50bc04d + 044f6a4 commit f3a582a
Show file tree
Hide file tree
Showing 31 changed files with 2,016 additions and 0 deletions.
520 changes: 520 additions & 0 deletions Example/ParrotExample.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Example/ParrotExample.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions Example/ParrotExample/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// AppDelegate.swift
// ParrotExample
//
// Created by Gonzalo Nunez on 11/16/17.
// Copyright © 2017 carrot. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
window = UIWindow(frame: UIScreen.main.bounds)
let vc = ViewController(nibName: nil, bundle: nil)
window?.rootViewController = vc
window?.makeKeyAndVisible()
return true
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
25 changes: 25 additions & 0 deletions Example/ParrotExample/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" systemVersion="17A277" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
47 changes: 47 additions & 0 deletions Example/ParrotExample/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>NSLocationWhenInUseUsageDescription</key>
<string>ParrotExample needs your location in order to monitor a CLBeaconRegion</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>ParrotExample needs your location in order to monitor a CLBeaconRegion</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
151 changes: 151 additions & 0 deletions Example/ParrotExample/ViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//
// ViewController.swift
// ParrotExample
//
// Created by Gonzalo Nunez on 11/16/17.
// Copyright © 2017 carrot. All rights reserved.
//

import UIKit
import Parrot

class ViewController: UIViewController {

// MARK: Lifecycle

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white

view.addSubview(rangeSwitch)
NSLayoutConstraint.activate([
rangeSwitch.centerXAnchor.constraint(equalTo: view.centerXAnchor),
rangeSwitch.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])

let shouldRangeLabel = UILabel(frame: .zero)
shouldRangeLabel.text = "Range"
shouldRangeLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(shouldRangeLabel)
NSLayoutConstraint.activate([
shouldRangeLabel.leftAnchor.constraint(equalTo: rangeSwitch.rightAnchor, constant: 8),
shouldRangeLabel.centerYAnchor.constraint(equalTo: rangeSwitch.centerYAnchor)
])

infoLabel.text = "Idle..."
infoLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(infoLabel)
NSLayoutConstraint.activate([
infoLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8),
infoLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])

view.addSubview(startButton)
NSLayoutConstraint.activate([
startButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
startButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16)
])
}

// MARK: Private

private var advertiser: BeaconAdvertiser?
private var monitor: BeaconMonitor?

private enum State {
case wantsMonitoring
case wantsAdvertising
case monitoring
case advertising
}

private var state: State = .wantsAdvertising {
didSet { handleStateChange(from: oldValue) }
}

lazy private var rangeSwitch: UISwitch = {
let uiSwitch = UISwitch(frame: .zero)
uiSwitch.translatesAutoresizingMaskIntoConstraints = false
uiSwitch.addTarget(self, action: #selector(handleSwitch), for: .valueChanged)
return uiSwitch
}()

lazy private var startButton: UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Start", for: .normal)
button.addTarget(self, action: #selector(handleStartButton), for: .touchUpInside)
return button
}()

private var infoLabel = UILabel(frame: .zero)

@objc private func handleSwitch(sender: Any) {
let shouldRange = rangeSwitch.isOn
state = shouldRange ? .wantsMonitoring : .wantsAdvertising
}

@objc private func handleStartButton(sender: Any) {
let uuid = UUID(uuidString: "CFC5128B-96FD-40FD-A4C4-D099F22E9E64")!
switch state {
case .wantsMonitoring:
monitor = BeaconMonitor(
uuid: uuid,
identifier: "com.ParrotExample.Beacon",
params: .none)
startButton.setTitle("Stop", for: .normal)
state = .monitoring
monitor?.startMonitoring(
onProximityUpdate: { [weak self] monitor, proximity in
switch proximity {
case .unknown:
self?.infoLabel.text = "Unknown..."
case .far:
self?.infoLabel.text = "Far..."
case .near:
self?.infoLabel.text = "Near..."
case .immediate:
self?.infoLabel.text = "Immediate!"
}
},
onError: { [weak self] error in
print("ERROR: \(error)")
self?.infoLabel.text = error.localizedDescription
}
)
case .wantsAdvertising:
advertiser = BeaconAdvertiser(
uuid: uuid,
identifier: "com.ParrotExample.Beacon",
params: .none)
startButton.setTitle("Stop", for: .normal)
state = .advertising
advertiser?.startAdvertising { [weak self] advertiser, state in
self?.infoLabel.text = "\(state)...".capitalized
}
case .monitoring:
startButton.setTitle("Start", for: .normal)
monitor?.stopMonitoring()
state = .wantsMonitoring
case .advertising:
startButton.setTitle("Start", for: .normal)
advertiser?.stopAdvertising()
state = .wantsAdvertising
}
}

private func handleStateChange(from previous: State) {
switch (state, previous) {
case (.wantsMonitoring, .wantsAdvertising):
advertiser = nil
startButton.setTitle("Start", for: .normal)
self.infoLabel.text = "Idle..."
case (.wantsAdvertising, .wantsMonitoring):
monitor = nil
startButton.setTitle("Start", for: .normal)
self.infoLabel.text = "Idle..."
default:
break
}
}
}
22 changes: 22 additions & 0 deletions Example/ParrotExampleTests/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
Loading

0 comments on commit f3a582a

Please sign in to comment.