Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Update ParseUIDemo-Swift to Swift 3. #263

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -3,7 +3,7 @@ branches:
- master
language: objective-c
os: osx
osx_image: xcode7.1
osx_image: xcode9.1
env:
global:
- LC_CTYPE=en_US.UTF-8
Expand Down
45 changes: 31 additions & 14 deletions ParseUIDemo/Swift/AppDelegate.swift
Expand Up @@ -30,25 +30,42 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

// MARK: UIApplicationDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

Parse.setApplicationId("UdNpOP2XFoEiXLZEBDl6xONmCMH8VjETmnEsl0xJ", clientKey: "wNJFho0fQaQFQ2Fe1x9b67lVBakJiAtFj1Uz30A9")
PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions);
PFTwitterUtils.initializeWithConsumerKey("3Q9hMEKqqSg4ie2pibZ2sVJuv", consumerSecret: "IEZ9wv2d1EpXNGFKGp7sAGdxRtyqtPwygyciFZwTHTGhPp4FMj")
PFFacebookUtils.initializeFacebook(applicationLaunchOptions: launchOptions);
PFTwitterUtils.initialize(withConsumerKey: "3Q9hMEKqqSg4ie2pibZ2sVJuv", consumerSecret: "IEZ9wv2d1EpXNGFKGp7sAGdxRtyqtPwygyciFZwTHTGhPp4FMj")

let configuration = ParseClientConfiguration {
// Add your Parse applicationId:
$0.applicationId = Configuration.parseParameters.kParseApplicationId
// Uncomment and add your clientKey (it's not required if you are using Parse Server):
$0.clientKey = Configuration.parseParameters.kParseClientKey
//$0.masterKey = Configuration.parseParameters.kParseMasterKey

// Uncomment the following line and change to your Parse Server address;
$0.server = Configuration.parseParameters.kParseServer

// Enable storing and querying data from Local Datastore.
// Remove this line if you don't want to use Local Datastore features or want to use cachePolicy.
$0.isLocalDatastoreEnabled = true
}
Parse.initialize(with: configuration)

window = UIWindow(frame: UIScreen.mainScreen().bounds)
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UINavigationController(rootViewController: UIDemoViewController())
window?.makeKeyAndVisible()

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
DispatchQueue.main.async {
self.setupTestData()
}

return true
}

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)
}

