Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/fix me routes #50

Merged
merged 3 commits into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions swift-base/Models/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,39 @@ import SwiftyJSON

class User: NSObject, NSCoding {
var id: Int
var username: String?
var email: String?
var image: String?
var username: String
var email: String
var image: URL?

init(id: Int, username: String? = nil, email: String? = nil, image: String? = nil) {
init(id: Int, username: String = "", email: String, image: String = "") {
self.id = id
self.username = username
self.email = email
self.image = image
self.image = image.isEmpty ? nil : URL(string: image)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you create a URL object with an empty string? If the URL object is nil you should replace this by: self.image = URL(string: image)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I thought it would create a dummy URL instead of nil.

}

required init(coder aDecoder: NSCoder) {
self.id = aDecoder.decodeInteger(forKey: "user-id")
self.username = aDecoder.decodeObject(forKey: "user-username") as? String
self.email = aDecoder.decodeObject(forKey: "user-email") as? String
self.image = aDecoder.decodeObject(forKey: "user-image") as? String
self.username = aDecoder.decodeObject(forKey: "user-username") as? String ?? ""
self.email = aDecoder.decodeObject(forKey: "user-email") as? String ?? ""
if let str = aDecoder.decodeObject(forKey: "user-image") as? String {
self.image = URL(string: str)
}
}

func encode(with aCoder: NSCoder) {
aCoder.encode(self.id, forKey: "user-id")
aCoder.encode(self.username, forKey: "user-username")
aCoder.encode(self.email, forKey: "user-email")
aCoder.encode(self.image, forKey: "user-image")
aCoder.encode(self.image?.absoluteString, forKey: "user-image")
}

//MARK Parser
class func parse(fromJSON json: JSON) -> User {
let user = json["user"]

return User(id: user["id"].intValue,
username: user["full_name"].stringValue,
username: user["username"].stringValue,
email: user["email"].stringValue,
image: user["profile_picture"].stringValue
)
Expand Down
13 changes: 9 additions & 4 deletions swift-base/Networking/Services/UserAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SwiftyJSON
class UserAPI {

fileprivate static let usersUrl = "/users/"
fileprivate static let currentUserUrl = "/user/"

class func login(_ email: String, password: String, success: @escaping (_ responseObject: String?) -> Void, failure: @escaping (_ error: Error) -> Void) {
let url = usersUrl + "sign_in"
Expand All @@ -22,7 +23,9 @@ class UserAPI {
]
]
APIClient.sendPostRequest(url, params: parameters as [String : AnyObject]?,
success: { (_) -> Void in
success: { (response) -> Void in
let json = JSON(response)
UserDataManager.storeUserObject(User.parse(fromJSON: json))
success("")
}) { (error) -> Void in
failure(error)
Expand Down Expand Up @@ -64,24 +67,26 @@ class UserAPI {

APIClient.sendPostRequest(usersUrl, params: parameters as [String : AnyObject]?,
success: { (response) -> Void in
let json = JSON(response)
UserDataManager.storeUserObject(User.parse(fromJSON: json))
success(response)
}) { (error) -> Void in
failure(error)
}
}

class func getMyProfile(_ success: @escaping (_ json: JSON) -> Void, failure: @escaping (_ error: Error) -> Void) {
let url = usersUrl + "me"
let url = currentUserUrl + "profile"
APIClient.sendGetRequest(url, success: { (responseObject) in
let json = JSON(responseObject)
success(json)
}) { (error) in
failure(error)
failure(error)
}
}

class func loginWithFacebook(token: String, success: @escaping () -> Void, failure: @escaping (_ error: Error) -> Void) {
let url = usersUrl + "facebook"
let url = currentUserUrl + "facebook"
let parameters = [
"access_token": token
] as [String : Any]
Expand Down
2 changes: 1 addition & 1 deletion swift-base/ViewControllers/SignUpViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SignUpViewController: UIViewController {

@IBAction func tapOnSignUpButton(_ sender: Any) {
showSpinner(message: "VC spinner")
UserAPI.signup("\(randomName())@gmail.com", password: "123456789", avatar: randomImage(), success: { (_) in
UserAPI.signup("\(randomName())@gmail.com", password: "123456789", avatar64: randomImage(), success: { (_) in
self.hideSpinner()
UIApplication.shared.keyWindow?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController")
}) { (error) in
Expand Down