-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.swift
More file actions
68 lines (59 loc) · 2.3 KB
/
User.swift
File metadata and controls
68 lines (59 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// User.swift
// twitterApp
//
// Created by Deepak on 9/27/14.
// Copyright (c) 2014 Deepak. All rights reserved.
//
import UIKit
var _currentUser: User?
let currentUserKey = "kCurrentUserKey"
let userDidLoginNotification = "userDidLoginNotification"
let userDidLogoutNotification = "userDidLogoutNotification"
class User: NSObject {
var name: String?
var screenname: String?
var profileImageUrl: String?
var tagline: String?
var dictionary: NSDictionary
var profileBackgroundImageUrl: String?
var numTweets: NSNumber?
var numFollowers: NSNumber?
var numFollowing: NSNumber?
init(dictionary: NSDictionary) {
self.dictionary = dictionary
name = dictionary["name"] as? String
screenname = dictionary["screen_name"] as? String
profileImageUrl = dictionary["profile_image_url"] as? String
tagline = dictionary["description"] as? String
profileBackgroundImageUrl = dictionary["profile_background_image_url"] as? String
numTweets = dictionary["statuses_count"] as? NSNumber
numFollowers = dictionary["friends_count"] as? NSNumber
numFollowing = dictionary["followers_count"] as? NSNumber
}
func logout() {
User.currentUser = nil
TwitterClient.sharedInstance.requestSerializer.removeAccessToken()
NSNotificationCenter.defaultCenter().postNotificationName(userDidLogoutNotification, object: nil)
}
class var currentUser: User? {
get {
if _currentUser == nil {
var data = NSUserDefaults.standardUserDefaults().objectForKey(currentUserKey) as? NSData
if data != nil {
var dictionary = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary
_currentUser = User(dictionary: dictionary)
}
}
return _currentUser
}
set(user) {
_currentUser = user
if _currentUser != nil {
var data = NSJSONSerialization.dataWithJSONObject(user!.dictionary, options: nil, error: nil)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: currentUserKey)
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}
}