Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: content transition #214

Merged
merged 3 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions BDKSwiftExampleWallet/Service/BDK Service/BDKService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import Foundation

private class BDKService {
static var shared: BDKService = BDKService()

private var balance: Balance?
var network: Network
private var wallet: Wallet?
private let keyService: KeyClient
private var connection: Connection?
private let esploraClient: EsploraClient
private let keyService: KeyClient
private var needsFullScan: Bool = false
private var connection: Connection?
var network: Network
private var wallet: Wallet?

init(
keyService: KeyClient = .live
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import Foundation

enum WalletError: Error {
case walletNotFound
case blockchainConfigNotFound
case dbNotFound
case notSigned
case walletNotFound
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

enum FeeServiceError: Error {
case invalidURL
case invalidServerResponse
case invalidURL
case serialization
}
99 changes: 48 additions & 51 deletions BDKSwiftExampleWallet/Service/Key Service/KeyService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ private struct KeyService {
self.keychain = keychain
}

func saveBackupInfo(backupInfo: BackupInfo) throws {
let encoder = JSONEncoder()
let data = try encoder.encode(backupInfo)
keychain[data: "BackupInfo"] = data
func deleteBackupInfo() throws {
try keychain.remove("BackupInfo")
}

func deleteEsploraURL() throws {
try keychain.remove("SelectedEsploraURL")
}

func deleteNetwork() throws {
try keychain.remove("SelectedNetwork")
}

func getBackupInfo() throws -> BackupInfo {
Expand All @@ -35,90 +41,83 @@ private struct KeyService {
return backupInfo
}

func deleteBackupInfo() throws {
try keychain.remove("BackupInfo")
}
}

extension KeyService {
func saveNetwork(network: String) throws {
keychain[string: "SelectedNetwork"] = network
func getEsploraURL() throws -> String? {
return keychain[string: "SelectedEsploraURL"]
}

func getNetwork() throws -> String? {
return keychain[string: "SelectedNetwork"]
}

func deleteNetwork() throws {
try keychain.remove("SelectedNetwork")
func saveBackupInfo(backupInfo: BackupInfo) throws {
let encoder = JSONEncoder()
let data = try encoder.encode(backupInfo)
keychain[data: "BackupInfo"] = data
}

func saveEsploraURL(url: String) throws {
keychain[string: "SelectedEsploraURL"] = url
}

func getEsploraURL() throws -> String? {
return keychain[string: "SelectedEsploraURL"]
}

func deleteEsploraURL() throws {
try keychain.remove("SelectedEsploraURL")
func saveNetwork(network: String) throws {
keychain[string: "SelectedNetwork"] = network
}
}

struct KeyClient {
let saveBackupInfo: (BackupInfo) throws -> Void
let getBackupInfo: () throws -> BackupInfo
let deleteBackupInfo: () throws -> Void

let saveNetwork: (String) throws -> Void
let deleteEsplora: () throws -> Void
let deleteNetwork: () throws -> Void
let getBackupInfo: () throws -> BackupInfo
let getEsploraURL: () throws -> String?
let getNetwork: () throws -> String?
let saveEsploraURL: (String) throws -> Void
let getEsploraURL: () throws -> String?
let deleteNetwork: () throws -> Void
let deleteEsplora: () throws -> Void
let saveBackupInfo: (BackupInfo) throws -> Void
let saveNetwork: (String) throws -> Void

private init(
saveBackupInfo: @escaping (BackupInfo) throws -> Void,
getBackupInfo: @escaping () throws -> BackupInfo,
deleteBackupInfo: @escaping () throws -> Void,
saveNetwork: @escaping (String) throws -> Void,
deleteEsplora: @escaping () throws -> Void,
deleteNetwork: @escaping () throws -> Void,
getBackupInfo: @escaping () throws -> BackupInfo,
getEsploraURL: @escaping () throws -> String?,
getNetwork: @escaping () throws -> String?,
saveBackupInfo: @escaping (BackupInfo) throws -> Void,
saveEsploraURL: @escaping (String) throws -> Void,
getEsploraURL: @escaping () throws -> String?,
deleteNetwork: @escaping () throws -> Void,
deleteEsplora: @escaping () throws -> Void
saveNetwork: @escaping (String) throws -> Void
) {
self.saveBackupInfo = saveBackupInfo
self.getBackupInfo = getBackupInfo
self.deleteBackupInfo = deleteBackupInfo
self.saveNetwork = saveNetwork
self.deleteEsplora = deleteEsplora
self.deleteNetwork = deleteNetwork
self.getBackupInfo = getBackupInfo
self.getEsploraURL = getEsploraURL
self.getNetwork = getNetwork
self.saveBackupInfo = saveBackupInfo
self.saveEsploraURL = saveEsploraURL
self.getEsploraURL = getEsploraURL
self.deleteNetwork = deleteNetwork
self.deleteEsplora = deleteEsplora
self.saveNetwork = saveNetwork
}
}

extension KeyClient {
static let live = Self(
saveBackupInfo: { backupInfo in try KeyService().saveBackupInfo(backupInfo: backupInfo) },
getBackupInfo: { try KeyService().getBackupInfo() },
deleteBackupInfo: { try KeyService().deleteBackupInfo() },
saveNetwork: { network in try KeyService().saveNetwork(network: network) },
deleteEsplora: { try KeyService().deleteEsploraURL() },
deleteNetwork: { try KeyService().deleteNetwork() },
getBackupInfo: { try KeyService().getBackupInfo() },
getEsploraURL: { try KeyService().getEsploraURL() },
getNetwork: { try KeyService().getNetwork() },
saveBackupInfo: { backupInfo in try KeyService().saveBackupInfo(backupInfo: backupInfo) },
saveEsploraURL: { url in try KeyService().saveEsploraURL(url: url) },
getEsploraURL: { try KeyService().getEsploraURL() },
deleteNetwork: { try KeyService().deleteNetwork() },
deleteEsplora: { try KeyService().deleteEsploraURL() }
saveNetwork: { network in try KeyService().saveNetwork(network: network) }
)
}

#if DEBUG
extension KeyClient {
static let mock = Self(
saveBackupInfo: { _ in },
deleteBackupInfo: { try KeyService().deleteBackupInfo() },
deleteEsplora: {},
deleteNetwork: {},
getBackupInfo: {
let words12 =
"space echo position wrist orient erupt relief museum myself grain wisdom tumble"
Expand All @@ -145,13 +144,11 @@ extension KeyClient {
)
return backupInfo
},
deleteBackupInfo: { try KeyService().deleteBackupInfo() },
saveNetwork: { _ in },
getEsploraURL: { nil },
getNetwork: { nil },
saveBackupInfo: { _ in },
saveEsploraURL: { _ in },
getEsploraURL: { nil },
deleteNetwork: {},
deleteEsplora: {}
saveNetwork: { _ in }
)
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import Foundation

enum KeyServiceError: Error {
case decodingError
case encodingError
case readError
case writeError
case urlError
case decodingError
case readError
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import Foundation
@Observable
class ActivityListViewModel {
let bdkClient: BDKClient
var walletSyncState: WalletSyncState

var displayMode: DisplayMode = .transactions
var inspectedScripts: UInt64 = 0
var localOutputs: [LocalOutput] = []
var progress: Float = 0.0
var transactions: [CanonicalTx]
var walletViewError: AppError?
var showingWalletViewErrorAlert = false
var progress: Float = 0.0
var inspectedScripts: UInt64 = 0
var totalScripts: UInt64 = 0
var localOutputs: [LocalOutput] = []
var displayMode: DisplayMode = .transactions
var walletSyncState: WalletSyncState
var walletViewError: AppError?

enum DisplayMode {
case transactions
Expand All @@ -29,17 +30,18 @@ class ActivityListViewModel {

init(
bdkClient: BDKClient = .live,
walletSyncState: WalletSyncState = .notStarted,
transactions: [CanonicalTx] = []
transactions: [CanonicalTx] = [],
walletSyncState: WalletSyncState = .notStarted
) {
self.bdkClient = bdkClient
self.walletSyncState = walletSyncState
self.transactions = transactions
self.walletSyncState = walletSyncState
}

func listUnspent() {
func getTransactions() {
do {
self.localOutputs = try bdkClient.listUnspent()
let transactionDetails = try bdkClient.transactions()
self.transactions = transactionDetails
} catch let error as WalletError {
self.walletViewError = .generic(message: error.localizedDescription)
self.showingWalletViewErrorAlert = true
Expand All @@ -49,10 +51,9 @@ class ActivityListViewModel {
}
}

func getTransactions() {
func listUnspent() {
do {
let transactionDetails = try bdkClient.transactions()
self.transactions = transactionDetails
self.localOutputs = try bdkClient.listUnspent()
} catch let error as WalletError {
self.walletViewError = .generic(message: error.localizedDescription)
self.showingWalletViewErrorAlert = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ class TransactionDetailViewModel {
let bdkClient: BDKClient
let keyClient: KeyClient

var network: String?
var esploraURL: String?

var esploraError: EsploraError?
var calculateFee: String?
var calculateFeeError: CalculateFeeError?
var transactionDetailsError: AppError?

var esploraError: EsploraError?
var esploraURL: String?
var network: String?
var showingTransactionDetailsViewErrorAlert = false
var calculateFee: String?
var transactionDetailsError: AppError?

init(
bdkClient: BDKClient = .live,
Expand All @@ -33,14 +31,16 @@ class TransactionDetailViewModel {
self.keyClient = keyClient
}

func getNetwork() {
func getCalulateFee(tx: BitcoinDevKit.Transaction) {
do {
self.network = try keyClient.getNetwork()
} catch {
let calculateFee = try bdkClient.calculateFee(tx)
let feeString = String(calculateFee.toSat())
self.calculateFee = feeString
} catch let error as CalculateFeeError {
DispatchQueue.main.async {
self.transactionDetailsError = .generic(message: error.localizedDescription)
self.calculateFeeError = error
}
}
} catch {}
}

func getEsploraUrl() {
Expand All @@ -58,28 +58,26 @@ class TransactionDetailViewModel {
} catch {}
}

func getSentAndReceived(tx: BitcoinDevKit.Transaction) -> SentAndReceivedValues? {
func getNetwork() {
do {
let sentAndReceived = try bdkClient.sentAndReceived(tx)
return sentAndReceived
self.network = try keyClient.getNetwork()
} catch {
DispatchQueue.main.async {
self.transactionDetailsError = .generic(message: error.localizedDescription)
}
return nil
}
}

func getCalulateFee(tx: BitcoinDevKit.Transaction) {
func getSentAndReceived(tx: BitcoinDevKit.Transaction) -> SentAndReceivedValues? {
do {
let calculateFee = try bdkClient.calculateFee(tx)
let feeString = String(calculateFee.toSat())
self.calculateFee = feeString
} catch let error as CalculateFeeError {
let sentAndReceived = try bdkClient.sentAndReceived(tx)
return sentAndReceived
} catch {
DispatchQueue.main.async {
self.calculateFeeError = error
self.transactionDetailsError = .generic(message: error.localizedDescription)
}
} catch {}
return nil
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import Foundation
@Observable
class TransactionListViewModel {
let bdkClient: BDKClient
var walletTransactionsViewError: AppError?

var showingWalletTransactionsViewErrorAlert = false
var walletTransactionsViewError: AppError?

init(
bdkClient: BDKClient = .live
Expand Down
Loading
Loading