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

Add UserSearchResultFeature

parent 790000c1
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!68Messenger example - send auth request
import ComposableArchitecture
import Foundation
import XCTestDynamicOverlay
import XXClient
public struct UserSearchResultState: Equatable, Identifiable {
public init(
id: Data,
contact: Contact,
username: String? = nil,
email: String? = nil,
phone: String? = nil
) {
self.id = id
self.contact = contact
self.username = username
self.email = email
self.phone = phone
}
public var id: Data
public var contact: XXClient.Contact
public var username: String?
public var email: String?
public var phone: String?
}
public enum UserSearchResultAction: Equatable {
case start
}
public struct UserSearchResultEnvironment {
public init() {}
}
#if DEBUG
extension UserSearchResultEnvironment {
public static let unimplemented = UserSearchResultEnvironment()
}
#endif
public let userSearchResultReducer = Reducer<UserSearchResultState, UserSearchResultAction, UserSearchResultEnvironment>
{ state, action, env in
switch action {
case .start:
let facts = (try? state.contact.getFacts()) ?? []
state.username = facts.first(where: { $0.type == 0 })?.fact
state.email = facts.first(where: { $0.type == 1 })?.fact
state.phone = facts.first(where: { $0.type == 2 })?.fact
return .none
}
}
import ComposableArchitecture
import SwiftUI
public struct UserSearchResultView: View {
public init(store: Store<UserSearchResultState, UserSearchResultAction>) {
self.store = store
}
let store: Store<UserSearchResultState, UserSearchResultAction>
struct ViewState: Equatable {
var username: String?
var email: String?
var phone: String?
init(state: UserSearchResultState) {
username = state.username
email = state.email
phone = state.phone
}
var isEmpty: Bool {
username == nil && email == nil && phone == nil
}
}
public var body: some View {
WithViewStore(store.scope(state: ViewState.init)) { viewStore in
Section {
if viewStore.isEmpty {
Image(systemName: "questionmark")
.frame(maxWidth: .infinity)
} else {
if let username = viewStore.username {
Text(username)
}
if let email = viewStore.email {
Text(email)
}
if let phone = viewStore.phone {
Text(phone)
}
}
}
.task { viewStore.send(.start) }
}
}
}
#if DEBUG
public struct UserSearchResultView_Previews: PreviewProvider {
public static var previews: some View {
UserSearchResultView(store: Store(
initialState: UserSearchResultState(
id: "contact-id".data(using: .utf8)!,
contact: .unimplemented("contact-data".data(using: .utf8)!)
),
reducer: .empty,
environment: ()
))
}
}
#endif
import ComposableArchitecture
import XCTest
import XXClient
@testable import UserSearchFeature
final class UserSearchResultFeatureTests: XCTestCase {
func testStart() {
var contact = Contact.unimplemented("contact-data".data(using: .utf8)!)
contact.getFactsFromContact.run = { _ in
[
Fact(fact: "contact-username", type: 0),
Fact(fact: "contact-email", type: 1),
Fact(fact: "contact-phone", type: 2),
]
}
let store = TestStore(
initialState: UserSearchResultState(
id: "contact-id".data(using: .utf8)!,
contact: contact
),
reducer: userSearchResultReducer,
environment: .unimplemented
)
store.send(.start) {
$0.username = "contact-username"
$0.email = "contact-email"
$0.phone = "contact-phone"
}
}
}
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