Skip to content

Commit

Permalink
Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmarcos committed Sep 20, 2016
1 parent bfaa7dd commit a4006bf
Show file tree
Hide file tree
Showing 48 changed files with 1,562 additions and 1,371 deletions.
9 changes: 9 additions & 0 deletions Arcohol.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,16 @@
TargetAttributes = {
9F15219B1D64B44C00B264B0 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0800;
};
9F1521AF1D64B44C00B264B0 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0800;
TestTargetID = 9F15219B1D64B44C00B264B0;
};
9F1521BA1D64B44C00B264B0 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0800;
TestTargetID = 9F15219B1D64B44C00B264B0;
};
};
Expand Down Expand Up @@ -702,6 +705,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = manuel.marcos.Arcohol;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 2;
};
name = Debug;
Expand All @@ -716,6 +720,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = manuel.marcos.Arcohol;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 2;
};
name = Release;
Expand All @@ -728,6 +733,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = manuel.marcos.ArcoholTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Arcohol.app/Arcohol";
};
name = Debug;
Expand All @@ -740,6 +746,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = manuel.marcos.ArcoholTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Arcohol.app/Arcohol";
};
name = Release;
Expand All @@ -751,6 +758,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = manuel.marcos.ArcoholUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_TARGET_NAME = Arcohol;
};
name = Debug;
Expand All @@ -762,6 +770,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = manuel.marcos.ArcoholUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_TARGET_NAME = Arcohol;
};
name = Release;
Expand Down
12 changes: 6 additions & 6 deletions Arcohol/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
10 changes: 5 additions & 5 deletions Arcohol/BaseCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import Foundation
import UIKit

protocol ContainterViewControllerProtocol {
func didSelectCategory(wineArray: [Wine])
func didSelectWine(row: Int)
func didSelectCategory(_ wineArray: [Wine])
func didSelectWine(_ row: Int)
}

class BaseCollectionViewController: UICollectionViewController {
var delegate: ContainterViewControllerProtocol?
private let reuseBottomIdentifier = "BottomCollectionViewCell"
fileprivate let reuseBottomIdentifier = "BottomCollectionViewCell"

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
self.collectionView!.collectionViewLayout.invalidateLayout()
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
return CGSize(width: self.collectionView!.frame.height, height: self.collectionView!.frame.height)
}
}
30 changes: 15 additions & 15 deletions Arcohol/BottomCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@ import Foundation
import UIKit

class BottomCollectionViewController: BaseCollectionViewController {
private let reuseBottomIdentifier = "BottomCollectionViewCell"
fileprivate let reuseBottomIdentifier = "BottomCollectionViewCell"
var array: [Wine] = [] {
didSet {
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
DispatchQueue.main.async { [unowned self] in
self.collectionView?.reloadData()
// Scroll to the beginning
let indexPath = NSIndexPath(forItem:0, inSection: 0)
self.collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
let indexPath = IndexPath(item:0, section: 0)
self.collectionView?.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: true)
}
}
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = self.collectionView!.dequeueReusableCellWithReuseIdentifier(reuseBottomIdentifier, forIndexPath: indexPath) as! BottomCollectionViewCell
cell.labelName.text = array[indexPath.row].wineName
cell.labelColour.text = array[indexPath.row].wineColour
cell.labelCountry.text = array[indexPath.row].wineCountryName
cell.labelTastingNotes.text = array[indexPath.row].wineNotes
let image = UIImage(named: array[indexPath.row].wineImageName())
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = self.collectionView!.dequeueReusableCell(withReuseIdentifier: reuseBottomIdentifier, for: indexPath) as! BottomCollectionViewCell
cell.labelName.text = array[(indexPath as NSIndexPath).row].wineName
cell.labelColour.text = array[(indexPath as NSIndexPath).row].wineColour
cell.labelCountry.text = array[(indexPath as NSIndexPath).row].wineCountryName
cell.labelTastingNotes.text = array[(indexPath as NSIndexPath).row].wineNotes
let image = UIImage(named: array[(indexPath as NSIndexPath).row].wineImageName())
cell.imageView.image = image
return cell
}

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.delegate?.didSelectWine(indexPath.row)
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.delegate?.didSelectWine((indexPath as NSIndexPath).row)
}
}
8 changes: 4 additions & 4 deletions Arcohol/Category.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import UIKit

