Commit fac9f892 authored by Romain Bertozzi's avatar Romain Bertozzi Committed by Romain B.

swift: migrate code to Swift 3

This patch takes care of migrating the existing base code to Swift 3.

Tuleap: #1327
Change-Id: Ibbb148e92d0477cda155a93a6459de90d57ae70d
parent 70d602f3
......@@ -521,13 +521,16 @@
043999F21D1C2D9D00E99CD9 = {
CreatedOnToolsVersion = 7.3.1;
DevelopmentTeam = KM95526DS8;
LastSwiftMigration = 0810;
};
04399A0B1D1C2D9D00E99CD9 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0810;
TestTargetID = 043999F21D1C2D9D00E99CD9;
};
04399A161D1C2D9D00E99CD9 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0810;
TestTargetID = 043999F21D1C2D9D00E99CD9;
};
};
......@@ -752,7 +755,7 @@
PROVISIONING_PROFILE = "";
SWIFT_OBJC_BRIDGING_HEADER = "Ring/Ring-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
......@@ -773,7 +776,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SWIFT_OBJC_BRIDGING_HEADER = "Ring/Ring-Bridging-Header.h";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
......@@ -785,6 +788,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = cx.ring.RingTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Ring.app/Ring";
};
name = Debug;
......@@ -797,6 +801,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = cx.ring.RingTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Ring.app/Ring";
};
name = Release;
......@@ -808,6 +813,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = cx.ring.RingUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_TARGET_NAME = Ring;
};
name = Debug;
......@@ -819,6 +825,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = cx.ring.RingUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_TARGET_NAME = Ring;
};
name = Release;
......
......@@ -110,7 +110,7 @@ struct Account {
}
set {
details["Account.enable"] = newValue.toString()
ConfigurationManagerAdaptator.sharedManager().setAccountActive(self.id, active: newValue)
(ConfigurationManagerAdaptator.sharedManager() as AnyObject).setAccountActive(self.id, active: newValue)
}
}
......@@ -153,10 +153,10 @@ struct Account {
// MARK: - Init
init(accID: String) {
id = accID
details = ConfigurationManagerAdaptator.sharedManager().getAccountDetails(id) as! Dictionary<String, String>
details = (ConfigurationManagerAdaptator.sharedManager() as AnyObject).getAccountDetails(id) as! Dictionary<String, String>
}
func save() {
ConfigurationManagerAdaptator.sharedManager().setAccountDetails(id, details: details)
(ConfigurationManagerAdaptator.sharedManager() as AnyObject).setAccountDetails(id, details: details)
}
}
\ No newline at end of file
}
......@@ -23,14 +23,14 @@ import Foundation
class AccountModel {
// MARK: - Properties
let confAdapt = ConfigurationManagerAdaptator.sharedManager()
let confAdapt = ConfigurationManagerAdaptator.sharedManager() as AnyObject
var accountList: Array<Account> = []
// MARK: - Singleton
static let sharedInstance = AccountModel()
private init() {
NSNotificationCenter.defaultCenter().addObserverForName("AccountsChanged", object: nil, queue: nil, usingBlock: { _ in
fileprivate init() {
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "AccountsChanged"), object: nil, queue: nil, using: { _ in
self.reload()
})
}
......@@ -45,18 +45,23 @@ class AccountModel {
}
func addAccount() {
// TODO: This need work for all account type
let details = confAdapt.getAccountTemplate("RING")
details.setValue("iOS", forKey: "Account.alias")
details.setValue("iOS", forKey: "Account.displayName")
confAdapt.addAccount(details! as [NSObject: AnyObject])
let details:NSMutableDictionary? = confAdapt.getAccountTemplate("RING")
if details == nil {
print("Error retrieving Ring account template, can not continue");
return;
}
details!.setValue("iOS", forKey: "Account.alias")
details!.setValue("iOS", forKey: "Account.displayName")
let convertedDetails = details as NSDictionary? as? [AnyHashable: Any] ?? [:]
let addResult:String! = confAdapt.addAccount(convertedDetails)
print(addResult);
}
func removeAccount(row: Int) {
func removeAccount(_ row: Int) {
if row < accountList.count {
confAdapt.removeAccount(accountList[row].id)
}
}
}
\ No newline at end of file
}
......@@ -34,12 +34,12 @@ class AccountTableViewCell: UITableViewCell {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
// MARK: - Actions
@IBAction func switchAccountState(sender: UISwitch) {
account.isEnabled = sender.on
@IBAction func switchAccountState(_ sender: UISwitch) {
account.isEnabled = sender.isOn
}
}
......@@ -27,10 +27,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let dRingAdapt = DRingAdaptator()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if (dRingAdapt.initDaemon() == true) {
if (dRingAdapt.startDaemon() == true) {
NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: #selector(AppDelegate.pollFunction), userInfo: nil, repeats: true)
Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector(AppDelegate.pollFunction), userInfo: nil, repeats: true)
AccountModel.sharedInstance.reload()
}
}
......@@ -41,56 +41,56 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
self.dRingAdapt.pollEvents()
}
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) {
self.saveContext()
self.dRingAdapt.fini()
}
// MARK: - Core Data stack
lazy var applicationDocumentsDirectory: NSURL = {
lazy var applicationDocumentsDirectory: URL = {
// The directory the application uses to store the Core Data store file. This code uses a directory named "cx.ring.Ring" in the application's documents Application Support directory.
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[urls.count - 1]
}()
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = NSBundle.mainBundle().URLForResource("Ring", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
let modelURL = Bundle.main.url(forResource: "Ring", withExtension: "momd")!
return NSManagedObjectModel(contentsOf: modelURL)!
}()
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
let url = self.applicationDocumentsDirectory.appendingPathComponent("SingleViewCoreData.sqlite")
var failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
} catch {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
dict[NSUnderlyingErrorKey] = error as NSError
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
......@@ -106,7 +106,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
lazy var managedObjectContext: NSManagedObjectContext = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
......
......@@ -46,9 +46,9 @@ class MeViewController: UIViewController, UITableViewDelegate, UITableViewDataSo
}
// MARK: - QRCode
func createQRFromString(str: String) {
func createQRFromString(_ str: String) {
let data = str.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)
let data = str.data(using: String.Encoding.isoLatin1, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter!.setValue(data, forKey: "inputMessage")
......@@ -58,23 +58,23 @@ class MeViewController: UIViewController, UITableViewDelegate, UITableViewDataSo
let scaleX = qrImageView.frame.size.width / qrImage.extent.size.width
let scaleY = qrImageView.frame.size.height / qrImage.extent.size.height
let resultQrImage = qrImage.imageByApplyingTransform(CGAffineTransformMakeScale(scaleX, scaleY))
qrImageView.image = UIImage(CIImage: resultQrImage)
let resultQrImage = qrImage.applying(CGAffineTransform(scaleX: scaleX, y: scaleY))
qrImageView.image = UIImage(ciImage: resultQrImage)
}
// MARK: - TableView
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return accountModel.accountList.count + 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < accountModel.accountList.count {
let cell = tableView.dequeueReusableCellWithIdentifier("accountTableCell", forIndexPath: indexPath) as! AccountTableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "accountTableCell", for: indexPath) as! AccountTableViewCell
let account = accountModel.accountList[indexPath.row]
cell.account = account
......@@ -84,45 +84,45 @@ class MeViewController: UIViewController, UITableViewDelegate, UITableViewDataSo
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("addAccountTableCell", forIndexPath: indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: "addAccountTableCell", for: indexPath)
return cell
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == accountModel.accountList.count {
accountModel.addAccount()
accountTableView.reloadData()
}
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.row == accountModel.accountList.count {
return false
}
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == UITableViewCellEditingStyle.delete) {
accountModel.removeAccount(indexPath.row)
accountTableView.reloadData()
}
}
// MARK: - Actions
@IBAction func addAccountClicked(sender: AnyObject) {
let index = NSIndexPath(forRow: accountModel.accountList.count, inSection: 0)
accountTableView.selectRowAtIndexPath(index, animated: false, scrollPosition: UITableViewScrollPosition.None)
tableView(accountTableView, didSelectRowAtIndexPath: index)
@IBAction func addAccountClicked(_ sender: AnyObject) {
let index = IndexPath(row: accountModel.accountList.count, section: 0)
accountTableView.selectRow(at: index, animated: false, scrollPosition: UITableViewScrollPosition.none)
tableView(accountTableView, didSelectRowAt: index)
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "accountDetails" {
let cell = sender as! AccountTableViewCell
let vc = segue.destinationViewController as! AccountDetailsViewController
let vc = segue.destination as! AccountDetailsViewController
vc.account = cell.account
}
}
......
......@@ -10,27 +10,27 @@ import XCTest
@testable import Ring
class RingTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
self.measure {
// Put the code you want to measure the time of here.
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment