Skip to content

Commit

Permalink
Refactor BtcBlockchainSettings module to SwiftUI
Browse files Browse the repository at this point in the history
  • Loading branch information
ealymbaev committed Sep 7, 2023
1 parent a6e1dcd commit 71143be
Show file tree
Hide file tree
Showing 17 changed files with 265 additions and 245 deletions.
65 changes: 57 additions & 8 deletions UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
enum BtcRestoreMode: String, CaseIterable {
enum BtcRestoreMode: String, CaseIterable, Identifiable {
case api
case blockchain

var id: Self {
self
}

var title: String {
switch self {
case .api: return "API"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import SwiftUI
import UIKit
import ThemeKit
import MarketKit

struct BtcBlockchainSettingsModule {

static func viewController(blockchain: Blockchain) -> UIViewController {
let service = BtcBlockchainSettingsService(blockchain: blockchain, btcBlockchainManager: App.shared.btcBlockchainManager)
let viewModel = BtcBlockchainSettingsViewModel(service: service)
let viewController = BtcBlockchainSettingsViewController(viewModel: viewModel)
let view = BtcBlockchainSettingsView(viewModel: viewModel)

return ThemeNavigationController(rootViewController: viewController)
return UIHostingController(rootView: view)
}

}
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
import RxSwift
import RxRelay
import MarketKit

class BtcBlockchainSettingsService {
let blockchain: Blockchain
private let btcBlockchainManager: BtcBlockchainManager
private let disposeBag = DisposeBag()

var restoreMode: BtcRestoreMode {
didSet {
syncHasChanges()
}
}

private let hasChangesRelay = BehaviorRelay<Bool>(value: false)
let currentRestoreMode: BtcRestoreMode

init(blockchain: Blockchain, btcBlockchainManager: BtcBlockchainManager) {
self.blockchain = blockchain
self.btcBlockchainManager = btcBlockchainManager

restoreMode = btcBlockchainManager.restoreMode(blockchainType: blockchain.type)
}

private func syncHasChanges() {
let initialRestoreMode = btcBlockchainManager.restoreMode(blockchainType: blockchain.type)

hasChangesRelay.accept(restoreMode != initialRestoreMode)
currentRestoreMode = btcBlockchainManager.restoreMode(blockchainType: blockchain.type)
}

}

extension BtcBlockchainSettingsService {

var hasChangesObservable: Observable<Bool> {
hasChangesRelay.asObservable()
}

func save() {
if restoreMode != btcBlockchainManager.restoreMode(blockchainType: blockchain.type) {
btcBlockchainManager.save(restoreMode: restoreMode, blockchainType: blockchain.type)
}
func save(restoreMode: BtcRestoreMode) {
btcBlockchainManager.save(restoreMode: restoreMode, blockchainType: blockchain.type)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import SwiftUI
import SDWebImageSwiftUI

struct BtcBlockchainSettingsView: View {
@ObservedObject var viewModel: BtcBlockchainSettingsViewModel

@Environment(\.presentationMode) var presentationMode
@State var infoPresented = false

var body: some View {
ThemeNavigationView {
ThemeView {
VStack(spacing: 0) {
ScrollView {
VStack(spacing: .margin24) {
HighlightedTextView(text: "btc_blockchain_settings.restore_source.alert".localized(viewModel.title))

VStack(spacing: 0) {
ListSectionInfoHeader(text: "btc_blockchain_settings.restore_source".localized) {
infoPresented = true
}
.sheet(isPresented: $infoPresented) {
InfoModule.restoreSourceInfo
}

ListSection {
ForEach(viewModel.restoreModes) { restoreMode in
ClickableRow(action: {
viewModel.selectedRestoreMode = restoreMode
}) {
VStack(spacing: 1) {
Text(restoreMode.title).themeBody()
Text(restoreMode.description).themeSubhead2()
}

if restoreMode == viewModel.selectedRestoreMode {
Image("check_1_20").themeIcon(color: .themeJacob)
}
}
}
}

ListSectionFooter(text: "btc_blockchain_settings.restore_source.description".localized)
}
}
.padding(EdgeInsets(top: .margin12, leading: .margin16, bottom: .margin32, trailing: .margin16))
}

Button(action: {
viewModel.onTapSave()
presentationMode.wrappedValue.dismiss()
}) {
Text("Save")
}
.disabled(!viewModel.saveEnabled)
}
}
.navigationBarTitle(viewModel.title)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
WebImage(url: URL(string: viewModel.iconUrl))
.resizable()
.scaledToFit()
.frame(width: .iconSize24, height: .iconSize24)
}

ToolbarItem(placement: .navigationBarTrailing) {
Button("button.cancel".localized) {
presentationMode.wrappedValue.dismiss()
}
}
}
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
import RxSwift
import RxRelay
import RxCocoa
import SwiftUI

class BtcBlockchainSettingsViewModel {
class BtcBlockchainSettingsViewModel: ObservableObject {
private let service: BtcBlockchainSettingsService
private let disposeBag = DisposeBag()

private let restoreModeViewItemsRelay = BehaviorRelay<[ViewItem]>(value: [])
private let finishRelay = PublishRelay<()>()
let restoreModes: [BtcRestoreMode] = BtcRestoreMode.allCases

init(service: BtcBlockchainSettingsService) {
self.service = service

syncRestoreModeState()
}

private func syncRestoreModeState() {
let viewItems = BtcRestoreMode.allCases.map { mode in
ViewItem(name: mode.title, description: mode.description, selected: mode == service.restoreMode)
@Published var selectedRestoreMode: BtcRestoreMode {
didSet {
saveEnabled = selectedRestoreMode != service.currentRestoreMode
}
restoreModeViewItemsRelay.accept(viewItems)
}

}
@Published var saveEnabled = false

extension BtcBlockchainSettingsViewModel {
init(service: BtcBlockchainSettingsService) {
self.service = service

var restoreModeViewItemsDriver: Driver<[ViewItem]> {
restoreModeViewItemsRelay.asDriver()
selectedRestoreMode = service.currentRestoreMode
}

var canSaveDriver: Driver<Bool> {
service.hasChangesObservable.asDriver(onErrorJustReturn: false)
}
}

var finishSignal: Signal<()> {
finishRelay.asSignal()
}
extension BtcBlockchainSettingsViewModel {

var title: String {
service.blockchain.name
Expand All @@ -46,24 +31,8 @@ extension BtcBlockchainSettingsViewModel {
service.blockchain.type.imageUrl
}

func onSelectRestoreMode(index: Int) {
service.restoreMode = BtcRestoreMode.allCases[index]
syncRestoreModeState()
}

func onTapSave() {
service.save()
finishRelay.accept(())
}

}

extension BtcBlockchainSettingsViewModel {

struct ViewItem {
let name: String
let description: String
let selected: Bool
service.save(restoreMode: selectedRestoreMode)
}

}
Loading

0 comments on commit 71143be

Please sign in to comment.