// MARK: Test Data
Expand All @@ -74,7 +91,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
do {
let todos = try PFQuery(className: "Todo").findObjects()
if todos.count == 0 {
for (index, title) in todoTitles.enumerate() {
for (index, title) in todoTitles.enumerated() {
let todo = PFObject(className: "Todo")
todo["title"] = title
todo["priority"] = index % 3
Expand All @@ -87,11 +104,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
do {
let apps = try PFQuery(className: "App").findObjects()
if apps.count == 0 {
for (index, appName) in appNames.enumerate() {
let bundle = NSBundle.mainBundle()
if let fileURL = bundle.URLForResource(String(index), withExtension: "png") {
if let data = NSData(contentsOfURL: fileURL) {
let file = PFFile(name: fileURL.lastPathComponent, data: data)
for (index, appName) in appNames.enumerated() {
let bundle = Bundle.main
if let fileURL = bundle.url(forResource: String(index), withExtension: "png") {
if let data : NSData = NSData(contentsOf: fileURL) {
let file = PFFile(name: fileURL.lastPathComponent, data: data as Data)
let object = PFObject(className: "App")
object["icon"] = file
object["name"] = appName
Expand Down
57 changes: 57 additions & 0 deletions ParseUIDemo/Swift/Configuration.swift
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2015, Parse, LLC. All rights reserved.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
* copy, modify, and distribute this software in source code or binary form for use
* in connection with the web services and APIs provided by Parse.
*
* As with any software that integrates with the Parse platform, your use of
* this software is subject to the Parse Terms of Service
* [https://www.parse.com/about/terms]. This copyright notice shall be
* included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

import UIKit

open class Configuration {

public struct parseParameters {
static let kParseApplicationId = "ParseUIDemo"
static let kParseMasterKey = ""
static let kParseClientKey = "unset"
static let kParseApiKey = "unset"
static let kParseServer = "http://localhost:1337/parse"
static let kParseAdminUserName = ""
static let kParseAdminPassword = ""
}

}

extension CGRect {
init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {
self.init(x:x, y:y, width:w, height:h)
}
}
extension CGSize {
init(_ width:CGFloat, _ height:CGFloat) {
self.init(width:width, height:height)
}
}
extension CGPoint {
init(_ x:CGFloat, _ y:CGFloat) {
self.init(x:x, y:y)
}
}
extension CGVector {
init (_ dx:CGFloat, _ dy:CGFloat) {
self.init(dx:dx, dy:dy)
}
}
Expand Up @@ -29,10 +29,10 @@ class CustomLogInViewController: PFLogInViewController {
override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = UIColor.blueColor()
view.backgroundColor = UIColor.blue

let label = UILabel()
label.textColor = UIColor.whiteColor()
label.textColor = UIColor.white
label.text = "All Custom!"
label.sizeToFit()
logInView?.logo = label
Expand Down
Expand Up @@ -26,16 +26,16 @@ import ParseUI

class CustomProductTableViewController: PFProductTableViewController {

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let product = objects?[indexPath.row]
if let identifier = product?["productIdentifier"] as? String where identifier == "Cooper" {
if let identifier = product?["productIdentifier"] as? String, identifier == "Cooper" {
PFPurchase.buyProduct(identifier) { error in
if error == nil {
UIAlertView(title: "Success!", message: "Yes!", delegate: nil, cancelButtonTitle: "OK").show()
}
}
}
super.tableView(tableView, didSelectRowAtIndexPath: indexPath)
super.tableView(tableView, didSelectRowAt: indexPath)
}

}
Expand Up @@ -29,8 +29,8 @@ class DeletionCollectionViewController: PFQueryCollectionViewController, UIAlert
collectionView?.allowsMultipleSelection = true

navigationItem.rightBarButtonItems = [
editButtonItem(),
UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addTodo")
editButtonItem,
UIBarButtonItem(barButtonSystemItem: .add, target: self, action: "addTodo")
]
}

Expand All @@ -39,17 +39,17 @@ class DeletionCollectionViewController: PFQueryCollectionViewController, UIAlert

if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
let bounds = UIEdgeInsetsInsetRect(view.bounds, layout.sectionInset)
let sideLength = min(CGRectGetWidth(bounds), CGRectGetHeight(bounds)) / 2.0 - layout.minimumInteritemSpacing
layout.itemSize = CGSizeMake(sideLength, sideLength)
let sideLength = min(bounds.size.width, bounds.size.height) / 2.0 - layout.minimumInteritemSpacing
layout.itemSize = CGSize(sideLength, sideLength)
}
}

override func setEditing(editing: Bool, animated: Bool) {
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)

if (editing) {
navigationItem.leftBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .Trash,
barButtonSystemItem: .trash,
target: self,
action: "deleteSelectedItems"
)
Expand All @@ -61,24 +61,24 @@ class DeletionCollectionViewController: PFQueryCollectionViewController, UIAlert
@objc
func addTodo() {
if #available(iOS 8.0, *) {
let alertDialog = UIAlertController(title: "Add Todo", message: nil, preferredStyle: .Alert)
let alertDialog = UIAlertController(title: "Add Todo", message: nil, preferredStyle: .alert)

var titleTextField : UITextField? = nil
alertDialog.addTextFieldWithConfigurationHandler() {
alertDialog.addTextField() {
titleTextField = $0
}

alertDialog.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
alertDialog.addAction(UIAlertAction(title: "Save", style: .Default) { action in
alertDialog.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertDialog.addAction(UIAlertAction(title: "Save", style: .default) { action in
if let title = titleTextField?.text {
let object = PFObject(className: self.parseClassName!, dictionary: [ "title": title ])
object.saveEventually().continueWithSuccessBlock { _ -> AnyObject! in
object.saveEventually().continue(successBlock: { _ -> AnyObject! in
return self.loadObjects()
}
})
}
})
})

presentViewController(alertDialog, animated: true, completion: nil)
present(alertDialog, animated: true, completion: nil)
} else {
let alertView = UIAlertView(
title: "Add Todo",
Expand All @@ -88,48 +88,48 @@ class DeletionCollectionViewController: PFQueryCollectionViewController, UIAlert
otherButtonTitles: "Save"
)

alertView.alertViewStyle = .PlainTextInput
alertView.textFieldAtIndex(0)?.placeholder = "Name"
alertView.alertViewStyle = .plainTextInput
alertView.textField(at: 0)?.placeholder = "Name"

alertView.show()
}
}

@objc
func deleteSelectedItems() {
removeObjectsAtIndexPaths(collectionView?.indexPathsForSelectedItems())
removeObjects(at: collectionView?.indexPathsForSelectedItems)
}

// MARK - UICollectionViewDataSource

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFCollectionViewCell? {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath, object: object)
cell?.textLabel.textAlignment = .Center
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath, object: PFObject?) -> PFCollectionViewCell? {
let cell = super.collectionView(collectionView, cellForItemAt: indexPath as IndexPath, object: object)
cell?.textLabel.textAlignment = .center
cell?.textLabel.text = object?["title"] as? String

cell?.contentView.layer.borderWidth = 1.0
cell?.contentView.layer.borderColor = UIColor.lightGrayColor().CGColor
cell?.contentView.layer.borderColor = UIColor.lightGray.cgColor

return cell
}

// MARK - UIAlertViewDelegate

@objc
func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
func alertView(_ alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
if (buttonIndex == alertView.cancelButtonIndex) {
return
}

if let title = alertView.textFieldAtIndex(0)?.text {
if let title = alertView.textField(at: 0)?.text {
let object = PFObject(
className: self.parseClassName!,
dictionary: [ "title": title ]
)

object.saveEventually().continueWithSuccessBlock { _ -> AnyObject! in
object.saveEventually().continue(successBlock: { _ -> AnyObject! in
return self.loadObjects()
}
})
}
}
}
Expand Up @@ -47,37 +47,37 @@ class PaginatedCollectionViewController: PFQueryCollectionViewController {

if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
let bounds = UIEdgeInsetsInsetRect(view.bounds, layout.sectionInset)
let sideLength = min(CGRectGetWidth(bounds), CGRectGetHeight(bounds)) / 2.0 - layout.minimumInteritemSpacing
layout.itemSize = CGSizeMake(sideLength, sideLength)
let sideLength = min(bounds.size.width, bounds.size.height) / 2.0 - layout.minimumInteritemSpacing
layout.itemSize = CGSize(sideLength, sideLength)
}
}

// MARK: Data

override func queryForCollection() -> PFQuery {
return super.queryForCollection().orderByAscending("priority")
override func queryForCollection() -> PFQuery<PFObject> {
return super.queryForCollection().order(byAscending: "priority")
}

// MARK: CollectionView

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFCollectionViewCell? {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath, object: object)
cell?.textLabel.textAlignment = .Center
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath, object: PFObject?) -> PFCollectionViewCell? {
let cell = super.collectionView(collectionView, cellForItemAt: indexPath, object: object)
cell?.textLabel.textAlignment = .center

if let title = object?["title"] as? String {
let attributedTitle = NSMutableAttributedString(string: title)
if let priority = object?["priority"] as? Int {
let attributes = [NSFontAttributeName : UIFont.systemFontOfSize(13.0), NSForegroundColorAttributeName : UIColor.grayColor()]
let attributes = [NSFontAttributeName : UIFont.systemFont(ofSize: 13.0), NSForegroundColorAttributeName : UIColor.gray]
let string = NSAttributedString(string: "\nPriority: \(priority)", attributes: attributes)
attributedTitle.appendAttributedString(string)
attributedTitle.append(string)
}
cell?.textLabel.attributedText = attributedTitle
} else {
cell?.textLabel.attributedText = NSAttributedString()
}

cell?.contentView.layer.borderWidth = 1.0
cell?.contentView.layer.borderColor = UIColor.lightGrayColor().CGColor
cell?.contentView.layer.borderColor = UIColor.lightGray.cgColor

return cell
}
Expand Down