Skip to content

Commit 02cf759

Browse files
authored
Fix edit while filtered bug in todos example app (#999)
1 parent ed4c236 commit 02cf759

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

Examples/Todos/Todos/Todos.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ let appReducer = Reducer<AppState, AppAction, AppEnvironment>.combine(
6666
return .none
6767

6868
case let .move(source, destination):
69-
state.todos.move(fromOffsets: source, toOffset: destination)
69+
// Get source and destination in unfiltered todos array
70+
let sourceInTodos = source
71+
.map { state.filteredTodos[$0] }
72+
.compactMap { state.todos.index(id: $0.id) }
73+
let destinationInTodos = state.todos.index(id: state.filteredTodos[destination].id)!
74+
75+
state.todos.move(fromOffsets: IndexSet(sourceInTodos), toOffset: destinationInTodos)
7076
return Effect(value: .sortCompletedTodos)
7177
.delay(for: .milliseconds(100), scheduler: environment.mainQueue)
7278
.eraseToEffect()

Examples/Todos/TodosTests/TodosTests.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,58 @@ class TodosTests: XCTestCase {
236236
store.receive(.sortCompletedTodos)
237237
}
238238

239+
func testEditModeMovingWithFilter() {
240+
let state = AppState(
241+
todos: [
242+
Todo(
243+
description: "",
244+
id: UUID(uuidString: "00000000-0000-0000-0000-000000000000")!,
245+
isComplete: false
246+
),
247+
Todo(
248+
description: "",
249+
id: UUID(uuidString: "00000000-0000-0000-0000-000000000001")!,
250+
isComplete: true
251+
),
252+
Todo(
253+
description: "",
254+
id: UUID(uuidString: "00000000-0000-0000-0000-000000000002")!,
255+
isComplete: false
256+
),
257+
Todo(
258+
description: "",
259+
id: UUID(uuidString: "00000000-0000-0000-0000-000000000003")!,
260+
isComplete: true
261+
),
262+
]
263+
)
264+
let store = TestStore(
265+
initialState: state,
266+
reducer: appReducer,
267+
environment: AppEnvironment(
268+
mainQueue: self.scheduler.eraseToAnyScheduler(),
269+
uuid: UUID.incrementing
270+
)
271+
)
272+
273+
store.send(.editModeChanged(.active)) {
274+
$0.editMode = .active
275+
}
276+
store.send(.filterPicked(.completed)) {
277+
$0.filter = .completed
278+
}
279+
store.send(.move([0], 1)) {
280+
$0.todos = [
281+
$0.todos[0],
282+
$0.todos[2],
283+
$0.todos[1],
284+
$0.todos[3],
285+
]
286+
}
287+
self.scheduler.advance(by: .milliseconds(100))
288+
store.receive(.sortCompletedTodos)
289+
}
290+
239291
func testFilteredEdit() {
240292
let state = AppState(
241293
todos: [

0 commit comments

Comments
 (0)