-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.swift
99 lines (83 loc) · 2.83 KB
/
User.swift
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// User.swift
// Twitter
//
// Created by Andrei Gurau on 2/7/16.
// Copyright © 2016 Andrei Gurau. 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 followers: Int?
var following: Int?
var favorites: Int?
//var profileBackgroundColor: String?
//var profileBackgroundURL: String?
var userDescription: String?
init(dictionary: NSDictionary)
{
name = dictionary["name"] as? String
screenName = dictionary["screen_name"] as? String
profileImageUrl = dictionary["profile_image_url"] as? String
tagline = dictionary["description"] as? String
followers = dictionary["followers_count"] as? Int
following = dictionary["friends_count"] as? Int
favorites = dictionary["favourites_count"] as? Int
//profileBackgroundColor = dictionary["profile_background_color"] as? String
//profileBackgroundURL = dictionary["profile_background_image_url"] as? String
userDescription = dictionary["description"] as? String
self.dictionary = dictionary
}
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
{
do
{
var dictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! NSDictionary
_currentUser = User( dictionary: dictionary as! NSDictionary)
}
catch
{
}
}
}
return _currentUser
}
set(user)
{
_currentUser = user
if _currentUser != nil
{
do{
var data = try NSJSONSerialization.dataWithJSONObject(user!.dictionary, options: [])
NSUserDefaults.standardUserDefaults().setObject(data, forKey: currentUserKey)
}
catch{
}
}
else
{
NSUserDefaults.standardUserDefaults().setObject(nil, forKey: currentUserKey)
}
NSUserDefaults.standardUserDefaults().synchronize()
}
}
}