git.iko.soy/dashared/GDproject
on master help sign in
user profile is set.
4bdbd67Darya Rednikina
GDproject/Controller/ News and channels/FullPostController.swift−2
@@ -20,8 +20,6 @@ class FullPostController: UITableViewController {
20
20
21 setUpNavigationBar()
21 setUpNavigationBar()
22 tableView.separatorStyle = .none
22 tableView.separatorStyle = .none
23−
24−
25 }
23 }
26
24
27 func setUpNavigationBar(){
25 func setUpNavigationBar(){
GDproject/Controller/ News and channels/NewsController.swift+13−18
@@ -16,7 +16,9 @@ protocol UpdateableWithChannel: class {
16protocol NewPostDelegate {
16protocol NewPostDelegate {
17 func addPost(post: Post)
17 func addPost(post: Post)
18}
18}
19−
19+protocol UpdateableWithUser: class {
20+ var user: Model.Users? { get set }
21+}
20// MARK:- Controller with posts and channels availiable.
22// MARK:- Controller with posts and channels availiable.
21// Search is availiable within every table (posts and channels). Has button-functionality for boths post and chnnels
23// Search is availiable within every table (posts and channels). Has button-functionality for boths post and chnnels
22class NewsController: UITableViewController, UISearchControllerDelegate, NewPostDelegate, UpdateableWithChannel, DataDelegate
24class NewsController: UITableViewController, UISearchControllerDelegate, NewPostDelegate, UpdateableWithChannel, DataDelegate
@@ -49,7 +51,7 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
49 navigationItem.title = channel?.title ?? ""
51 navigationItem.title = channel?.title ?? ""
50 Model.getUsers(for: hh ?? []) { [weak self] (dict) in
52 Model.getUsers(for: hh ?? []) { [weak self] (dict) in
51 self?.dictionary = dict
53 self?.dictionary = dict
52− self?.refreshContr?.endRefreshing()
54+ self?.refreshContr.endRefreshing()
53 }
55 }
54 }
56 }
55 }
57 }
@@ -65,11 +67,7 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
65
67
66 var news = NewsVC()
68 var news = NewsVC()
67
69
68− var refreshContr: UIRefreshControl? = {
69− let rc = UIRefreshControl()
70+ var refreshContr = UIRefreshControl()
70− rc.addTarget(self, action: #selector(refreshPostsData(_:)), for: .valueChanged)
71− return rc
72− }()
73
71
74 override func viewDidLoad()
72 override func viewDidLoad()
75 {
73 {
@@ -77,11 +75,8 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
77 navigationItem.title = "Loading ..."
75 navigationItem.title = "Loading ..."
78 tableView.refreshControl = refreshContr
76 tableView.refreshControl = refreshContr
79 // Configure Refresh Control
77 // Configure Refresh Control
80−
78+ refreshContr.addTarget(self, action: #selector(refreshPostsData(_:)), for: .valueChanged)
81− Model.getLast { [weak self] (channel) in
79+
82− self?.channel = channel
83− }
84−
85 tableView.register(PostViewCell.self, forCellReuseIdentifier: postCellId)
80 tableView.register(PostViewCell.self, forCellReuseIdentifier: postCellId)
86 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
81 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
87
82
@@ -104,11 +99,9 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
104 }
99 }
105
100
106 @objc func refreshPostsData( _ ff: UIRefreshControl){
101 @objc func refreshPostsData( _ ff: UIRefreshControl){
107− if ff.isRefreshing {
108− Model.getLast { [weak self] (channel) in
102+ Model.getLast { [weak self] (channel) in
109− self?.channel = channel
103+ self?.channel = channel
110− self?.refreshContr?.endRefreshing()
104+ self?.refreshContr.endRefreshing()
111− }
112 }
105 }
113 }
106 }
114
107
@@ -131,7 +124,9 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
131 searchController.isActive = false
124 searchController.isActive = false
132 // TODO:- display something if no posts are availiable
125 // TODO:- display something if no posts are availiable
133
126
134− refreshContr?.beginRefreshing()
127+ Model.getLast { [weak self] (channel) in
128+ self?.channel = channel
129+ }
135
130
136 // tableView.reloadData()
131 // tableView.reloadData()
137 }
132 }
GDproject/Controller/Coordinator tab bar?/TabbarCoordinator.swift+13−1
@@ -2,12 +2,24 @@ import UIKit
2
2
3class TabbarCoordinator{
3class TabbarCoordinator{
4 var window: UIWindow!
4 var window: UIWindow!
5+ static let tabBar: UITabBarController = UIStoryboard.tabBarController()
6+
7+ // MARK: - Properties
8+ //private var user: Model.Users { didSet { updateInterfaces() } }
5
9
6 func start(){
10 func start(){
7− window.rootViewController = UIStoryboard.tabBarController()
11+ window.rootViewController = TabbarCoordinator.tabBar
12+ TabbarCoordinator.tabBar.selectedIndex = 0
8 }
13 }
9
14
10 init(window: UIWindow) {
15 init(window: UIWindow) {
11 self.window = window
16 self.window = window
12 }
17 }
18+
19+// // MARK:- update only viewControllers which are depandable on chosen channel
20+// private func updateInterfaces() {
21+// TabbarCoordinator.tabBar.viewControllers?.forEach({
22+// ($0 as? UpdateableWithUser)?.user = user
23+// })
24+// }
13}
25}
GDproject/Controller/Log In/LogInCoordinator.swift+3−1
@@ -33,7 +33,9 @@ class LogInCoordinator{
33 controller.onLogIn = {
33 controller.onLogIn = {
34 (id) in
34 (id) in
35 Model.authenticate(with: id) {
35 Model.authenticate(with: id) {
36+ (res) in
37+ if (res) { DataStorage.standard.setUserKey(with: id) }
36− (res) in controller.authenticateSucceeded = res
38+ controller.authenticateSucceeded = res
37 }
39 }
38 }
40 }
39
41
GDproject/Controller/Log In/LogInViewController.swift+2−1
@@ -21,9 +21,10 @@ class LogInViewController: UIViewController {
21 if !authenticateSucceeded! {
21 if !authenticateSucceeded! {
22 indicatorView.stopAnimating()
22 indicatorView.stopAnimating()
23 indicatorView.isHidden = true
23 indicatorView.isHidden = true
24− }
24+ }
25 }
25 }
26 }
26 }
27+
27 static let titleColor = UIColor(red: 0, green: 137/255, blue: 249/255, alpha: 0.5)
28 static let titleColor = UIColor(red: 0, green: 137/255, blue: 249/255, alpha: 0.5)
28
29
29 var bottomConstraint: NSLayoutConstraint?
30 var bottomConstraint: NSLayoutConstraint?
GDproject/Controller/Profile/ProfileViewController.swift+26−11
@@ -31,15 +31,19 @@ class ProfileViewController: UIViewController
31 @IBOutlet weak var newMessageButton: UIButton!
31 @IBOutlet weak var newMessageButton: UIButton!
32
32
33
33
34− func fill(with user: User){
34+ func fill(with user: Model.Users){
35− self.facultyLabel.text = user.faculty ?? ""
35+ self.facultyLabel.text = "Студент: Факультет Компьютерных Наук"
36− self.nameLabel.text = "\(user.initials.name) \(user.initials.optional ?? "")"
36+ self.nameLabel.text = "\(user.firstName)"
37− self.surnameLabel.text = user.initials.surname
37+ self.surnameLabel.text = "\(user.secondName)"
38 self.profileImageView.image = #imageLiteral(resourceName: "kitten").roundedImage
38 self.profileImageView.image = #imageLiteral(resourceName: "kitten").roundedImage
39− self.placeLabel.text = user.placeOfWork
39+ self.placeLabel.text = "Москва"
40 }
40 }
41
41
42− var user: User = User(surname: "Богомазова", name: "Вероника", optional: "Львовна", emailName: "vbogomazova", id: 2, place: "📍Москва, Кочновский пр. 3", faculty: "Методист: Факультет Компьютерных наук")
42+ var user: Model.Users? {
43+ didSet {
44+ self.fill(with: user!)
45+ }
46+ }
43
47
44 var basicInfo = BasicInfoController()
48 var basicInfo = BasicInfoController()
45 var posts = NewsVC()
49 var posts = NewsVC()
@@ -55,9 +59,7 @@ class ProfileViewController: UIViewController
55 {
59 {
56 super.viewDidLoad()
60 super.viewDidLoad()
57
61
58− Model.getPostsForUser(with: 42) { [weak self] (posts) in
62+
59− self?.dataSourse = posts
60− }
61
63
62 posts.viewController = self
64 posts.viewController = self
63 posts.type = .NONE
65 posts.type = .NONE
@@ -72,19 +74,32 @@ class ProfileViewController: UIViewController
72
74
73 posts.viewController = self
75 posts.viewController = self
74
76
75− fill(with: user)
76 tableView.delegate = posts
77 tableView.delegate = posts
77 tableView.dataSource = posts
78 tableView.dataSource = posts
78 tableView.reloadData()
79 tableView.reloadData()
79 }
80 }
80
81
81 override func viewWillAppear(_ animated: Bool) {
82 override func viewWillAppear(_ animated: Bool) {
83+ if let id = DataStorage.standard.getUserId() {
84+ if let user = Model.idUser[id] {
85+ self.user = user
86+ } else {
87+ Model.getUsers(for: [id]) { [weak self] (dic) in
88+ self?.user = dic[id]
89+ }
90+ }
91+
92+ Model.getPostsForUser(with: id) { [weak self] (posts) in
93+ self?.dataSourse = posts
94+ }
95+ }
96+
82 setUpNavigarionBar()
97 setUpNavigarionBar()
83 }
98 }
84
99
85 func setUpNavigarionBar(){
100 func setUpNavigarionBar(){
86 navigationController?.navigationBar.prefersLargeTitles = true
101 navigationController?.navigationBar.prefersLargeTitles = true
87− navigationItem.title = user.login
102+ navigationItem.title = "\(DataStorage.standard.getUserId() ?? 0)"
88 let uibarbutton = UIBarButtonItem(title: "More", style: .plain, target: self, action: #selector(showInformation))
103 let uibarbutton = UIBarButtonItem(title: "More", style: .plain, target: self, action: #selector(showInformation))
89 navigationItem.rightBarButtonItems = [uibarbutton]
104 navigationItem.rightBarButtonItems = [uibarbutton]
90 navigationItem.largeTitleDisplayMode = .always
105 navigationItem.largeTitleDisplayMode = .always
GDproject/DataStorage.swift+8−1
@@ -28,6 +28,13 @@ class DataStorage{
28 isLoggedIn = UserDefaults.standard.bool(forKey: UserDefaultsKeys.loggedIn.rawValue)
28 isLoggedIn = UserDefaults.standard.bool(forKey: UserDefaultsKeys.loggedIn.rawValue)
29 }
29 }
30
30
31+ func setUserKey(with id: Int){
32+ UserDefaults.standard.set(id, forKey: UserDefaultsKeys.id.rawValue)
33+ }
34+
35+ func getUserId() -> Int?{
36+ return UserDefaults.standard.integer(forKey: UserDefaultsKeys.id.rawValue)
37+ }
31 /**
38 /**
32 Function to determine is user logged in already or not
39 Function to determine is user logged in already or not
33 */
40 */
@@ -47,5 +54,5 @@ class DataStorage{
47 */
54 */
48enum UserDefaultsKeys: String{
55enum UserDefaultsKeys: String{
49 case loggedIn
56 case loggedIn
50− case cookie
57+ case id
51}
58}
GDproject/Simple model/Model.swift+4−3
@@ -235,6 +235,8 @@ class Model{
235 }
235 }
236 }
236 }
237
237
238+ static var idUser: [Int:Users] = [:]
239+
238 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
240 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
239 let json = "\(Set(ids))"
241 let json = "\(Set(ids))"
240 print(json)
242 print(json)
@@ -258,12 +260,11 @@ class Model{
258
260
259 guard let users = try? decoder.decode([Users].self, from: json) else { return }
261 guard let users = try? decoder.decode([Users].self, from: json) else { return }
260
262
261− var dict: [Int:Users] = [:]
262 users.forEach({ (user) in
263 users.forEach({ (user) in
263− dict[user.id] = user
264+ Model.idUser[user.id] = user
264 })
265 })
265
266
266− completion(dict)
267+ completion(Model.idUser)
267 }
268 }
268 }
269 }
269}
270}