git.iko.soy/dashared/GDproject
on master help sign in
working on coordination between anonymous channels. in close future: design and implement channels' card
commite5bb705035639020507cd0e2b347c1c6b71c1a1e authorDarya Rednikina <rednikina.com@yandex.ru> dateWed 20 Mar 2019, 22:22 (8 years ago) parente8b7ede
7 files changed+161−46
GDproject/Base.lproj/Main.storyboard+2 GDproject/Controller/ News and channels/ChannelListController.swift+1−1 GDproject/Controller/ News and channels/NewsController.swift+30−32 GDproject/Controller/ News and channels/NewsVC.swift+14−6 GDproject/Controller/ News and channels/PostViewCell.swift+8−3 GDproject/Controller/Coordinators/ChannelsCoordinator.swift+24−4 GDproject/Simple model/Model.swift+82
GDproject/Base.lproj/Main.storyboard+2
@@ -526,6 +526,7 @@
526526 <constraint firstItem="4pw-5n-XsC" firstAttribute="leading" secondItem="Qwh-6O-iyN" secondAttribute="leading" id="nau-1I-Bh7"/>
527527 <constraint firstAttribute="trailing" secondItem="4pw-5n-XsC" secondAttribute="trailing" id="vnT-kX-h1h"/>
528528 </constraints>
529+ <viewLayoutGuide key="safeArea" id="uUX-ik-SiW"/>
529530 </view>
530531 <connections>
531532 <outlet property="tableView" destination="4pw-5n-XsC" id="usq-Vi-rXA"/>
@@ -543,6 +544,7 @@
543544 <rect key="frame" x="0.0" y="0.0" width="375" height="323.5"/>
544545 <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
545546 <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
547+ <viewLayoutGuide key="safeArea" id="Vki-Ji-EOU"/>
546548 </view>
547549 </viewController>
548550 <placeholder placeholderIdentifier="IBFirstResponder" id="KTe-P9-g8t" userLabel="First Responder" sceneMemberID="firstResponder"/>
GDproject/Controller/ News and channels/ChannelListController.swift+1−1
@@ -100,7 +100,7 @@ class ChannelListController: UITableViewController {
100100 navigationController?.pushViewController(vc, animated: true)
101101 }
102102
103− static let generalChannel = Model.Channels(people: [], name: "General", id: -1, tags: [])
103+ static let generalChannel = Model.Channels(people: [], name: "General", tags: [])
104104
105105 var dataSource : [Model.Channels] = [ ChannelListController.generalChannel ]
106106
GDproject/Controller/ News and channels/NewsController.swift+30−32
@@ -21,19 +21,10 @@ protocol UpdateableWithUser: class {
2121}
2222// MARK:- Controller with posts and channels availiable.
2323// Search is availiable within every table (posts and channels). Has button-functionality for boths post and chnnels
24−class NewsController: UIViewController, UISearchControllerDelegate, NewPostDelegate, UpdateableWithChannel, DataDelegate
24+class NewsController: UIViewController, UISearchControllerDelegate, NewPostDelegate, UpdateableWithChannel
2525{
26−
2726 var changedChannelName: ((String)->())?
2827
29− func passData(for row: Int, channel: Model.Channels) {
30− if channel.id == -1{
31− self.channel = nil
32− } else {
33− self.channel = channel
34− }
35− }
36−
3728 @IBOutlet weak var tableView: UITableView!
3829
3930 var dictionary: [Int: Model.Users]? {
@@ -49,19 +40,15 @@ class NewsController: UIViewController, UISearchControllerDelegate, NewPostDeleg
4940
5041 })
5142
52−
5343 news.dataSourse = newPosts
5444 tableView.reloadData()
5545 }
5646 }
5747
58− var posts: [Model.Posts]? {
59− didSet{
60−
61− }
62− }
48+ var posts: [Model.Posts]?
6349
6450 var channel: Model.Channels?
51+ var anonymousChannel: (users: [Int: Model.Users],posts: [Model.Posts])?
6552
6653 // MARK: - Output -
6754 var onSelectChannel: (() -> Void)?
@@ -73,6 +60,7 @@ class NewsController: UIViewController, UISearchControllerDelegate, NewPostDeleg
7360 var searchController = UISearchController(searchResultsController: nil)
7461
7562 var news = NewsVC()
63+ var type: HeaderType? = .NEWS
7664
7765 var refreshContr = UIRefreshControl()
7866
@@ -90,7 +78,7 @@ class NewsController: UIViewController, UISearchControllerDelegate, NewPostDeleg
9078 // setUpSearchContr()
9179
9280 news.viewController = self
93− news.type = .NEWS
81+ news.type = type == .NEWS ? .NEWS : type!
9482
9583 setUpNavigationItemsforPosts()
9684
@@ -134,22 +122,32 @@ class NewsController: UIViewController, UISearchControllerDelegate, NewPostDeleg
134122 }
135123
136124 func decideWhatChannelDisplay(){
137− if let channel = channel, let id = channel.id {
138−
139− Model.getChannel(with: id) { [weak self] in
140− self?.posts = $0.posts
141− self?.dictionary = $0.users
142− self?.refreshContr.endRefreshing()
143− self?.changedChannelName?(channel.name)
125+ switch type! {
126+ case .NEWS, .NONE:
127+ if let channel = channel, let id = channel.id {
128+
129+ Model.getChannel(with: id) { [weak self] in
130+ self?.posts = $0.posts
131+ self?.dictionary = $0.users
132+ self?.refreshContr.endRefreshing()
133+ self?.changedChannelName?(channel.name)
134+ }
135+
136+ } else if let _ = posts {
137+ print("here!")
138+ } else {
139+ Model.getLast { [weak self] in
140+ self?.posts = $0.posts
141+ self?.dictionary = $0.users
142+ self?.refreshContr.endRefreshing()
143+ self?.changedChannelName?("General")
144+ }
144145 }
145−
146− } else {
147−
148− Model.getLast { [weak self] in
149− self?.posts = $0.posts
150− self?.dictionary = $0.users
151− self?.refreshContr.endRefreshing()
152− self?.changedChannelName?("General")
146+ default:
147+ if let anonChannel = anonymousChannel {
148+ posts = anonChannel.posts
149+ dictionary = anonChannel.users
150+ changedChannelName?("Anonymous")
153151 }
154152 }
155153 }
GDproject/Controller/ News and channels/NewsVC.swift+14−6
@@ -27,6 +27,10 @@ struct PostCellData{
2727
2828class NewsVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
2929
30+ var onChannelDidChange: ((([Int: Model.Users],[Model.Posts]))->())?
31+
32+ var onFullPost: ((HeaderType, Model.Posts)->())?
33+
3034 var dataSourse: [Model.Posts] = [] {
3135 didSet {
3236 cellDataSourse = []
@@ -48,11 +52,7 @@ class NewsVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
4852
4953 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
5054 {
51− tableView.deselectRow(at: indexPath, animated: true)
52− let vc = viewController!.storyboard!.instantiateViewController(withIdentifier: fullPostControllerId) as! FullPostController
53− vc.type = type
54− vc.post = dataSourse[indexPath.row]
55− viewController!.navigationController!.pushViewController(vc, animated: true)
55+ onFullPost?(type, dataSourse[indexPath.row])
5656 }
5757
5858 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
@@ -72,12 +72,19 @@ class NewsVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
7272 vc.idProfile = id
7373 self?.viewController!.navigationController!.pushViewController(vc, animated: true)
7474 }
75− case .NONE:
75+ default:
7676 cell.onUserDisplay = { (id) in
7777 print("tapped when profile is open already \(id)")
7878 }
7979 }
8080
81+ cell.onAnonymousChannelDisplay = {
82+ [weak self] (tag) in
83+ Model.getAnonymousChannel(by: Model.AnonymousChannel(people: [], tags: [tag]),
84+ completion: { (tuple) in self?.onChannelDidChange?(tuple) }
85+ )
86+ }
87+
8188 cell.selectionStyle = .none
8289 return cell
8390 }
@@ -95,4 +102,5 @@ class NewsVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
95102enum HeaderType {
96103 case NONE
97104 case NEWS
105+ case ANONYMOUS
98106}
GDproject/Controller/ News and channels/PostViewCell.swift+8−3
@@ -15,6 +15,8 @@ class PostViewCell: UITableViewCell
1515{
1616 var onUserDisplay: ((Int)->())?
1717
18+ var onAnonymousChannelDisplay: ((String)->())?
19+
1820 let nameLabel: UIButton = {
1921 let button = UIButton()
2022 button.setTitleColor(.black, for: .normal)
@@ -120,8 +122,7 @@ class PostViewCell: UITableViewCell
120122 for hash in hashtags {
121123 let button = UIButton()
122124 button.setTitle("#" + hash, for: .normal)
123− //button.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
124− //button.layer.cornerRadius = 10
125+ button.addTarget(self, action: #selector(setAnonymousChannel(on:)), for: .touchUpInside)
125126 button.titleLabel?.font = UIFont.monospacedDigitSystemFont(ofSize: 15, weight: .semibold)
126127 button.setTitleColor(#colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1), for: .normal)
127128 buttons.append(button)
@@ -179,7 +180,11 @@ class PostViewCell: UITableViewCell
179180 }
180181
181182 @objc func displayProfile(){
182− print("buttonTapped")
183183 onUserDisplay?(post!.authorId)
184184 }
185+
186+ @objc func setAnonymousChannel(on button: UIButton){
187+ print("\(button.titleLabel?.text ?? "nothing")")
188+ onAnonymousChannelDisplay?(String(button.titleLabel!.text!.dropFirst()))
189+ }
185190}
GDproject/Controller/Coordinators/ChannelsCoordinator.swift+24−4
@@ -34,18 +34,38 @@ class ChannelsCoordinator: BaseCoordinator{
3434 navigationController?.setViewControllers([channels,presentNewsController()], animated: false)
3535 }
3636
37− private func presentNewsController(with channel: Model.Channels? = nil) -> PulleyViewController {
37+ func presentNewsController(with channel: Model.Channels? = nil) -> PulleyViewController {
3838
3939 let mainContentVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: newsController) as! NewsController
40+ let drawerContentVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DrawerContentViewController")
41+
4042 mainContentVC.channel = channel
43+ mainContentVC.news.onFullPost = {
44+ [weak self] (type,post) in
45+
46+ let vc = self?.storyboard.instantiateViewController(withIdentifier: fullPostControllerId) as! FullPostController
47+ vc.type = type
48+ vc.post = post
49+ self?.navigationController!.pushViewController(vc, animated: true)
50+ }
4151
42− let drawerContentVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DrawerContentViewController")
52+ mainContentVC.news.onChannelDidChange = {
53+ [weak self, weak mainContentVC] (tuple) in print("hep")
54+ switch mainContentVC!.news.type {
55+ case .ANONYMOUS:
56+ mainContentVC!.posts = tuple.1
57+ mainContentVC!.dictionary = tuple.0
58+ default:
59+ let vc = self!.presentNewsController()
60+ (vc.primaryContentViewController as! NewsController).anonymousChannel = tuple
61+ (vc.primaryContentViewController as! NewsController).type = .ANONYMOUS
62+ self?.navigationController?.pushViewController(vc, animated: true)
63+ }
64+ }
4365
4466 let pulleyDrawerVC = PulleyViewController(contentViewController: mainContentVC, drawerViewController: drawerContentVC)
4567
4668 pulleyDrawerVC.initialDrawerPosition = .collapsed
47−
48−
4969 mainContentVC.changedChannelName = {
5070 [weak pulleyDrawerVC] (title) in pulleyDrawerVC?.navigationItem.title = title
5171 }
GDproject/Simple model/Model.swift+82
@@ -14,6 +14,7 @@ class Model{
1414 static let invalidTocken = 498
1515
1616 private static var isValidTocken: ((Int)->())? = { responce in
17+ print(responce)
1718 if responce == invalidTocken {
1819 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
1920 }
@@ -33,6 +34,7 @@ class Model{
3334 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
3435 static let channelsCreateURL = URL(string: "\(baseUrl)/channels/create")!
3536 static let channelsDeleteURL = URL(string: "\(baseUrl)/channels/delete")!
37+ static let channelsGetAnonURL = URL(string: "\(baseUrl)/channels/getAnonymous")!
3638
3739
3840 struct QueryPosts: Codable{
@@ -417,4 +419,84 @@ class Model{
417419 isValidTocken?(response.response?.statusCode ?? 498)
418420 }
419421 }
422+
423+ struct AnonymousChannel: Codable {
424+
425+ var limit = 10
426+ var request: RequestPeopleTags
427+
428+ enum CodingKeys: String, CodingKey {
429+ case limit
430+ case request
431+ }
432+
433+ init(people: [Int], tags: [String]) {
434+ self.request = RequestPeopleTags(people: people, tags: tags)
435+ }
436+
437+ func encode(to encoder: Encoder) throws {
438+ var container = encoder.container(keyedBy: CodingKeys.self)
439+ try container.encode(limit, forKey: .limit)
440+ try container.encode(request, forKey: .request)
441+ }
442+
443+ struct RequestPeopleTags: Codable {
444+
445+ var people: [Int]
446+ var tags: [String]
447+
448+ init(people: [Int], tags: [String]) {
449+ self.people = people
450+ self.tags = tags
451+ }
452+
453+ func encode(to encoder: Encoder) throws {
454+ var container = encoder.container(keyedBy: CodingKeys.self)
455+ try container.encode(people, forKey: .people)
456+ try container.encode(tags, forKey: .tags)
457+ }
458+
459+ enum CodingKeys: String, CodingKey {
460+ case people
461+ case tags
462+ }
463+
464+ }
465+ }
466+ /*
467+ {
468+ "limit": 11,
469+ "request": {
470+ "people": [
471+ 2,
472+ 7,
473+ 8
474+ ],
475+ "tags": [
476+ "thisIsHashTag",
477+ "thisIsAlsoHashTag"
478+ ]
479+ }
480+ }
481+ */
482+ static func getAnonymousChannel(by anonymousChannel: Model.AnonymousChannel, completion: @escaping (((users:[Int: Users], posts:[Posts]))->())){
483+
484+ var request = URLRequest(url: channelsGetAnonURL)
485+ request.httpMethod = "POST"
486+ request.httpBody = try? JSONEncoder().encode(anonymousChannel)
487+
488+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
489+ AF.request(request).response {
490+ (response) in
491+
492+ isValidTocken?(response.response?.statusCode ?? 498)
493+
494+ guard let json = response.data else { return }
495+
496+ guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { return }
497+
498+ // idUser = newQueery.users
499+ completion((newQueery.users, newQueery.posts))
500+ }
501+ }
420502}