-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProfileContactViewController.swift
More file actions
350 lines (294 loc) · 12.4 KB
/
ProfileContactViewController.swift
File metadata and controls
350 lines (294 loc) · 12.4 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// ProfileContactViewController.swift
// BankexWallet
//
// Created by Vladislav on 28.07.2018.
// Copyright © 2018 Alexander Vlasov. All rights reserved.
//
import UIKit
import MobileCoreServices
import CoreSpotlight
import SkeletonView
struct Mediator {
public static var contactAddr:String?
}
struct HistoryMediator {
enum TabItem: Int {
case wallet = 0, history, contacts, settings
}
public static var addr:String?
public static func handleDidSelect(item: Int) {
if item == TabItem.history.rawValue {
addr = nil
}
}
}
class ProfileContactViewController: BaseViewController,UITextFieldDelegate,UITextViewDelegate {
enum State {
case loading,empty,fill
}
@IBOutlet weak var nameContactLabel:UILabel!
@IBOutlet weak var addrContactLabel:UILabel!
@IBOutlet weak var infoView:UIView!
@IBOutlet weak var tableVIew:UITableView!
@IBOutlet weak var seeAllBtn:UIButton!
@IBOutlet weak var heightConstraint:NSLayoutConstraint!
//MARK: - Properties
lazy var alertViewController:UIAlertController = {
let alertVC = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let delButton = UIAlertAction(title:NSLocalizedString("Delete", comment: ""), style: .destructive) { _ in
guard let address = self.addrContactLabel?.text, self.service.contains(address: address) else { return }
self.service.delete(with: address) { _ in
self.searchManager.deindex()
self.navigationController?.popViewController(animated: true)
}
}
alertVC.addAction(delButton)
alertVC.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .default))
return alertVC
}()
override var navigationBarAppearance: NavigationBarAppearance? {
get {
return super.navigationBarAppearance ?? .whiteStyle
}
set {
super.navigationBarAppearance = newValue
}
}
var state:State = .loading {
didSet {
switch state {
case .loading:
tableVIew.isHidden = true
seeAllBtn.isHidden = true
case .empty:
tableVIew.isHidden = false
seeAllBtn.isHidden = true
case .fill:
tableVIew.isHidden = false
seeAllBtn.isHidden = false
}
}
}
var transactions:[ETHTransactionModel] = []
let service = ContactService()
var selectedContact:FavoriteModel!
var searchManager:SearchManager!
var sendService = SendEthServiceImplementation()
var clipboardView:ClipboardView!
//MARK: - LifeCircle
override func viewDidLoad() {
super.viewDidLoad()
setupClipboardView()
commonPrepare()
configureTableView()
prepareUserActivity()
}
func setupClipboardView() {
clipboardView = ClipboardView(frame: CGRect(x: 0, y: view.bounds.height, width: view.bounds.width, height: 58))
clipboardView.title = "Address copied to clipboard"
clipboardView.color = UIColor.clipboardColor
view.addSubview(clipboardView)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
manageTop()
updateUI()
state = .loading
updateTransactions()
}
func updateTransactions() {
TransactionsService().refreshTransactionsInSelectedNetwork(type: .ETH, forAddress: selectedContact!.address, node: nil) { isGood in
if isGood {
self.transactions = Array(self.sendService.getAllTransactions(addr:nil).prefix(3).filter({ tr -> Bool in
if tr.to == self.selectedContact.address.lowercased() {
return true
}
return false
}))
self.state = self.transactions.isEmpty ? .empty : .fill
self.tableVIew.reloadData()
}else {
print("Appear error")
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
manageTop(isHide: false)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? HomeViewController {
guard let address = selectedContact?.address,service.contains(address: address) else { return }
Mediator.contactAddr = address
controller.isFromContact = true
}else if let editVC = segue.destination as? EditViewController {
editVC.selectedContact = selectedContact
editVC.delegate = self
}
}
@IBAction func editContact() {
if UIDevice.isIpad {
let editContact = UIStoryboard(name: "ContactEdit", bundle: nil).instantiateInitialViewController() as! EditViewController
editContact.addCancelButtonIfNeed()
editContact.delegate = self
editContact.selectedContact = selectedContact
presentPopUp(editContact, size: CGSize(width: splitViewController!.view.bounds.width/2, height: splitViewController!.view.bounds.height/2))
}else {
self.performSegue(withIdentifier: "editSegue", sender: nil)
}
}
@IBAction func backButtonTapped() {
navigationController?.popViewController(animated: true)
}
private func commonPrepare() {
title = NSLocalizedString("Contacts", comment: "")
infoView.backgroundColor = UIColor.mainColor
}
private func manageTop(isHide:Bool = true) {
if isHide {
navigationController?.setNavigationBarHidden(isHide, animated: false)
return
}
navigationController?.isNavigationBarHidden = isHide
}
@IBAction func seeAll() {
HistoryMediator.addr = selectedContact.address
let historyVC = UIStoryboard(name: "TransactionHistory", bundle: nil).instantiateInitialViewController() as! TransactionHistoryViewController
show(historyVC, sender: self)
}
private func configureTableView() {
tableVIew.delegate = self
tableVIew.dataSource = self
tableVIew.register(UINib(nibName: TransactionInfoCell.identifer, bundle: nil), forCellReuseIdentifier: TransactionInfoCell.identifer)
tableVIew.register(UINib(nibName: EmptyTableCell.identifier, bundle: nil), forCellReuseIdentifier: EmptyTableCell.identifier)
tableVIew.register(UINib(nibName: DeleteCell.identifier, bundle: nil), forCellReuseIdentifier: DeleteCell.identifier)
tableVIew.backgroundColor = UIColor.bgMainColor
tableVIew.isScrollEnabled = true
heightConstraint.setMultiplier(multiplier: UIDevice.isIpad ? 1/4.76 : 1/3.3)
if #available(iOS 11.0, *) {
tableVIew.separatorInsetReference = .fromCellEdges
} else {
}
tableVIew.separatorInset.left = 48
tableVIew.tableFooterView = UIDevice.isIpad ? UIView() : UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 1))
}
private func prepareUserActivity() {
searchManager = SearchManager(contact: selectedContact)
let activity = selectedContact.userActivity
activity.isEligibleForSearch = true
self.userActivity = activity
searchManager.index()
}
private func updateUI() {
guard let selectedContact = selectedContact else { return }
nameContactLabel.text = selectedContact.name
addrContactLabel.text = selectedContact.address.formattedAddrToken(number: 5)
}
func removeContact() {
let alertViewController = UIAlertController(title: "Delete contact?", message: nil, preferredStyle: .alert)
alertViewController.addCancel()
alertViewController.addDestructive(title: NSLocalizedString("Delete", comment: "")) {
self.service.delete(with: self.selectedContact.address, completionHandler: { isSuccess in
if isSuccess {
self.navigationController?.popViewController(animated: true)
}
})
}
alertViewController.addPopover(in: view, rect: CGRect(x: 0, y: 0, width: 270, height: 105))
present(alertViewController, animated: true)
}
//MARK: - IBAction
@IBAction func sendFunds() {
if UIDevice.isIpad {
selectSection(0)
}
self.performSegue(withIdentifier: "isFromContact", sender: nil)
}
@IBAction func shareContact() {
let text = "\(selectedContact!.address)"
let activity = UIActivityViewController.activity(content: text)
activity.completionWithItemsHandler = { (type, isSuccess, items, error) in
if isSuccess && error == nil {
self.clipboardView.showClipboard()
}
}
if UIDevice.isIpad {
activity.addPopover(in: view, rect: CGRect(x: view.bounds.midX, y: view.bounds.maxY, width: 0, height: 0))
}
present(activity, animated: true)
}
}
extension ProfileContactViewController:EditViewContollerDelegate {
func didUpdateContact(name: String, address: String) {
service.contactByAddress(address) { contact in
self.selectedContact = contact
self.updateUI()
}
}
}
extension ProfileContactViewController:DeleteCellDelegate {
func didTapRemoveButton() {
removeContact()
}
}
extension ProfileContactViewController:UITableViewDataSource,UITableViewDelegate,SkeletonTableViewDataSource {
func numSections(in collectionSkeletonView: UITableView) -> Int {
return 1
}
func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
return TransactionInfoCell.identifer
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if UIDevice.isIpad && transactions.isEmpty {
return 2
}else if !UIDevice.isIpad && transactions.isEmpty {
return 1
}else if !transactions.isEmpty && UIDevice.isIpad {
return transactions.count + 1
}else {
return transactions.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if transactions.isEmpty && indexPath.row == 0 {
let emptyCell = tableView.dequeueReusableCell(withIdentifier: EmptyTableCell.identifier, for: indexPath) as! EmptyTableCell
emptyCell.setData("You don't have any transaction history yet")
return emptyCell
}else if transactions.isEmpty && UIDevice.isIpad && indexPath.row == 1 {
let delCell = tableView.dequeueReusableCell(withIdentifier: DeleteCell.identifier, for: indexPath) as! DeleteCell
delCell.delegate = self
return delCell
}else if indexPath.row == transactions.count {
let delCell = tableView.dequeueReusableCell(withIdentifier: DeleteCell.identifier, for: indexPath) as! DeleteCell
delCell.delegate = self
return delCell
}
let cell = tableVIew.dequeueReusableCell(withIdentifier: TransactionInfoCell.identifer, for: indexPath) as! TransactionInfoCell
cell.transaction = transactions[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if transactions.isEmpty && UIDevice.isIpad && indexPath.row == 0 {
return 240
}else if transactions.isEmpty && !UIDevice.isIpad {
return tableVIew.bounds.height
}else if (transactions.isEmpty && UIDevice.isIpad && indexPath.row == 1) || transactions.count == indexPath.row {
return 44
}else if !transactions.isEmpty && UIDevice.isIpad && indexPath.row == transactions.count - 1 {
return 71
}else {
return 53
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if UIDevice.isIpad && indexPath.row == transactions.count {
removeContact()
}else if UIDevice.isIpad && transactions.isEmpty && indexPath.row == 1 {
removeContact()
}
}
}