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

Home Screen load laggines #1053

Merged
merged 10 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -45,7 +45,7 @@ extension AccountPortfoliosClient.State {
}

func portfolioForAccount(_ address: AccountAddress) -> AnyAsyncSequence<AccountPortfoliosClient.AccountPortfolio> {
portfoliosSubject.compactMap { $0[address].unwrap()?.wrappedValue }.eraseToAnyAsyncSequence()
portfoliosSubject.compactMap { $0[address].unwrap()?.wrappedValue }.removeDuplicates().eraseToAnyAsyncSequence()
GhenadieVP marked this conversation as resolved.
Show resolved Hide resolved
}

private func setOrUpdateAccountPortfolio(_ portfolio: AccountPortfoliosClient.AccountPortfolio) {
Expand Down
23 changes: 18 additions & 5 deletions RadixWallet/Core/FeaturePrelude/Loadable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,29 @@ extension Loadable {
public mutating func refresh(
from other: Loadable<Value>,
valueChangeMap: (_ old: Value, _ new: Value) -> Value = { _, new in new }
) {
) where Value: Equatable {
GhenadieVP marked this conversation as resolved.
Show resolved Hide resolved
switch (self, other) {
// If `other` is success, update the content regardless of the current state
case let (.success(oldValue), .success(newValue)):
self = .success(valueChangeMap(oldValue, newValue))
case let (_, .success(otherValue)):
// Update to success if no current value
case let (.idle, .success(otherValue)),
let (.loading, .success(otherValue)),
let (.failure, .success(otherValue)):

self = .success(otherValue)

// Update to new value only if it changed
case let (.success(oldValue), .success(newValue)):
if oldValue != newValue {
self = .success(valueChangeMap(oldValue, newValue))
}

// If current state is success, don't update if `other` is loading or failed
case (.success, _):
break

case (.loading, .loading),
(.idle, .idle):
break

// If current state is other than .success
case let (_, other):
self = other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ public struct AssetsView: Sendable, FeatureReducer {

extension AccountPortfoliosClient.AccountPortfolio {
mutating func refresh(from portfolio: AccountPortfoliosClient.AccountPortfolio) {
self.account = portfolio.account
self.stakeUnitDetails.refresh(from: portfolio.stakeUnitDetails)
self.poolUnitDetails.refresh(from: portfolio.poolUnitDetails)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ extension Home {
public var id: AccountAddress { account.address }
public var accountWithInfo: AccountWithInfo

public var portfolio: Loadable<AccountPortfoliosClient.AccountPortfolio>
public var portfolio: Loadable<AccountPortfoliosClient.AccountPortfolio> {
didSet {
totalFiatWorth.refresh(from: portfolio.totalFiatWorth.flatten())
}
}

public var showFiatWorth: Bool = true
public var totalFiatWorth: Loadable<FiatWorth>

Expand Down Expand Up @@ -75,12 +80,15 @@ extension Home {
public func reduce(into state: inout State, internalAction: InternalAction) -> Effect<Action> {
switch internalAction {
case let .accountPortfolioUpdate(portfolio):
state.isDappDefinitionAccount = portfolio.account.metadata.accountType == .dappDefinition

assert(portfolio.account.address == state.account.address)

state.portfolio = .success(portfolio)
state.totalFiatWorth.refresh(from: portfolio.totalFiatWorth)
guard state.portfolio != .success(portfolio) else {
return .none
}

state.isDappDefinitionAccount = portfolio.account.metadata.accountType == .dappDefinition
state.portfolio.refresh(from: .success(portfolio))

return .send(.internal(.checkAccountAccessToMnemonic))

case .checkAccountAccessToMnemonic:
Expand Down
Loading