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

[ABW-3026] Account settings XRD faucet placement #1061

Merged
merged 3 commits into from
Apr 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import SwiftUI
public struct AccountPreferences: Sendable, FeatureReducer {
public struct State: Sendable, Hashable {
public var account: Profile.Network.Account
public var faucetButtonState: ControlState
public var address: AccountAddress { account.address }
public var isOnMainnet: Bool { account.networkID == .mainnet }

@PresentationState
var destination: Destination.State? = nil

public init(account: Profile.Network.Account) {
public init(
account: Profile.Network.Account,
faucetButtonState: ControlState = .enabled
) {
self.account = account
self.faucetButtonState = faucetButtonState
}
}

Expand All @@ -21,10 +28,15 @@ public struct AccountPreferences: Sendable, FeatureReducer {
case qrCodeButtonTapped
case rowTapped(AccountPreferences.Section.SectionRow)
case hideAccountTapped
case faucetButtonTapped
}

public enum InternalAction: Sendable, Equatable {
case accountUpdated(Profile.Network.Account)
case isAllowedToUseFaucet(TaskResult<Bool>)
case callDone(updateControlState: WritableKeyPath<State, ControlState>, changeTo: ControlState = .enabled)
case refreshAccountCompleted(TaskResult<OnLedgerEntity.Account>)
case hideLoader(updateControlState: WritableKeyPath<State, ControlState>)
}

public enum DelegateAction: Sendable, Equatable {
Expand Down Expand Up @@ -75,6 +87,9 @@ public struct AccountPreferences: Sendable, FeatureReducer {
@Dependency(\.accountsClient) var accountsClient
@Dependency(\.entitiesVisibilityClient) var entitiesVisibilityClient
@Dependency(\.overlayWindowClient) var overlayWindowClient
@Dependency(\.faucetClient) var faucetClient
@Dependency(\.accountPortfoliosClient) var accountPortfoliosClient
@Dependency(\.gatewaysClient) var gatewaysClient
@Dependency(\.errorQueue) var errorQueue

public init() {}
Expand All @@ -97,6 +112,7 @@ public struct AccountPreferences: Sendable, FeatureReducer {
await send(.internal(.accountUpdated(accountUpdate)))
}
}
.merge(with: state.isOnMainnet ? .none : loadIsAllowedToUseFaucet(&state))

case .qrCodeButtonTapped:
state.destination = .showQR(.init(accountAddress: state.account.address))
Expand All @@ -115,6 +131,11 @@ public struct AccountPreferences: Sendable, FeatureReducer {
]
))
return .none

case .faucetButtonTapped:
return call(buttonState: \.faucetButtonState, into: &state) {
try await faucetClient.getFreeXRD(.init(recipientAccountAddress: $0))
}
}
}

Expand All @@ -123,6 +144,32 @@ public struct AccountPreferences: Sendable, FeatureReducer {
case let .accountUpdated(updated):
state.account = updated
return .none

case let .isAllowedToUseFaucet(.success(value)):
state.faucetButtonState = value ? .enabled : .disabled
return .none

case let .isAllowedToUseFaucet(.failure(error)):
state.faucetButtonState = .disabled
errorQueue.schedule(error)
return .none

case let .hideLoader(controlStateKeyPath):
state[keyPath: controlStateKeyPath] = .enabled
return .none

case .refreshAccountCompleted:
state.faucetButtonState = .disabled
return .none

case let .callDone(controlStateKeyPath, changeTo):
if controlStateKeyPath == \State.faucetButtonState {
// NB: This call to update might be superfluous, since after any transaction we fetch all accounts
return updateAccountPortfolio(state).concatenate(with: loadIsAllowedToUseFaucet(&state))
} else {
state[keyPath: controlStateKeyPath] = changeTo
return .none
}
}
}

Expand Down Expand Up @@ -167,6 +214,45 @@ public struct AccountPreferences: Sendable, FeatureReducer {
}
}

extension AccountPreferences {
private func call(
buttonState: WritableKeyPath<State, ControlState>,
into state: inout State,
onSuccess: ControlState = .enabled,
call: @escaping @Sendable (AccountAddress) async throws -> Void
) -> Effect<Action> {
state[keyPath: buttonState] = .loading(.local)
return .run { [address = state.address] send in
try await call(address)
await send(.internal(.callDone(updateControlState: buttonState, changeTo: onSuccess)))
} catch: { error, send in
await send(.internal(.hideLoader(updateControlState: buttonState)))
if !Task.isCancelled {
errorQueue.schedule(error)
}
}
}

private func updateAccountPortfolio(_ state: State) -> Effect<Action> {
.run { [address = state.address] send in
await send(.internal(.refreshAccountCompleted(
TaskResult { try await accountPortfoliosClient.fetchAccountPortfolio(address, true).account }
)))
}
}

private func loadIsAllowedToUseFaucet(_ state: inout State) -> Effect<Action> {
state.faucetButtonState = .loading(.local)
return .run { [address = state.address] send in
await send(.internal(.isAllowedToUseFaucet(
TaskResult {
await faucetClient.isAllowedToUseFaucet(address)
}
)))
}
}
}

extension OverlayWindowClient.Item.HUD {
static let accountHidden = Self(text: L10n.AccountSettings.accountHidden)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ extension AccountPreferences.State {

#if DEBUG
addDevAccountPreferencesSection(to: &sections)
#else
if account.networkID != .mainnet {
addDevAccountPreferencesSection(to: &sections)
}
#endif

return sections
}()
}(),
faucetButtonState: faucetButtonState,
isOnMainnet: isOnMainnet
)
}

Expand All @@ -49,6 +47,8 @@ extension AccountPreferences {
typealias Section = PreferenceSection<AccountPreferences.Section, AccountPreferences.Section.SectionRow>.ViewState
let account: Profile.Network.Account
var sections: [Section]
var faucetButtonState: ControlState
var isOnMainnet: Bool
}

@MainActor
Expand Down Expand Up @@ -78,7 +78,7 @@ extension AccountPreferences {
PreferencesList(
viewState: .init(sections: viewStore.sections),
onRowSelected: { _, rowId in viewStore.send(.rowTapped(rowId)) },
footer: { hideAccountButton() }
footer: { footer(with: viewStore) }
)
.task {
viewStore.send(.task)
Expand All @@ -97,8 +97,34 @@ extension AccountPreferences {
}

extension AccountPreferences.View {
@ViewBuilder
private func footer(with viewStore: ViewStoreOf<AccountPreferences>) -> some View {
VStack {
if !viewStore.isOnMainnet {
faucetButton(with: viewStore)
}

hideAccountButton()
}
}

@ViewBuilder
private func faucetButton(with viewStore: ViewStoreOf<AccountPreferences>) -> some View {
Button(L10n.AccountSettings.getXrdTestTokens) {
viewStore.send(.faucetButtonTapped)
}
.buttonStyle(.secondaryRectangular(shouldExpand: true))
.controlState(viewStore.faucetButtonState)

if viewStore.faucetButtonState.isLoading {
Text(L10n.AccountSettings.loadingPrompt)
.font(.app.body2Regular)
.foregroundColor(.app.gray1)
}
}

@MainActor
func hideAccountButton() -> some View {
private func hideAccountButton() -> some View {
Button(L10n.AccountSettings.HideAccount.button) {
store.send(.view(.hideAccountTapped))
}
Expand Down
Loading
Loading