git.iko.soy/dashared/GDproject
on master help sign in
Added userChats
commit1a8769e664df7787206a22011b17ee93c1665b69 authorDarya Rednikina <rednikina.com@yandex.ru> dateWed 8 May 2019, 19:41 (7 years ago) parent0722718
10 files changed+111−37
GDproject/Base.lproj/Main.storyboard+3 GDproject/Controller/ News and channels/FullPostController.swift+1 GDproject/Controller/ News and channels/NewsController.swift+1−5 GDproject/Controller/Coordinators/MessagesCoordinator.swift+31−9 GDproject/Controller/Messages/ChatInfoViewController.swift+6−1 GDproject/Controller/Messages/Dialog/DialogCell.swift+7−4 GDproject/Controller/Messages/Dialog/DialogViewController.swift+42−16 GDproject/Controller/Messages/MessagesViewController.swift+1−1 GDproject/Controller/Profile/ProfileViewController.swift+13 GDproject/Simple model/Model.swift+6−1
GDproject/Base.lproj/Main.storyboard+3
@@ -90,6 +90,9 @@
9090 <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="yir-N6-Q6f">
9191 <rect key="frame" x="246" y="0.0" width="97" height="30"/>
9292 <state key="normal" title="New message"/>
93+ <connections>
94+ <action selector="sendMessage:" destination="R4D-Ao-uvG" eventType="touchUpInside" id="tQc-5f-SU3"/>
95+ </connections>
9396 </button>
9497 </subviews>
9598 </stackView>
GDproject/Controller/ News and channels/FullPostController.swift+1
@@ -102,6 +102,7 @@ class FullPostController: UITableViewController {
102102 cell.onUserDisplay = { [weak self] (id) in
103103 let vc = self?.storyboard!.instantiateViewController(withIdentifier: profileViewController) as! ProfileViewController
104104 vc.idProfile = id
105+
105106 self?.navigationController?.pushViewController(vc, animated: true)
106107 }
107108 default:
GDproject/Controller/ News and channels/NewsController.swift+1−5
@@ -121,11 +121,7 @@ class NewsController: UIViewController, UISearchControllerDelegate, UpdateableWi
121121 }
122122 }
123123 default:
124− if let anonChannel = anonymousChannel {
125− news.dataSourse = anonChannel.posts
126− news.dictionary = anonChannel.users
127− changedChannelName?("Anonymous")
128− }
124+ break
129125 }
130126
131127 }
GDproject/Controller/Coordinators/MessagesCoordinator.swift+31−9
@@ -36,24 +36,46 @@ class MessagesCoordinator: BaseCoordinator {
3636 newVC.whatToDoWithSelection = { [weak newVC, weak self] in
3737 newVC?.navigationController?.popViewController(animated: true)
3838
39− var group = Model.Group(users: $0, name: "Untitled", id: 0)
40− Model.createGroupChat(from: group, completion:
41− { [weak self] id in
42−
43− let newVC1 = DialogViewController()
44− group.id = id
45− newVC1.dialog = Model.Dialog.groupChat(Model.GroupChat(group: group))
46− self?.navigationController?.pushViewController(newVC1, animated: true)
47− })
39+ // detect is it a user or group dialog
40+ let count = $0.count
41+ if count == 1 {
42+
43+ let user = $0.first!.key
44+ let createdDialog = Model.Dialog.userChat(Model.UserChat(user: user))
45+
46+ let vc = DialogViewController()
47+ vc.users = Model.Channels.fullPeopleDict
48+ vc.dialog = createdDialog
49+ self?.navigationController?.pushViewController(vc, animated: true)
50+
51+ } else {
52+ var group = Model.Group(users: $0, name: "Untitled", id: 0)
53+ Model.createGroupChat(from: group, completion: { [weak self] id in
54+
55+ let newVC1 = DialogViewController()
56+ group.id = id
57+ newVC1.dialog = Model.Dialog.groupChat(Model.GroupChat(group: group))
58+ self?.navigationController?.pushViewController(newVC1, animated: true)
59+ })
60+ }
4861 }
4962
5063 vc?.navigationController?.pushViewController(newVC, animated: true)
5164 }
5265
5366 vc.onDialogDisplay = { [weak vc] in
67+
5468 let newVC = DialogViewController()
5569 newVC.dialog = $0.dialog
5670 newVC.users = $0.users
71+ newVC.onUserDisplay = { [weak self] id in
72+
73+ let vc = self?.storyboard.instantiateViewController(withIdentifier: profileViewController) as! ProfileViewController
74+ vc.idProfile = id
75+ self?.navigationController!.pushViewController(vc, animated: true)
76+
77+ }
78+
5779 vc?.navigationController?.pushViewController(newVC, animated: true)
5880 }
5981
GDproject/Controller/Messages/ChatInfoViewController.swift+6−1
@@ -190,7 +190,12 @@ class ChatInfoViewController: UITableViewController {
190190 self?.navigationController?.popViewController(animated: true)
191191 }
192192 default:
193− showUserChoiceVC()
193+ switch myPermissions{
194+ case .admin:
195+ showUserChoiceVC()
196+ default:
197+ break
198+ }
194199 }
195200 }
196201 }
GDproject/Controller/Messages/Dialog/DialogCell.swift+7−4
@@ -16,14 +16,14 @@ class DialogCell: UITableViewCell {
1616 let button = UIButton()
1717 button.setTitleColor(.black, for: .normal)
1818 button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
19− button.addTarget(self, action: #selector(displayProfile), for: .touchUpInside)
2019 button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
2120 return button
2221 }()
2322
2423 @objc func displayProfile()
2524 {
26− if let id = self.user?.id{
25+ if let id = self.user?.id {
26+ print("diplay")
2727 onUserDisplay?(id)
2828 }
2929 }
@@ -80,6 +80,8 @@ class DialogCell: UITableViewCell {
8080 // important
8181 contentView.subviews.forEach({ $0.removeFromSuperview() })
8282
83+ nameLabel.addTarget(self, action: #selector(displayProfile), for: .touchUpInside)
84+
8385 self.user = byUser
8486 textView = createTextView(with: markdownText, true)
8587 textView.layer.cornerRadius = 10
@@ -110,7 +112,7 @@ class DialogCell: UITableViewCell {
110112
111113 textView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
112114 textView.textColor = .white
113− timeLabel.edgesToSuperview(excluding: .top, insets: .right(70) + .left(4))
115+ timeLabel.edgesToSuperview(excluding: .top, insets: .right(70) + .left(8))
114116 timeLabel.topToBottom(of: textView, offset: 4)
115117 timeLabel.textAlignment = .left
116118 }
@@ -120,8 +122,9 @@ class DialogCell: UITableViewCell {
120122 textView.edgesToSuperview(excluding: .left, insets: .right(8) + .bottom(20) + .top(4))
121123 textView.backgroundColor = UIColor(red:0.08, green:0.49, blue:0.98, alpha:1.0)
122124 textView.textColor = .white
123− timeLabel.edgesToSuperview(excluding: .top, insets: .left(70) + .right(4))
125+ timeLabel.edgesToSuperview(excluding: .top, insets: .left(70) + .right(8))
124126 timeLabel.topToBottom(of: textView, offset: 4)
127+ timeLabel.textAlignment = .right
125128 }
126129}
127130
GDproject/Controller/Messages/Dialog/DialogViewController.swift+42−16
@@ -16,6 +16,7 @@ protocol UpdatableGroup:class {
1616
1717class DialogViewController: UIViewController, UpdatableGroup, UITableViewDelegate, UITableViewDataSource
1818{
19+ var onUserDisplay: ((Int)->())?
1920
2021 var tableView: UITableView = UITableView()
2122
@@ -47,6 +48,7 @@ class DialogViewController: UIViewController, UpdatableGroup, UITableViewDelegat
4748
4849 var cellData: [PostCellData] = [] {
4950 didSet {
51+ prevLast = cellData.count - 1
5052 tableView.reloadData()
5153 }
5254 }
@@ -198,7 +200,14 @@ class DialogViewController: UIViewController, UpdatableGroup, UITableViewDelegat
198200 }
199201
200202 func setTitleForChat(userChat: Model.UserChat){
201− navigationItem.title = "👤 \(users![userChat.user]!.fullName())"
203+ navigationItem.title = "\(users![userChat.user]!.fullName())"
204+ navigationItem.rightBarButtonItem = UIBarButtonItem(title: "👤", style: .plain, target: self, action: #selector(showPersonPage))
205+ }
206+
207+ @objc func showPersonPage(){
208+ if let id = userChat?.user{
209+ onUserDisplay?(id)
210+ }
202211 }
203212
204213 func setTitleForGroup(groupChat: Model.GroupChat){
@@ -206,6 +215,7 @@ class DialogViewController: UIViewController, UpdatableGroup, UITableViewDelegat
206215 navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Info", style: .plain, target: self, action: #selector(moveToInfoVC))
207216 }
208217
218+ // onInfoShow
209219 @objc func moveToInfoVC(){
210220 let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: chatInfoViewController) as! ChatInfoViewController
211221
@@ -219,11 +229,10 @@ class DialogViewController: UIViewController, UpdatableGroup, UITableViewDelegat
219229 navigationController?.pushViewController(vc, animated: true)
220230 }
221231
222− var currentMessagesInChat: [Model.LastMessage]? {
223− didSet {
224− if let currentMessagesInChat = currentMessagesInChat {
225− cellData = currentMessagesInChat.map { PostCellData(attributedData: PostCellData.create(with: [$0.body])) }
226− }
232+ var currentMessagesInChat: [Model.LastMessage] = [] {
233+ didSet
234+ {
235+ cellData = currentMessagesInChat.map { PostCellData(attributedData: PostCellData.create(with: [$0.body])) }
227236 }
228237 }
229238
@@ -245,18 +254,18 @@ class DialogViewController: UIViewController, UpdatableGroup, UITableViewDelegat
245254 }
246255 }
247256
248− func getMessages(for dialog: Model.Dialog){
249− print("Here")
257+ func getMessages(for dialog: Model.Dialog, starting from: Int? = nil)
258+ {
250259 switch dialog {
251260 case .groupChat(let groupChat):
252− Model.getMessagesFor(typeOfChat: Model.Dialog.groupChat(groupChat), chat: groupChat.group.id)
253− { [unowned self] in
254− self.currentMessagesInChat = $0
261+ Model.getMessagesFor(typeOfChat: Model.Dialog.groupChat(groupChat), chat: groupChat.group.id, exclusiveFrom: from)
262+ { [weak self] in
263+ self?.currentMessagesInChat = $0
255264 }
256265 case .userChat(let userChat):
257− Model.getMessagesFor(typeOfChat: Model.Dialog.userChat(userChat), chat: userChat.user)
258− { [unowned self] in
259− self.currentMessagesInChat = $0
266+ Model.getMessagesFor(typeOfChat: Model.Dialog.userChat(userChat), chat: userChat.user, exclusiveFrom: from)
267+ { [weak self] in
268+ self?.currentMessagesInChat = $0
260269 }
261270 }
262271 }
@@ -270,13 +279,30 @@ class DialogViewController: UIViewController, UpdatableGroup, UITableViewDelegat
270279 cell.selectionStyle = .none
271280
272281
273− if let author = currentMessagesInChat?[indexPath.row].author, let user = users?[author]
282+ if let user = users?[currentMessagesInChat[indexPath.row].author]
274283 {
275− cell.fill(with: cellData[indexPath.row].attributedData, byUser: user, when: (currentMessagesInChat?[indexPath.row].time)!)
284+ cell.fill(with: cellData[indexPath.row].attributedData, byUser: user, when: currentMessagesInChat[indexPath.row].time)
285+
286+ cell.onUserDisplay = onUserDisplay
276287 }
277288
278289 return cell
279290 }
291+
292+ var prevLast = -1
293+ func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
294+ {
295+ // pagination
296+// if indexPath.row == cellData.count - 1 && prevLast != indexPath.row
297+// {
298+// if let dialog = dialog
299+// {
300+// getMessages(for: dialog, starting: prevLast)
301+// }
302+//
303+// prevLast = indexPath.row
304+// }
305+ }
280306}
281307
282308
GDproject/Controller/Messages/MessagesViewController.swift+1−1
@@ -73,7 +73,7 @@ class MessagesViewController: UITableViewController {
7373 cell.detailTextLabel?.text = group.lastMessage!.body.markdown
7474 case .userChat(let userChat):
7575 cell.textLabel?.text = "👤 \(users[userChat.user]!.fullName())"
76− cell.detailTextLabel?.text = userChat.lastMessage.body.markdown
76+ cell.detailTextLabel?.text = userChat.lastMessage!.body.markdown
7777 }
7878
7979 return cell
GDproject/Controller/Profile/ProfileViewController.swift+13
@@ -34,6 +34,18 @@ class ProfileViewController: UIViewController
3434
3535 var onSettings: (()->())?
3636
37+ @IBAction func sendMessage(_ sender: UIButton)
38+ {
39+ if let userId = idProfile
40+ {
41+ let createdDialog = Model.Dialog.userChat(Model.UserChat(user: userId))
42+ let vc = DialogViewController()
43+ vc.users = Model.Channels.fullPeopleDict
44+ vc.dialog = createdDialog
45+ self.navigationController?.pushViewController(vc, animated: true)
46+ }
47+ }
48+
3749 var onChannelsListToAddAPerson: ((Model.Users)->())?
3850
3951 var protoDictionary: [Int: UIImage] = [9: #imageLiteral(resourceName: "9"), 5051: #imageLiteral(resourceName: "5051"), 69: #imageLiteral(resourceName: "69"), 42: #imageLiteral(resourceName: "42")]
@@ -110,6 +122,7 @@ class ProfileViewController: UIViewController
110122 }
111123
112124 override func viewWillAppear(_ animated: Bool) {
125+ tabBarController?.tabBar.isHidden = false
113126
114127 if idProfile == nil {
115128 idProfile = DataStorage.standard.getUserId()
GDproject/Simple model/Model.swift+6−1
@@ -622,7 +622,12 @@ class Model{
622622
623623 struct UserChat: Codable {
624624 var user: Int
625− var lastMessage: LastMessage
625+ var lastMessage: LastMessage?
626+
627+ init(user: Int, lastMessage: LastMessage? = nil) {
628+ self.user = user
629+ self.lastMessage = lastMessage
630+ }
626631 }
627632
628633 struct Group: Codable {