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

Fetch db contact in ContactFeature

parent d0b2595b
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!68Messenger example - send auth request
......@@ -24,6 +24,7 @@ public struct ContactState: Equatable {
public enum ContactAction: Equatable {
case start
case dbContactFetched(XXModels.Contact?)
}
public struct ContactEnvironment {
......@@ -58,8 +59,21 @@ extension ContactEnvironment {
public let contactReducer = Reducer<ContactState, ContactAction, ContactEnvironment>
{ state, action, env in
enum DBFetchEffectID {}
switch action {
case .start:
return try! env.db().fetchContactsPublisher(.init(id: [state.id]))
.assertNoFailure()
.map(\.first)
.map(ContactAction.dbContactFetched)
.subscribe(on: env.bgQueue)
.receive(on: env.mainQueue)
.eraseToEffect()
.cancellable(id: DBFetchEffectID.self, cancelInFlight: true)
case .dbContactFetched(let contact):
state.dbContact = contact
return .none
}
}
import AppCore
import ComposableArchitecture
import SwiftUI
import XXClient
import XXModels
public struct ContactView: View {
public init(store: Store<ContactState, ContactAction>) {
......@@ -9,13 +12,123 @@ public struct ContactView: View {
let store: Store<ContactState, ContactAction>
struct ViewState: Equatable {
init(state: ContactState) {}
var dbContact: XXModels.Contact?
var xxContact: XXClient.Contact?
init(state: ContactState) {
dbContact = state.dbContact
xxContact = state.xxContact
}
}
public var body: some View {
WithViewStore(store.scope(state: ViewState.init)) { viewStore in
Form {
Section {
if let dbContact = viewStore.dbContact {
Label(dbContact.username ?? "", systemImage: "person")
Label(dbContact.email ?? "", systemImage: "envelope")
Label(dbContact.phone ?? "", systemImage: "phone")
} else {
Text("Contact not saved locally")
}
} header: {
Text("Local data")
}
Section {
Label(viewStore.xxContact?.username ?? "", systemImage: "person")
Label(viewStore.xxContact?.email ?? "", systemImage: "envelope")
Label(viewStore.xxContact?.phone ?? "", systemImage: "phone")
} header: {
Text("Facts")
}
Section {
switch viewStore.dbContact?.authStatus {
case .none, .stranger:
HStack {
Text("Stranger")
Spacer()
Image(systemName: "person.fill.questionmark")
}
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 {
Text("Friend")
Spacer()
Image(systemName: "person.fill.checkmark")
}
case .hidden:
HStack {
Text("Hidden")
Spacer()
Image(systemName: "eye.slash")
}
}
} header: {
Text("Auth status")
}
}
.navigationTitle("Contact")
}
......
import Combine
import ComposableArchitecture
import CustomDump
import XCTest
import XXModels
@testable import ContactFeature
final class ContactFeatureTests: XCTestCase {
......@@ -12,6 +15,33 @@ final class ContactFeatureTests: XCTestCase {
environment: .unimplemented
)
var dbDidFetchContacts: [XXModels.Contact.Query] = []
let dbContactsPublisher = PassthroughSubject<[XXModels.Contact], Error>()
store.environment.mainQueue = .immediate
store.environment.bgQueue = .immediate
store.environment.db.run = {
var db: Database = .failing
db.fetchContactsPublisher.run = { query in
dbDidFetchContacts.append(query)
return dbContactsPublisher.eraseToAnyPublisher()
}
return db
}
store.send(.start)
XCTAssertNoDifference(dbDidFetchContacts, [
.init(id: ["contact-id".data(using: .utf8)!])
])
let dbContact = XXModels.Contact(id: "contact-id".data(using: .utf8)!)
dbContactsPublisher.send([dbContact])
store.receive(.dbContactFetched(dbContact)) {
$0.dbContact = dbContact
}
dbContactsPublisher.send(completion: .finished)
}
}
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