Skip to content
Snippets Groups Projects
Commit b5bbc0ca authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Update UserSearchResultFeature

parent 20f14b6a
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!68Messenger example - send auth request
This commit is part of merge request !68. Comments created here will be created in the context of that merge request.
...@@ -158,7 +158,6 @@ let package = Package( ...@@ -158,7 +158,6 @@ let package = Package(
.target( .target(
name: "UserSearchFeature", name: "UserSearchFeature",
dependencies: [ dependencies: [
.target(name: "AppCore"),
.product(name: "ComposableArchitecture", package: "swift-composable-architecture"), .product(name: "ComposableArchitecture", package: "swift-composable-architecture"),
.product(name: "ComposablePresentation", package: "swift-composable-presentation"), .product(name: "ComposablePresentation", package: "swift-composable-presentation"),
.product(name: "XXClient", package: "elixxir-dapps-sdk-swift"), .product(name: "XXClient", package: "elixxir-dapps-sdk-swift"),
......
import AppCore
import ComposableArchitecture import ComposableArchitecture
import Foundation import Foundation
import XCTestDynamicOverlay import XCTestDynamicOverlay
import XXClient import XXClient
import XXModels
public struct UserSearchResultState: Equatable, Identifiable { public struct UserSearchResultState: Equatable, Identifiable {
public init( public init(
id: Data, id: Data,
xxContact: XXClient.Contact, xxContact: XXClient.Contact,
dbContact: XXModels.Contact? = nil,
username: String? = nil, username: String? = nil,
email: String? = nil, email: String? = nil,
phone: String? = nil phone: String? = nil
) { ) {
self.id = id self.id = id
self.xxContact = xxContact self.xxContact = xxContact
self.dbContact = dbContact
self.username = username self.username = username
self.email = email self.email = email
self.phone = phone self.phone = phone
...@@ -24,7 +20,6 @@ public struct UserSearchResultState: Equatable, Identifiable { ...@@ -24,7 +20,6 @@ public struct UserSearchResultState: Equatable, Identifiable {
public var id: Data public var id: Data
public var xxContact: XXClient.Contact public var xxContact: XXClient.Contact
public var dbContact: XXModels.Contact?
public var username: String? public var username: String?
public var email: String? public var email: String?
public var phone: String? public var phone: String?
...@@ -32,60 +27,30 @@ public struct UserSearchResultState: Equatable, Identifiable { ...@@ -32,60 +27,30 @@ public struct UserSearchResultState: Equatable, Identifiable {
public enum UserSearchResultAction: Equatable { public enum UserSearchResultAction: Equatable {
case start case start
case didUpdateContact(XXModels.Contact?) case tapped
case sendRequestButtonTapped
} }
public struct UserSearchResultEnvironment { public struct UserSearchResultEnvironment {
public init( public init() {}
db: DBManagerGetDB,
mainQueue: AnySchedulerOf<DispatchQueue>,
bgQueue: AnySchedulerOf<DispatchQueue>
) {
self.db = db
self.mainQueue = mainQueue
self.bgQueue = bgQueue
}
public var db: DBManagerGetDB
public var mainQueue: AnySchedulerOf<DispatchQueue>
public var bgQueue: AnySchedulerOf<DispatchQueue>
} }
#if DEBUG #if DEBUG
extension UserSearchResultEnvironment { extension UserSearchResultEnvironment {
public static let unimplemented = UserSearchResultEnvironment( public static let unimplemented = UserSearchResultEnvironment()
db: .unimplemented,
mainQueue: .unimplemented,
bgQueue: .unimplemented
)
} }
#endif #endif
public let userSearchResultReducer = Reducer<UserSearchResultState, UserSearchResultAction, UserSearchResultEnvironment> public let userSearchResultReducer = Reducer<UserSearchResultState, UserSearchResultAction, UserSearchResultEnvironment>
{ state, action, env in { state, action, env in
enum DBFetchEffectID {}
switch action { switch action {
case .start: case .start:
let facts = (try? state.xxContact.getFacts()) ?? [] let facts = (try? state.xxContact.getFacts()) ?? []
state.username = facts.first(where: { $0.type == 0 })?.fact state.username = facts.first(where: { $0.type == 0 })?.fact
state.email = facts.first(where: { $0.type == 1 })?.fact state.email = facts.first(where: { $0.type == 1 })?.fact
state.phone = facts.first(where: { $0.type == 2 })?.fact state.phone = facts.first(where: { $0.type == 2 })?.fact
return try! env.db().fetchContactsPublisher(.init(id: [state.id]))
.assertNoFailure()
.map(\.first)
.map(UserSearchResultAction.didUpdateContact)
.subscribe(on: env.bgQueue)
.receive(on: env.mainQueue)
.eraseToEffect()
.cancellable(id: DBFetchEffectID.self, cancelInFlight: true)
case .didUpdateContact(let contact):
state.dbContact = contact
return .none return .none
case .sendRequestButtonTapped: case .tapped:
return .none return .none
} }
} }
...@@ -13,13 +13,11 @@ public struct UserSearchResultView: View { ...@@ -13,13 +13,11 @@ public struct UserSearchResultView: View {
var username: String? var username: String?
var email: String? var email: String?
var phone: String? var phone: String?
var dbContactAuth: XXModels.Contact.AuthStatus?
init(state: UserSearchResultState) { init(state: UserSearchResultState) {
username = state.username username = state.username
email = state.email email = state.email
phone = state.phone phone = state.phone
dbContactAuth = state.dbContact?.authStatus
} }
var isEmpty: Bool { var isEmpty: Bool {
...@@ -30,103 +28,28 @@ public struct UserSearchResultView: View { ...@@ -30,103 +28,28 @@ public struct UserSearchResultView: View {
public var body: some View { public var body: some View {
WithViewStore(store.scope(state: ViewState.init)) { viewStore in WithViewStore(store.scope(state: ViewState.init)) { viewStore in
Section { Section {
if viewStore.isEmpty { Button {
Image(systemName: "questionmark") viewStore.send(.tapped)
.frame(maxWidth: .infinity) } label: {
} else {
if let username = viewStore.username {
Text(username)
}
if let email = viewStore.email {
Text(email)
}
if let phone = viewStore.phone {
Text(phone)
}
}
switch viewStore.dbContactAuth {
case .none, .stranger:
Button {
viewStore.send(.sendRequestButtonTapped)
} label: {
HStack {
Text("Send request")
Spacer()
Image(systemName: "person.badge.plus")
}
}
case .requesting:
HStack {
Text("Sending auth request")
Spacer()
ProgressView()
}
case .requested:
HStack {
Text("Request sent")
Spacer()
Image(systemName: "paperplane")
}
case .requestFailed:
HStack {
Text("Sending request failed")
Spacer()
Image(systemName: "xmark.diamond.fill")
.foregroundColor(.red)
}
case .verificationInProgress:
HStack {
Text("Verification is progress")
Spacer()
ProgressView()
}
case .verified:
HStack {
Text("Verified")
Spacer()
Image(systemName: "person.fill.checkmark")
}
case .verificationFailed:
HStack {
Text("Verification failed")
Spacer()
Image(systemName: "xmark.diamond.fill")
.foregroundColor(.red)
}
case .confirming:
HStack {
Text("Confirming auth request")
Spacer()
ProgressView()
}
case .confirmationFailed:
HStack {
Text("Confirmation failed")
Spacer()
Image(systemName: "xmark.diamond.fill")
.foregroundColor(.red)
}
case .friend:
HStack { HStack {
Text("Friend") VStack {
Spacer() if viewStore.isEmpty {
Image(systemName: "person.fill.checkmark") Image(systemName: "questionmark")
} .frame(maxWidth: .infinity)
} else {
case .hidden: if let username = viewStore.username {
HStack { Text(username)
Text("Hidden") }
if let email = viewStore.email {
Text(email)
}
if let phone = viewStore.phone {
Text(phone)
}
}
}
Spacer() Spacer()
Image(systemName: "eye.slash") Image(systemName: "chevron.forward")
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment