HistoryViewController.swift
4.24 KB
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
//
// HistoryViewController.swift
// SwiftWarplyFramework
//
// Created by Manos Chorianopoulos on 18/7/22.
//
import UIKit
class HistoryViewController: AnalysisChildViewController {
var loading: Bool = false
var items: Array<swiftApi.CouponItemModel> = swiftApi().getOldCouponList()
var showMore = false
// TODO: remove this when configuring model
let hasMessage = true
// lifecycle
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.refreshControl = UIRefreshControl()
self.refreshControl?.addTarget(self, action: #selector(handleRefresh(_:)), for: .valueChanged)
handleRefresh(self.refreshControl!)
}
// mvp
@objc func load() {
if (loading) {
return;
}
showLoading()
items = swiftApi().getOldCouponList()
self.showMore = (self.items.count > 3)
if (self.showMore == true) {
self.items = Array(self.items[0...2])
}
showContent()
self.tableView.reloadData()
}
private func showLoading() {
loading = true
if (self.refreshControl!.isRefreshing) {
return;
}
self.refreshControl!.beginRefreshing()
}
private func showError() {
}
private func showContent() {
loading = false
self.refreshControl!.endRefreshing()
}
// private
func responseCallback (_ data: Array<swiftApi.CouponItemModel>?) -> Void {
self.items = data!
self.showMore = (self.items.count > 3)
if (self.showMore == true) {
self.items = Array(self.items[0...2])
}
showContent()
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
@objc func handleRefresh(_ refreshControl: UIRefreshControl) {
self.perform(_: #selector(load), with: nil, afterDelay: 0.5)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0) {
return 1
}
if (self.showMore == true) {
return 4
}
return items.count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (indexPath.section == 0) {
return hasMessage ? (items.count > 0 ? 380.0 : 480.0) : 280
}
return 140.0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// header
if (indexPath.section == 0) {
if (hasMessage) {
let cell = tableView.dequeueReusableCell(withIdentifier: "AnalysisHeaderMessageViewCell", for: indexPath) as! SwiftWarplyFramework.AnalysisHeaderMessageViewCell
cell.configureCell(isEmpty: items.count == 0)
return cell
}
return tableView.dequeueReusableCell(withIdentifier: "AnalysisHeaderViewCell", for: indexPath) as! SwiftWarplyFramework.AnalysisHeaderViewCell
}
if (self.showMore && indexPath.row == 3) {
let cell = tableView.dequeueReusableCell(withIdentifier: "AnalysisMoreViewCell", for: indexPath) as! SwiftWarplyFramework.AnalysisMoreViewCell
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "AnalysisItemViewCell", for: indexPath) as! SwiftWarplyFramework.AnalysisItemViewCell
cell.configureCell(item: items[indexPath.row])
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (indexPath.section == 0) {
return
}
if (self.showMore && indexPath.row == 3) {
self.items = swiftApi().getOldCouponList()
self.showMore = false
self.tableView.reloadData()
}
}
}