forked from xmartlabs/XLActionController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Twitter.swift
151 lines (131 loc) · 6.4 KB
/
Twitter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Twitter.swift
// Twitter ( https://github.com/xmartlabs/XLActionController )
//
// Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
#if XLACTIONCONTROLLER_EXAMPLE
import XLActionController
#endif
open class TwitterCell: ActionCell {
public override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
open override func awakeFromNib() {
super.awakeFromNib()
initialize()
}
func initialize() {
backgroundColor = .white
actionImageView?.clipsToBounds = true
actionImageView?.layer.cornerRadius = 5.0
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.15)
selectedBackgroundView = backgroundView
}
}
open class TwitterActionControllerHeader: UICollectionReusableView {
lazy var label: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textAlignment = .center
label.backgroundColor = .white
label.font = UIFont.boldSystemFont(ofSize: 17)
return label
}()
lazy var bottomLine: UIView = {
let bottomLine = UIView()
bottomLine.translatesAutoresizingMaskIntoConstraints = false
bottomLine.backgroundColor = .lightGray
return bottomLine
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
addSubview(label)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[label]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["label": label]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[label]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["label": label]))
addSubview(bottomLine)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[line(1)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["line": bottomLine]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[line]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["line": bottomLine]))
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
open class TwitterActionController: ActionController<TwitterCell, ActionData, TwitterActionControllerHeader, String, UICollectionReusableView, Void> {
public override init(nibName nibNameOrNil: String? = nil, bundle nibBundleOrNil: Bundle? = nil) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
settings.animation.present.duration = 0.6
settings.animation.dismiss.duration = 0.6
cellSpec = CellSpec.nibFile(nibName: "TwitterCell", bundle: Bundle(for: TwitterCell.self), height: { _ in 56 })
headerSpec = .cellClass(height: { _ -> CGFloat in return 45 })
onConfigureHeader = { header, title in
header.label.text = title
}
onConfigureCellForAction = { [weak self] cell, action, indexPath in
cell.setup(action.data?.title, detail: action.data?.subtitle, image: action.data?.image)
cell.separatorView?.isHidden = indexPath.item == (self?.collectionView.numberOfItems(inSection: indexPath.section))! - 1
cell.alpha = action.enabled ? 1.0 : 0.5
}
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open override func viewDidLoad() {
super.viewDidLoad()
collectionView.clipsToBounds = false
let hideBottomSpaceView: UIView = {
let hideBottomSpaceView = UIView(frame: CGRect(x: 0, y: 0, width: collectionView.bounds.width, height: contentHeight + 20))
hideBottomSpaceView.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
hideBottomSpaceView.backgroundColor = .white
return hideBottomSpaceView
}()
collectionView.addSubview(hideBottomSpaceView)
collectionView.sendSubview(toBack: hideBottomSpaceView)
}
override open func dismissView(_ presentedView: UIView, presentingView: UIView, animationDuration: Double, completion: ((_ completed: Bool) -> Void)?) {
onWillDismissView()
let animationSettings = settings.animation.dismiss
let upTime = 0.1
UIView.animate(withDuration: upTime, delay: 0, options: .curveEaseIn, animations: { [weak self] in
self?.collectionView.frame.origin.y -= 10
}, completion: { [weak self] (completed) -> Void in
UIView.animate(withDuration: animationDuration - upTime,
delay: 0,
usingSpringWithDamping: animationSettings.damping,
initialSpringVelocity: animationSettings.springVelocity,
options: UIViewAnimationOptions.curveEaseIn,
animations: { [weak self] in
presentingView.transform = CGAffineTransform.identity
self?.performCustomDismissingAnimation(presentedView, presentingView: presentingView)
},
completion: { [weak self] finished in
self?.onDidDismissView()
completion?(finished)
})
})
}
}