Skip to content

Commit

Permalink
add webview controller
Browse files Browse the repository at this point in the history
  • Loading branch information
iwbrhwfy committed Apr 25, 2018
1 parent f26bbaf commit 39e6bb4
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 51 deletions.
8 changes: 8 additions & 0 deletions loopr-ios.xcodeproj/project.pbxproj
Expand Up @@ -26,6 +26,8 @@
940B452D2061206900ADE2B9 /* Token.swift in Sources */ = {isa = PBXBuildFile; fileRef = 940B452C2061206800ADE2B9 /* Token.swift */; };
94189CC12078B88700AA2CF4 /* OrderSearchResultViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94189CBF2078B88700AA2CF4 /* OrderSearchResultViewController.swift */; };
94189CC22078B88700AA2CF4 /* OrderSearchResultViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 94189CC02078B88700AA2CF4 /* OrderSearchResultViewController.xib */; };
943A9A4120909597008BCBFD /* AssetTransactionWebViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 943A9A3F20909597008BCBFD /* AssetTransactionWebViewController.swift */; };
943A9A4220909597008BCBFD /* AssetTransactionWebViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 943A9A4020909597008BCBFD /* AssetTransactionWebViewController.xib */; };
943BDED32056350000D5BF81 /* PriceQuote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 943BDED22056350000D5BF81 /* PriceQuote.swift */; };
945800212058FEB500E08DEF /* tokens.json in Resources */ = {isa = PBXBuildFile; fileRef = 945800202058FEB500E08DEF /* tokens.json */; };
945800232059006200E08DEF /* AssetDataManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 945800222059006200E08DEF /* AssetDataManagerTests.swift */; };
Expand Down Expand Up @@ -567,6 +569,8 @@
940B452C2061206800ADE2B9 /* Token.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Token.swift; sourceTree = "<group>"; };
94189CBF2078B88700AA2CF4 /* OrderSearchResultViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderSearchResultViewController.swift; sourceTree = "<group>"; };
94189CC02078B88700AA2CF4 /* OrderSearchResultViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = OrderSearchResultViewController.xib; sourceTree = "<group>"; };
943A9A3F20909597008BCBFD /* AssetTransactionWebViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssetTransactionWebViewController.swift; sourceTree = "<group>"; };
943A9A4020909597008BCBFD /* AssetTransactionWebViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AssetTransactionWebViewController.xib; sourceTree = "<group>"; };
943BDED22056350000D5BF81 /* PriceQuote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PriceQuote.swift; sourceTree = "<group>"; };
945800202058FEB500E08DEF /* tokens.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = tokens.json; sourceTree = "<group>"; };
945800222059006200E08DEF /* AssetDataManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssetDataManagerTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1390,6 +1394,8 @@
AC5E0DD9203AA01F00AF03C7 /* AssetTransactionTableViewCell.xib */,
ACE6F7162041FDAD0039273D /* AssetTransactionDetailViewController.swift */,
ACE6F7172041FDAD0039273D /* AssetTransactionDetailViewController.xib */,
943A9A3F20909597008BCBFD /* AssetTransactionWebViewController.swift */,
943A9A4020909597008BCBFD /* AssetTransactionWebViewController.xib */,
);
path = AssetDetail;
sourceTree = "<group>";
Expand Down Expand Up @@ -2190,6 +2196,7 @@
ACBA47EF207932DA0032C2B4 /* ExportKeystoreEnterPasswordViewController.xib in Resources */,
ACBDB1C62032D050003463A4 /* OpenOrderTableViewCell.xib in Resources */,
ACB9118920265DEA00F14644 /* MarketDetailViewController.xib in Resources */,
943A9A4220909597008BCBFD /* AssetTransactionWebViewController.xib in Resources */,
AC31CE6B205B995100A30335 /* Roboto-LightItalic.ttf in Resources */,
AC31CE93205B9C0600A30335 /* Montserrat-BlackItalic.ttf in Resources */,
AC31CE34205B603200A30335 /* Linotype-DIN-Next-LT-Pro-UltraLight.ttf in Resources */,
Expand Down Expand Up @@ -2544,6 +2551,7 @@
AC4F600E20773E9D00F1D954 /* Sign.swift in Sources */,
940B452A2060F67E00ADE2B9 /* MarketDataManager.swift in Sources */,
AC5E3E6B205DEAC00051ACFC /* TradeCompleteViewController.swift in Sources */,
943A9A4120909597008BCBFD /* AssetTransactionWebViewController.swift in Sources */,
AC387517205F12F100A76637 /* ClippedContainerViewController.swift in Sources */,
AC387516205F12F100A76637 /* ContextMenu+Options.swift in Sources */,
AC9ADF91205EE3B900CBE368 /* ConvertETHViewController.swift in Sources */,
Expand Down
9 changes: 7 additions & 2 deletions loopr-ios/DataManagers/SendAssetDataManager.swift
Expand Up @@ -180,16 +180,21 @@ class SendCurrentAppWalletDataManager {

func _transfer(data: Data, address: GethAddress, amount: GethBigInt, gasPrice: GethBigInt, gasLimit: GethBigInt, completion: @escaping (String?, Error?) -> Void) {
_keystore()
var userInfo: [String: Any] = [:]
do {
let nonce: Int64 = getNonce()
let signedTransaction = web3swift.sign(address: address, encodedFunctionData: data, nonce: nonce, amount: amount, gasLimit: gasLimit, gasPrice: gasPrice, password: wallet!.password)
if let signedTransactionData = try signedTransaction?.encodeRLP() {
sendTransactionToServer("0x" + signedTransactionData.hexString, completion: completion)
} else {
print("Failed to sign/encode")
userInfo["message"] = NSLocalizedString("Failed to sign/encode", comment: "")
let error = NSError(domain: "TRANSFER", code: 0, userInfo: userInfo)
completion(nil, error)
}
} catch {
print("Failed in encoding transaction ")
userInfo["message"] = NSLocalizedString("Failed to encode transaction", comment: "")
let error = NSError(domain: "TRANSFER", code: 0, userInfo: userInfo)
completion(nil, error)
}
}
}
2 changes: 1 addition & 1 deletion loopr-ios/Files/tokens.json
Expand Up @@ -496,7 +496,7 @@
},
{
"decimals" : 1000000000000000000,
"source" : "ethereum",
"source" : "wrapped ETH",
"protocol" : "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"isMarket" : true,
"time" : 0,
Expand Down
6 changes: 0 additions & 6 deletions loopr-ios/Models/Asset.swift
Expand Up @@ -25,14 +25,11 @@ class Asset: CustomStringConvertible, Equatable {
init(json: JSON) {
self.enable = true
self.display = "0"

self.symbol = json["symbol"].stringValue
self.allowance = json["allowance"].stringValue
self.icon = UIImage(named: self.symbol) ?? nil

self.name = TokenDataManager.shared.getTokenBySymbol(symbol)?.source ?? "unknown token"
self.description = self.name

self.rawBalance = json["balance"].stringValue
self.balance = 0.0
if let balance = Asset.getAmount(of: symbol, from: rawBalance) {
Expand All @@ -45,13 +42,10 @@ class Asset: CustomStringConvertible, Equatable {
self.name = ""
self.enable = true
self.description = self.name

self.allowance = "0"
self.icon = UIImage(named: self.symbol) ?? nil

self.rawBalance = "0"
self.balance = 0.0

self.display = Double(0).currency
}

Expand Down
Expand Up @@ -154,7 +154,10 @@ class AssetTransactionDetailViewController: UIViewController {
}
}
if let url = URL(string: etherUrl) {
UIApplication.shared.open(url, options: [:])
let viewController = AssetTransactionWebViewController()
viewController.url = url
viewController.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(viewController, animated: true)
}
}

Expand All @@ -164,7 +167,10 @@ class AssetTransactionDetailViewController: UIViewController {
etherUrl += tx.txHash
}
if let url = URL(string: etherUrl) {
UIApplication.shared.open(url, options: [:])
let viewController = AssetTransactionWebViewController()
viewController.url = url
viewController.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(viewController, animated: true)
}
}

Expand Down
@@ -0,0 +1,30 @@
//
// AssetTransactionWebViewController.swift
// loopr-ios
//
// Created by kenshin on 2018/4/25.
// Copyright © 2018年 Loopring. All rights reserved.
//

import UIKit
import WebKit

class AssetTransactionWebViewController: UIViewController {

@IBOutlet weak var wkWebView: WKWebView!
var url: URL?

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
setBackButton()
let request = URLRequest(url: url!)
wkWebView.load(request)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
43 changes: 43 additions & 0 deletions loopr-ios/Wallet/AssetDetail/AssetTransactionWebViewController.xib
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="AssetTransactionWebViewController" customModule="loopr_ios" customModuleProvider="target">
<connections>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
<outlet property="wkWebView" destination="tKL-CR-AVX" id="fEs-Oh-Na2"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<wkWebView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="tKL-CR-AVX">
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
<color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<wkWebViewConfiguration key="configuration">
<audiovisualMediaTypes key="mediaTypesRequiringUserActionForPlayback" none="YES"/>
<wkPreferences key="preferences"/>
</wkWebViewConfiguration>
</wkWebView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="fnl-2z-Ty3" firstAttribute="bottom" secondItem="tKL-CR-AVX" secondAttribute="bottom" id="1CL-Ud-0Us"/>
<constraint firstItem="fnl-2z-Ty3" firstAttribute="trailing" secondItem="tKL-CR-AVX" secondAttribute="trailing" id="1iH-uM-TAB"/>
<constraint firstItem="tKL-CR-AVX" firstAttribute="top" secondItem="fnl-2z-Ty3" secondAttribute="top" id="ArJ-g3-kFJ"/>
<constraint firstItem="tKL-CR-AVX" firstAttribute="leading" secondItem="fnl-2z-Ty3" secondAttribute="leading" id="nOK-MW-wWa"/>
</constraints>
<viewLayoutGuide key="safeArea" id="fnl-2z-Ty3"/>
</view>
</objects>
</document>
77 changes: 55 additions & 22 deletions loopr-ios/Wallet/ConvertETH/ConvertETHViewController.swift
Expand Up @@ -7,6 +7,7 @@
//

import UIKit
import Geth
import NotificationBannerSwift

class ConvertETHViewController: UIViewController, UITextFieldDelegate, NumericKeyboardDelegate, NumericKeyboardProtocol {
Expand All @@ -17,7 +18,7 @@ class ConvertETHViewController: UIViewController, UITextFieldDelegate, NumericKe
@IBOutlet weak var convertButton: UIButton!
@IBOutlet weak var convertBackgroundView: UIView!

var asset: Asset? = nil
var asset: Asset?

var infoLabel: UILabel = UILabel()

Expand Down Expand Up @@ -183,29 +184,57 @@ class ConvertETHViewController: UIViewController, UITextFieldDelegate, NumericKe
}

func completion(_ txHash: String?, _ error: Error?) {
// // Show toast
// let notificationTitle = NSLocalizedString("Convert Successfully.", comment: "")
// let attribute = [NSAttributedStringKey.font: UIFont.init(name: FontConfigManager.shared.getRegular(), size: 17)!]
// let attributeString = NSAttributedString(string: notificationTitle, attributes: attribute)
// let banner = NotificationBanner(attributedTitle: attributeString, style: .success, colors: NotificationBannerStyle())
// banner.duration = 1.0
// banner.show()
//
// // Reset the text field
// amountTextField.text = ""
//
// availableLabel.text = "Available \(ConvertDataManager.shared.getMaxAmount()) ETH"
guard error == nil && txHash != nil else {
// Show toast
DispatchQueue.main.async {
let notificationTitle = NSLocalizedString("Insufficient funds for gas x price + value", comment: "")
let attribute = [NSAttributedStringKey.font: UIFont.init(name: FontConfigManager.shared.getRegular(), size: 17)!]
let attributeString = NSAttributedString(string: notificationTitle, attributes: attribute)
let banner = NotificationBanner(attributedTitle: attributeString, style: .danger)
banner.duration = 5
banner.show()
}
return
}
print("Result of transfer is \(txHash!)")
// Show toast
DispatchQueue.main.async {
let notificationTitle = NSLocalizedString("Success. Result of transfer is \(txHash!)", comment: "")
let attribute = [NSAttributedStringKey.font: UIFont.init(name: FontConfigManager.shared.getRegular(), size: 17)!]
let attributeString = NSAttributedString(string: notificationTitle, attributes: attribute)
let banner = NotificationBanner(attributedTitle: attributeString, style: .success)
banner.duration = 5
banner.show()
}
}

@IBAction func pressedConvertButton(_ sender: Any) {
let amount = Double(amountTextField.text!)
func validation() -> GethBigInt? {
var result: GethBigInt? = nil
if let asset = self.asset {
if asset.symbol.uppercased() == "ETH" {
// SendCurrentAppWalletDataManager.shared._transfer(method: "withdraw", contractAddress: GethAddress, toAddress: GethAddress, amount: <#T##GethBigInt#>, gasType: "withdraw", gasPrice: <#T##Int64#>, completion: completion)
} else if asset.symbol.uppercased() == "WETH" {
// SendCurrentAppWalletDataManager.shared._transfer(method: "withdraw", contractAddress: GethAddress, toAddress: GethAddress, amount: <#T##GethBigInt#>, gasType: "withdraw", gasPrice: <#T##Int64#>, completion: completion)
if let amountString = amountTextField.text {
if let amount = Double(amountString) {
if asset.balance >= amount {
if let amount = GethBigInt.bigInt(amountString) {
result = amount
}
}
}
}
}
return result
}

@IBAction func pressedConvertButton(_ sender: Any) {
guard let amount = validation() else {
// TODO: tip in ui
print("Invalid Amount")
return
}
if asset!.symbol.uppercased() == "ETH" {
SendCurrentAppWalletDataManager.shared._deposit(amount: amount, gasPrice: gasPrice, completion: completion)
} else if asset!.symbol.uppercased() == "WETH" {
SendCurrentAppWalletDataManager.shared._withDraw(amount: amount, gasPrice: gasPrice, completion: completion)
}
}

func numericKeyboard(_ numericKeyboard: NumericKeyboard, itemTapped item: NumericKeyboardItem, atPosition position: Position) {
Expand All @@ -215,9 +244,7 @@ class ConvertETHViewController: UIViewController, UITextFieldDelegate, NumericKe
guard activeTextField != nil else {
return
}

var currentText = activeTextField!.text ?? ""

switch (position.row, position.column) {
case (3, 0):
activeTextField!.text = currentText + "."
Expand All @@ -233,5 +260,11 @@ class ConvertETHViewController: UIViewController, UITextFieldDelegate, NumericKe
activeTextField!.text = currentText + String(itemValue)
}
}

}

extension ConvertETHViewController {
var gasPrice: GethBigInt {
// TODO: get value from transactionSpeedSlider in advanced setting
return GethBigInt(20000000000)
}
}

0 comments on commit 39e6bb4

Please sign in to comment.