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-3675] Approved dApps background issue #1267

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -1,7 +1,26 @@
import ComposableArchitecture
import SwiftUI

// MARK: - AuthorizedDapps.View
extension AuthorizedDappsFeature.State {
struct Dapp: Equatable, Identifiable {
let id: AuthorizedDapp.ID
let name: String
let thumbnail: URL?
}

var dAppsDetails: IdentifiedArrayOf<Dapp> {
dApps.map {
Dapp(
id: $0.id,
name: $0.displayName ?? L10n.DAppRequest.Metadata.unknownName,
thumbnail: thumbnails[$0.id]
)
}
.asIdentified()
}
}

// MARK: - AuthorizedDappsFeature.View
extension AuthorizedDappsFeature {
@MainActor
public struct View: SwiftUI.View {
Expand All @@ -10,71 +29,44 @@ extension AuthorizedDappsFeature {
public init(store: Store) {
self.store = store
}
}

public struct ViewState: Equatable {
let dApps: IdentifiedArrayOf<Dapp>

struct Dapp: Equatable, Identifiable {
let id: AuthorizedDapp.ID
let name: String
let thumbnail: URL?
}
}
}
public var body: some SwiftUI.View {
WithViewStore(store, observe: { $0 }) { viewStore in
ScrollView {
VStack(spacing: .medium1) {
Text(L10n.AuthorizedDapps.subtitle)
.textBlock
.frame(maxWidth: .infinity, alignment: .leading)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this under the ScrollView. Otherwise, if we ever remove or modify this Text, is possible that we make the same mistake again.


// MARK: - Body

extension AuthorizedDappsFeature.View {
public var body: some View {
WithViewStore(store, observe: \.viewState, send: { .view($0) }) { viewStore in
ScrollView {
VStack(spacing: .medium1) {
Text(L10n.AuthorizedDapps.subtitle)
.textBlock

VStack(spacing: .small1) {
ForEach(viewStore.dApps) { dApp in
Card {
viewStore.send(.didSelectDapp(dApp.id))
} contents: {
PlainListRow(context: .dappAndPersona, title: dApp.name) {
Thumbnail(.dapp, url: dApp.thumbnail)
VStack(spacing: .small1) {
ForEach(viewStore.dAppsDetails) { dApp in
Card {
viewStore.send(.view(.didSelectDapp(dApp.id)))
} contents: {
PlainListRow(context: .dappAndPersona, title: dApp.name) {
Thumbnail(.dapp, url: dApp.thumbnail)
}
}
}
}
.animation(.easeInOut, value: viewStore.dApps)
}
.padding(.vertical, .small1)
.padding(.horizontal, .medium3)
.onAppear {
viewStore.send(.view(.appeared))
}
.animation(.easeInOut, value: viewStore.dApps)
}
.padding(.vertical, .small1)
.padding(.horizontal, .medium3)
.onAppear {
viewStore.send(.appeared)
}
.background(.app.gray5)
.radixToolbar(title: L10n.AuthorizedDapps.title)
.destinations(with: store)
}
.background(Color.app.gray5)
.radixToolbar(title: L10n.AuthorizedDapps.title)
.destinations(with: store)
}
}
}

// MARK: - Extensions

extension AuthorizedDappsFeature.State {
var viewState: AuthorizedDappsFeature.ViewState {
let dAppViewStates = dApps.map {
AuthorizedDappsFeature.ViewState.Dapp(
id: $0.id,
name: $0.displayName ?? L10n.DAppRequest.Metadata.unknownName,
thumbnail: thumbnails[$0.id]
)
}

return .init(dApps: dAppViewStates.asIdentified())
}
}

private extension StoreOf<AuthorizedDappsFeature> {
var destination: PresentationStoreOf<AuthorizedDappsFeature.Destination> {
func scopeState(state: State) -> PresentationState<AuthorizedDappsFeature.Destination.State> {
Expand All @@ -88,11 +80,8 @@ private extension StoreOf<AuthorizedDappsFeature> {
private extension View {
func destinations(with store: StoreOf<AuthorizedDappsFeature>) -> some View {
let destinationStore = store.destination
return navigationDestination(
store: destinationStore,
state: /AuthorizedDappsFeature.Destination.State.presentedDapp,
action: AuthorizedDappsFeature.Destination.Action.presentedDapp,
destination: { DappDetails.View(store: $0) }
)
return navigationDestination(store: destinationStore.scope(state: \.presentedDapp, action: \.presentedDapp)) {
DappDetails.View(store: $0)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Sargon
import SwiftUI

// MARK: - AuthorizedDapps
@Reducer
public struct AuthorizedDappsFeature: Sendable, FeatureReducer {
@Dependency(\.authorizedDappsClient) var authorizedDappsClient
@Dependency(\.onLedgerEntitiesClient) var onLedgerEntitiesClient
Expand All @@ -12,6 +13,7 @@ public struct AuthorizedDappsFeature: Sendable, FeatureReducer {

// MARK: State

// TODO: Add `@ObservableState` after migrating `DappDetails` to `ObservableState`
public struct State: Sendable, Hashable {
public var dApps: AuthorizedDapps = []
public var thumbnails: [AuthorizedDapp.ID: URL] = [:]
Expand All @@ -24,8 +26,11 @@ public struct AuthorizedDappsFeature: Sendable, FeatureReducer {
}
}

public typealias Action = FeatureAction<Self>

// MARK: Action

@CasePathable
public enum ViewAction: Sendable, Equatable {
case appeared
case didSelectDapp(AuthorizedDapp.ID)
Expand All @@ -41,16 +46,18 @@ public struct AuthorizedDappsFeature: Sendable, FeatureReducer {
// MARK: Destination

public struct Destination: DestinationReducer {
@CasePathable
public enum State: Hashable, Sendable {
case presentedDapp(DappDetails.State)
}

@CasePathable
public enum Action: Equatable, Sendable {
case presentedDapp(DappDetails.Action)
}

public var body: some ReducerOf<Self> {
Scope(state: /State.presentedDapp, action: /Action.presentedDapp) {
Scope(state: \.presentedDapp, action: \.presentedDapp) {
DappDetails()
}
}
Expand All @@ -62,7 +69,7 @@ public struct AuthorizedDappsFeature: Sendable, FeatureReducer {

public var body: some ReducerOf<Self> {
Reduce(core)
.ifLet(destinationPath, action: /Action.destination) {
.ifLet(destinationPath, action: \.destination) {
Destination()
}
}
Expand Down
Loading