git.iko.soy/dashared/GDproject
on master help sign in
channels are completed. needs minor modifications
commitdaecc84a367fb706def4df8cd7adb6b5230bf759 authorDarya Rednikina <rednikina.com@yandex.ru> dateWed 20 Feb 2019, 22:37 (8 years ago) parent5c69380
8 files changed+107−32
GDproject/Base.lproj/Main.storyboard+1−1 GDproject/Constants.swift+2 GDproject/Controller/ChannelController.swift+3−1 GDproject/Controller/ChannelListController.swift+64−9 GDproject/Controller/News/NewPostViewController.swift+15−17 GDproject/Controller/News/NewsController.swift+16−2 GDproject/Controller/News/NewsVC.swift+1−1 GDproject/Simple model/Channel.swift+5−1
GDproject/Base.lproj/Main.storyboard+1−1
@@ -14,7 +14,7 @@
1414 <!--News Controller-->
1515 <scene sceneID="tne-QT-ifu">
1616 <objects>
17− <viewController id="BYZ-38-t0r" customClass="NewsController" customModule="GDproject" customModuleProvider="target" sceneMemberID="viewController">
17+ <viewController storyboardIdentifier="NewsController" id="BYZ-38-t0r" customClass="NewsController" customModule="GDproject" customModuleProvider="target" sceneMemberID="viewController">
1818 <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
1919 <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
2020 <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
GDproject/Constants.swift+2
@@ -26,3 +26,5 @@ let channelListControllerId = "ChannelListController"
2626let itemCellId = "ItemCell"
2727
2828let channelControllerId = "ChannelController"
29+
30+let newsController = "NewsController"
GDproject/Controller/ChannelController.swift+3−1
@@ -68,8 +68,10 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
6868 switch searchBar.selectedScopeButtonIndex{
6969 case 0:
7070 channel?.people.append(dataSource[indexPath.row])
71+ print("people = \(channel!.people)")
7172 case 1:
7273 channel?.hashtags.append(dataSource[indexPath.row])
74+ print("hashtags = \(channel!.hashtags)")
7375 default:
7476 break
7577 }
@@ -85,7 +87,7 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
8587 })
8688 // TODO: dataPass
8789 case 1:
88− channel?.hashtags = channel!.people.filter({ (s) -> Bool in
90+ channel?.hashtags = channel!.hashtags.filter({ (s) -> Bool in
8991 !fullTags[indexPath.row].contains(s)
9092 })
9193 // TODO: dataPass
GDproject/Controller/ChannelListController.swift+64−9
@@ -16,6 +16,28 @@ struct ChannelData{
1616// TODO: make search controller availiable
1717class ChannelListController: UITableViewController, DataDelegate {
1818
19+ // MARK:- filter search controller
20+
21+ var filteredDataSource = [Channel]()
22+ var myProtocol: DataDelegate?
23+
24+ var isFiltering: Bool {
25+ return searchController.isActive && !searchBarIsEmpty()
26+ }
27+
28+ func searchBarIsEmpty() -> Bool {
29+ return searchController.searchBar.text?.isEmpty ?? true
30+ }
31+
32+ func filterContentForSearchText(_ searchText: String, scope: String = "All")
33+ {
34+ filteredDataSource = ChannelListController.dataSource.filter({(keys : Channel) -> Bool in
35+ return keys.title.lowercased().contains(searchText.lowercased())
36+ })
37+
38+ tableView.reloadData()
39+ }
40+
1941 func passData(for row: Int, channel: Channel) {
2042 ChannelListController.dataSource[row].hashtags = channel.hashtags
2143 ChannelListController.dataSource[row].people = channel.people
@@ -29,8 +51,8 @@ class ChannelListController: UITableViewController, DataDelegate {
2951 super.viewDidLoad()
3052 setUpNavigationBar()
3153 navigationItem.title = "Channels"
32−
33− searchController.searchBar.placeholder = "Search anything"
54+ searchController.obscuresBackgroundDuringPresentation = false
55+ searchController.searchBar.placeholder = "Search channel"
3456 navigationItem.searchController = searchController
3557 }
3658
@@ -48,7 +70,7 @@ class ChannelListController: UITableViewController, DataDelegate {
4870 {
4971 // insertion
5072 tableView.beginUpdates()
51− ChannelListController.dataSource.insert(Channel(title: "Untitled", subtitle: "No", hashtags: [], people: []), at: 0)
73+ ChannelListController.dataSource.insert(Channel(title: "Untitled", subtitle: "No", hashtags: [], people: [], posts: []), at: 0)
5274 tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .none)
5375 tableView.endUpdates()
5476
@@ -61,22 +83,37 @@ class ChannelListController: UITableViewController, DataDelegate {
6183 }
6284
6385 static var dataSource : [Channel] = [
64− Channel(title: "Title", subtitle: "subtitle", hashtags: ["# sad", "# happy"], people: ["Seva", "Andrey"]),
65− Channel(title: "Title2", subtitle: "subtitle2", hashtags: ["# studyhard", "# university"], people: ["Pasha", "Olya", "Andrey", "Ilya"]),
66− Channel(title: "Title3", subtitle: "subtitle3", hashtags: ["# lol", "# meme", "# hehe"], people: ["Superman"])
86+ Channel(title: "Title", subtitle: "subtitle", hashtags: ["# sad", "# happy"], people: ["Seva", "Andrey"], posts: [Post(dataArray: [.text("Le Lorem Ipsum est simplement du faux texte employé dans la composition et la mise en page avant impression. Le Lorem Ipsum est le faux texte standard.")], from: User(name: "vbogomazova", id: 2, fullName: "Богомазова Вероника Львовна"), date: "14.02.19 в 12:05")]),
87+ Channel(title: "Title2", subtitle: "subtitle2", hashtags: ["# studyhard", "# university"], people: ["Pasha", "Olya", "Andrey", "Ilya"], posts:
88+ [
89+ Post(dataArray: [.text("L'avantage du Lorem Ipsum sur un texte générique comme 'Du texte. Du texte. Du texte.' est qu'il possède une distribution de lettres plus ou moins normale.")], from: User(name: "vbogomazova", id: 2, fullName: "Богомазова Вероника Львовна"), date: "14.02.19 в 12:05"),
90+ Post(dataArray: [.text("par accident, souvent intentionnellement (histoire d'y rajouter de petits clins d'oeil, voire des phrases embarassantes).")], from: User(name: "vbogomazova", id: 2, fullName: "Богомазова Вероника Львовна"), date: "14.02.19 в 12:05")
91+ ]),
92+ Channel(title: "Title3", subtitle: "subtitle3", hashtags: ["# lol", "# meme", "# hehe"], people: ["Superman"], posts: [Post(dataArray: [.text("Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen de polices de texte. Il n'a pas fait que survivre cinq siècles, mais s'est aussi adapté à la bureautique informatique, sans que son contenu n'en soit modifié.")], from: User(name: "vbogomazova", id: 2, fullName: "Богомазова Вероника Львовна"), date: "14.02.19 в 12:05")])
6793 ]
6894
6995 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
70− return ChannelListController.dataSource.count
96+ if isFiltering {
97+ return filteredDataSource.count
98+ } else {
99+ return ChannelListController.dataSource.count
100+ }
71101 }
72102
73103 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
74104 {
105+
75106 let cell = tableView.dequeueReusableCell(withIdentifier: channelCellId, for: indexPath)
76− cell.textLabel?.text = ChannelListController.dataSource[indexPath.row].title
77− cell.detailTextLabel?.text = ChannelListController.dataSource[indexPath.row].subtitle
78107
79108
109+ if isFiltering{
110+ cell.textLabel?.text = filteredDataSource[indexPath.row].title
111+ cell.detailTextLabel?.text = filteredDataSource[indexPath.row].subtitle
112+ } else {
113+ cell.textLabel?.text = ChannelListController.dataSource[indexPath.row].title
114+ cell.detailTextLabel?.text = ChannelListController.dataSource[indexPath.row].subtitle
115+ }
116+
80117 return cell
81118 }
82119
@@ -109,4 +146,22 @@ class ChannelListController: UITableViewController, DataDelegate {
109146
110147 return [editButton, deleteButton]
111148 }
149+
150+ override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
151+ {
152+ if isFiltering {
153+ navigationController?.popViewController(animated: true)
154+ myProtocol?.passData(for: 0, channel: filteredDataSource[indexPath.row])
155+ } else {
156+ navigationController?.popViewController(animated: true)
157+ myProtocol?.passData(for: 0, channel: ChannelListController.dataSource[indexPath.row])
158+ }
159+ }
160+}
161+
162+
163+extension ChannelListController : UISearchResultsUpdating {
164+ func updateSearchResults(for searchController: UISearchController) {
165+ filterContentForSearchText(searchController.searchBar.text!)
166+ }
112167}
GDproject/Controller/News/NewPostViewController.swift+15−17
@@ -18,6 +18,7 @@ class NewPostViewController: UIViewController, UITextViewDelegate {
1818 let textStorage = MarklightTextStorage()
1919
2020 weak var parentVC: NewsController?
21+ var myProtocol: NewPostDelegate?
2122
2223 static var draft: String = ""
2324 var textView: UITextView!
@@ -110,13 +111,13 @@ class NewPostViewController: UIViewController, UITextViewDelegate {
110111 NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotifications), name: UIResponder.keyboardWillHideNotification, object: nil)
111112
112113 // Partial fixes to a long standing bug, to keep the caret inside the `UITextView` always visible
113− NotificationCenter.default.addObserver(forName: UITextView.textDidChangeNotification, object: textView, queue: OperationQueue.main) { (notification) -> Void in
114− if self.textView.textStorage.string.hasSuffix("\n") {
114+ NotificationCenter.default.addObserver(forName: UITextView.textDidChangeNotification, object: textView, queue: OperationQueue.main) { [weak self] (notification) -> Void in
115+ if self!.textView.textStorage.string.hasSuffix("\n") {
115116 CATransaction.setCompletionBlock({ () -> Void in
116− self.scrollToCaret(self.textView, animated: false)
117+ self!.scrollToCaret(self!.textView, animated: false)
117118 })
118119 } else {
119− self.scrollToCaret(self.textView, animated: false)
120+ self!.scrollToCaret(self!.textView, animated: false)
120121 }
121122 }
122123
@@ -125,8 +126,11 @@ class NewPostViewController: UIViewController, UITextViewDelegate {
125126 @objc func handleKeyboardNotifications(notification: NSNotification){
126127 if let userInfo = notification.userInfo{
127128 // UIKeyboardFrameEndUserInfoKey
129+
128130 let keyBoardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
129− bottomTextViewConstraint?.constant = notification.name == UIResponder.keyboardWillShowNotification ? keyBoardFrame.height : 0
131+
132+ bottomTextViewConstraint?.constant = notification.name == UIResponder.keyboardWillShowNotification ? keyBoardFrame.height - tabBarController!.tabBar.frame.height : 0
133+
130134 print(bottomTextViewConstraint!.constant)
131135
132136 UIView.animate(withDuration: 0, delay: 0, options: .curveEaseOut, animations: {
@@ -188,12 +192,7 @@ class NewPostViewController: UIViewController, UITextViewDelegate {
188192
189193
190194 // adding row to uiTableView after adding new post
191− guard let vc = parentVC else {return}
192− vc.tableView.beginUpdates()
193− let indexPath1: IndexPath = IndexPath(row: 0, section: 0)
194− vc.dataSourse.insert(p, at: 0)
195− vc.tableView.insertRows(at: [indexPath1], with: .fade)
196− vc.tableView.endUpdates()
195+ myProtocol?.addPost(post: p)
197196 moveBackToParentVC()
198197 // somewhere here i will be sending server notifications about new post arrival
199198 }
@@ -203,17 +202,18 @@ class NewPostViewController: UIViewController, UITextViewDelegate {
203202
204203 let saveAction = UIAlertAction(title: "Save draft", style: .default)
205204 {
205+ [weak self]
206206 _ in
207− NewPostViewController.draft = self.textView.text
208− self.moveBackToParentVC()
207+ NewPostViewController.draft = self?.textView.text ?? ""
208+ self?.moveBackToParentVC()
209209 }
210210
211211 let deleteAction = UIAlertAction(title: "Delete draft", style: .destructive)
212212 {
213− (_)
213+ [weak self] (_)
214214 in
215215 NewPostViewController.draft = ""
216− self.moveBackToParentVC()
216+ self?.moveBackToParentVC()
217217 }
218218
219219 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
@@ -242,8 +242,6 @@ class NewPostViewController: UIViewController, UITextViewDelegate {
242242 }
243243
244244
245−
246−
247245 func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
248246 print("Should interact with: \(URL)")
249247 return true
GDproject/Controller/News/NewsController.swift+16−2
@@ -10,11 +10,24 @@ import UIKit
1010import TinyConstraints
1111import Cartography
1212
13+
14+protocol NewPostDelegate {
15+ func addPost(post: Post)
16+}
17+
1318// MARK:- Controller with posts and channels availiable.
1419// Search is availiable within every table (posts and channels). Has button-functionality for boths post and chnnels
15−class NewsController: UIViewController, UISearchControllerDelegate, UITableViewDelegate, UITableViewDataSource
20+class NewsController: UIViewController, UISearchControllerDelegate, UITableViewDelegate, UITableViewDataSource, DataDelegate, NewPostDelegate
1621{
1722
23+ func passData(for row: Int, channel: Channel) {
24+ news.dataSourse = channel.posts
25+ navigationItem.title = channel.title
26+ }
27+
28+ func addPost(post: Post) {
29+ news.dataSourse.insert(post, at: 0)
30+ }
1831
1932 var array = ["Faculty 1", "Faculty 2","Faculty 3","Faculty 4","Faculty 5","Faculty 6","Faculty 7","Faculty 8","Faculty 9","Faculty 10","Faculty 11","Faculty 12","Faculty 13","Faculty 14","Faculty 15","Faculty 16","Faculty 17","Faculty 18"]
2033
@@ -128,6 +141,7 @@ class NewsController: UIViewController, UISearchControllerDelegate, UITableViewD
128141 super.viewWillAppear(animated)
129142 print("hello")
130143 navigationController?.navigationBar.prefersLargeTitles = true
144+ tableView.reloadData()
131145 }
132146
133147 func setUpNavigationItemsforPosts(){
@@ -147,7 +161,7 @@ class NewsController: UIViewController, UISearchControllerDelegate, UITableViewD
147161 vc.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Post", style: .plain, target: vc, action: #selector(vc.newPost))
148162 vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: vc, action: #selector(vc.closeView))
149163
150− vc.parentVC = self
164+ vc.myProtocol = self
151165
152166 let transition = CATransition()
153167 transition.duration = 0.5
GDproject/Controller/News/NewsVC.swift+1−1
@@ -37,7 +37,7 @@ class NewsVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
3737 }
3838 else if indexPath.section == 0 && type == .NEWS{
3939 let vc = viewController!.storyboard!.instantiateViewController(withIdentifier: channelListControllerId) as! ChannelListController
40−
40+ vc.myProtocol = viewController as? NewsController
4141 viewController!.navigationController!.pushViewController(vc, animated: true)
4242 }
4343 }
GDproject/Simple model/Channel.swift+5−1
@@ -15,10 +15,14 @@ class Channel {
1515 var hashtags: [String] = []
1616 var people: [String] = []
1717
18− init(title: String, subtitle: String, hashtags: [String], people: [String]) {
18+ var posts: [Post] = []
19+
20+ init(title: String, subtitle: String, hashtags: [String], people: [String], posts: [Post])
21+ {
1922 self.title = title
2023 self.subtitle = subtitle
2124 self.people = people
2225 self.hashtags = hashtags
26+ self.posts = posts
2327 }
2428}