-
Notifications
You must be signed in to change notification settings - Fork 0
/
7C10AB20-FED1-47CF-BE59-A5861686FB46.codesnippet
207 lines (172 loc) · 6.19 KB
/
7C10AB20-FED1-47CF-BE59-A5861686FB46.codesnippet
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>q_collectVC_VM_View</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>All</string>
</array>
<key>IDECodeSnippetContents</key>
<string>
import Foundation
import UIKit
import RxSwift
import HandyJSON
import MJRefresh
/// 输入协议
protocol <#Name#>ListViewModelInputs {
/// 请求数据
/// - Parameter offset: 偏移量(页码)
func getData(_ offset: Int)
func touchAction(_ action: ActionModel)
}
/// 输出协议
protocol <#Name#>ListViewModelOutputs {
/// 数据序列
var dataObs: Observable<ListLatestFeedsResponse?>{ get }
/// 事件序列
var touchActionModel: Observable<ActionModel> { get }
}
/// VM协议
protocol <#Name#>ListViewModelType {
var inputs: <#Name#>ListViewModelInputs { get }
var outputs: <#Name#>ListViewModelOutputs { get }
}
/// VM实现
class <#Name#>ListViewModel: <#Name#>ListViewModelType,<#Name#>ListViewModelInputs, <#Name#>ListViewModelOutputs {
init() {
let dataObs = dataRelay.flatMap { offset in
NetworkService.rx_request(.listLatestFeeds(offset, 30))
.map { BaseResponse<ListLatestFeedsResponse>.deserialize(from: $0)?.data }
.catchErrorJustReturn(nil)
}.share()
self.dataObs = dataObs
touchActionModel = touchRelay
}
// MARK: - 输入协议
func getData(_ offset: Int) {
dataRelay.on(.next(offset))
}
func touchAction(_ action: ActionModel) {
touchRelay.on(.next(action))
}
// MARK: - 输出协议
/// 数据
let dataObs: Observable<ListLatestFeedsResponse?>
fileprivate let dataRelay = PublishSubject<Int>()
/// 点击事件
let touchActionModel: Observable<ActionModel>
fileprivate let touchRelay = PublishSubject<ActionModel>()
/// 实现VM协议
var inputs: <#Name#>ListViewModelInputs { return self }
var outputs: <#Name#>ListViewModelOutputs { return self }
}
class <#Name#>ListViewController: BaseViewController {
// MARK: - Data
fileprivate lazy var viewModel: <#Name#>ListViewModelType = <#Name#>ListViewModel()
let bag = DisposeBag()
// 偏移量
var offset = 0
// 是否有下一页
var hasNext = false
// 数据源
var dataSource = [PostModel]()
// MARK: - Subviews
@IBOutlet weak var collectV: UICollectionView!
// MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
initViews()
bindRx()
getData()
}
// MARK: - BindRx
private func bindRx() {
viewModel.outputs.dataObs.bind { [unowned self] res0 in
guard let res = res0 else { return }
// 是否有下一页
self.hasNext = res.hasNext
self.setFooter(res.hasNext, self.collectV)
// 处理分页数据
if self.offset == 0 {
self.dataSource = res.dataList
self.collectV.mj_header?.endRefreshing()
}else{
self.dataSource.append(contentsOf: res.dataList)
}
self.offset = res.nextOffset
self.collectV.reloadData()
}.disposed(by: bag)
}
// MARK: - initViews
private func initViews() {
title = ""
let width = (screenWidth - 6) / 3
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: width, height: width)
layout.minimumLineSpacing = 3
layout.minimumInteritemSpacing = 3
collectV.collectionViewLayout = layout
collectV.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[unowned self] in
self.offset = 0
self.getData()
})
collectV.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {[unowned self] in
self.setFooter(self.hasNext, self.collectV)
if self.hasNext {
self.getData()
}
})
}
// MARK: - Events
func getData() {
viewModel.inputs.getData(offset)
}
func setFooter(_ hasNext: Bool, _ scrollV: UIScrollView) {
hasNext ? scrollV.mj_footer?.resetNoMoreData()
: scrollV.mj_footer?.endRefreshingWithNoMoreData()
}
}
// MARK: - DataSource & Delegate
extension <#Name#>ListViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let item = dataSource[indexPath.row]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: <#Name#>ListCell.identifier, for: indexPath) as! <#Name#>ListCell
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = dataSource[indexPath.row]
print("didSelectItemAt = \(indexPath), item = \(item)")
}
}
// MARK: - cell
class <#Name#>ListCell: UICollectionViewCell {
@IBOutlet weak var bgImgV: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
layer.masksToBounds = true
layer.cornerRadius = 3
}
func setData(_ bgImgUrl: String) {
// bgImgV.setImage(bgImgUrl)
}
}</string>
<key>IDECodeSnippetIdentifier</key>
<string>7C10AB20-FED1-47CF-BE59-A5861686FB46</string>
<key>IDECodeSnippetLanguage</key>
<string>Xcode.SourceCodeLanguage.Swift</string>
<key>IDECodeSnippetSummary</key>
<string></string>
<key>IDECodeSnippetTitle</key>
<string>快速创建collectVC_VM_View</string>
<key>IDECodeSnippetUserSnippet</key>
<true/>
<key>IDECodeSnippetVersion</key>
<integer>2</integer>
</dict>
</plist>