Merge pull request #1596 from AlphaWallet/fix-tokenscript-view-cells-height-wrong-remove-margins

Fix TokenScript view cells height detection and remove margins
pull/1602/head
James Sangalli 5 years ago committed by GitHub
commit 547bb46861
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      AlphaWallet/Tokens/ViewControllers/TokenInstanceActionViewController.swift
  2. 16
      AlphaWallet/Tokens/ViewControllers/TokensCardViewController.swift
  3. 4
      AlphaWallet/Tokens/Views/BaseTokenCardTableViewCell.swift
  4. 2
      AlphaWallet/Tokens/Views/TokenInstanceWebView.swift
  5. 92
      AlphaWallet/UI/Views/TokenCardRowView.swift

@ -279,4 +279,8 @@ extension TokenInstanceActionViewController: TokenInstanceWebViewDelegate {
}
delegate?.shouldCloseFlow(inViewController: self)
}
func heightChangedFor(tokenInstanceWebView: TokenInstanceWebView) {
//no-op. Auto layout handles it
}
}

@ -318,7 +318,11 @@ extension TokensCardViewController: UITableViewDelegate, UITableViewDataSource {
case .backedByOpenSea:
rowView = OpenSeaNonFungibleTokenCardRowView(tokenView: .viewIconified, showCheckbox: cell.showCheckbox())
case .notBackedByOpenSea:
rowView = TokenCardRowView(server: .main, tokenView: .viewIconified, showCheckbox: cell.showCheckbox(), assetDefinitionStore: assetDefinitionStore)
rowView = {
let rowView = TokenCardRowView(server: .main, tokenView: .viewIconified, showCheckbox: cell.showCheckbox(), assetDefinitionStore: assetDefinitionStore)
rowView.delegate = self
return rowView
}()
}
cell.delegate = self
cell.rowView = rowView
@ -388,3 +392,13 @@ extension TokensCardViewController: TokenCardsViewControllerHeaderDelegate {
showContractWebPage()
}
}
extension TokensCardViewController: TokenCardRowViewDelegate {
func heightChangedFor(tokenCardRowView: TokenCardRowView) {
//Important to not reload the cell because that will trigger an infinite recursion as the height will be calculated again and this func fired again
UIView.setAnimationsEnabled(false)
tableView.beginUpdates()
tableView.endUpdates()
UIView.setAnimationsEnabled(true)
}
}

@ -88,12 +88,14 @@ class BaseTokenCardTableViewCell: UITableViewCell {
rowView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(rowView)
var rowHeightConstraint = rowView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -GroupedTable.Metric.cellSeparatorHeight)
rowHeightConstraint.priority = .defaultHigh
NSLayoutConstraint.activate([
rowView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
rowView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
rowView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: GroupedTable.Metric.cellSpacing + GroupedTable.Metric.cellSeparatorHeight),
rowView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -GroupedTable.Metric.cellSeparatorHeight)
rowHeightConstraint
])
}
}

@ -10,6 +10,7 @@ protocol TokenInstanceWebViewDelegate: class {
//TODO not good. But quick and dirty to ship
func navigationControllerFor(tokenInstanceWebView: TokenInstanceWebView) -> UINavigationController?
func shouldClose(tokenInstanceWebView: TokenInstanceWebView)
func heightChangedFor(tokenInstanceWebView: TokenInstanceWebView)
}
@ -168,6 +169,7 @@ class TokenInstanceWebView: UIView {
private func makeIntroductionWebViewFullHeight() {
heightConstraint.constant = webView.scrollView.contentSize.height
delegate?.heightChangedFor(tokenInstanceWebView: self)
}
}

