ZcashLightClientKit/Example/ZcashLightClientSample/ZcashLightClientSample/Sync Blocks/SyncBlocksViewController.swift

166 lines
4.8 KiB
Swift
Raw Normal View History

//
// SyncBlocksViewController.swift
// ZcashLightClientSample
//
// Created by Francisco Gindre on 11/1/19.
// Copyright © 2019 Electric Coin Company. All rights reserved.
//
import UIKit
import ZcashLightClientKit
/**
2021-09-17 06:49:58 -07:00
Sync blocks view controller leverages Compact Block Processor directly. This provides more detail on block processing if needed.
We advise to use the SDKSynchronizer first since it provides a lot of functionality out of the box.
*/
class SyncBlocksViewController: UIViewController {
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var progressLabel: UILabel!
@IBOutlet weak var startPause: UIButton!
2021-09-17 06:49:58 -07:00
// swiftlint:disable:next implicitly_unwrapped_optional
2021-06-14 15:56:32 -07:00
private var processor: CompactBlockProcessor!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let wallet = Initializer.shared
2021-09-17 06:49:58 -07:00
// swiftlint:disable:next force_try
2021-06-14 15:56:32 -07:00
try! wallet.initialize()
processor = CompactBlockProcessor(initializer: wallet)
[476] CompactBlockProcessor to async/await - getting rid of the Operation Queue - the cleanup is needed - the update of tests is needed - tested and it successfully finishes the sync process [476] CompactBlockProcessor to async/await - old processNewBlocks() removed [476] CompactBlockProcessor to async/await - unused operations removed [476] CompactBlockProcessor to async/await - unit tests update [476] CompactBlockProcessor to async/await - unit tests refactored [476] CompactBlockProcessor to async/await - cleanup of deprecated method [476] CompactBlockProcessor to async/await - fail(error) was called even for canceled tasks but that must be excluded [476] CompactBlockProcessor to async/await - removal of all ZcashOperations from the code (unit test will follow) [476] CompactBlockProcessor to async/await - network tests in building and success order again [476] CompactBlockProcessor to async/await - offline tests in building and success order [476] CompactBlockProcessor to async/await (519) - cleanup of suspending the task [476] CompactBlockProcessor to async/await (519) - most comments resolved [476] CompactBlockProcessor to async/await (519) - thread safe state for both sync and async context [476] CompactBlockProcessor to async/await (519) - fixed build for a sample project [476] CompactBlockProcessor to async/await (519) - func testStartNotifiesSuscriptors() reverted [476] CompactBlockProcessor to async/await (519) - TODO added to track why we used NSLock instead of an Actor - Task priority enhanced [476] CompactBlockProcessor to async/await (519) - cleanup in Tasks and priorities
2022-09-01 05:58:41 -07:00
statusLabel.text = textFor(state: processor?.state.getState() ?? .stopped)
progressBar.progress = 0
2021-09-17 06:49:58 -07:00
NotificationCenter.default.addObserver(
self,
selector: #selector(processorNotification(_:)),
name: nil,
object: processor
)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
guard let processor = self.processor else { return }
processor.stop()
}
@objc func processorNotification(_ notification: Notification) {
DispatchQueue.main.async {
guard self.processor != nil else { return }
self.updateUI()
switch notification.name {
case let not where not == Notification.Name.blockProcessorUpdated:
2021-06-14 15:56:32 -07:00
guard let progress = notification.userInfo?[CompactBlockProcessorNotificationKey.progress] as? CompactBlockProgress else { return }
self.progressBar.progress = progress.progress
self.progressLabel.text = "\(progress.progress)%"
default:
return
}
}
}
@IBAction func startStop() {
guard let processor = processor else { return }
2021-09-17 06:49:58 -07:00
[476] CompactBlockProcessor to async/await - getting rid of the Operation Queue - the cleanup is needed - the update of tests is needed - tested and it successfully finishes the sync process [476] CompactBlockProcessor to async/await - old processNewBlocks() removed [476] CompactBlockProcessor to async/await - unused operations removed [476] CompactBlockProcessor to async/await - unit tests update [476] CompactBlockProcessor to async/await - unit tests refactored [476] CompactBlockProcessor to async/await - cleanup of deprecated method [476] CompactBlockProcessor to async/await - fail(error) was called even for canceled tasks but that must be excluded [476] CompactBlockProcessor to async/await - removal of all ZcashOperations from the code (unit test will follow) [476] CompactBlockProcessor to async/await - network tests in building and success order again [476] CompactBlockProcessor to async/await - offline tests in building and success order [476] CompactBlockProcessor to async/await (519) - cleanup of suspending the task [476] CompactBlockProcessor to async/await (519) - most comments resolved [476] CompactBlockProcessor to async/await (519) - thread safe state for both sync and async context [476] CompactBlockProcessor to async/await (519) - fixed build for a sample project [476] CompactBlockProcessor to async/await (519) - func testStartNotifiesSuscriptors() reverted [476] CompactBlockProcessor to async/await (519) - TODO added to track why we used NSLock instead of an Actor - Task priority enhanced [476] CompactBlockProcessor to async/await (519) - cleanup in Tasks and priorities
2022-09-01 05:58:41 -07:00
switch processor.state.getState() {
case .stopped:
startProcessor()
default:
stopProcessor()
}
}
func startProcessor() {
guard let processor = processor else { return }
2021-09-17 06:49:58 -07:00
do {
try processor.start()
updateUI()
} catch {
fail(error: error)
}
}
func stopProcessor() {
guard let processor = processor else { return }
2021-09-17 06:49:58 -07:00
[476] CompactBlockProcessor to async/await - getting rid of the Operation Queue - the cleanup is needed - the update of tests is needed - tested and it successfully finishes the sync process [476] CompactBlockProcessor to async/await - old processNewBlocks() removed [476] CompactBlockProcessor to async/await - unused operations removed [476] CompactBlockProcessor to async/await - unit tests update [476] CompactBlockProcessor to async/await - unit tests refactored [476] CompactBlockProcessor to async/await - cleanup of deprecated method [476] CompactBlockProcessor to async/await - fail(error) was called even for canceled tasks but that must be excluded [476] CompactBlockProcessor to async/await - removal of all ZcashOperations from the code (unit test will follow) [476] CompactBlockProcessor to async/await - network tests in building and success order again [476] CompactBlockProcessor to async/await - offline tests in building and success order [476] CompactBlockProcessor to async/await (519) - cleanup of suspending the task [476] CompactBlockProcessor to async/await (519) - most comments resolved [476] CompactBlockProcessor to async/await (519) - thread safe state for both sync and async context [476] CompactBlockProcessor to async/await (519) - fixed build for a sample project [476] CompactBlockProcessor to async/await (519) - func testStartNotifiesSuscriptors() reverted [476] CompactBlockProcessor to async/await (519) - TODO added to track why we used NSLock instead of an Actor - Task priority enhanced [476] CompactBlockProcessor to async/await (519) - cleanup in Tasks and priorities
2022-09-01 05:58:41 -07:00
processor.stop()
updateUI()
}
func fail(error: Error) {
let alert = UIAlertController(title: "Error", message: "\(error)", preferredStyle: .alert)
2021-09-17 06:49:58 -07:00
alert.addAction(
UIAlertAction(
title: "Ok",
style: .cancel,
handler: { _ in
self.navigationController?.popViewController(animated: true)
}
)
)
self.present(alert, animated: true, completion: nil)
updateUI()
}
func updateUI() {
[476] CompactBlockProcessor to async/await - getting rid of the Operation Queue - the cleanup is needed - the update of tests is needed - tested and it successfully finishes the sync process [476] CompactBlockProcessor to async/await - old processNewBlocks() removed [476] CompactBlockProcessor to async/await - unused operations removed [476] CompactBlockProcessor to async/await - unit tests update [476] CompactBlockProcessor to async/await - unit tests refactored [476] CompactBlockProcessor to async/await - cleanup of deprecated method [476] CompactBlockProcessor to async/await - fail(error) was called even for canceled tasks but that must be excluded [476] CompactBlockProcessor to async/await - removal of all ZcashOperations from the code (unit test will follow) [476] CompactBlockProcessor to async/await - network tests in building and success order again [476] CompactBlockProcessor to async/await - offline tests in building and success order [476] CompactBlockProcessor to async/await (519) - cleanup of suspending the task [476] CompactBlockProcessor to async/await (519) - most comments resolved [476] CompactBlockProcessor to async/await (519) - thread safe state for both sync and async context [476] CompactBlockProcessor to async/await (519) - fixed build for a sample project [476] CompactBlockProcessor to async/await (519) - func testStartNotifiesSuscriptors() reverted [476] CompactBlockProcessor to async/await (519) - TODO added to track why we used NSLock instead of an Actor - Task priority enhanced [476] CompactBlockProcessor to async/await (519) - cleanup in Tasks and priorities
2022-09-01 05:58:41 -07:00
guard let state = processor?.state.getState() else { return }
2021-09-17 06:49:58 -07:00
statusLabel.text = textFor(state: state)
startPause.setTitle(buttonText(for: state), for: .normal)
if case CompactBlockProcessor.State.synced = state {
startPause.isEnabled = false
} else {
startPause.isEnabled = true
}
}
func buttonText(for state: CompactBlockProcessor.State) -> String {
switch state {
case .downloading, .scanning, .validating:
return "Pause"
case .stopped:
return "Start"
2021-09-17 06:49:58 -07:00
case .error:
return "Retry"
case .synced:
return "Chill!"
case .enhancing:
return "Enhance"
case .fetching:
return "fetch"
}
}
func textFor(state: CompactBlockProcessor.State) -> String {
switch state {
case .downloading:
return "Downloading ⛓"
2021-09-17 06:49:58 -07:00
case .error:
return "error 💔"
case .scanning:
return "Scanning Blocks 🤖"
case .stopped:
return "Stopped 🚫"
case .validating:
return "Validating chain 🕵️‍♀️"
case .synced:
return "Synced 😎"
case .enhancing:
return "Enhancing 🤖"
case .fetching:
return "Fetching UTXOs"
}
}
}