@@ -11,11 +11,21 @@ import IGListKit
11
11
12
12
final class IssueLabelsSectionController : ListBindingSectionController < IssueLabelsModel > ,
13
13
ListBindingSectionControllerDataSource ,
14
- ListBindingSectionControllerSelectionDelegate {
15
-
16
- var expanded = false
17
-
18
- override init ( ) {
14
+ ListBindingSectionControllerSelectionDelegate ,
15
+ LabelsViewControllerDelegate {
16
+
17
+ private var expanded = false
18
+ private var owner : String
19
+ private var repo : String
20
+ private var number : Int
21
+ private var client : GithubClient
22
+ private var labelsOverride : [ RepositoryLabel ] ? = nil
23
+
24
+ init ( owner: String , repo: String , number: Int , client: GithubClient ) {
25
+ self . owner = owner
26
+ self . repo = repo
27
+ self . number = number
28
+ self . client = client
19
29
super. init ( )
20
30
selectionDelegate = self
21
31
dataSource = self
@@ -24,16 +34,20 @@ ListBindingSectionControllerSelectionDelegate {
24
34
// MARK: ListBindingSectionControllerDataSource
25
35
26
36
func sectionController( _ sectionController: ListBindingSectionController < ListDiffable > , viewModelsFor object: Any ) -> [ ListDiffable ] {
27
- guard let object = self . object,
28
- object. labels. count > 0
29
- else { return [ ] }
30
- let colors = object. labels. map { UIColor . fromHex ( $0. color) }
37
+ var viewModels = [ ListDiffable] ( )
31
38
32
- var viewModels : [ ListDiffable ] = [ IssueLabelSummaryModel ( colors: colors) ]
39
+ // use override labels when available
40
+ let labels = ( labelsOverride ?? self . object? . labels ?? [ ] )
41
+ let colors = labels. map { UIColor . fromHex ( $0. color) }
42
+
43
+ // avoid an empty summary cell
44
+ if labels. count > 0 {
45
+ viewModels. append ( IssueLabelSummaryModel ( colors: colors) )
46
+ }
33
47
if expanded {
34
- viewModels += object . labels as [ ListDiffable ]
48
+ viewModels += labels as [ ListDiffable ]
35
49
}
36
- if object. viewerCanUpdate {
50
+ if self . object? . viewerCanUpdate == true {
37
51
viewModels. append ( " edit " as ListDiffable )
38
52
}
39
53
@@ -53,7 +67,7 @@ ListBindingSectionControllerSelectionDelegate {
53
67
let cellClass : AnyClass
54
68
switch viewModel {
55
69
case is IssueLabelSummaryModel : cellClass = IssueLabelSummaryCell . self
56
- case is IssueLabelModel : cellClass = IssueLabelCell . self
70
+ case is RepositoryLabel : cellClass = IssueLabelCell . self
57
71
default : cellClass = IssueLabelEditCell . self
58
72
}
59
73
@@ -66,12 +80,39 @@ ListBindingSectionControllerSelectionDelegate {
66
80
collectionContext? . deselectItem ( at: index, sectionController: self , animated: true )
67
81
68
82
if collectionContext? . cellForItem ( at: index, sectionController: self ) is IssueLabelEditCell {
69
- // TODO
83
+ guard let controller = UIStoryboard ( name: " Labels " , bundle: nil ) . instantiateInitialViewController ( ) as? LabelsViewController
84
+ else { fatalError ( " Missing labels view controller " ) }
85
+ controller. configure (
86
+ selected: labelsOverride ?? self . object? . labels ?? [ ] ,
87
+ client: client,
88
+ owner: owner,
89
+ repo: repo,
90
+ delegate: self
91
+ )
92
+ viewController? . present ( UINavigationController ( rootViewController: controller) , animated: true )
70
93
} else {
71
94
expanded = !expanded
72
95
update ( animated: true )
73
96
}
74
97
}
75
98
76
- }
99
+ // MARK: LabelsViewControllerDelegate
100
+
101
+ func didDismiss( controller: LabelsViewController , selectedLabels: [ RepositoryLabel ] ) {
102
+ labelsOverride = selectedLabels
103
+ update ( animated: false )
104
+
105
+ let request = GithubClient . Request (
106
+ path: " repos/ \( owner) / \( repo) /issues/ \( number) " ,
107
+ method: . patch,
108
+ parameters: [ " labels " : selectedLabels. map { $0. name } ]
109
+ ) { [ weak self] ( response, _) in
110
+ if response. response? . statusCode != 200 {
111
+ self ? . labelsOverride = nil
112
+ self ? . update ( animated: true )
113
+ }
114
+ }
115
+ client. request ( request)
116
+ }
77
117
118
+ }
0 commit comments