Skip to content

Commit

Permalink
🦺[Tech debt] Refactor send class (#80)
Browse files Browse the repository at this point in the history
* Removed the launch options

- This  method had  `launchOptions`  that was never used.

* Removed all unused code.
  • Loading branch information
kcw-grunt authored Aug 24, 2021
1 parent d58723d commit aa497a6
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 203 deletions.
2 changes: 1 addition & 1 deletion loafwallet/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

UIView.swizzleSetFrame()

applicationController.launch(application: application, window: thisWindow, options: launchOptions)
applicationController.launch(application: application, window: thisWindow)

LWAnalytics.logEventWithParameters(itemName:._20191105_AL)

Expand Down
18 changes: 2 additions & 16 deletions loafwallet/src/ApplicationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ class ApplicationController : Subscriber, Trackable {
}
}

func launch(application: UIApplication, window: UIWindow?, options: [UIApplicationLaunchOptionsKey: Any]?) {
func launch(application: UIApplication, window: UIWindow?) {
self.application = application
self.window = window
application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
setup()
handleLaunchOptions(options)
reachability.didChange = { isReachable in
if !isReachable {
self.reachability.didChange = { isReachable in
Expand Down Expand Up @@ -292,20 +291,7 @@ class ApplicationController : Subscriber, Trackable {
DispatchQueue.global(qos: .background).async {
let _ = Rate.symbolMap //Initialize currency symbol map
}
}

private func handleLaunchOptions(_ options: [UIApplication.LaunchOptionsKey: Any]?) {
if let url = options?[.url] as? URL {
do {
let file = try Data(contentsOf: url)
if file.count > 0 {
store.trigger(name: .openFile(file))
}
} catch let error {
print("Could not open file at: \(url), error: \(error)")
}
}
}
}

func performBackgroundFetch() {
saveEvent("appController.performBackgroundFetch")
Expand Down
27 changes: 3 additions & 24 deletions loafwallet/src/FlowControllers/URLController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import UIKit

//DEV: This whole class should be removed.
//Need more testing.
class URLController : Trackable {

init(store: Store, walletManager: WalletManager) {
Expand Down Expand Up @@ -58,27 +60,13 @@ class URLController : Trackable {
if let success = xSuccess {
copyAddress(callback: success)
}
} else if let uri = isBitcoinUri(url: url, uri: uri) {
return handleBitcoinUri(uri)
}
return true
case "litecoin":
return handleBitcoinUri(url)
default:
return false
}
}

private func isBitcoinUri(url: URL, uri: String?) -> URL? {
guard let uri = uri else { return nil }
guard let bitcoinUrl = URL(string: uri) else { return nil }
if (url.host == "litecoin-uri" || url.path == "/litecoin-uri") && bitcoinUrl.scheme == "litecoin" {
return url
} else {
return nil
}
}

private func copyAddress(callback: String) {
if let url = URL(string: callback), let wallet = walletManager.wallet {
let queryLength = url.query?.utf8.count ?? 0
Expand All @@ -88,16 +76,7 @@ class URLController : Trackable {
}
}
}

private func handleBitcoinUri(_ uri: URL) -> Bool {
if let request = PaymentRequest(string: uri.absoluteString) {
store.trigger(name: .receivedPaymentRequest(request))
return true
} else {
return false
}
}


private func present(alert: UIAlertController) {
store.trigger(name: .showAlert(alert))
}
Expand Down
60 changes: 2 additions & 58 deletions loafwallet/src/ModalPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ class ModalPresenter : Subscriber, Trackable {
store.subscribe(self,
selector: { $0.alert != $1.alert && $1.alert != nil },
callback: { self.handleAlertChange($0.alert) })

store.subscribe(self, name: .openFile(Data()), callback: {
guard let trigger = $0 else { return }
if case .openFile(let file) = trigger {
self.handleFile(file)
}
})


//Subscribe to prompt actions
store.subscribe(self, name: .promptUpgradePin, callback: { _ in
self.presentUpgradePin()
Expand All @@ -68,25 +61,10 @@ class ModalPresenter : Subscriber, Trackable {
store.subscribe(self, name: .promptShareData, callback: { _ in
self.promptShareData()
})
store.subscribe(self, name: .openFile(Data()), callback: {
guard let trigger = $0 else { return }
if case .openFile(let file) = trigger {
self.handleFile(file)
}
})
store.subscribe(self, name: .recommendRescan, callback: { _ in
self.presentRescan()
})

//URLs
store.subscribe(self, name: .receivedPaymentRequest(nil), callback: {
guard let trigger = $0 else { return }
if case let .receivedPaymentRequest(request) = trigger {
if let request = request {
self.handlePaymentRequest(request: request)
}
}
})

store.subscribe(self, name: .scanQr, callback: { _ in
self.handleScanQrURL()
})
Expand Down Expand Up @@ -760,40 +738,6 @@ class ModalPresenter : Subscriber, Trackable {
topViewController?.present(alert, animated: true, completion: nil)
}

private func handleFile(_ file: Data) {
if let request = PaymentProtocolRequest(data: file) {
if let topVC = topViewController as? ModalViewController {
let attemptConfirmRequest: () -> Bool = {
if let send = topVC.childViewController as? SendViewController {
send.confirmProtocolRequest(protoReq: request)
return true
}
return false
}
if !attemptConfirmRequest() {
modalTransitionDelegate.reset()
topVC.dismiss(animated: true, completion: {
self.store.perform(action: RootModalActions.Present(modal: .send))
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { //This is a hack because present has no callback
let _ = attemptConfirmRequest()
})
})
}
}
} else if let ack = PaymentProtocolACK(data: file) {
if let memo = ack.memo {
let alert = UIAlertController(title: "", message: memo, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: S.Button.ok, style: .cancel, handler: nil))
topViewController?.present(alert, animated: true, completion: nil)
}
//TODO - handle payment type
} else {
let alert = UIAlertController(title: S.LitewalletAlert.error, message: S.PaymentProtocol.Errors.corruptedDocument, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: S.Button.ok, style: .cancel, handler: nil))
topViewController?.present(alert, animated: true, completion: nil)
}
}

private func handlePaymentRequest(request: PaymentRequest) {
self.currentRequest = request
guard !store.state.isLoginRequired else { presentModal(.send); return }
Expand Down
Loading

0 comments on commit aa497a6

Please sign in to comment.