4

I have a simple view in my app that displays a list of checklists. When there are no checklists I display a placeholder view.

The issue I'm having is that when I delete the last item in the list, the EditButton() doesn't switch back to "Edit"; it still says "Done," even thought the list is empty If I then add a new item, the list appears again but in edit mode (a bad UX).

Is there a way to manually "turn off" edit mode after the last item in the list gets deleted?

Image: https://i.stack.imgur.com/lE1yu.png

import SwiftUI

struct HomeView: View {
    // MARK: - PROPERTIES
    @EnvironmentObject var checklistsVM: Store
    @StateObject var homeVM: HomeViewModel = HomeViewModel()
    
    
    // MARK: - BODY
    var body: some View {
        NavigationView {
            Group {
                if(checklistsVM.checklists.isEmpty) {
                    EmptyList()
                        .environmentObject(homeVM)                    
                } else {
                    HomeListResults()
                        .environmentObject(homeVM)
                }
            }
            .navigationBarTitle("Checklists", displayMode: .large)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}
Nik Payne
  • 151
  • 8

1 Answers1

2

You can turn off the edit mode using the Environment value:

    @Environment(\.editMode) private var editMode

    var body: some View {
        Button("Turn off edit mode") {
            editMode?.wrappedValue = .inactive
        }
    }
Cuneyt
  • 931
  • 5
  • 11