From 33b983374fcafa32ff9c50bc48c888123625721a Mon Sep 17 00:00:00 2001 From: Omar Albeik Date: Mon, 7 Feb 2022 22:47:41 +0100 Subject: [PATCH] Fix edit while filtered bug in todos example app --- Examples/Todos/Todos/Todos.swift | 8 +++- Examples/Todos/TodosTests/TodosTests.swift | 52 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/Examples/Todos/Todos/Todos.swift b/Examples/Todos/Todos/Todos.swift index 3e9d407ba429..7ad5ed7ec5b3 100644 --- a/Examples/Todos/Todos/Todos.swift +++ b/Examples/Todos/Todos/Todos.swift @@ -66,7 +66,13 @@ let appReducer = Reducer.combine( return .none case let .move(source, destination): - state.todos.move(fromOffsets: source, toOffset: destination) + // Get source and destination in unfiltered todos array + let sourceInTodos = source + .map { state.filteredTodos[$0] } + .compactMap { state.todos.index(id: $0.id) } + let destinationInTodos = state.todos.index(id: state.filteredTodos[destination].id)! + + state.todos.move(fromOffsets: IndexSet(sourceInTodos), toOffset: destinationInTodos) return Effect(value: .sortCompletedTodos) .delay(for: .milliseconds(100), scheduler: environment.mainQueue) .eraseToEffect() diff --git a/Examples/Todos/TodosTests/TodosTests.swift b/Examples/Todos/TodosTests/TodosTests.swift index a988781d2c08..260572ebe6ec 100644 --- a/Examples/Todos/TodosTests/TodosTests.swift +++ b/Examples/Todos/TodosTests/TodosTests.swift @@ -236,6 +236,58 @@ class TodosTests: XCTestCase { store.receive(.sortCompletedTodos) } + func testEditModeMovingWithFilter() { + let state = AppState( + todos: [ + Todo( + description: "", + id: UUID(uuidString: "00000000-0000-0000-0000-000000000000")!, + isComplete: false + ), + Todo( + description: "", + id: UUID(uuidString: "00000000-0000-0000-0000-000000000001")!, + isComplete: true + ), + Todo( + description: "", + id: UUID(uuidString: "00000000-0000-0000-0000-000000000002")!, + isComplete: false + ), + Todo( + description: "", + id: UUID(uuidString: "00000000-0000-0000-0000-000000000003")!, + isComplete: true + ), + ] + ) + let store = TestStore( + initialState: state, + reducer: appReducer, + environment: AppEnvironment( + mainQueue: self.scheduler.eraseToAnyScheduler(), + uuid: UUID.incrementing + ) + ) + + store.send(.editModeChanged(.active)) { + $0.editMode = .active + } + store.send(.filterPicked(.completed)) { + $0.filter = .completed + } + store.send(.move([0], 1)) { + $0.todos = [ + $0.todos[0], + $0.todos[2], + $0.todos[1], + $0.todos[3], + ] + } + self.scheduler.advance(by: .milliseconds(100)) + store.receive(.sortCompletedTodos) + } + func testFilteredEdit() { let state = AppState( todos: [