@@ -10,7 +10,12 @@ import UIKit
10
10
import CoreData
11
11
12
12
class FeedDefaultVC : FeedListVC {
13
+ private var _textField : UITextField ?
13
14
15
+ override var secAllArticles : Int { return 0 }
16
+ var secGroupList : Int { return 1 }
17
+ override var secFeedList : Int { return 2 }
18
+
14
19
override func viewDidLoad( ) {
15
20
super. viewDidLoad ( )
16
21
self . navigationItem. title = " Feeds "
@@ -28,35 +33,55 @@ class FeedDefaultVC: FeedListVC {
28
33
// 1 = group list
29
34
// 2 = feeds in default group
30
35
switch section {
31
- case 0 : return 1
32
- case 1 : return rss. manager. notDefaultGroups. count
33
- case 2 : return group. feeds. count
36
+ case secAllArticles : return 1
37
+ case secGroupList : return rss. manager. notDefaultGroups. count
38
+ case secFeedList : return group. feeds. count
34
39
default : return 0
35
40
}
41
+
36
42
}
37
43
38
44
override func tableView( tableView: UITableView , heightForRowAtIndexPath indexPath: NSIndexPath ) -> CGFloat {
39
45
return 70
40
46
}
41
47
42
48
override func tableView( tableView: UITableView , commitEditingStyle editingStyle: UITableViewCellEditingStyle , forRowAtIndexPath indexPath: NSIndexPath ) {
43
- switch editingStyle {
49
+ switch indexPath. section {
50
+
51
+ // group list edit.
52
+ case secGroupList:
53
+ return
44
54
45
- case . Delete:
46
-
47
- let feed = group. managedFeeds [ indexPath. row] as Feed
48
- group. mutableFeeds. removeObject ( feed)
49
- rss. manager. removeFeed ( feed)
50
- tableView. deleteRowsAtIndexPaths ( [ indexPath] , withRowAnimation: . Automatic)
55
+ // default group feed list edit.
56
+ case secFeedList:
57
+ switch editingStyle {
58
+
59
+ case . Delete:
60
+
61
+ let feed = group. managedFeeds [ indexPath. row] as Feed
62
+ group. mutableFeeds. removeObject ( feed)
63
+ rss. manager. removeFeed ( feed)
64
+ tableView. deleteRowsAtIndexPaths ( [ indexPath] , withRowAnimation: . Automatic)
51
65
52
- // case .Insert:
53
- // case .None:
66
+ // case .Insert:
67
+ // case .None:
68
+
69
+ default : break
70
+ }
54
71
55
- default :
56
- break
72
+ default : break
57
73
}
58
74
}
59
75
76
+ override func tableView( tableView: UITableView , canEditRowAtIndexPath indexPath: NSIndexPath ) -> Bool {
77
+ return indexPath. section != secAllArticles
78
+ }
79
+
80
+ // the "all articles" button cannot be moved.
81
+ override func tableView( tableView: UITableView , canMoveRowAtIndexPath indexPath: NSIndexPath ) -> Bool {
82
+ return indexPath. section != secAllArticles
83
+ }
84
+
60
85
override func tableView( tableView: UITableView , moveRowAtIndexPath sourceIndexPath: NSIndexPath , toIndexPath destinationIndexPath: NSIndexPath ) {
61
86
//FIXME: doesn't work.
62
87
swap ( & group. mutableFeeds [ sourceIndexPath. row] , & group. mutableFeeds [ destinationIndexPath. row] )
@@ -67,9 +92,9 @@ class FeedDefaultVC: FeedListVC {
67
92
// 1 = group list
68
93
// 2 = feeds in default group
69
94
switch indexPath. section {
70
- case 0 : return super. tableView ( tableView, cellForRowAtIndexPath: indexPath)
71
- case 1 : return cellForGroupAtRow ( indexPath. row)
72
- case 2 :
95
+ case secAllArticles : return super. tableView ( tableView, cellForRowAtIndexPath: indexPath)
96
+ case secGroupList : return cellForGroupAtRow ( indexPath. row)
97
+ case secFeedList :
73
98
let path = NSIndexPath ( forRow: indexPath. row, inSection: 1 )
74
99
return super. tableView ( tableView, cellForRowAtIndexPath: path)
75
100
default : return UITableViewCell ( )
@@ -86,13 +111,19 @@ class FeedDefaultVC: FeedListVC {
86
111
// user selected a feed.
87
112
override func tableView( tableView: UITableView , didSelectRowAtIndexPath indexPath: NSIndexPath ) {
88
113
89
- // group .
90
- if indexPath. section == 0 {
114
+ // show all articles in all groups .
115
+ if indexPath. section == secAllArticles {
91
116
pushArticleViewToCollection ( group)
92
117
return
93
118
}
94
119
95
- // feed.
120
+ // show feeds in group.
121
+ if indexPath. section == secGroupList {
122
+ let group = rss. manager. notDefaultGroups [ indexPath. row]
123
+ return pushFeedViewForGroup ( group)
124
+ }
125
+
126
+ // show articles in feed.
96
127
let feed = group. feeds [ indexPath. row]
97
128
98
129
// no articles; fetch them
@@ -105,18 +136,17 @@ class FeedDefaultVC: FeedListVC {
105
136
106
137
107
138
func addButtonTappedTwo( sender: AnyObject ) {
108
- let alert = UIAlertController ( title: nil , message: nil , preferredStyle: . ActionSheet)
109
-
139
+ let alert = UIAlertController ( title: " Create new... " , message: nil , preferredStyle: . ActionSheet)
140
+
110
141
// feed button.
111
142
let action1 = UIAlertAction ( title: " Feed " , style: . Default) { _ in
112
- return self . addButtonTapped ( sender)
143
+ return self . presentFeedCreator ( sender)
113
144
}
114
145
alert. addAction ( action1)
115
146
116
147
// group button.
117
148
let action2 = UIAlertAction ( title: " Group " , style: . Default) { _ in
118
-
119
- return
149
+ return self . presentGroupCreator ( sender)
120
150
}
121
151
alert. addAction ( action2)
122
152
@@ -127,4 +157,38 @@ class FeedDefaultVC: FeedListVC {
127
157
self . presentViewController ( alert, animated: true , completion: nil )
128
158
129
159
}
160
+
161
+ func presentGroupCreator( sender: AnyObject ) {
162
+ let alert = UIAlertController ( title: " Create group " , message: nil , preferredStyle: . Alert)
163
+
164
+ // text field.
165
+ alert. addTextFieldWithConfigurationHandler { textField in
166
+ self . _textField = textField
167
+ textField. placeholder = " URL "
168
+ }
169
+
170
+ // OK button.
171
+ let action = UIAlertAction ( title: " OK " , style: . Default) { _ in
172
+
173
+ // empty string?
174
+ let string = self . _textField!. text!
175
+ self . _textField = nil
176
+ if string. isEmpty { return }
177
+
178
+ // create the group.
179
+ let group = rss. manager. newGroupTitled ( string)
180
+ rss. manager. addGroup ( group)
181
+ let path = NSIndexPath ( forRow: rss. manager. notDefaultGroups. count - 1 , inSection: self . secGroupList)
182
+ self . tableView. insertRowsAtIndexPaths ( [ path] , withRowAnimation: . Automatic)
183
+
184
+ return
185
+ }
186
+ alert. addAction ( action)
187
+ alert. addAction ( UIAlertAction ( title: " Cancel " , style: . Cancel, handler: nil ) )
188
+
189
+ // present it.
190
+ self . presentViewController ( alert, animated: true , completion: nil )
191
+
192
+ }
193
+
130
194
}
0 commit comments