Skip to content

Commit

Permalink
Update empty state for Wallet module
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Nov 3, 2023
1 parent 05ff25d commit 3c493bd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ class WalletHeaderCell: UITableViewCell {
make.top.equalTo(amountView.snp.bottom)
make.height.equalTo(BalanceButtonsView.height)
}

let separatorView = UIView()
contentView.addSubview(separatorView)
separatorView.snp.makeConstraints { make in
make.leading.trailing.equalToSuperview()
make.bottom.equalToSuperview()
make.height.equalTo(CGFloat.heightOneDp)
}

separatorView.backgroundColor = .themeSteel20
}

required init?(coder aDecoder: NSCoder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class WalletHeaderView: UITableViewHeaderFooterView {
backgroundView = UIView()
backgroundView?.backgroundColor = .themeNavigationBarBackground

let separatorView = UIView()
contentView.addSubview(separatorView)
separatorView.snp.makeConstraints { make in
make.leading.trailing.equalToSuperview()
make.top.equalToSuperview()
make.height.equalTo(CGFloat.heightOneDp)
}

separatorView.backgroundColor = .themeSteel20

addSubview(sortByButton)
sortByButton.snp.makeConstraints { maker in
maker.leading.centerY.equalToSuperview()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ class WalletViewController: ThemeViewController {

private let spinner = HUDActivityView.create(with: .medium24)

private let emptyView = PlaceholderView()
private let failedView = PlaceholderView()
private let invalidApiKeyView = PlaceholderView()

private var viewItems = [BalanceViewItem]()
private var headerViewItem: WalletModule.HeaderViewItem?

private var warningViewItem: CancellableTitledCaution?
private var viewItemsOffset: Int {
warningViewItem != nil ? 1 : 0
}

private var sortBy: String?
private var controlViewItem: WalletViewModel.ControlViewItem?
Expand Down Expand Up @@ -81,6 +77,7 @@ class WalletViewController: ThemeViewController {
tableView.registerCell(forClass: WalletHeaderCell.self)
tableView.registerCell(forClass: BalanceCell.self)
tableView.registerCell(forClass: TitledHighlightedDescriptionCell.self)
tableView.registerCell(forClass: PlaceholderCell.self)
tableView.registerHeaderFooter(forClass: WalletHeaderView.self)
tableView.registerHeaderFooter(forClass: SectionColorHeader.self)

Expand Down Expand Up @@ -118,20 +115,6 @@ class WalletViewController: ThemeViewController {
}
spinner.startAnimating()

view.addSubview(emptyView)
emptyView.snp.makeConstraints { maker in
maker.edges.equalTo(view.safeAreaLayoutGuide)
}

emptyView.image = UIImage(named: "add_to_wallet_2_48")
emptyView.text = "balance.empty.description".localized
emptyView.addPrimaryButton(
style: .yellow,
title: "balance.empty.add_coins".localized,
target: self,
action: #selector(onTapAddCoin)
)

view.addSubview(failedView)
failedView.snp.makeConstraints { maker in
maker.edges.equalTo(view.safeAreaLayoutGuide)
Expand Down Expand Up @@ -265,10 +248,6 @@ class WalletViewController: ThemeViewController {
// todo
}

@objc private func onTapAddCoin() {
openManageWallets()
}

private func sync(nftVisible _: Bool) {
// navigationItem.rightBarButtonItem = nftVisible ? UIBarButtonItem(image: UIImage(named: "nft_24"), style: .plain, target: self, action: #selector(onTapNft)) : nil
}
Expand Down Expand Up @@ -305,11 +284,6 @@ class WalletViewController: ThemeViewController {
tableView.isHidden = true
}

switch state {
case .empty: emptyView.isHidden = false
default: emptyView.isHidden = true
}

switch state {
case .syncFailed: failedView.isHidden = false
default: failedView.isHidden = true
Expand Down Expand Up @@ -353,12 +327,12 @@ class WalletViewController: ThemeViewController {
}

private func sync(warning: CancellableTitledCaution?) {
let needToRemove = warning == nil && warningViewItem != nil
let warningWasVisible = warningVisible
warningViewItem = warning
if isLoaded {
if needToRemove {
if warningWasVisible, !warningVisible {
tableView.beginUpdates()
tableView.deleteRows(at: [IndexPath(row: 0, section: 1)], with: .fade)
tableView.deleteRows(at: [IndexPath(row: 1, section: 0)], with: .fade)
tableView.endUpdates()
} else {
tableView.reloadData()
Expand Down Expand Up @@ -422,7 +396,7 @@ class WalletViewController: ThemeViewController {
}

updateIndexes.forEach {
if let cell = tableView.cellForRow(at: IndexPath(row: $0 + viewItemsOffset, section: 1)) as? BalanceCell {
if let cell = tableView.cellForRow(at: IndexPath(row: $0, section: 1)) as? BalanceCell {
bind(cell: cell, viewItem: viewItems[$0])
}
}
Expand Down Expand Up @@ -513,7 +487,7 @@ class WalletViewController: ThemeViewController {
}

private func handleRemove(indexPath: IndexPath) {
let index = indexPath.row - viewItemsOffset
let index = indexPath.row

guard index < viewItems.count else {
return
Expand All @@ -524,7 +498,11 @@ class WalletViewController: ThemeViewController {
viewItems.remove(at: index)

tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .fade)
if viewItems.isEmpty {
tableView.reloadRows(at: [indexPath], with: .fade)
} else {
tableView.deleteRows(at: [indexPath], with: .fade)
}
tableView.endUpdates()

viewModel.onDisable(element: element)
Expand Down Expand Up @@ -572,6 +550,10 @@ class WalletViewController: ThemeViewController {
let viewController = BottomSheetModule.backupPromptAfterCreate(account: account, sourceViewController: self)
present(viewController, animated: true)
}

private var warningVisible: Bool {
warningViewItem != nil
}
}

extension WalletViewController: UITableViewDataSource {
Expand All @@ -581,25 +563,30 @@ extension WalletViewController: UITableViewDataSource {

func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0: return 1
default: return viewItemsOffset + viewItems.count
case 0: return 1 + (warningVisible ? 1 : 0)
default: return viewItems.isEmpty ? 1 : viewItems.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: WalletHeaderCell.self), for: indexPath)
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: WalletHeaderCell.self), for: indexPath)

if let headerCell = cell as? WalletHeaderCell {
bindHeaderActions(cell: headerCell)
}
if let headerCell = cell as? WalletHeaderCell {
bindHeaderActions(cell: headerCell)
}

return cell
default:
if warningViewItem != nil, indexPath.row == 0 {
return cell
} else {
return tableView.dequeueReusableCell(withIdentifier: String(describing: TitledHighlightedDescriptionCell.self), for: indexPath)
}
default:
if viewItems.isEmpty {
return tableView.dequeueReusableCell(withIdentifier: String(describing: PlaceholderCell.self), for: indexPath)
}

return tableView.dequeueReusableCell(withIdentifier: String(describing: BalanceCell.self), for: indexPath)
}
}
Expand All @@ -612,17 +599,23 @@ extension WalletViewController: UITableViewDelegate {
if let cell = cell as? WalletHeaderCell {
bind(headerCell: cell)
}
default:

if let cell = cell as? TitledHighlightedDescriptionCell, let warningViewItem = warningViewItem {
cell.set(backgroundStyle: .transparent, isFirst: true)
cell.topOffset = .margin12
cell.bind(caution: warningViewItem)
cell.onBackgroundButton = { [weak self] in self?.onOpenWarning() }
cell.onCloseButton = warningViewItem.cancellable ? { [weak self] in self?.onCloseWarning() } : nil
}

default:
if let cell = cell as? BalanceCell {
bind(cell: cell, viewItem: viewItems[indexPath.row - viewItemsOffset])
bind(cell: cell, viewItem: viewItems[indexPath.row])
}

if let cell = cell as? PlaceholderCell {
cell.set(backgroundStyle: .transparent, isFirst: true)
cell.icon = UIImage(named: "add_to_wallet_2_48")
cell.text = "balance.empty.description".localized
}
}
}
Expand All @@ -641,21 +634,40 @@ extension WalletViewController: UITableViewDelegate {
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let headerHeight = {
WalletHeaderCell.height(viewItem: self.headerViewItem)
}

let warningHeight = {
TitledHighlightedDescriptionCell.height(containerWidth: tableView.width, text: self.warningViewItem?.text ?? "") + .margin32
}

switch indexPath.section {
case 0:
return WalletHeaderCell.height(viewItem: headerViewItem)
if indexPath.row == 0 {
return headerHeight()
} else {
return warningHeight()
}
default:
if warningViewItem != nil, indexPath.row == 0 {
return TitledHighlightedDescriptionCell.height(containerWidth: tableView.width, text: warningViewItem?.text ?? "") + .margin32
if viewItems.isEmpty {
var contentHeight: CGFloat = headerHeight() + WalletHeaderView.height

if warningVisible {
contentHeight += warningHeight()
}

return max(200, tableView.height - tableView.safeAreaInsets.height - contentHeight)
}

return BalanceCell.height()
}
}

func tableView(_: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch section {
case 0: return 0
default: return viewItems.isEmpty ? 0 : WalletHeaderView.height
default: return WalletHeaderView.height
}
}

Expand All @@ -669,7 +681,7 @@ extension WalletViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
switch section {
case 0: return nil
default: return viewItems.isEmpty ? nil : tableView.dequeueReusableHeaderFooterView(withIdentifier: String(describing: WalletHeaderView.self))
default: return tableView.dequeueReusableHeaderFooterView(withIdentifier: String(describing: WalletHeaderView.self))
}
}

Expand All @@ -682,10 +694,11 @@ extension WalletViewController: UITableViewDelegate {
case 0:
() // do nothing
default:
if warningViewItem != nil, indexPath.row == 0 {
if viewItems.isEmpty {
return
}
viewModel.onTap(element: viewItems[indexPath.item - viewItemsOffset].element)

viewModel.onTap(element: viewItems[indexPath.item].element)
}
}

Expand All @@ -694,7 +707,7 @@ extension WalletViewController: UITableViewDelegate {
case 0:
return nil
default:
if warningViewItem != nil, indexPath.row == 0 {
if viewItems.isEmpty {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ class WalletViewModel {
case .noAccount: state = .noAccount
case .loading: state = .loading
case let .loaded(items):
if items.isEmpty, !service.cexAccount {
state = .empty
} else {
state = .list(viewItems: items.map { _viewItem(item: $0) })
}
state = .list(viewItems: items.map { _viewItem(item: $0) })
case let .failed(reason):
switch reason {
case .syncFailed: state = .syncFailed
Expand All @@ -84,7 +80,7 @@ class WalletViewModel {
}

switch service.state {
case let .loaded(items): qrScanVisible = !service.watchAccount && !items.isEmpty
case .loaded: qrScanVisible = !service.watchAccount
default: qrScanVisible = false
}
}
Expand Down Expand Up @@ -305,8 +301,6 @@ extension WalletViewModel {
enum State: CustomStringConvertible {
case list(viewItems: [BalanceViewItem])
case noAccount
case empty
case watchEmpty
case loading
case syncFailed
case invalidApiKey
Expand All @@ -315,8 +309,6 @@ extension WalletViewModel {
switch self {
case let .list(viewItems): return "list: \(viewItems.count) view items"
case .noAccount: return "noAccount"
case .empty: return "empty"
case .watchEmpty: return "watchEmpty"
case .loading: return "loading"
case .syncFailed: return "syncFailed"
case .invalidApiKey: return "invalidApiKey"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,8 @@
"balance.searching.count" = "%@ tx";
"balance.syncing_percent" = "Syncing... %@";
"balance.synced_through" = "until %@";
"balance.add_coin" = "Add Coin";
"balance.invalid_api_key" = "Invalid API Key";
"balance.empty.add_coins" = "Add Coins";
"balance.empty.description" = "You haven't added any coins to this wallet.";
"balance.sort_by" = "Sort By";
"balance.sort.header" = "Sort by";
"balance.sort.valueHighToLow" = "Balance";
"balance.sort.az" = "Name";
Expand Down

0 comments on commit 3c493bd

Please sign in to comment.