-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show warning on TRON token page if the account is inactive
- Loading branch information
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
UnstoppableWallet/UnstoppableWallet/Modules/Wallet/Token/DataSources/CautionDataSource.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import Combine | ||
import ComponentKit | ||
import Foundation | ||
import HUD | ||
import MarketKit | ||
import SectionsTableView | ||
import ThemeKit | ||
import UIKit | ||
|
||
class CautionDataSource: NSObject { | ||
private let viewModel: ICautionDataSourceViewModel | ||
private var cancellables: [AnyCancellable] = [] | ||
|
||
private var caution: TitledCaution? | ||
private var tableView: UITableView? | ||
|
||
weak var parentViewController: UIViewController? | ||
weak var delegate: ISectionDataSourceDelegate? | ||
|
||
init(viewModel: ICautionDataSourceViewModel) { | ||
self.viewModel = viewModel | ||
|
||
super.init() | ||
|
||
viewModel.cautionPublisher | ||
.receive(on: DispatchQueue.main) | ||
.sink { [weak self] in | ||
self?.sync(caution: $0) | ||
} | ||
.store(in: &cancellables) | ||
|
||
sync(caution: viewModel.caution) | ||
} | ||
|
||
private func sync(caution: TitledCaution?) { | ||
let oldCautionExists = self.caution != nil | ||
let newCautionExists = caution != nil | ||
self.caution = caution | ||
|
||
guard oldCautionExists == newCautionExists else { | ||
tableView?.reloadData() | ||
return | ||
} | ||
|
||
if let tableView { | ||
if newCautionExists { | ||
let indexPath = IndexPath(row: 0, section: 0) | ||
let originalIndexPath = delegate? | ||
.originalIndexPath(tableView: tableView, dataSource: self, indexPath: indexPath) ?? indexPath | ||
|
||
if let cell = tableView.cellForRow(at: originalIndexPath) as? TitledHighlightedDescriptionCell { | ||
bind(cell: cell, row: 0) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func bind(cell: TitledHighlightedDescriptionCell, row _: Int) { | ||
guard let caution else { | ||
return | ||
} | ||
cell.set(backgroundStyle: .externalBorderOnly, cornerRadius: .margin12, isFirst: true, isLast: true) | ||
cell.bind(caution: caution) | ||
} | ||
} | ||
|
||
extension CautionDataSource: ISectionDataSource { | ||
func prepare(tableView: UITableView) { | ||
tableView.registerCell(forClass: TitledHighlightedDescriptionCell.self) | ||
tableView.registerHeaderFooter(forClass: SectionColorHeader.self) | ||
self.tableView = tableView | ||
} | ||
} | ||
|
||
extension CautionDataSource: UITableViewDataSource { | ||
func numberOfSections(in _: UITableView) -> Int { | ||
1 | ||
} | ||
|
||
func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int { | ||
caution == nil ? 0 : 1 | ||
} | ||
|
||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
let originalIndexPath = delegate?.originalIndexPath(tableView: tableView, dataSource: self, indexPath: indexPath) ?? indexPath | ||
return tableView.dequeueReusableCell(withIdentifier: String(describing: TitledHighlightedDescriptionCell.self), for: originalIndexPath) | ||
} | ||
} | ||
|
||
extension CautionDataSource: UITableViewDelegate { | ||
func tableView(_: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { | ||
if let cell = cell as? TitledHighlightedDescriptionCell { | ||
bind(cell: cell, row: indexPath.row) | ||
} | ||
} | ||
|
||
func tableView(_ tableView: UITableView, heightForRowAt _: IndexPath) -> CGFloat { | ||
guard let caution else { | ||
return 0 | ||
} | ||
|
||
return TitledHighlightedDescriptionCell.height(containerWidth: tableView.width, text: caution.text) | ||
} | ||
|
||
func tableView(_ tableView: UITableView, viewForHeaderInSection _: Int) -> UIView? { | ||
guard caution != nil else { | ||
return nil | ||
} | ||
|
||
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: String(describing: SectionColorHeader.self)) as? SectionColorHeader | ||
view?.backgroundView?.backgroundColor = .clear | ||
return view | ||
} | ||
|
||
func tableView(_: UITableView, heightForHeaderInSection _: Int) -> CGFloat { | ||
caution == nil ? .zero : .margin8 | ||
} | ||
|
||
func tableView(_: UITableView, heightForFooterInSection _: Int) -> CGFloat { | ||
caution == nil ? .zero : .margin16 | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...allet/UnstoppableWallet/Modules/Wallet/Token/DataSources/CautionDataSourceViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Combine | ||
import HsExtensions | ||
import TronKit | ||
|
||
protocol ICautionDataSourceViewModel { | ||
var caution: TitledCaution? { get } | ||
var cautionPublisher: AnyPublisher<TitledCaution?, Never> { get } | ||
} | ||
|
||
class TronAccountInactiveViewModel { | ||
private let cautionSubject = PassthroughSubject<TitledCaution?, Never>() | ||
private(set) var caution: TitledCaution? { | ||
didSet { | ||
cautionSubject.send(caution) | ||
} | ||
} | ||
|
||
init(adapter: BaseTronAdapter) { | ||
caution = (adapter.receiveAddress as? ActivatedDepositAddress)?.isActive == true | ||
? nil | ||
: TitledCaution(title: "balance.token.account.inactive.title".localized, text: "balance.token.account.inactive.description".localized, type: .warning) | ||
} | ||
} | ||
|
||
extension TronAccountInactiveViewModel: ICautionDataSourceViewModel { | ||
var cautionPublisher: AnyPublisher<TitledCaution?, Never> { | ||
cautionSubject.eraseToAnyPublisher() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters