Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable managing coins for Watch wallets #5362

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions UnstoppableWallet/UnstoppableWallet/Models/AccountType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,6 @@ enum AccountType {
}
}

var hideZeroBalances: Bool {
switch self {
case .evmAddress: return true
default: return false
}
}

var description: String {
switch self {
case let .mnemonic(words, salt, _):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@ class WalletTokenListService: IWalletTokenListService {
didSet {
switch internalState {
case .loaded(let items):
let hideZeroBalances = account.type.hideZeroBalances

if hideZeroBalances {
state = .loaded(items: items.filter {
$0.balanceData.balanceTotal != 0 || ($0.element.wallet?.token.type.isNative ?? false)
})
} else {
state = .loaded(items: items)
}
state = .loaded(items: items)
default:
state = internalState
}
Expand Down Expand Up @@ -84,14 +76,12 @@ class WalletTokenListService: IWalletTokenListService {
let priceItemMap = coinPriceService.itemMap(coinUids: elements.compactMap {
$0.priceCoinUid
})
let watchAccount = account.watchAccount

let items: [Item] = elements.filter { elementFilter?($0) ?? true }
.map { element in
let item = Item(
element: element,
isMainNet: elementService.isMainNet(element: element) ?? fallbackIsMainNet,
watchAccount: watchAccount,
balanceData: elementService.balanceData(element: element) ?? _cachedBalanceData(element: element, cacheContainer: cacheContainer) ?? fallbackBalanceData,
state: elementService.state(element: element) ?? fallbackAdapterState
)
Expand Down Expand Up @@ -341,15 +331,13 @@ extension WalletTokenListService {
class Item {
let element: WalletModule.Element
var isMainNet: Bool
var watchAccount: Bool
var balanceData: BalanceData
var state: AdapterState
var priceItem: WalletCoinPriceService.Item?

init(element: WalletModule.Element, isMainNet: Bool, watchAccount: Bool, balanceData: BalanceData, state: AdapterState) {
init(element: WalletModule.Element, isMainNet: Bool, balanceData: BalanceData, state: AdapterState) {
self.element = element
self.isMainNet = isMainNet
self.watchAccount = watchAccount
self.balanceData = balanceData
self.state = state
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,70 @@
import UIKit
import ThemeKit
import SnapKit
import ComponentKit
import HUD
import SnapKit
import ThemeKit
import UIKit

class WalletHeaderView: UITableViewHeaderFooterView {
static var height: CGFloat = TextDropDownAndSettingsView.height
static var height: CGFloat = .heightSingleLineCell

private let sortByButton = SecondaryButton()

private let sortAddCoinView = TextDropDownAndSettingsView()
private let stackView = UIStackView()
private let settingsButton = SecondaryCircleButton()
private let watchAccountImage = ImageComponent(size: .iconSize24)

var onTapSortBy: (() -> ())?
var onTapAddCoin: (() -> ())?
var onTapSortBy: (() -> Void)?
var onTapSettings: (() -> Void)?

override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)

backgroundView = UIView()
backgroundView?.backgroundColor = .themeNavigationBarBackground

contentView.addSubview(sortAddCoinView)
sortAddCoinView.snp.makeConstraints { maker in
maker.leading.top.trailing.equalToSuperview()
maker.height.equalTo(TextDropDownAndSettingsView.height)
addSubview(sortByButton)
sortByButton.snp.makeConstraints { maker in
maker.leading.centerY.equalToSuperview()
}

sortAddCoinView.onTapDropDown = { [weak self] in self?.onTapSortBy?() }
sortAddCoinView.onTapSettings = { [weak self] in self?.onTapAddCoin?() }
sortByButton.set(style: .transparent, image: UIImage(named: "arrow_small_down_20"))
sortByButton.addTarget(self, action: #selector(onTapSortByButton), for: .touchUpInside)

contentView.addSubview(watchAccountImage)
watchAccountImage.snp.makeConstraints { maker in
addSubview(stackView)
stackView.snp.makeConstraints { maker in
maker.trailing.equalToSuperview().inset(CGFloat.margin16)
maker.centerY.equalTo(sortAddCoinView)
maker.centerY.equalToSuperview()
}

stackView.axis = .horizontal
stackView.spacing = .margin16

stackView.addArrangedSubview(watchAccountImage)
watchAccountImage.imageView.image = UIImage(named: "binocule_24")?.withTintColor(.themeGray)

stackView.addArrangedSubview(settingsButton)
settingsButton.set(image: UIImage(named: "manage_2_20"))
settingsButton.addTarget(self, action: #selector(onTapSettingsButton), for: .touchUpInside)
}

required init?(coder aDecoder: NSCoder) {
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func bind(sortBy: String?) {
sortAddCoinView.set(dropdownTitle: sortBy)
@objc private func onTapSortByButton() {
onTapSortBy?()
}

func bind(controlViewItem: WalletViewModel.ControlViewItem) {
sortAddCoinView.set(settingsHidden: !controlViewItem.coinManagerVisible)
watchAccountImage.isHidden = !controlViewItem.watchVisible
@objc private func onTapSettingsButton() {
onTapSettings?()
}

func set(sortByTitle: String?) {
sortByButton.setTitle(sortByTitle, for: .normal)
}

func set(controlViewItem: WalletViewModel.ControlViewItem) {
watchAccountImage.isHidden = !controlViewItem.watchVisible
settingsButton.isHidden = !controlViewItem.coinManagerVisible
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ class WalletService {
didSet {
switch internalState {
case .loaded(let items):
let hideZeroBalances = activeAccount?.type.hideZeroBalances ?? false

if hideZeroBalances {
state = .loaded(items: items.filter { $0.balanceData.balanceTotal != 0 || ($0.element.wallet?.token.type.isNative ?? false) })
} else {
state = .loaded(items: items)
}
state = .loaded(items: items)
default:
state = internalState
}
Expand Down Expand Up @@ -158,13 +152,11 @@ class WalletService {
case .loaded(let elements):
let cacheContainer = activeAccount.map { cacheManager.cacheContainer(accountId: $0.id) }
let priceItemMap = coinPriceService.itemMap(coinUids: elements.compactMap { $0.priceCoinUid })
let watchAccount = watchAccount

let items: [Item] = elements.map { element in
let item = Item(
element: element,
isMainNet: elementService.isMainNet(element: element) ?? fallbackIsMainNet,
watchAccount: watchAccount,
balanceData: elementService.balanceData(element: element) ?? _cachedBalanceData(element: element, cacheContainer: cacheContainer) ?? fallbackBalanceData,
state: elementService.state(element: element) ?? fallbackAdapterState
)
Expand Down Expand Up @@ -554,15 +546,13 @@ extension WalletService {
class Item {
let element: WalletModule.Element
var isMainNet: Bool
var watchAccount: Bool
var balanceData: BalanceData
var state: AdapterState
var priceItem: WalletCoinPriceService.Item?

init(element: WalletModule.Element, isMainNet: Bool, watchAccount: Bool, balanceData: BalanceData, state: AdapterState) {
init(element: WalletModule.Element, isMainNet: Bool, balanceData: BalanceData, state: AdapterState) {
self.element = element
self.isMainNet = isMainNet
self.watchAccount = watchAccount
self.balanceData = balanceData
self.state = state
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class WalletViewController: ThemeViewController {
private let spinner = HUDActivityView.create(with: .medium24)

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

Expand Down Expand Up @@ -133,14 +132,6 @@ class WalletViewController: ThemeViewController {
action: #selector(onTapAddCoin)
)

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

watchEmptyView.image = UIImage(named: "empty_wallet_48")
watchEmptyView.text = "balance.watch_empty.description".localized

view.addSubview(failedView)
failedView.snp.makeConstraints { maker in
maker.edges.equalTo(view.safeAreaLayoutGuide)
Expand Down Expand Up @@ -319,11 +310,6 @@ class WalletViewController: ThemeViewController {
default: emptyView.isHidden = true
}

switch state {
case .watchEmpty: watchEmptyView.isHidden = false
default: watchEmptyView.isHidden = true
}

switch state {
case .syncFailed: failedView.isHidden = false
default: failedView.isHidden = true
Expand Down Expand Up @@ -354,15 +340,15 @@ class WalletViewController: ThemeViewController {
self.sortBy = sortBy

if isLoaded, let headerView = tableView.headerView(forSection: 1) as? WalletHeaderView {
headerView.bind(sortBy: sortBy)
headerView.set(sortByTitle: sortBy)
}
}

private func sync(controlViewItem: WalletViewModel.ControlViewItem?) {
self.controlViewItem = controlViewItem

if isLoaded, let controlViewItem, let headerView = tableView.headerView(forSection: 1) as? WalletHeaderView {
headerView.bind(controlViewItem: controlViewItem)
headerView.set(controlViewItem: controlViewItem)
}
}

Expand Down Expand Up @@ -643,13 +629,14 @@ extension WalletViewController: UITableViewDelegate {

func tableView(_: UITableView, willDisplayHeaderView view: UIView, forSection _: Int) {
if let headerView = view as? WalletHeaderView {
headerView.bind(sortBy: sortBy)
headerView.set(sortByTitle: sortBy)

if let controlViewItem {
headerView.bind(controlViewItem: controlViewItem)
headerView.set(controlViewItem: controlViewItem)
}

headerView.onTapSortBy = { [weak self] in self?.openSortType() }
headerView.onTapAddCoin = { [weak self] in self?.openManageWallets() }
headerView.onTapSettings = { [weak self] in self?.openManageWallets() }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class WalletViewModel {
case .loading: state = .loading
case let .loaded(items):
if items.isEmpty, !service.cexAccount {
state = service.watchAccount ? .watchEmpty : .empty
state = .empty
} else {
state = .list(viewItems: items.map { _viewItem(item: $0) })
}
Expand All @@ -94,7 +94,7 @@ class WalletViewModel {
nftVisible = activeAccount?.type.supportsNft ?? false

controlViewItem = activeAccount.map {
ControlViewItem(watchVisible: $0.watchAccount, coinManagerVisible: !$0.cexAccount && !$0.watchAccount)
ControlViewItem(watchVisible: $0.watchAccount, coinManagerVisible: !$0.cexAccount)
}

if let account = activeAccount {
Expand Down Expand Up @@ -202,7 +202,7 @@ extension WalletViewModel {
}

var swipeActionsEnabled: Bool {
!service.watchAccount && !service.cexAccount
!service.cexAccount
}

var lastCreatedAccount: Account? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@
"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.watch_empty.description" = "This wallet address does not have any balance";
"balance.sort_by" = "Sort By";
"balance.sort.header" = "Sort by";
"balance.sort.valueHighToLow" = "Balance";
Expand Down