Skip to content

Commit

Permalink
Update Zcash library to 2.0.0 version
Browse files Browse the repository at this point in the history
  • Loading branch information
ant013 committed Sep 26, 2023
1 parent d0bac97 commit 9b766bd
Show file tree
Hide file tree
Showing 14 changed files with 397 additions and 408 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10927,7 +10927,7 @@
repositoryURL = "https://github.com/zcash/ZcashLightClientKit";
requirement = {
kind = exactVersion;
version = "0.22.0-beta";
version = "2.0.0";
};
};
D3993DAA28F42549008720FB /* XCRemoteSwiftPackageReference "WalletConnectSwiftV2" */ = {
Expand Down
465 changes: 233 additions & 232 deletions UnstoppableWallet/UnstoppableWallet/Core/Adapters/ZcashAdapter.swift

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class ZcashTransactionPool {

func initTransactions() async {
let overviews = await synchronizer.transactions
let pending = await synchronizer.pendingTransactions
// let pending = await synchronizer.pendingTransactions

pendingTransactions = await Set(zcashTransactions(pending, lastBlockHeight: 0))
// pendingTransactions = await Set(zcashTransactions(pending, lastBlockHeight: 0))
confirmedTransactions = Set(await zcashTransactions(overviews, lastBlockHeight: 0))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import ZcashLightClientKit
import HsExtensions

class ZcashTransactionWrapper {
let id: String?
let raw: Data?
let transactionHash: String
let transactionIndex: Int
Expand All @@ -18,7 +17,6 @@ class ZcashTransactionWrapper {
let failed: Bool

init?(tx: ZcashTransaction.Overview, memo: Memo?, recipient: TransactionRecipient?, lastBlockHeight: Int) {
id = tx.id.description
raw = tx.raw
transactionHash = tx.rawID.hs.reversedHex
transactionIndex = tx.index ?? 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
import Foundation
import Alamofire
import RxSwift
import RxRelay
import Combine
import Foundation

class DownloadService {
private let queue: DispatchQueue
private var downloads = [String: Double]()

private let stateRelay = PublishRelay<State>()
private(set) var state: State = .idle {
didSet {
if state != oldValue {
stateRelay.accept(state)
}
}
}
@Published private(set) var state: State = .idle

init(queueLabel: String = "io.SynchronizedDownloader") {
queue = DispatchQueue(label: queueLabel, qos: .background)
}

private func request(source: URLConvertible, destination: @escaping DownloadRequest.Destination, progress: ((Double) -> ())? = nil, completion: ((Bool) -> ())? = nil) {
private func request(source: URLConvertible, destination: @escaping DownloadRequest.Destination, progress: ((Double) -> Void)? = nil, completion: ((Bool) -> Void)? = nil) {
guard let key = try? source.asURL().path else {
return
}

let alreadyDownloading = queue.sync {
downloads.contains(where: { (existKey, _) in key == existKey })
downloads.contains(where: { existKey, _ in key == existKey })
}

guard !alreadyDownloading else {
state = .success
return
}

handle(progress: 0, key: key)
AF.download(source, to: destination)
.downloadProgress(queue: DispatchQueue.global(qos: .background)) { [weak self] progressValue in
self?.handle(progress: progressValue.fractionCompleted, key: key)
progress?(progressValue.fractionCompleted)
}
.responseData(queue: DispatchQueue.global(qos: .background)) { [weak self] response in
self?.handle(response: response, key: key)
switch response.result { // extend errors/data to completion if needed
case .success: completion?(true)
case .failure: completion?(false)
}
.downloadProgress(queue: DispatchQueue.global(qos: .background)) { [weak self] progressValue in
self?.handle(progress: progressValue.fractionCompleted, key: key)
progress?(progressValue.fractionCompleted)
}
.responseData(queue: DispatchQueue.global(qos: .background)) { [weak self] response in
self?.handle(response: response, key: key)
switch response.result { // extend errors/data to completion if needed
case .success: completion?(true)
case .failure: completion?(false)
}
}
}

private func handle(progress: Double, key: String) {
Expand All @@ -55,7 +48,7 @@ class DownloadService {
}
}

private func handle(response: AFDownloadResponse<Data>, key: String) {
private func handle(response _: AFDownloadResponse<Data>, key: String) {
queue.async {
self.downloads[key] = nil
self.syncState()
Expand All @@ -70,49 +63,42 @@ class DownloadService {
}

guard downloads.count != 0 else {
state = .idle
state = .success
return
}

let minimalProgress = downloads.min(by: { a, b in a.value < b.value })?.value ?? lastProgress
state = .inProgress(value: max(minimalProgress, lastProgress))
}

}

extension DownloadService {

public func download(source: URLConvertible, destination: URL, progress: ((Double) -> ())? = nil, completion: ((Bool) -> ())? = nil) {
public func download(source: URLConvertible, destination: URL, progress: ((Double) -> Void)? = nil, completion: ((Bool) -> Void)? = nil) {
let destination: DownloadRequest.Destination = { _, _ in
(destination, [.removePreviousFile, .createIntermediateDirectories])
}

request(source: source, destination: destination, progress: progress, completion: completion)
}

public var stateObservable: Observable<State> {
stateRelay.asObservable()
}

}

extension DownloadService {

public static func existing(url: URL) -> Bool {
(try? FileManager.default.attributesOfItem(atPath: url.path)) != nil
}

public enum State: Equatable {
case idle
case inProgress(value: Double)
case success

public static func ==(lhs: State, rhs: State) -> Bool {
public static func == (lhs: State, rhs: State) -> Bool {
switch (lhs, rhs) {
case (.idle, .idle): return true
case (.inProgress(let lhsValue), .inProgress(let rhsValue)): return lhsValue == rhsValue
case (.success, .success): return true
case let (.inProgress(lhsValue), .inProgress(rhsValue)): return lhsValue == rhsValue
default: return false
}
}
}

}
2 changes: 2 additions & 0 deletions UnstoppableWallet/UnstoppableWallet/Models/AdapterState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ enum AdapterState {
case syncing(progress: Int?, lastBlockDate: Date?)
case customSyncing(main: String, secondary: String?, progress: Int?)
case notSynced(error: Error)
case stopped

var isSynced: Bool {
switch self {
Expand All @@ -29,6 +30,7 @@ extension AdapterState: Equatable {
case (.syncing(let lProgress, let lLastBlockDate), .syncing(let rProgress, let rLastBlockDate)): return lProgress == rProgress && lLastBlockDate == rLastBlockDate
case (.customSyncing(let lMain, let lSecondary, let lProgress), .customSyncing(let rMain, let rSecondary, let rProgress)): return lMain == rMain && lSecondary == rSecondary && lProgress == rProgress
case (.notSynced, .notSynced): return true
case (.stopped, .stopped): return true
default: return false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ class TransactionsTableViewDataSource: NSObject {
self.allLoaded = allLoaded
}

guard loaded else {
guard let tableView, loaded else {
return
}

if let updateInfo = viewData.updateInfo {
// print("Update Item: \(updateInfo.sectionIndex)-\(updateInfo.index)")
let indexPath = IndexPath(row: updateInfo.index, section: updateInfo.sectionIndex)
let originalIndexPath = delegate?
.originalIndexPath(tableView: tableView, dataSource: self, indexPath: indexPath) ?? indexPath

if let tableView,
let originalIndexPath = delegate?.originalIndexPath(tableView: tableView, dataSource: self, indexPath: indexPath),
let cell = tableView.cellForRow(at: originalIndexPath) as? BaseThemeCell {
if let cell = tableView.cellForRow(at: originalIndexPath) as? BaseThemeCell {
cell.bind(rootElement: rootElement(viewItem: sectionViewItems[updateInfo.sectionIndex].viewItems[updateInfo.index]))
}
} else {
// print("RELOAD TABLE VIEW")
tableView?.reloadData()
tableView.reloadData()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ protocol ISectionDataSourceDelegate: AnyObject {
}

extension ISectionDataSourceDelegate {

func originalIndexPath(tableView: UITableView, dataSource: ISectionDataSource, indexPath: IndexPath) -> IndexPath {
func originalIndexPath(tableView _: UITableView, dataSource _: ISectionDataSource, indexPath: IndexPath) -> IndexPath {
indexPath
}

func height(tableView: UITableView, before dataSource: ISectionDataSource) -> CGFloat {
func height(tableView _: UITableView, before _: ISectionDataSource) -> CGFloat {
.zero
}

func height(tableView: UITableView, except dataSource: ISectionDataSource) -> CGFloat {
func height(tableView _: UITableView, except _: ISectionDataSource) -> CGFloat {
.zero
}

}

class DataSourceChain: NSObject {
Expand All @@ -42,12 +40,12 @@ class DataSourceChain: NSObject {

private func sectionCount(tableView: UITableView, before section: Int) -> Int {
dataSources
.prefix(section)
.map { $0.numberOfSections?(in: tableView) ?? 0 }
.reduce(0, +)
.prefix(section)
.map { $0.numberOfSections?(in: tableView) ?? 0 }
.reduce(0, +)
}

private func sourceIndex(_ tableView: UITableView, `for` section: Int) -> Int {
private func sourceIndex(_ tableView: UITableView, for section: Int) -> Int {
var shift = 0
for (index, dataSource) in dataSources.enumerated() {
let count = dataSource.numberOfSections?(in: tableView) ?? 0
Expand All @@ -70,26 +68,24 @@ class DataSourceChain: NSObject {

private func height(_ tableView: UITableView, dataSource: ISectionDataSource, section: Int) -> CGFloat {
let numberOfRows = dataSource.tableView(tableView, numberOfRowsInSection: section)
return (0..<numberOfRows)
.map { dataSource.tableView?(tableView, heightForRowAt: IndexPath(row: $0, section: section)) ?? 0 }
.reduce(0, +)
return (0 ..< numberOfRows)
.map { dataSource.tableView?(tableView, heightForRowAt: IndexPath(row: $0, section: section)) ?? 0 }
.reduce(0, +)
}

private func height(_ tableView: UITableView, dataSource: ISectionDataSource) -> CGFloat {
let sections = dataSource.numberOfSections?(in: tableView) ?? 0
return (0..<sections)
.map {
height(tableView, dataSource: dataSource, section: $0) +
(dataSource.tableView?(tableView, heightForHeaderInSection: $0) ?? 0) +
(dataSource.tableView?(tableView, heightForFooterInSection: $0) ?? 0)
}
.reduce(0, +)
return (0 ..< sections)
.map {
height(tableView, dataSource: dataSource, section: $0) +
(dataSource.tableView?(tableView, heightForHeaderInSection: $0) ?? 0) +
(dataSource.tableView?(tableView, heightForFooterInSection: $0) ?? 0)
}
.reduce(0, +)
}

}

extension DataSourceChain: ISectionDataSourceDelegate {

func originalIndexPath(tableView: UITableView, dataSource: ISectionDataSource, indexPath: IndexPath) -> IndexPath {
guard let dataSourceIndex = dataSources.firstIndex(where: { $0.isEqual(dataSource) }) else {
return indexPath
Expand All @@ -105,9 +101,9 @@ extension DataSourceChain: ISectionDataSourceDelegate {
}

return dataSources
.prefix(dataSourceIndex)
.map { height(tableView, dataSource: $0) }
.reduce(0, +)
.prefix(dataSourceIndex)
.map { height(tableView, dataSource: $0) }
.reduce(0, +)
}

func height(tableView: UITableView, except dataSource: ISectionDataSource) -> CGFloat {
Expand All @@ -118,23 +114,19 @@ extension DataSourceChain: ISectionDataSourceDelegate {
let sources = dataSources.prefix(dataSourceIndex) + dataSources.suffix(from: dataSourceIndex + 1)

return sources
.prefix(dataSourceIndex)
.map { height(tableView, dataSource: $0) }
.reduce(0, +)
.prefix(dataSourceIndex)
.map { height(tableView, dataSource: $0) }
.reduce(0, +)
}

}

extension DataSourceChain: ISectionDataSource {

func prepare(tableView: UITableView) {
dataSources.forEach { $0.prepare(tableView: tableView) }
}

}

extension DataSourceChain: UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
sectionCount(tableView: tableView, before: dataSources.count)
}
Expand All @@ -150,11 +142,9 @@ extension DataSourceChain: UITableViewDataSource {
let sourcePath = sourcePath(tableView, forRowAt: indexPath)
return dataSources[sourcePath.source].tableView(tableView, cellForRowAt: sourcePath.indexPath)
}

}

extension DataSourceChain: UITableViewDelegate {

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let sourcePath = sourcePath(tableView, forRowAt: indexPath)
dataSources[sourcePath.source].tableView?(tableView, willDisplay: cell, forRowAt: sourcePath.indexPath)
Expand Down Expand Up @@ -185,15 +175,11 @@ extension DataSourceChain: UITableViewDelegate {
let sourcePath = sourcePath(tableView, forRowAt: IndexPath(row: 0, section: section))
dataSources[sourcePath.source].tableView?(tableView, willDisplayHeaderView: view, forSection: sourcePath.indexPath.section)
}


}

extension DataSourceChain {

private struct SourceIndexPath {
let source: Int
let indexPath: IndexPath
}

}
Loading

0 comments on commit 9b766bd

Please sign in to comment.