git.iko.soy/dashared/GDproject
on master help sign in
Strated to work on dialogs: getAll is done, need to add other functionality and change design
commita32c3421d1d46be00e27f291f1c5c9979368f8bb authorDarya Rednikina <rednikina.com@yandex.ru> dateFri 3 May 2019, 22:52 (7 years ago) parent6850d76
8 files changed+287−180
GDproject/Base.lproj/Main.storyboard+1−1 GDproject/Constants.swift+2 GDproject/Controller/Coordinators/MessagesCoordinator.swift+2−2 GDproject/Controller/Messages/DialogViewController.swift+36−61 GDproject/Controller/Messages/MessagesViewController.swift+18−51 GDproject/Controller/Messages/PeopleToWriteViewController.swift+25−57 GDproject/Simple model/CompletionTree.swift+2−1 GDproject/Simple model/Model.swift+201−7
GDproject/Base.lproj/Main.storyboard+1−1
@@ -570,7 +570,7 @@
570570 <scene sceneID="6Td-bq-Ebf">
571571 <objects>
572572 <tableViewController storyboardIdentifier="PeopleToWriteViewController" id="rUc-Tv-hWE" customClass="PeopleToWriteViewController" customModule="GDproject" customModuleProvider="target" sceneMemberID="viewController">
573− <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" id="PDb-NB-Ey6">
573+ <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" allowsSelectionDuringEditing="YES" allowsMultipleSelection="YES" allowsMultipleSelectionDuringEditing="YES" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" id="PDb-NB-Ey6">
574574 <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
575575 <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
576576 <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
GDproject/Constants.swift+2
@@ -61,5 +61,7 @@ let addToChannelVCId = "AddToChannelVC"
6161
6262let taggsSelectionViewController = "TaggsSelectionViewController"
6363
64+let dialogViewController = "DialogViewController"
65+
6466let blueSystemColor = UIColor(red: 0, green: 137/255, blue: 249/255, alpha: 0.5)
6567
GDproject/Controller/Coordinators/MessagesCoordinator.swift+2−2
@@ -35,9 +35,9 @@ class MessagesCoordinator: BaseCoordinator {
3535 vc?.navigationController?.pushViewController(newVC, animated: true)
3636 }
3737
38− vc.onDialogDisplay = { [weak vc, unowned self] in
38+ vc.onDialogDisplay = { [weak vc, unowned self] _ in
3939 let newVC = self.storyboard.instantiateViewController(withIdentifier: dialogVC) as! DialogViewController
40− newVC.currentDialog = $0
40+ //newVC.currentDialog = $0
4141 vc?.navigationController?.pushViewController(newVC, animated: true)
4242 }
4343 navigationController?.viewControllers = [vc]
GDproject/Controller/Messages/DialogViewController.swift+36−61
@@ -11,82 +11,57 @@ import UIKit
1111class DialogViewController: UITableViewController {
1212
1313 var onInfoShow: (()->())?
14−
15− var currentDialog: (id: Int, name: String)?
14+ var group: Model.Group?
1615
1716 override func viewDidLoad() {
1817 super.viewDidLoad()
1918
20− if let person = currentDialog {
21− navigationItem.title = person.name
19+ tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
20+
21+ if let id = group {
22+ navigationItem.title = "🌌 \(id.id) \(id.name)"
23+ navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Info", style: .plain, target: self, action: #selector(moveToInfoVC))
24+ }
25+ }
26+
27+
28+ @objc func moveToInfoVC(){
29+
30+ }
31+
32+ var currentMessagesInChat: [Model.LastMessage]? {
33+ didSet {
34+ tableView.reloadData()
2235 }
2336 }
2437
2538 // MARK: - Table view data source
2639
2740 override func numberOfSections(in tableView: UITableView) -> Int {
28− // #warning Incomplete implementation, return the number of sections
29− return 0
41+ return 1
3042 }
3143
3244 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
33− // #warning Incomplete implementation, return the number of rows
34− return 0
45+ return currentMessagesInChat?.count ?? 0
3546 }
3647
37− /*
38− override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
39− let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
40−
41− // Configure the cell...
42−
43− return cell
44− }
45− */
46−
47− /*
48− // Override to support conditional editing of the table view.
49− override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
50− // Return false if you do not want the specified item to be editable.
51− return true
52− }
53− */
54−
55− /*
56− // Override to support editing the table view.
57− override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
58− if editingStyle == .delete {
59− // Delete the row from the data source
60− tableView.deleteRows(at: [indexPath], with: .fade)
61− } else if editingStyle == .insert {
62− // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
63− }
64− }
65− */
66−
67− /*
68− // Override to support rearranging the table view.
69− override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
70−
71− }
72− */
73−
74− /*
75− // Override to support conditional rearranging of the table view.
76− override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
77− // Return false if you do not want the item to be re-orderable.
78− return true
48+ override func viewWillAppear(_ animated: Bool) {
49+ super.viewWillAppear(animated)
50+
51+ if let group = group {
52+ Model.getMessagesForGroupChat(chat: group.id) { [unowned self] in
53+ self.currentMessagesInChat = $0
54+ }
55+ }
7956 }
80− */
81−
82− /*
83− // MARK: - Navigation
84−
85− // In a storyboard-based application, you will often want to do a little preparation before navigation
86− override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
87− // Get the new view controller using segue.destination.
88− // Pass the selected object to the new view controller.
57+
58+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
59+ {
60+ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
61+
62+ cell.detailTextLabel?.text = currentMessagesInChat![indexPath.row].body.markdown
63+ cell.textLabel?.text = "\(currentMessagesInChat![indexPath.row].author)"
64+
65+ return cell
8966 }
90− */
91−
9267}
GDproject/Controller/Messages/MessagesViewController.swift+18−51
@@ -11,11 +11,15 @@ import UIKit
1111class MessagesViewController: UITableViewController {
1212
1313 // curreent Active which can be displayed
14− var currentActiveDialogs: [(id: Int, name: String)] = [(id: Int, name: String)]()
14+ var currentActiveDialogs: [Model.Dialog] = [] {
15+ didSet{
16+ tableView.reloadData()
17+ }
18+ }
1519
1620 var onUserDisplayList: (()->())?
1721
18− var onDialogDisplay: (((id: Int, name: String))->())?
22+ var onDialogDisplay: ((Model.Dialog)->())?
1923
2024 let searchC = UISearchController(searchResultsController: nil)
2125
@@ -39,10 +43,12 @@ class MessagesViewController: UITableViewController {
3943 override func viewWillAppear(_ animated: Bool) {
4044 super.viewWillAppear(animated)
4145
42− tableView.reloadData()
46+ Model.getChatAll { [weak self] in
47+ self?.currentActiveDialogs = $0
48+ }
4349 }
50+
4451 // MARK: - Table view data source
45−
4652 override func numberOfSections(in tableView: UITableView) -> Int {
4753 return 1
4854 }
@@ -56,8 +62,14 @@ class MessagesViewController: UITableViewController {
5662 {
5763 let cell = tableView.dequeueReusableCell(withIdentifier: "MessagesCell", for: indexPath)
5864
59− cell.textLabel?.text = currentActiveDialogs[indexPath.row].name
60− cell.detailTextLabel?.text = "from \(currentActiveDialogs[indexPath.row].name) some message is displayed here..."
65+ switch currentActiveDialogs[indexPath.row].self {
66+ case .groupChat(let group):
67+ cell.textLabel?.text = group.group.name
68+ cell.detailTextLabel?.text = group.lastMessage.body.markdown
69+ case .userChat(let userChat):
70+ cell.textLabel?.text = "\(userChat.user)"
71+ cell.detailTextLabel?.text = userChat.lastMessage.body.markdown
72+ }
6173
6274 return cell
6375 }
@@ -65,49 +77,4 @@ class MessagesViewController: UITableViewController {
6577 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
6678 onDialogDisplay?(currentActiveDialogs[indexPath.row])
6779 }
68− /*
69− // Override to support conditional editing of the table view.
70− override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
71− // Return false if you do not want the specified item to be editable.
72− return true
73− }
74− */
75−
76− /*
77− // Override to support editing the table view.
78− override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
79− if editingStyle == .delete {
80− // Delete the row from the data source
81− tableView.deleteRows(at: [indexPath], with: .fade)
82− } else if editingStyle == .insert {
83− // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
84− }
85− }
86− */
87−
88− /*
89− // Override to support rearranging the table view.
90− override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
91−
92− }
93− */
94−
95− /*
96− // Override to support conditional rearranging of the table view.
97− override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
98− // Return false if you do not want the item to be re-orderable.
99− return true
100− }
101− */
102−
103− /*
104− // MARK: - Navigation
105−
106− // In a storyboard-based application, you will often want to do a little preparation before navigation
107− override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
108− // Get the new view controller using segue.destination.
109− // Pass the selected object to the new view controller.
110− }
111− */
112−
11380}
GDproject/Controller/Messages/PeopleToWriteViewController.swift+25−57
@@ -14,17 +14,24 @@ class PeopleToWriteViewController: UITableViewController {
1414
1515 let searchC = UISearchController(searchResultsController: nil)
1616
17− let users = [(id: 9,name: "Anna Mikhaleva")]
17+ let users = Model.Channels.fullPeople
18+
19+ var chosenUsers: [Int: Model.UserPermission] = [:]
1820
1921 override func viewDidLoad() {
2022 super.viewDidLoad()
2123
24+ tableView.isEditing = true
2225 self.navigationItem.title = "People"
26+ self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(newMessage))
2327 self.navigationItem.largeTitleDisplayMode = .never
2428 self.navigationItem.searchController = searchC
2529 self.navigationItem.hidesSearchBarWhenScrolling = false
2630 }
2731
32+ override func setEditing(_ editing: Bool, animated: Bool) {
33+ super.setEditing(true, animated: animated)
34+ }
2835 // MARK: - Table view data source
2936
3037 override func numberOfSections(in tableView: UITableView) -> Int {
@@ -37,72 +44,33 @@ class PeopleToWriteViewController: UITableViewController {
3744 return users.count
3845 }
3946
47+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
48+ chosenUsers[users[indexPath.row].id] = Model.UserPermission(isAdmin: false)
49+ }
4050
4151 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
4252 {
4353 let cell = tableView.dequeueReusableCell(withIdentifier: "peopleToWriteCell", for: indexPath)
4454
45− cell.textLabel?.text = users[indexPath.row].name
55+ cell.textLabel?.text = users[indexPath.row].fullName()
4656 cell.detailTextLabel?.text = "\(users[indexPath.row].id)"
4757
4858 return cell
4959 }
50−
51− override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
52− navigationController?.popViewController(animated: true)
53−
54− guard let _ = self.navigationController?.viewControllers.lastIndex(of: self) else
60+
61+ @objc func newMessage()
62+ {
63+ if chosenUsers.count != 0
5564 {
56− (navigationController?.viewControllers.last as? MessagesViewController)?.currentActiveDialogs.append(users[indexPath.row])
57− (navigationController?.viewControllers.last as? MessagesViewController)?.onDialogDisplay?(users[indexPath.row])
58− return
65+ navigationController?.popViewController(animated: false)
66+ var group = Model.Group(users: chosenUsers, name: "Untitled", id: 0)
67+
68+ Model.createGroupChat(from: group) { [unowned self] (id) in
69+ let vc = self.storyboard?.instantiateViewController(withIdentifier: dialogViewController) as! DialogViewController
70+ group.id = id
71+ vc.group = group
72+ self.navigationController?.pushViewController(vc, animated: true)
73+ }
5974 }
6075 }
61−
62−
63− /*
64− // Override to support conditional editing of the table view.
65− override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
66− // Return false if you do not want the specified item to be editable.
67− return true
68− }
69− */
70−
71− /*
72− // Override to support editing the table view.
73− override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
74− if editingStyle == .delete {
75− // Delete the row from the data source
76− tableView.deleteRows(at: [indexPath], with: .fade)
77− } else if editingStyle == .insert {
78− // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
79− }
80− }
81− */
82−
83− /*
84− // Override to support rearranging the table view.
85− override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
86−
87− }
88− */
89−
90− /*
91− // Override to support conditional rearranging of the table view.
92− override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
93− // Return false if you do not want the item to be re-orderable.
94− return true
95− }
96− */
97−
98− /*
99− // MARK: - Navigation
100−
101− // In a storyboard-based application, you will often want to do a little preparation before navigation
102− override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
103− // Get the new view controller using segue.destination.
104− // Pass the selected object to the new view controller.
105− }
106− */
107−
10876}
GDproject/Simple model/CompletionTree.swift+2−1
@@ -27,7 +27,8 @@ struct CompletionTree: Codable {
2727 }
2828 }
2929
30− static func getValues(tree: CompletionTree) -> [String] {
30+ static func getValues(tree: CompletionTree) -> [String]
31+ {
3132 var out = [String]()
3233
3334 if let treeVal = tree.value {
GDproject/Simple model/Model.swift+201−7
@@ -51,9 +51,9 @@ class Model{
5151 static let messagesUserChatURL = URL(string: "\(baseUrl)/messages/userChat")!
5252
5353
54− struct QueryPosts: Codable {
54+ struct QueryPosts<T: Codable>: Codable {
5555 var users: [Int: Users]
56− var response: [Posts]
56+ var response: [T]
5757 }
5858
5959 struct Posts: Codable {
@@ -269,7 +269,7 @@ class Model{
269269
270270 guard let json = response.data else { return }
271271
272− guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { print("no")
272+ guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { print("no")
273273 return }
274274
275275 idUser = newQueery.users
@@ -319,7 +319,7 @@ class Model{
319319
320320 guard let json = response.data else { return }
321321
322− guard let newPost = try? decoder.decode(QueryPosts.self, from: json) else { return }
322+ guard let newPost = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
323323
324324 completion(newPost.response)
325325 }
@@ -377,7 +377,7 @@ class Model{
377377
378378 guard let json = response.data else { return }
379379
380− guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { return }
380+ guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
381381
382382 idUser = newQueery.users
383383 completion((newQueery.users, newQueery.response))
@@ -539,7 +539,7 @@ class Model{
539539
540540 guard let json = response.data else { return }
541541
542− guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { return }
542+ guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
543543
544544 // idUser = newQueery.users
545545 completion((newQueery.users, newQueery.response))
@@ -550,11 +550,205 @@ class Model{
550550
551551 AF.request(URLRequest(url: hashTagTreeURL)).responseJSON {
552552 (response) in
553−
553+ isValidTocken?(response.response?.statusCode ?? 498)
554554 guard let json = response.data else { return }
555555 guard let tree = try? decoder.decode(CompletionTree.self, from: json) else { return }
556556 completion(tree)
557557 }
558558 }
559559
560+ static func getChatAll(limit: Int = 10, exclusiveFrom: Int? = nil, request: [Int] = [], completion: @escaping (([Dialog])->()))
561+ {
562+ let req = GeneralRequest<[Int]>(limit: limit, exclusiveFrom: exclusiveFrom, request: request)
563+ var request = URLRequest(url: chatsGetAllURL)
564+ request.httpMethod = "POST"
565+ request.httpBody = try? JSONEncoder().encode(req)
566+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
567+
568+ AF.request(request).responseJSON { response in
569+ isValidTocken?(response.response?.statusCode ?? 498)
570+
571+ guard let json = response.data else { return }
572+ let dialogs = try! decoder.decode(QueryPosts<Dialog>.self, from: json)
573+
574+ print(dialogs.response)
575+ completion(dialogs.response)
576+ }
577+ }
578+
579+ enum Dialog: Codable
580+ {
581+ case groupChat(GroupChat)
582+ case userChat(UserChat)
583+
584+ private enum DialogKeys: CodingKey{
585+ case groupChat
586+ case userChat
587+ }
588+
589+ init(from decoder: Decoder) throws {
590+ let container = try decoder.container(keyedBy: DialogKeys.self)
591+ if container.contains(.groupChat){
592+ self = .groupChat(try GroupChat(from: container.superDecoder(forKey: .groupChat)))
593+ } else if container.contains(.userChat){
594+ self = .userChat(try UserChat(from: container.superDecoder(forKey: .userChat)))
595+ } else {
596+ throw DecodingError.keyNotFound(DialogKeys.groupChat, DecodingError.Context(codingPath: container.codingPath, debugDescription: "hz"))
597+ }
598+ }
599+
600+ func encode(to encoder: Encoder) throws {
601+ var container = encoder.container(keyedBy: DialogKeys.self)
602+ switch self{
603+ case .groupChat(let chat):
604+ try chat.encode(to: container.superEncoder(forKey: .groupChat))
605+ case .userChat(let chat):
606+ try chat.encode(to: container.superEncoder(forKey: .userChat))
607+ }
608+ }
609+ }
610+
611+ struct GroupChat: Codable {
612+ var group: Group
613+ var lastMessage: LastMessage
614+ }
615+
616+ struct UserChat: Codable {
617+ var user: Int
618+ var lastMessage: LastMessage
619+ }
620+
621+ struct Group: Codable {
622+ var users: [Int: UserPermission]
623+ var name: String
624+ var id: Int
625+ }
626+
627+ struct UserPermission: Codable {
628+ var isAdmin: Bool
629+ }
630+
631+ struct LastMessage: Codable {
632+ var body: Attachments
633+ var destination: MessageDestination
634+ var time: String
635+ var author: Int
636+ var id: Int
637+
638+ private enum MessageCodingKeys: CodingKey{
639+ case user
640+ case group
641+ case body
642+ case time
643+ case author
644+ case id
645+ }
646+
647+ init(from decoder: Decoder) throws
648+ {
649+ let container = try decoder.container(keyedBy: MessageCodingKeys.self)
650+ time = try container.decode(String.self, forKey: .time)
651+ body = try container.decode(Attachments.self, forKey: .body)
652+ author = try container.decode(Int.self, forKey: .author)
653+ id = try container.decode(Int.self, forKey: .id)
654+
655+ if container.contains(.user){
656+ destination = MessageDestination.userChatDestination(try Int(from: container.superDecoder(forKey: .user)))
657+ } else if container.contains(.group){
658+ destination = MessageDestination.groupChatDestination(try Int(from: container.superDecoder(forKey: .group)))
659+ } else {
660+ throw DecodingError.keyNotFound(MessageCodingKeys.group, DecodingError.Context(codingPath: container.codingPath, debugDescription: "hz gr"))
661+ }
662+ }
663+
664+ func encode(to encoder: Encoder) throws {
665+ var container = encoder.container(keyedBy: MessageCodingKeys.self)
666+
667+ try container.encode(time, forKey: .time)
668+ try container.encode(id, forKey: .id)
669+ try container.encode(author, forKey: .author)
670+ try container.encode(body, forKey: .body)
671+
672+ switch destination {
673+ case .userChatDestination(let uId):
674+ try uId.encode(to: container.superEncoder(forKey: .user))
675+ case .groupChatDestination(let gId):
676+ try gId.encode(to: container.superEncoder(forKey: .group))
677+ }
678+ }
679+ }
680+
681+ enum MessageDestination: Codable
682+ {
683+ func encode(to encoder: Encoder) throws
684+ {
685+ var container = encoder.container(keyedBy: MessCodingKeys.self)
686+ switch self {
687+ case .userChatDestination(let uId):
688+ try uId.encode(to: container.superEncoder(forKey: .user))
689+ case .groupChatDestination(let gId):
690+ try gId.encode(to: container.superEncoder(forKey: .group))
691+ }
692+ }
693+
694+ init(from decoder: Decoder) throws
695+ {
696+ let container = try decoder.container(keyedBy: MessCodingKeys.self)
697+ if container.contains(.user){
698+ self = .userChatDestination(try Int(from: container.superDecoder(forKey: .user)))
699+ } else if container.contains(.group){
700+ self = .groupChatDestination(try Int(from: container.superDecoder(forKey: .group)))
701+ } else {
702+ throw DecodingError.keyNotFound(MessCodingKeys.group, DecodingError.Context(codingPath: container.codingPath, debugDescription: "hz gr"))
703+ }
704+ }
705+
706+ enum MessCodingKeys: CodingKey{
707+ case user
708+ case group
709+ }
710+
711+ case userChatDestination(Int)
712+ case groupChatDestination(Int)
713+
714+ }
715+
716+ static func createGroupChat(from group: Group, completion: @escaping ((Int)->())) {
717+ let req = group
718+ var request = URLRequest(url: createGroupChatURL)
719+ request.httpMethod = "POST"
720+ request.httpBody = try? JSONEncoder().encode(req)
721+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
722+
723+ AF.request(request).response { (response) in
724+
725+ isValidTocken?(response.response?.statusCode ?? 498)
726+
727+ guard let json = response.data else { return }
728+ let stringInt = String.init(data: json, encoding: String.Encoding.utf8)
729+ let intId = Int.init(stringInt!)
730+
731+ completion(intId!)
732+ }
733+ }
734+
735+ static func getMessagesForGroupChat(chat id: Int, exclusiveFrom: Int? = nil, limit l: Int = 10, direction: String = "backward", completion: @escaping (([LastMessage])->()))
736+ {
737+ let req = GeneralRequest<Int>(direction: direction, limit: l, exclusiveFrom: exclusiveFrom, request: id)
738+
739+ var request = URLRequest(url: messagesGetGroupChatURL)
740+ request.httpMethod = "POST"
741+ request.httpBody = try? JSONEncoder().encode(req)
742+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
743+
744+ AF.request(request).response { (response) in
745+
746+ isValidTocken?(response.response?.statusCode ?? 498)
747+
748+ guard let json = response.data else { return }
749+ guard let messages = try? decoder.decode([LastMessage].self, from: json) else { return }
750+
751+ completion(messages)
752+ }
753+ }
560754}