git.iko.soy/dashared/GDproject
on master help sign in
channels now can be created and edited. display function left
commit5c693807a5994376394b0fece9a24f070075ea02 authorDarya Rednikina <rednikina.com@yandex.ru> dateWed 20 Feb 2019, 17:20 (8 years ago) parentddfc937
5 files changed+183−46
GDproject.xcodeproj/project.pbxproj+4 GDproject/Base.lproj/Main.storyboard+24−21 GDproject/Controller/ChannelController.swift+83−12 GDproject/Controller/ChannelListController.swift+48−13 GDproject/Simple model/Channel.swift+24
GDproject.xcodeproj/project.pbxproj+4
@@ -12,6 +12,7 @@
1212 125BD5812217314A008A3575 /* NewsVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 125BD5802217314A008A3575 /* NewsVC.swift */; };
1313 12D7D133221C321600B35452 /* ChannelListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12D7D132221C321600B35452 /* ChannelListController.swift */; };
1414 12D7D135221C42B700B35452 /* ChannelController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12D7D134221C42B700B35452 /* ChannelController.swift */; };
15+ 12D7D137221D78E800B35452 /* Channel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12D7D136221D78E800B35452 /* Channel.swift */; };
1516 12DB7FD722181CD20096878E /* BasicInfoController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12DB7FD622181CD20096878E /* BasicInfoController.swift */; };
1617 12DB7FD922181E660096878E /* BasicInfoCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12DB7FD822181E660096878E /* BasicInfoCell.swift */; };
1718 12DB7FDB2218590C0096878E /* InfoCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12DB7FDA2218590C0096878E /* InfoCell.swift */; };
@@ -65,6 +66,7 @@
6566 125BD5802217314A008A3575 /* NewsVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewsVC.swift; sourceTree = "<group>"; };
6667 12D7D132221C321600B35452 /* ChannelListController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelListController.swift; sourceTree = "<group>"; };
6768 12D7D134221C42B700B35452 /* ChannelController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelController.swift; sourceTree = "<group>"; };
69+ 12D7D136221D78E800B35452 /* Channel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Channel.swift; sourceTree = "<group>"; };
6870 12DB7FD622181CD20096878E /* BasicInfoController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasicInfoController.swift; sourceTree = "<group>"; };
6971 12DB7FD822181E660096878E /* BasicInfoCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasicInfoCell.swift; sourceTree = "<group>"; };
7072 12DB7FDA2218590C0096878E /* InfoCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoCell.swift; sourceTree = "<group>"; };
@@ -148,6 +150,7 @@
148150 isa = PBXGroup;
149151 children = (
150152 12E36DB62214400A006FCDD7 /* Post.swift */,
153+ 12D7D136221D78E800B35452 /* Channel.swift */,
151154 12E36DB822144016006FCDD7 /* User.swift */,
152155 );
153156 path = "Simple model";
@@ -271,6 +274,7 @@
271274 buildActionMask = 2147483647;
272275 files = (
273276 12E36DD522156559006FCDD7 /* MyStackView.swift in Sources */,
277+ 12D7D137221D78E800B35452 /* Channel.swift in Sources */,
274278 12E36DB922144016006FCDD7 /* User.swift in Sources */,
275279 125BD5812217314A008A3575 /* NewsVC.swift in Sources */,
276280 12E36DB72214400A006FCDD7 /* Post.swift in Sources */,
GDproject/Base.lproj/Main.storyboard+24−21
@@ -297,35 +297,38 @@
297297 <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
298298 <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
299299 <subviews>
300+ <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="5My-GH-WDq">
301+ <rect key="frame" x="0.0" y="20" width="375" height="54"/>
302+ <subviews>
303+ <textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Title" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="VFE-We-ctc">
304+ <rect key="frame" x="16" y="12" width="343" height="30"/>
305+ <color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
306+ <fontDescription key="fontDescription" type="system" pointSize="14"/>
307+ <textInputTraits key="textInputTraits"/>
308+ </textField>
309+ </subviews>
310+ <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
311+ <constraints>
312+ <constraint firstAttribute="height" constant="54" id="AX7-No-rwt"/>
313+ <constraint firstItem="VFE-We-ctc" firstAttribute="leading" secondItem="5My-GH-WDq" secondAttribute="leading" constant="16" id="HBN-OE-miW"/>
314+ <constraint firstItem="VFE-We-ctc" firstAttribute="top" secondItem="5My-GH-WDq" secondAttribute="top" constant="12" id="N8q-hz-5yP"/>
315+ <constraint firstAttribute="bottom" secondItem="VFE-We-ctc" secondAttribute="bottom" constant="12" id="jqO-53-maF"/>
316+ <constraint firstAttribute="trailing" secondItem="VFE-We-ctc" secondAttribute="trailing" constant="16" id="zYt-AY-R9F"/>
317+ </constraints>
318+ </view>
300319 <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="fye-z6-WKO">
301− <rect key="frame" x="0.0" y="20" width="375" height="647"/>
320+ <rect key="frame" x="0.0" y="74" width="375" height="593"/>
302321 <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
303− <view key="tableHeaderView" contentMode="scaleToFill" id="5My-GH-WDq">
304− <rect key="frame" x="0.0" y="0.0" width="375" height="54"/>
305− <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
306− <subviews>
307− <textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Title" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="VFE-We-ctc">
308− <rect key="frame" x="8" y="12" width="359" height="30"/>
309− <color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
310− <fontDescription key="fontDescription" type="system" pointSize="14"/>
311− <textInputTraits key="textInputTraits"/>
312− </textField>
313− </subviews>
314− <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
315− <constraints>
316− <constraint firstItem="VFE-We-ctc" firstAttribute="leading" secondItem="5My-GH-WDq" secondAttribute="leading" constant="8" id="HBN-OE-miW"/>
317− <constraint firstItem="VFE-We-ctc" firstAttribute="top" secondItem="5My-GH-WDq" secondAttribute="top" constant="12" id="N8q-hz-5yP"/>
318− <constraint firstAttribute="bottom" secondItem="VFE-We-ctc" secondAttribute="bottom" constant="12" id="jqO-53-maF"/>
319− <constraint firstAttribute="trailing" secondItem="VFE-We-ctc" secondAttribute="trailing" constant="8" id="zYt-AY-R9F"/>
320− </constraints>
321− </view>
322322 </tableView>
323323 </subviews>
324324 <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
325325 <constraints>
326+ <constraint firstItem="fye-z6-WKO" firstAttribute="top" secondItem="5My-GH-WDq" secondAttribute="bottom" id="73C-jP-zKv"/>
326327 <constraint firstItem="C5s-a1-LTb" firstAttribute="bottom" secondItem="fye-z6-WKO" secondAttribute="bottom" id="HUf-vU-oDj"/>
327328 <constraint firstItem="fye-z6-WKO" firstAttribute="leading" secondItem="C5s-a1-LTb" secondAttribute="leading" id="Hbj-fv-SB5"/>
328− <constraint firstItem="fye-z6-WKO" firstAttribute="top" secondItem="C5s-a1-LTb" secondAttribute="top" id="dcM-jB-W3m"/>
329+ <constraint firstItem="5My-GH-WDq" firstAttribute="top" secondItem="C5s-a1-LTb" secondAttribute="top" id="Ms5-z0-6i1"/>
330+ <constraint firstItem="5My-GH-WDq" firstAttribute="trailing" secondItem="C5s-a1-LTb" secondAttribute="trailing" id="Pt4-do-bce"/>
331+ <constraint firstItem="5My-GH-WDq" firstAttribute="leading" secondItem="C5s-a1-LTb" secondAttribute="leading" id="pgz-uB-BfM"/>
329332 <constraint firstItem="C5s-a1-LTb" firstAttribute="trailing" secondItem="fye-z6-WKO" secondAttribute="trailing" id="zHE-tP-Siv"/>
330333 </constraints>
331334 <viewLayoutGuide key="safeArea" id="C5s-a1-LTb"/>
GDproject/Controller/ChannelController.swift+83−12
@@ -8,15 +8,22 @@
88
99import UIKit
1010
11+protocol DataDelegate {
12+ func passData(for row: Int, channel: Channel)
13+}
14+
1115class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UISearchBarDelegate
1216{
1317 @IBOutlet weak var tableView: UITableView!
1418
19+ var index: Int?
20+ var channel: Channel?
21+ var myProtocol: DataDelegate?
22+
1523 var dataSource: [String] = []
1624
17− var people: [String] = ["Ann", "Ilya", "Kostya"]
1825 var fullPeople: [String] = ["Other Ann", "Other Ilya", "Other Kostya", "Pasha", "Valera"]
19− var tags: [String] = ["# foo", "# bar", "# kek"]
26+
2027 var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
2128
2229 @IBOutlet weak var viewww: UIView!
@@ -27,15 +34,22 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
2734 super.viewDidLoad()
2835 tableView.delegate = self
2936 tableView.dataSource = self
30− dataSource = people
37+ dataSource = channel!.people
3138 tableView.register(UITableViewCell.self, forCellReuseIdentifier: itemCellId)
3239 tableView.reloadData()
3340 textField.delegate = self
3441 searchBar.delegate = self
3542 searchBar.selectedScopeButtonIndex = 0
36− navigationItem.title = "Title"
43+ navigationItem.title = channel?.title
44+ textField.placeholder = channel?.title
45+ textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
3746 }
3847
48+ @objc func changedText(_ textField: UITextField){
49+ navigationItem.title = textField.text
50+ channel?.title = textField.text!
51+ myProtocol?.passData(for: index!, channel: channel!)
52+ }
3953
4054 func numberOfSections(in tableView: UITableView) -> Int {
4155 return 1
@@ -45,10 +59,73 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
4559 return dataSource.count
4660 }
4761
62+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
63+ if searchBar.isFirstResponder {
64+ if let cell = tableView.cellForRow(at: indexPath)
65+ {
66+
67+ if cell.accessoryType == .none{
68+ switch searchBar.selectedScopeButtonIndex{
69+ case 0:
70+ channel?.people.append(dataSource[indexPath.row])
71+ case 1:
72+ channel?.hashtags.append(dataSource[indexPath.row])
73+ default:
74+ break
75+ }
76+
77+ cell.accessoryType = .checkmark
78+ } else {
79+ cell.accessoryType = .none
80+
81+ switch searchBar.selectedScopeButtonIndex{
82+ case 0:
83+ channel?.people = channel!.people.filter({ (s) -> Bool in
84+ !fullPeople[indexPath.row].contains(s)
85+ })
86+ // TODO: dataPass
87+ case 1:
88+ channel?.hashtags = channel!.people.filter({ (s) -> Bool in
89+ !fullTags[indexPath.row].contains(s)
90+ })
91+ // TODO: dataPass
92+ default:
93+ break
94+ }
95+ }
96+ }
97+ } else
98+ {
99+ tableView.beginUpdates()
100+ dataSource.remove(at: indexPath.row)
101+ switch searchBar.selectedScopeButtonIndex{
102+ case 0:
103+ channel?.people.remove(at: indexPath.row)
104+ // TODO: dataPass
105+ case 1:
106+ channel?.hashtags.remove(at: indexPath.row)
107+ // TODO: dataPass
108+ default:
109+ break
110+ }
111+ tableView.deleteRows(at: [indexPath], with: .none)
112+ tableView.endUpdates()
113+ }
114+ }
115+
48116 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
49117 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
50118
51119 cell.textLabel?.text = dataSource[indexPath.row]
120+ cell.selectionStyle = .none
121+
122+ if channel!.hashtags.contains(dataSource[indexPath.row]) || channel!.people.contains(dataSource[indexPath.row])
123+ {
124+ cell.accessoryType = .checkmark
125+ } else {
126+ cell.accessoryType = .none
127+ }
128+
52129 return cell
53130 }
54131
@@ -78,12 +155,6 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
78155 return view
79156 }
80157
81− //change
82− func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
83− navigationItem.title = textField.text
84− return true
85− }
86−
87158 func textFieldShouldReturn(_ textField: UITextField) -> Bool
88159 {
89160 textField.resignFirstResponder()
@@ -128,14 +199,14 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
128199 }
129200 tableView.reloadData()
130201 } else if !(searchBar.isFirstResponder){
131− dataSource = searchBar.selectedScopeButtonIndex == 0 ? people : tags
202+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
132203 flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
133204 tableView.reloadData()
134205 }
135206 }
136207
137208 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
138− dataSource = searchBar.selectedScopeButtonIndex == 0 ? people : tags
209+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
139210 flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
140211 tableView.reloadData()
141212 }
GDproject/Controller/ChannelListController.swift+48−13
@@ -14,7 +14,14 @@ struct ChannelData{
1414}
1515
1616// TODO: make search controller availiable
17−class ChannelListController: UITableViewController{
17+class ChannelListController: UITableViewController, DataDelegate {
18+
19+ func passData(for row: Int, channel: Channel) {
20+ ChannelListController.dataSource[row].hashtags = channel.hashtags
21+ ChannelListController.dataSource[row].people = channel.people
22+ ChannelListController.dataSource[row].title = channel.title
23+ ChannelListController.dataSource[row].subtitle = channel.subtitle
24+ }
1825
1926 var searchController = UISearchController(searchResultsController: nil)
2027
@@ -31,22 +38,44 @@ class ChannelListController: UITableViewController{
3138 navigationItem.rightBarButtonItems = [UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addChannel))]
3239 }
3340
34− @objc func addChannel(){
35− print("Add channel")
41+ override func viewWillAppear(_ animated: Bool)
42+ {
43+ super.viewWillAppear(animated)
44+ tableView.reloadData()
45+ }
46+
47+ @objc func addChannel()
48+ {
49+ // insertion
50+ tableView.beginUpdates()
51+ ChannelListController.dataSource.insert(Channel(title: "Untitled", subtitle: "No", hashtags: [], people: []), at: 0)
52+ tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .none)
53+ tableView.endUpdates()
54+
55+ // editing mode is one automatically
56+ let vc = storyboard?.instantiateViewController(withIdentifier: channelControllerId) as! ChannelController
57+ vc.index = 0
58+ vc.myProtocol = self
59+ vc.channel = ChannelListController.dataSource[0]
60+ navigationController?.pushViewController(vc, animated: true)
3661 }
3762
38− var dataSource: [ChannelData] = [ ChannelData(title: "Channel 1", subtitle: "Some info about channel"),
39− ChannelData(title: "Channel 2", subtitle: "Some info about channel"),
40− ChannelData(title: "Channel 3", subtitle: "Some info about channel")]
63+ 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"])
67+ ]
4168
4269 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
43− return dataSource.count
70+ return ChannelListController.dataSource.count
4471 }
4572
46− override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
73+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
74+ {
4775 let cell = tableView.dequeueReusableCell(withIdentifier: channelCellId, for: indexPath)
48− cell.textLabel?.text = dataSource[indexPath.row].title
49− cell.detailTextLabel?.text = dataSource[indexPath.row].subtitle
76+ cell.textLabel?.text = ChannelListController.dataSource[indexPath.row].title
77+ cell.detailTextLabel?.text = ChannelListController.dataSource[indexPath.row].subtitle
78+
5079
5180 return cell
5281 }
@@ -56,22 +85,28 @@ class ChannelListController: UITableViewController{
5685 return true
5786 }
5887
59− override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
88+ override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
89+ {
90+
6091 let editButton = UITableViewRowAction(style: .normal, title: "Edit") { [weak self] (action, indexPath) in
6192 let vc = self?.storyboard?.instantiateViewController(withIdentifier: channelControllerId) as! ChannelController
62−
93+ vc.index = indexPath.row
94+ vc.myProtocol = self!
95+ vc.channel = ChannelListController.dataSource[indexPath.row]
6396 self?.navigationController?.pushViewController(vc, animated: true)
6497 }
6598 editButton.backgroundColor = .green
6699
100+
67101 let deleteButton = UITableViewRowAction(style: .normal, title: "Delete") { [weak self] (action, indexPath) in
68102 self?.tableView.beginUpdates()
69− self?.dataSource.remove(at: indexPath.row)
103+ ChannelListController.dataSource.remove(at: indexPath.row)
70104 self?.tableView.deleteRows(at: [indexPath], with: .none)
71105 self?.tableView.endUpdates()
72106 }
73107 deleteButton.backgroundColor = .red
74108
109+
75110 return [editButton, deleteButton]
76111 }
77112}
GDproject/Simple model/Channel.swiftnew file+24
@@ -0,0 +1,24 @@
1+//
2+// Channel.swift
3+// GDproject
4+//
5+// Created by cstore on 20/02/2019.
6+// Copyright © 2019 drHSE. All rights reserved.
7+//
8+
9+import Foundation
10+
11+class Channel {
12+ var title = String()
13+ var subtitle = String()
14+
15+ var hashtags: [String] = []
16+ var people: [String] = []
17+
18+ init(title: String, subtitle: String, hashtags: [String], people: [String]) {
19+ self.title = title
20+ self.subtitle = subtitle
21+ self.people = people
22+ self.hashtags = hashtags
23+ }
24+}