0

I am picking up a project which is using SWIFTUI (TCA) with integrate kotlin for handling business logic for both Android and iOS.

I am facing an issue which is the Listview is not refresh after created additional object. I guess it is due to the state is not updated. But the most strange issue is when the app is killed and re-open again. The object will show on list. Does it mean this is already added into the state?

Below are some of the code, it seem creating a globalDependencies with factory.

Can anyone help me.

extension ObjectiveEvidenceListEnvironment {
    init(globalDependencies: GlobalDependencies) {
        
        
        getNCNObservationDetails = { nonConformityId in
            createPublisher(wrapper: globalDependencies.factory.getNonConformity().getNonConformityFields(nonConformityId: nonConformityId))
                .compactMap { $0 }
                .mapToNSError()
                .eraseToEffect()
        }    
    }
}
import SwiftUI
import ComposableArchitecture
import SVCore

struct ObjectiveEvidenceListView: View {
    let store: Store<ObjectiveEvidenceListState, LifecycleAction<ObjectiveEvidenceListAction>>
    
    var body: some View {
        WithViewStore(store) { viewStore in

            VStack {
                List {
                    Section(header: Text("CUSTOM")) {
                        ForEach (viewStore.customEvidences) { evidence in
                            
                            NavigationLink(
                                destination: {
                                    ObjectiveEvidenceDetailsView.build(tag: evidence.id, using: NCNDetailsStruct(detailsID: evidence.id, isCustom: true))
                                },
                                label: {
                                    VStack(alignment: .leading){
                                        //Text("HI, i am here")
                                        Text(evidence.name ?? "")
                                            .foregroundColor(.fl_grayText)
                                    }
                                }
                            )
                        }.onDelete(perform: { viewStore.send(.delete($0)) })
                    }
                    
                    
                    Section(header: Text("CHECKLIST")) {
                        var _y = print(":: defectEvidences = \(viewStore.defectEvidences)")
                        ForEach (viewStore.defectEvidences) { defect in
                            NavigationLink(
                                destination: {
                                    ObjectiveEvidenceDetailsView.build(tag: defect.id, using: NCNDetailsStruct(detailsID: defect.id, isCustom: false))
                                },
                                label: {
                                    VStack(alignment: .leading){
                                        Text(defect.name ?? "")
                                            .foregroundColor(.fl_grayText)
                                    }
                                }
                            )
                        }
                    }
                }
                .refreshable {
                    print(":: .refreshable ")
                }
                .listStyle(GroupedListStyle())
                .navigationBarItems(
                    trailing:
                    
                        NavigationLink(
                            destination: IfLetStore(
                                self.store.scope(
                                    state: \.selectedEvidence?.value,
                                    action: { LifecycleAction.action(ObjectiveEvidenceListAction.evidence($0)) }
                                ),
                                then: ObjectiveEvidenceDetailsView.init(store:)
                            ),
                            isActive: viewStore.binding(
                                get: \.navigateToNewEvidence,
                                send: ObjectiveEvidenceListAction.navigateToNewEvidence
                            ),
                            label: { Text("+ Create") }
                        )
                )
                
            }
        }
    }
}

extension ObjectiveEvidenceListView: BuildableView {
    init(viewModel: Store<ObjectiveEvidenceListState, LifecycleAction<ObjectiveEvidenceListAction>>) {
        store = viewModel
    }
}

import Dip
import SVCore

extension ObjectiveEvidenceListView: Composable {
    static func configure(_ container: DependencyContainer) {
        container.register(.weakSingleton) { (details: [SVCore.NonConformityDetails], factory: InteractorFactory) -> Store<ObjectiveEvidenceListState, LifecycleAction<ObjectiveEvidenceListAction>> in
            let dependencies = GlobalDependencies(factory: factory)
            return Store(
                initialState: ObjectiveEvidenceListState(details: details.first!, customDetails: details.last!),
                reducer: ObjectiveEvidenceListReducer.reducer,
                environment: GlobalEnvironment<ObjectiveEvidenceListEnvironment>.live(
                    environment: ObjectiveEvidenceListEnvironment(globalDependencies: dependencies),
                    dependencies: dependencies
                )
            )
        }
    }
}

struct NCNDetailsStruct {
    var detailsID: String
    var isCustom: Bool
}

import SwiftUI
import ComposableArchitecture
import SVCore
import IdentifiedCollections


enum ObjectiveEvidenceListAction {

    case createCustomEvidence(nonConformity: String)

    case recievedNonConformityId(Result<String, NSError>)
    case recievedEvidences(Result<NonConformityFields, NSError>)

    
    case recievedChecklistEvidences(Result<[NonConformityDetails.CustomNonConformity], NSError>)
    case evidence(ObjectiveEvidenceDetailsAction)
    case navigateToNewEvidence(isActive: Bool)
    
    case delete(IndexSet)
    case didAppear
    case completed(VoidResult<NSError>)
}

struct ObjectiveEvidenceListState: Equatable {
    var details: NonConformityDetails
    var customEvidences: IdentifiedArrayOf<NonConformityImages> = []
    var defectEvidences: IdentifiedArrayOf<Defect> = []
    var defects: IdentifiedArrayOf<Defect> = []
    var navigateToSelection: Bool = false
    var selectedEvidence: Identified<String, ObjectiveEvidenceDetailsState>?
//    var selectedNonConformity: Identified<String, NCNObservationDetailsState>?
    
    init(details: NonConformityDetails, customDetails: NonConformityDetails) {
        self.details = details
        print(":: self.details = \(self.details)")
        switch details {
        case let section as NonConformityDetails.CustomNonConformity:
            self.customEvidences = IdentifiedArray(uniqueElements: section.evidence)
        default:
            break
        }

    }
    mutating func update(details: NonConformityDetails) {
        //    self.details = details
        switch details {
        case let section as NonConformityDetails.CustomNonConformity:
            self.customEvidences = IdentifiedArray(uniqueElements: section.evidence)
        case let section as NonConformityDetails.SectionNonConformity:
            self.defectEvidences = IdentifiedArray(uniqueElements: section.defects)
            self.defects = IdentifiedArray(uniqueElements: section.defects)
        default:
            break
        }
    }
}


extension ObjectiveEvidenceListState {
    var navigateToNewEvidence: Bool {
        return selectedEvidence != nil
    }
}

extension NonConformityImages: Identifiable {}

What can i do to Get the list update dynamically with the state update.

jLeung
  • 1
  • 1
  • There isn’t a single line of code that notifies the body to reload. I suggest starting with the Apple SwiftUI tutorials, SwiftUI depends on dynamic property or observed object to tell it to reload. – lorem ipsum Mar 06 '23 at 12:59

0 Answers0