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

feat(coinjoin): send screen mixing info #666

Merged
merged 2 commits into from
Oct 1, 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
2 changes: 1 addition & 1 deletion DashSyncCurrentCommit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
315879b4d157a026fc760d3742f6a55a8883fde5
49b001b3a003a443fd3fc6d2cd1a470e2cb7638a
60 changes: 45 additions & 15 deletions DashWallet/Sources/Models/CoinJoin/CoinJoinService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ enum MixingStatus: Int {
}
}

enum CoinJoinMode {
enum CoinJoinMode: Int {
case none
case intermediate
case advanced
}

private let kDefaultMultisession = false // for stability, need to investigate
private let kDefaultRounds: Int32 = 1 //4 TODO
private let kDefaultSessions: Int32 = 1 //6 TODO
private let kDefaultRounds: Int32 = 4
private let kDefaultSessions: Int32 = 6
private let kDefaultDenominationGoal: Int32 = 50
private let kDefaultDenominationHardcap: Int32 = 300
private let kCoinJoinMode = "coinJoinModeKey"
Expand All @@ -74,7 +74,21 @@ class CoinJoinService: NSObject {
private var hasAnonymizableBalance: Bool = false
private var networkStatus: NetworkStatus = .online

@Published private(set) var mode: CoinJoinMode = .none
private var _savedMode: Int? = nil
private var savedMode: Int {
get { _savedMode ?? UserDefaults.standard.integer(forKey: kCoinJoinMode) }
set(value) {
_savedMode = value
UserDefaults.standard.set(value, forKey: kCoinJoinMode)
}
}

@Published private(set) var mode: CoinJoinMode = .none {
didSet {
savedMode = mode.rawValue
}
}

@Published var mixingState: MixingStatus = .notStarted
@Published private(set) var progress: Double = 0.0
@Published private(set) var totalBalance: UInt64 = 0
Expand All @@ -83,9 +97,16 @@ class CoinJoinService: NSObject {

override init() {
super.init()

NotificationCenter.default.publisher(for: NSNotification.Name.DSWalletBalanceDidChange)
.sink { [weak self] _ in self?.updateBalance(balance: DWEnvironment.sharedInstance().currentAccount.balance) }
.store(in: &cancellableBag)

let mode = CoinJoinMode(rawValue: savedMode) ?? .none

if mode != .none {
updateMode(mode: mode)
}
}

func updateMode(mode: CoinJoinMode) {
Expand Down Expand Up @@ -140,11 +161,20 @@ class CoinJoinService: NSObject {
}

private func updateProgress() {
guard let coinJoinManager = self.coinJoinManager else { return }
self.progress = coinJoinManager.getMixingProgress()
let coinJoinBalance = coinJoinManager.getBalance()
self.totalBalance = coinJoinBalance.myTrusted
self.coinJoinBalance = coinJoinBalance.anonymized
DispatchQueue.global(qos: .background).async { [weak self] in
guard let self = self, let coinJoinManager = self.coinJoinManager else { return }

let progress = coinJoinManager.getMixingProgress()
let coinJoinBalance = coinJoinManager.getBalance()
let totalBalance = coinJoinBalance.myTrusted
let anonymizedBalance = coinJoinBalance.anonymized

DispatchQueue.main.async {
self.progress = progress
self.totalBalance = totalBalance
self.coinJoinBalance = anonymizedBalance
}
}
}

private func createCoinJoinManager() -> DSCoinJoinManager? {
Expand All @@ -153,12 +183,6 @@ class CoinJoinService: NSObject {
return self.coinJoinManager
}

private func synchronized(_ lock: NSLock, closure: () -> Void) {
lock.lock()
defer { lock.unlock() }
closure()
}

private func updateBalance(balance: UInt64) {
guard let coinJoinManager = self.coinJoinManager else { return }

Expand Down Expand Up @@ -294,5 +318,11 @@ extension CoinJoinService: DSCoinJoinManagerDelegate {

DSLogger.log("[SW] CoinJoin: Active sessions: \(activeSessions)")
}

private func synchronized(_ lock: NSLock, closure: () -> Void) {
lock.lock()
defer { lock.unlock() }
closure()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,23 @@ extension CoinJoinLevelsViewController {

@objc
private func selectIntermediate() {
if viewModel.selectedMode == .intermediate {
return
}

if viewModel.mixingState == .mixing {
confirmFor(.intermediate)
} else {
viewModel.selectedMode = .intermediate
}
selectMode(.intermediate)
}

@objc
private func selectAdvanced() {
if viewModel.selectedMode == .advanced {
selectMode(.advanced)
}

private func selectMode(_ mode: CoinJoinMode) {
if viewModel.selectedMode == mode {
return
}

if viewModel.mixingState == .mixing {
confirmFor(.advanced)
if viewModel.selectedMode == .none {
viewModel.selectedMode = mode
} else {
viewModel.selectedMode = .advanced
confirmFor(mode)
}
}

Expand Down Expand Up @@ -176,6 +172,7 @@ extension CoinJoinLevelsViewController {
let alert = UIAlertController(title: "", message: NSLocalizedString("Are you sure you want to change the privacy level?", comment: "CoinJoin"), preferredStyle: .alert)
alert.addAction(UIAlertAction(title: title, style: .default, handler: { [weak self] _ in
self?.viewModel.selectedMode = mode
self?.viewModel.startMixing()
}))
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel)
alert.addAction(cancelAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class CoinJoinViewModel: ObservableObject {
}

init() {
coinJoinService.$mode
.receive(on: DispatchQueue.main)
.sink { [weak self] mode in
self?.selectedMode = mode
}
.store(in: &cancellableBag)

coinJoinService.$mixingState
.receive(on: DispatchQueue.main)
.sink { [weak self] state in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ final class BuyCreditsViewController: SendAmountViewController, ObservableObject

let intro = SendIntro(
title: NSLocalizedString("Buy credits", comment: "Credits"),
dashBalance: Int64(model.walletBalance),
balanceLabel: NSLocalizedString("Dash balance", comment: "") + ":"
dashBalance: model.walletBalance,
balanceLabel: NSLocalizedString("Dash balance", comment: "") + ":",
avatarView: { }
)
let swiftUIController = UIHostingController(rootView: intro)
swiftUIController.view.backgroundColor = UIColor.dw_secondaryBackground()
Expand Down Expand Up @@ -131,7 +132,9 @@ final class BuyCreditsViewController: SendAmountViewController, ObservableObject
rateLabel.topAnchor.constraint(equalTo: rateContainer.topAnchor, constant: 4),
rateLabel.leadingAnchor.constraint(equalTo: rateContainer.leadingAnchor, constant: 8),
rateLabel.trailingAnchor.constraint(equalTo: rateContainer.trailingAnchor, constant: -8),
rateLabel.bottomAnchor.constraint(equalTo: rateContainer.bottomAnchor, constant: -4)
rateLabel.bottomAnchor.constraint(equalTo: rateContainer.bottomAnchor, constant: -4),

swiftUIController.view.heightAnchor.constraint(equalToConstant: 100)
])
}

Expand Down
22 changes: 11 additions & 11 deletions DashWallet/Sources/UI/Home/Views/Cells/CoinJoinProgressView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ struct CoinJoinProgressInfo: View {
.foregroundColor(textColor)
.font(font)
.padding(.leading, 4)

Spacer()
Text("\(mixed, format: .number.precision(.fractionLength(0...3))) of \(total, format: .number.precision(.fractionLength(0...3)))") // TODO
.foregroundColor(textColor)
.font(font)
Image("icon_dash_currency")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: font.pointSize, height: font.pointSize)
.padding(.leading, 2)
.foregroundColor(textColor)
}

Spacer()
Text("\(mixed, format: .number.precision(.fractionLength(0...3))) of \(total, format: .number.precision(.fractionLength(0...3)))") // TODO
.foregroundColor(textColor)
.font(font)
Image("icon_dash_currency")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: font.pointSize, height: font.pointSize)
.padding(.leading, 2)
.foregroundColor(textColor)
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions DashWallet/Sources/UI/Home/Views/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,6 @@ struct TransactionList<Content: View>: View {
.sheet(item: $selectedTxDataItem) { item in
TransactionDetailsSheet(item: item)
}
.onChange(of: viewModel.coinJoinItem) { new in
DSLogger.log("[SW] CoinJoin: on change of coinJoinItem: \(viewModel.coinJoinItem.description)")
}
}

@ViewBuilder
Expand Down
Loading