@ -3,6 +3,10 @@
import UIKit
import WebKit
protocol TokenCardRowViewDelegate {
func heightChangedFor(tokenCardRowView: TokenCardRowView)
}
class TokenCardRowView: UIView, TokenCardRowViewProtocol {
private let server: RPCServer
private let assetDefinitionStore: AssetDefinitionStore
@ -43,9 +47,18 @@ class TokenCardRowView: UIView, TokenCardRowViewProtocol {
//TODO pass in keystore or wallet address instead
let walletAddress = EtherKeystore.current!.address
//TODO this can't sign personal message because we didn't set a delegate, but we don't need it also
return TokenInstanceWebView(server: server, walletAddress: walletAddress, assetDefinitionStore: assetDefinitionStore)
let webView = TokenInstanceWebView(server: server, walletAddress: walletAddress, assetDefinitionStore: assetDefinitionStore)
webView.delegate = self
return webView
}()
//These are necessary because non-TokenScript views have margins whereas TokenScript views doesn't
private var constraintsWithLeadingMarginsThatDependsOnWhetherTokenScriptIsUsed: [NSLayoutConstraint] = []
private var constraintsWithTrailingMarginsThatDependsOnWhetherTokenScriptIsUsed: [NSLayoutConstraint] = []
private var constraintsWithTopMarginsThatDependsOnWhetherTokenScriptIsUsed: [NSLayoutConstraint] = []
private var constraintsWithBottomMarginsThatDependsOnWhetherTokenScriptIsUsed: [NSLayoutConstraint] = []
var delegate: TokenCardRowViewDelegate?
let background = UIView()
var checkboxImageView = UIImageView(image: R.image.ticket_bundle_unchecked())
var stateLabel = UILabel()
@ -115,9 +128,25 @@ class TokenCardRowView: UIView, TokenCardRowViewProtocol {
nativelyRenderedAttributeViews = [stateLabel, row0, row1, spaceAboveBottomRowStack, row3, detailsRowStack!]
constraintsWithLeadingMarginsThatDependsOnWhetherTokenScriptIsUsed = [
stackView.leadingAnchor.constraint(equalTo: background.leadingAnchor),
]
constraintsWithTrailingMarginsThatDependsOnWhetherTokenScriptIsUsed = [
background.trailingAnchor.constraint(equalTo: trailingAnchor),
stackView.trailingAnchor.constraint(equalTo: background.trailingAnchor),
]
constraintsWithTopMarginsThatDependsOnWhetherTokenScriptIsUsed = [
background.topAnchor.constraint(equalTo: topAnchor),
stackView.topAnchor.constraint(equalTo: background.topAnchor),
]
constraintsWithBottomMarginsThatDependsOnWhetherTokenScriptIsUsed = [
background.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor),
stackView.bottomAnchor.constraint(lessThanOrEqualTo: background.bottomAnchor),
]
// TODO extract constant. Maybe StyleLayout.sideMargin
let xMargin = CGFloat(7)
let yMargin = CGFloat(5)
checkboxRelatedConstraintsWhenShown.append(checkboxImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: xMargin))
checkboxRelatedConstraintsWhenShown.append(checkboxImageView.centerYAnchor.constraint(equalTo: centerYAnchor))
checkboxRelatedConstraintsWhenShown.append(background.leadingAnchor.constraint(equalTo: checkboxImageView.trailingAnchor, constant: xMargin))
@ -129,7 +158,9 @@ class TokenCardRowView: UIView, TokenCardRowViewProtocol {
checkboxRelatedConstraintsWhenShown.append(checkboxImageView.widthAnchor.constraint(equalToConstant: 28))
checkboxRelatedConstraintsWhenShown.append(checkboxImageView.heightAnchor.constraint(equalToConstant: 28))
}
checkboxRelatedConstraintsWhenHidden.append(background.leadingAnchor.constraint(equalTo: leadingAnchor, constant: xMargin))
let c1 = background.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0)
constraintsWithLeadingMarginsThatDependsOnWhetherTokenScriptIsUsed.append(c1)
checkboxRelatedConstraintsWhenHidden.append(c1)
if showCheckbox {
NSLayoutConstraint.activate(checkboxRelatedConstraintsWhenShown)
} else {
@ -137,16 +168,12 @@ class TokenCardRowView: UIView, TokenCardRowViewProtocol {
}
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: background.leadingAnchor, constant: 21),
stackView.trailingAnchor.constraint(equalTo: background.trailingAnchor, constant: -21),
stackView.topAnchor.constraint(equalTo: background.topAnchor, constant: 16),
stackView.bottomAnchor.constraint(lessThanOrEqualTo: background.bottomAnchor, constant: -16),
detailsRowStack!.widthAnchor.constraint(equalTo: stackView.widthAnchor),
background.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -xMargin),
background.topAnchor.constraint(equalTo: topAnchor, constant: yMargin),
background.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor, constant: -yMargin),
constraintsWithLeadingMarginsThatDependsOnWhetherTokenScriptIsUsed,
constraintsWithTrailingMarginsThatDependsOnWhetherTokenScriptIsUsed,
constraintsWithTopMarginsThatDependsOnWhetherTokenScriptIsUsed,
constraintsWithBottomMarginsThatDependsOnWhetherTokenScriptIsUsed,
tokenScriptRendererView.widthAnchor.constraint(equalTo: stackView.widthAnchor),
@ -259,6 +286,35 @@ class TokenCardRowView: UIView, TokenCardRowViewProtocol {
tokenScriptRendererView.isHidden = true
}
for each in constraintsWithLeadingMarginsThatDependsOnWhetherTokenScriptIsUsed {
if viewModel.hasTokenScriptHtml {
each.constant = 0
} else {
each.constant = 7
}
}
for each in constraintsWithTrailingMarginsThatDependsOnWhetherTokenScriptIsUsed {
if viewModel.hasTokenScriptHtml {
each.constant = 0
} else {
each.constant = -7
}
}
for each in constraintsWithTopMarginsThatDependsOnWhetherTokenScriptIsUsed {
if viewModel.hasTokenScriptHtml {
each.constant = 0
} else {
each.constant = 5
}
}
for each in constraintsWithBottomMarginsThatDependsOnWhetherTokenScriptIsUsed {
if viewModel.hasTokenScriptHtml {
each.constant = 0
} else {
each.constant = -5
}
}
adjustmentsToHandleWhenCategoryLabelTextIsTooLong()
}
@ -273,3 +329,17 @@ extension TokenCardRowView: TokenRowView {
configure(viewModel: TokenCardRowViewModel(tokenHolder: tokenHolder, tokenView: tokenView, assetDefinitionStore: assetDefinitionStore))
}
}
extension TokenCardRowView: TokenInstanceWebViewDelegate {
func navigationControllerFor(tokenInstanceWebView: TokenInstanceWebView) -> UINavigationController? {
return nil
}
func shouldClose(tokenInstanceWebView: TokenInstanceWebView) {
//no-op
}
func heightChangedFor(tokenInstanceWebView: TokenInstanceWebView) {
delegate?.heightChangedFor(tokenCardRowView: self)
}
}

Loading…
Cancel
Save