Skip to content

Commit b4fe7be

Browse files
omaralbeikmluisbrown
authored andcommitted
Fix edit while filtered bug in todos example app (#999)
1 parent c3971f5 commit b4fe7be

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
@@ -67,7 +67,13 @@ let appReducer = Reducer<AppState, AppAction, AppEnvironment>.combine(
6767
return .none
6868

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

Examples/Todos/TodosTests/TodosTests.swift

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

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

0 commit comments

Comments
 (0)