0

I'm new to swift, swiftUI, and to apple accessibility.

Current behavior:

I have a location list with a prefix on it that loads 10 items at a time. When a user selects the load more button accessibilityFocused is set to the next assignment specifically the facility number at the top of each assignment. When I run voice over focus starts on the last assignment instead of the new assignment that was loaded after the user selected the load more button.

@StateObject private var viewModel = AssignmentViewModel()

List{
ForEach(
                            Array(viewModel.locationList.prefix(viewModel.visibleItems))
                        ) { (location) in
                            letindexPath = viewModel.locationList.firstIndex{$0.self== location.self} ?? -1
                          
                            
                            HStack{
                                Text("Location Number: ")
                                    .font(
                                        .title2
                                            .weight(.bold)
                                    )
                                    .foregroundColor(Color.body_Text)
                                    .fontWeight(.bold)
                                
                                Text("\(location.facilityNumber?? "")")
                                    .fontWeight(.bold)
                                    .padding(.trailing)
                                    .padding(.leading)
                                    .background( Color.storeNum)
                                    .foregroundColor(Color.body_Text)
                                /*MARK: Pin graphic**/
                                ZStack{
                                    Image(systemName: "pin.fill")
                                        .foregroundColor(Color.HHS_Purple)
                                        .rotationEffect(.degrees(45))
                                }
                                .accessibilityHidden(true)
                                    //MARK: use geometry reader to detect landscape and portrait modes for ipad
                                .offset(x: bounds.size.width< bounds.size.height? 30: 725)
                                /*Pin graphic**/
                                
                            }
                            .listRowSeparator(.hidden)
                            .accessibility(sortPriority: 5)
                            .frame(maxWidth: .infinity, alignment: .center)
                            .accessibilityElement(children: .ignore)
                            .accessibilityLabel("Location Number: \(location.facilityNumber ?? "") ")
                            .accessibilityHeading(AccessibilityHeadingLevel.h2)
                            /*Leaving the focus as is for now. When user loads next assignment, the focus is on the last assignment.
                It should be on the next assignment showing. There is not alot of documentation available on how to set
                focus on a foreach loop array. */
                            .accessibilityFocused($focus, equals: .nextAssignment)
                            
                       }// end foreach 
                        // I also tried accessibilityFocused here. It went to the blank 
                        //spaces in between assignments. Almost the behavior I want but not 
                        //quiet.
            if viewModel.isFilteredEmpty {
                                EmptyView()
                            }
                            else
                            {
                                buttonLoadMore
                            }
} // end list

Load more button:

    var buttonLoadMore: some View {
        HStack{
            if (viewModel.isLastAssignment())
            {
                Text("End of Assignments")
            }
            else
            {
                Button("Load More") {
                    withAnimation {
                        
                        focusIndicator = .loadMore
                        focus = .nextAssignment
                        print("Load More button tapped. \(String(describing: focus))")
                        viewModel.showMore()
                        if isSearchEnabled() {
                            print("Search assignments enabled. \(String(describing: focus))")
                            viewModel.filter(byLocation: searchAssignments)
                        }
                          
                    }
                    
                }
                .buttonStyle(LinkStyle())
                .font(
                    .body
                        .weight(.bold)
                )
                .frame(width: 300, alignment: .center)
                .contentShape(Capsule())
                .padding()
                .background(
                    Capsule()
                        .strokeBorder(focusIndicator == .loadMore  ? Color.HHS_Purple : Color.clear, lineWidth:  focusIndicator == .loadMore  ? 3 : 1)
                        .background( Color.HHS_Purple.opacity(0.1))
                        .clipped()
                )
                .clipShape(Capsule())
                .foregroundColor(Color.HHS_Purple)
                .multilineTextAlignment(.leading)
                .fixedSize(horizontal: false, vertical: true)
                
            }
        }
        .accessibility(sortPriority: 0)
        .frame(maxWidth: .infinity, alignment: .center)
        .listRowSeparator(.hidden)
        .padding()
        
    }

Desired behavior:

I need help setting the focus to the next item loaded from an array. Specifically the facilityNumber.

What I tried:

I tried the solution from this post. and it did not work for me. Any suggestions are greatly appreciated! Thank you!

0 Answers0