Skip to content

Commit

Permalink
Integrate prefilled and predefined modes into new Send module
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed May 2, 2024
1 parent 52d7aed commit 0c78460
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SendBinanceService {
private let scheduler = SerialDispatchQueueScheduler(qos: .userInitiated, internalSerialQueueName: "\(AppConfig.label).send-bitcoin-service")

let token: Token
let mode: SendBaseService.Mode
let mode: PreSendViewModel.Mode

private let amountService: IAmountInputService
private let amountCautionService: SendAmountCautionService
Expand All @@ -24,7 +24,7 @@ class SendBinanceService {
}
}

init(amountService: IAmountInputService, amountCautionService: SendAmountCautionService, addressService: AddressService, memoService: SendMemoInputService, adapter: ISendBinanceAdapter, reachabilityManager: IReachabilityManager, token: Token, mode: SendBaseService.Mode) {
init(amountService: IAmountInputService, amountCautionService: SendAmountCautionService, addressService: AddressService, memoService: SendMemoInputService, adapter: ISendBinanceAdapter, reachabilityManager: IReachabilityManager, token: Token, mode: PreSendViewModel.Mode) {
self.amountService = amountService
self.amountCautionService = amountCautionService
self.addressService = addressService
Expand All @@ -38,7 +38,7 @@ class SendBinanceService {
addressService.set(text: address)
if let amount { addressService.publishAmountRelay.accept(amount) }
case let .predefined(address): addressService.set(text: address)
case .send: ()
case .regular: ()
}

subscribe(MainScheduler.instance, disposeBag, reachabilityManager.reachabilityObservable) { [weak self] isReachable in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SendBitcoinService {
private let scheduler = SerialDispatchQueueScheduler(qos: .userInitiated, internalSerialQueueName: "\(AppConfig.label).send-bitcoin-service")

let token: Token
let mode: SendBaseService.Mode
let mode: PreSendViewModel.Mode

private let amountService: IAmountInputService
private let amountCautionService: SendAmountCautionService
Expand All @@ -25,7 +25,7 @@ class SendBitcoinService {
}
}

init(amountService: IAmountInputService, amountCautionService: SendAmountCautionService, addressService: AddressService, adapterService: SendBitcoinAdapterService, feeRateService: FeeRateService, timeLockErrorService: SendTimeLockErrorService?, reachabilityManager: IReachabilityManager, token: Token, mode: SendBaseService.Mode) {
init(amountService: IAmountInputService, amountCautionService: SendAmountCautionService, addressService: AddressService, adapterService: SendBitcoinAdapterService, feeRateService: FeeRateService, timeLockErrorService: SendTimeLockErrorService?, reachabilityManager: IReachabilityManager, token: Token, mode: PreSendViewModel.Mode) {
self.amountService = amountService
self.amountCautionService = amountCautionService
self.addressService = addressService
Expand All @@ -40,7 +40,7 @@ class SendBitcoinService {
addressService.set(text: address)
if let amount { addressService.publishAmountRelay.accept(amount) }
case let .predefined(address): addressService.set(text: address)
case .send: ()
case .regular: ()
}

subscribe(MainScheduler.instance, disposeBag, reachabilityManager.reachabilityObservable) { [weak self] isReachable in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ extension SendBaseService {
case notReady
}

enum Mode {
case send
case prefilled(address: String, amount: Decimal?)
case predefined(address: String)

var amount: Decimal? {
switch self {
case let .prefilled(_, amount): return amount
default: return nil
}
}
}

enum AmountError: Error {
case invalidDecimal
case insufficientBalance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protocol ITitledCautionViewModel {
}

enum SendModule {
static func controller(wallet: Wallet, mode: SendBaseService.Mode = .send) -> UIViewController? {
static func controller(wallet: Wallet, mode: PreSendViewModel.Mode = .regular) -> UIViewController? {
guard let adapter = App.shared.adapterManager.adapter(for: wallet) else {
return nil
}
Expand All @@ -33,7 +33,7 @@ enum SendModule {
}
}

private static func viewController(token: Token, mode: SendBaseService.Mode, adapter: ISendBitcoinAdapter) -> UIViewController? {
private static func viewController(token: Token, mode: PreSendViewModel.Mode, adapter: ISendBitcoinAdapter) -> UIViewController? {
guard let feeRateProvider = App.shared.feeRateProviderFactory.provider(blockchainType: token.blockchainType) else {
return nil
}
Expand Down Expand Up @@ -174,7 +174,7 @@ enum SendModule {
return viewController
}

private static func viewController(token: Token, mode: SendBaseService.Mode, adapter: ISendBinanceAdapter) -> UIViewController? {
private static func viewController(token: Token, mode: PreSendViewModel.Mode, adapter: ISendBinanceAdapter) -> UIViewController? {
let feeToken = App.shared.feeCoinProvider.feeToken(token: token) ?? token

let switchService = AmountTypeSwitchService(userDefaultsStorage: App.shared.userDefaultsStorage)
Expand Down Expand Up @@ -267,7 +267,7 @@ enum SendModule {
return viewController
}

private static func viewController(token: Token, mode: SendBaseService.Mode, adapter: ISendZcashAdapter) -> UIViewController? {
private static func viewController(token: Token, mode: PreSendViewModel.Mode, adapter: ISendZcashAdapter) -> UIViewController? {
let switchService = AmountTypeSwitchService(userDefaultsStorage: App.shared.userDefaultsStorage)
let coinService = CoinService(token: token, currencyManager: App.shared.currencyManager, marketKit: App.shared.marketKit)
let fiatService = FiatService(switchService: switchService, currencyManager: App.shared.currencyManager, marketKit: App.shared.marketKit, amount: mode.amount ?? 0)
Expand Down Expand Up @@ -357,7 +357,7 @@ enum SendModule {
return viewController
}

private static func viewController(token: Token, mode: SendBaseService.Mode, adapter: ISendTonAdapter) -> UIViewController? {
private static func viewController(token: Token, mode: PreSendViewModel.Mode, adapter: ISendTonAdapter) -> UIViewController? {
let switchService = AmountTypeSwitchService(userDefaultsStorage: App.shared.userDefaultsStorage)
let coinService = CoinService(token: token, currencyManager: App.shared.currencyManager, marketKit: App.shared.marketKit)
let fiatService = FiatService(switchService: switchService, currencyManager: App.shared.currencyManager, marketKit: App.shared.marketKit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import RxSwift

protocol ISendBaseService {
var token: Token { get }
var mode: SendBaseService.Mode { get }
var mode: PreSendViewModel.Mode { get }
var state: SendBaseService.State { get }
var stateObservable: Observable<SendBaseService.State> { get }
}
Expand Down Expand Up @@ -57,14 +57,14 @@ extension SendViewModelOld {

var title: String {
switch service.mode {
case .send, .prefilled: return "send.title".localized(token.coin.code)
case .regular, .prefilled: return "send.title".localized(token.coin.code)
case .predefined: return "donate.title".localized(token.coin.code)
}
}

var showAddress: Bool {
switch service.mode {
case .send, .prefilled: return true
case .regular, .prefilled: return true
case .predefined: return false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SendTonService {
private let scheduler = SerialDispatchQueueScheduler(qos: .userInitiated, internalSerialQueueName: "\(AppConfig.label).send-bitcoin-service")

let token: Token
let mode: SendBaseService.Mode
let mode: PreSendViewModel.Mode

private let amountService: IAmountInputService
private let amountCautionService: SendAmountCautionService
Expand Down Expand Up @@ -44,7 +44,7 @@ class SendTonService {
}
}

init(amountService: IAmountInputService, amountCautionService: SendAmountCautionService, addressService: AddressService, memoService: SendMemoInputService, adapter: ISendTonAdapter, reachabilityManager: IReachabilityManager, token: Token, mode: SendBaseService.Mode) {
init(amountService: IAmountInputService, amountCautionService: SendAmountCautionService, addressService: AddressService, memoService: SendMemoInputService, adapter: ISendTonAdapter, reachabilityManager: IReachabilityManager, token: Token, mode: PreSendViewModel.Mode) {
self.amountService = amountService
self.amountCautionService = amountCautionService
self.addressService = addressService
Expand All @@ -58,7 +58,7 @@ class SendTonService {
addressService.set(text: address)
if let amount { addressService.publishAmountRelay.accept(amount) }
case let .predefined(address): addressService.set(text: address)
case .send: ()
case .regular: ()
}

availableBalance = .completed(adapter.availableBalance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SendZcashService {
private let scheduler = SerialDispatchQueueScheduler(qos: .userInitiated, internalSerialQueueName: "\(AppConfig.label).send-bitcoin-service")

let token: Token
let mode: SendBaseService.Mode
let mode: PreSendViewModel.Mode

private let amountService: IAmountInputService
private let amountCautionService: SendAmountCautionService
Expand All @@ -32,7 +32,7 @@ class SendZcashService {
}
}

init(amountService: IAmountInputService, amountCautionService: SendAmountCautionService, addressService: AddressService, memoService: SendMemoInputService, adapter: ISendZcashAdapter, reachabilityManager: IReachabilityManager, token: Token, mode: SendBaseService.Mode) {
init(amountService: IAmountInputService, amountCautionService: SendAmountCautionService, addressService: AddressService, memoService: SendMemoInputService, adapter: ISendZcashAdapter, reachabilityManager: IReachabilityManager, token: Token, mode: PreSendViewModel.Mode) {
self.amountService = amountService
self.amountCautionService = amountCautionService
self.addressService = addressService
Expand All @@ -46,7 +46,7 @@ class SendZcashService {
addressService.set(text: address)
if let amount { addressService.publishAmountRelay.accept(amount) }
case let .predefined(address): addressService.set(text: address)
case .send: ()
case .regular: ()
}

subscribe(MainScheduler.instance, disposeBag, reachabilityManager.reachabilityObservable) { [weak self] isReachable in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ThemeKit
import UIKit

enum SendEvmModule {
static func viewController(token: Token, mode: SendBaseService.Mode, adapter: ISendEthereumAdapter) -> UIViewController {
static func viewController(token: Token, mode: PreSendViewModel.Mode, adapter: ISendEthereumAdapter) -> UIViewController {
let evmAddressParserItem = EvmAddressParser()
let udnAddressParserItem = UdnAddressParserItem.item(rawAddressParserItem: evmAddressParserItem, coinCode: token.coin.code, token: token)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import RxSwift

class SendEvmService {
let sendToken: Token
let mode: SendBaseService.Mode
let mode: PreSendViewModel.Mode

private let disposeBag = DisposeBag()
private let adapter: ISendEthereumAdapter
Expand All @@ -31,7 +31,7 @@ class SendEvmService {
}
}

init(token: Token, mode: SendBaseService.Mode, adapter: ISendEthereumAdapter, addressService: AddressService) {
init(token: Token, mode: PreSendViewModel.Mode, adapter: ISendEthereumAdapter, addressService: AddressService) {
sendToken = token
self.mode = mode
self.adapter = adapter
Expand All @@ -42,7 +42,7 @@ class SendEvmService {
addressService.set(text: address)
if let amount { addressService.publishAmountRelay.accept(amount) }
case let .predefined(address): addressService.set(text: address)
case .send: ()
case .regular: ()
}

subscribe(disposeBag, addressService.stateObservable) { [weak self] in self?.sync(addressState: $0) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class SendEvmViewModel {
extension SendEvmViewModel {
var title: String {
switch service.mode {
case .send, .prefilled: return "send.title".localized(token.coin.code)
case .regular, .prefilled: return "send.title".localized(token.coin.code)
case .predefined: return "donate.title".localized(token.coin.code)
}
}

var showAddress: Bool {
switch service.mode {
case .send, .prefilled: return true
case .regular, .prefilled: return true
case .predefined: return false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ struct PreSendView: View {

@State private var confirmPresented = false

init(wallet: Wallet) {
_viewModel = StateObject(wrappedValue: PreSendViewModel(wallet: wallet))
init(wallet: Wallet, mode: PreSendViewModel.Mode = .regular) {
_viewModel = StateObject(wrappedValue: PreSendViewModel(wallet: wallet, mode: mode))
}

var body: some View {
Expand All @@ -24,7 +24,10 @@ struct PreSendView: View {
availableBalanceView(value: balanceValue())
}

addressView()
if viewModel.addressVisible {
addressView()
}

buttonView()
}
.padding(EdgeInsets(top: .margin12, leading: .margin16, bottom: .margin16, trailing: .margin16))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import RxSwift

class PreSendViewModel: ObservableObject {
private let wallet: Wallet
private let mode: Mode
private let currencyManager = App.shared.currencyManager
private let marketKit = App.shared.marketKit
private let walletManager = App.shared.walletManager
Expand Down Expand Up @@ -106,12 +107,32 @@ class PreSendViewModel: ObservableObject {
private var handler: IPreSendHandler?
@Published var sendData: SendData?

init(wallet: Wallet) {
let addressVisible: Bool

init(wallet: Wallet, mode: Mode) {
self.wallet = wallet
handler = SendHandlerFactory.preSendHandler(wallet: wallet)
self.mode = mode

handler = SendHandlerFactory.preSendHandler(wallet: wallet)
currency = currencyManager.baseCurrency

switch mode {
case let .predefined(address):
addressResult = .valid(.init(address: .init(raw: address), uri: nil))
addressVisible = false
default:
addressVisible = true
}

defer {
switch mode {
case let .prefilled(address, amount):
self.address = address
self.amount = amount
default: ()
}
}

currencyManager.$baseCurrency
.receive(on: DispatchQueue.main)
.sink { [weak self] in self?.currency = $0 }
Expand Down Expand Up @@ -229,3 +250,18 @@ extension PreSendViewModel {
isAddressActive = active
}
}

extension PreSendViewModel {
enum Mode {
case regular
case prefilled(address: String, amount: Decimal?)
case predefined(address: String)

var amount: Decimal? {
switch self {
case let .prefilled(_, amount): return amount
default: return nil
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SendViewModel: ObservableObject {
transactionSettingsModified = transactionService?.modified ?? false
}

private func syncRates(coins: [Coin]) {
@MainActor private func syncRates(coins: [Coin]) {
let coinUids = Array(Set(coins)).map(\.uid)

rates = marketKit.coinPriceMap(coinUids: coinUids, currencyCode: currency.code).mapValues { $0.value }
Expand Down Expand Up @@ -106,7 +106,7 @@ extension SendViewModel {

let data = try await handler.sendData(transactionSettings: transactionService.transactionSettings)

self?.syncRates(coins: [handler.baseToken.coin] + data.rateCoins)
await self?.syncRates(coins: [handler.baseToken.coin] + data.rateCoins)

state = .success(data: data)
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ThemeKit
import UIKit

enum SendTronModule {
static func viewController(token: Token, mode: SendBaseService.Mode, adapter: ISendTronAdapter) -> UIViewController {
static func viewController(token: Token, mode: PreSendViewModel.Mode, adapter: ISendTronAdapter) -> UIViewController {
let tronAddressParserItem = TronAddressParser()
let addressParserChain = AddressParserChain().append(handler: tronAddressParserItem)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import TronKit

class SendTronService {
let sendToken: Token
let mode: SendBaseService.Mode
let mode: PreSendViewModel.Mode

private let disposeBag = DisposeBag()
private let adapter: ISendTronAdapter
Expand Down Expand Up @@ -41,7 +41,7 @@ class SendTronService {

private let activeAddressRelay = PublishRelay<Bool>()

init(token: Token, mode: SendBaseService.Mode, adapter: ISendTronAdapter, addressService: AddressService, memoService: SendMemoInputService) {
init(token: Token, mode: PreSendViewModel.Mode, adapter: ISendTronAdapter, addressService: AddressService, memoService: SendMemoInputService) {
sendToken = token
self.mode = mode
self.adapter = adapter
Expand All @@ -53,7 +53,7 @@ class SendTronService {
addressService.set(text: address)
if let amount { addressService.publishAmountRelay.accept(amount) }
case let .predefined(address): addressService.set(text: address)
case .send: ()
case .regular: ()
}

subscribe(disposeBag, addressService.stateObservable) { [weak self] in self?.sync(addressState: $0) }
Expand Down
Loading

0 comments on commit 0c78460

Please sign in to comment.