class Category: UIView {
@IBOutlet private var contentView: UIView?
@IBOutlet fileprivate var contentView: UIView?
@IBOutlet var imageView: UIImageView?
@IBOutlet var labelName: UILabel?
var name: String? {
Expand All @@ -32,12 +32,12 @@ class Category: UIView {
self.commonInit()
}

private func commonInit() {
fileprivate func commonInit() {
self.translatesAutoresizingMaskIntoConstraints = false
NSBundle.mainBundle().loadNibNamed("Category", owner: self, options: nil)
Bundle.main.loadNibNamed("Category", owner: self, options: nil)
guard let content = contentView else { return }
content.frame = self.bounds
content.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]
content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.addSubview(content)
}
}
23 changes: 11 additions & 12 deletions Arcohol/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,39 @@
//

import Foundation
import SocketIOClientSwift

import SocketIO
// TODO: Fix and Create the network layer

class NetworkManager {

let socket = SocketIOClient(socketURL: NSURL(string: Constants.EndPoints.raspberryPiEndPoint)!, options: [.Log(true), .ForcePolling(true)])
let socket = SocketIOClient(socketURL: URL(string: Constants.EndPoints.raspberryPiEndPoint)!)

private func connectSocket(completionHandler:(connected: Bool) -> ()) {
fileprivate func connectSocket(_ completionHandler:@escaping (_ connected: Bool) -> ()) {

if self.socket.status == SocketIOClientStatus.Connected {
completionHandler(connected: true)
if self.socket.status == SocketIOClientStatus.connected {
completionHandler(true)
}
self.socket.reconnects = false
socket.on("error") {data in
completionHandler(connected: false)
completionHandler(false)
}
self.socket.on("control") {data, ack in
completionHandler(connected: true)
completionHandler(true)
}
self.socket.connect()
}

private func emit(segmentsArray: [Int]) {
fileprivate func emit(_ segmentsArray: [Int]) {
self.socket.emit("control", ["segmentSet": segmentsArray])
}

func emitToSocket(segmentsArray: [Int], completionHandler:(success: Bool) -> ()) {
func emitToSocket(_ segmentsArray: [Int], completionHandler:@escaping (_ success: Bool) -> ()) {
self.connectSocket { (connected) in
if connected {
self.emit(segmentsArray)
completionHandler(success: true)
completionHandler(true)
} else {
completionHandler(success: false)
completionHandler(false)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions Arcohol/Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import Foundation
import UIKit

class Test: UIView {
@IBOutlet private var contentView: UIView?
@IBOutlet private var textField: UITextField?
@IBOutlet fileprivate var contentView: UIView?
@IBOutlet fileprivate var textField: UITextField?

// for using CustomView in code
override init(frame: CGRect) {
Expand All @@ -25,16 +25,16 @@ class Test: UIView {
self.commonInit()
}

private func commonInit() {
fileprivate func commonInit() {
self.translatesAutoresizingMaskIntoConstraints = false
NSBundle.mainBundle().loadNibNamed("Test", owner: self, options: nil)
Bundle.main.loadNibNamed("Test", owner: self, options: nil)
guard let content = contentView else { return }
content.frame = self.bounds
content.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]
content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.addSubview(content)
}

@IBAction func test(sender: AnyObject) {
@IBAction func test(_ sender: AnyObject) {
NetworkManager.sharedInstance.emitToSocket([Int((self.textField?.text)!)!]) { (success) in
if !success {
print("Error")
Expand Down
46 changes: 23 additions & 23 deletions Arcohol/TopCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,56 @@ import Foundation
import UIKit

class TopCollectionViewController: BaseCollectionViewController {
private let reuseTopIdentifier = "TopCollectionViewCell"
fileprivate let reuseTopIdentifier = "TopCollectionViewCell"
var array: [WineCategory] = [] {
didSet {
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
DispatchQueue.main.async { [unowned self] in
self.collectionView?.reloadData()
}
}
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = self.collectionView!.dequeueReusableCellWithReuseIdentifier(reuseTopIdentifier, forIndexPath: indexPath) as! TopCollectionViewCell
cell.label.text = array[indexPath.row].wineCategoryName
let image = UIImage(named: cell.selected ? array[indexPath.row].wineCategoryImageNameHighlight() : array[indexPath.row].wineCategoryImageNameNormal())
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = self.collectionView!.dequeueReusableCell(withReuseIdentifier: reuseTopIdentifier, for: indexPath) as! TopCollectionViewCell
cell.label.text = array[(indexPath as NSIndexPath).row].wineCategoryName
let image = UIImage(named: cell.isSelected ? array[(indexPath as NSIndexPath).row].wineCategoryImageNameHighlight() : array[(indexPath as NSIndexPath).row].wineCategoryImageNameNormal())
cell.imageView.image = image
return cell
}

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.delegate?.didSelectCategory(array[indexPath.row].winesArray)
if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? TopCollectionViewCell {
self.categoryCellSelected(true, cell: cell, index: indexPath.row)
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.delegate?.didSelectCategory(array[(indexPath as NSIndexPath).row].winesArray)
if let cell = collectionView.cellForItem(at: indexPath) as? TopCollectionViewCell {
self.categoryCellSelected(true, cell: cell, index: (indexPath as NSIndexPath).row)
}
}

override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? TopCollectionViewCell {
self.categoryCellSelected(false, cell: cell, index: indexPath.row)
override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? TopCollectionViewCell {
self.categoryCellSelected(false, cell: cell, index: (indexPath as NSIndexPath).row)
}
}

func findItemRowForName(name: String) {
if let index = array.indexOf({$0.wineCategoryName == name}) {
let indexPath = NSIndexPath(forItem:index, inSection: 0)
self.collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem:index, inSection: 0), atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
if let cell = self.collectionView?.cellForItemAtIndexPath(indexPath) as? TopCollectionViewCell {
self.categoryCellSelected(true, cell: cell, index: indexPath.row)
self.delegate?.didSelectCategory(array[indexPath.row].winesArray)
func findItemRowForName(_ name: String) {
if let index = array.index(where: {$0.wineCategoryName == name}) {
let indexPath = IndexPath(item:index, section: 0)
self.collectionView?.scrollToItem(at: IndexPath(item:index, section: 0), at: UICollectionViewScrollPosition.left, animated: true)
if let cell = self.collectionView?.cellForItem(at: indexPath) as? TopCollectionViewCell {
self.categoryCellSelected(true, cell: cell, index: (indexPath as NSIndexPath).row)
self.delegate?.didSelectCategory(array[(indexPath as NSIndexPath).row].winesArray)
}
}
}

func categoryCellSelected(selected: Bool, cell: TopCollectionViewCell, index: Int) {
func categoryCellSelected(_ selected: Bool, cell: TopCollectionViewCell, index: Int) {
cell.imageView.image = UIImage(named: selected ? array[index].wineCategoryImageNameHighlight() : array[index].wineCategoryImageNameNormal())
}

Expand Down
Loading

0 comments on commit a4006bf

Please sign in to comment.