From 6c37bcb1fdbc0d7077ddfd301047b82b39c74728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20D=C3=B6nicke?= Date: Tue, 31 Oct 2023 00:24:55 +0100 Subject: [PATCH] use callAsFunction --- .../DomainLayer/Sources/UseCases/AddTodoUseCase.swift | 4 ++-- .../DomainLayer/Sources/UseCases/CompleteTodoUseCase.swift | 4 ++-- .../DomainLayer/Sources/UseCases/GetAllTodosUseCase.swift | 4 ++-- .../Tests/UseCases/DefaultAddTodoUseCaseTests.swift | 2 +- .../Tests/UseCases/DefaultCompleteTodoUseCaseTests.swift | 2 +- .../Tests/UseCases/GetAllTodosUseCaseTests.swift | 2 +- .../Preview Content/UseCases/PreviewAddTodoUseCase.swift | 2 +- .../UseCases/PreviewCompleteTodoUseCase.swift | 2 +- .../UseCases/PreviewGetAllTodosUseCase.swift | 2 +- .../Sources/ViewModels/TodoListViewModel.swift | 6 +++--- .../Tests/Mocks/UseCases/MockAddTodoUseCase.swift | 2 +- .../Tests/Mocks/UseCases/MockCompleteTodoUseCase.swift | 2 +- .../Tests/Mocks/UseCases/MockGetAllTodosUseCase.swift | 2 +- 13 files changed, 18 insertions(+), 18 deletions(-) diff --git a/CleanApp/Modules/DomainLayer/Sources/UseCases/AddTodoUseCase.swift b/CleanApp/Modules/DomainLayer/Sources/UseCases/AddTodoUseCase.swift index e3d6838..8130357 100644 --- a/CleanApp/Modules/DomainLayer/Sources/UseCases/AddTodoUseCase.swift +++ b/CleanApp/Modules/DomainLayer/Sources/UseCases/AddTodoUseCase.swift @@ -8,7 +8,7 @@ import Foundation public protocol AddTodoUseCase { - func execute(todo: Todo) async -> Result + func callAsFunction(todo: Todo) async -> Result } public class DefaultAddTodoUseCase: AddTodoUseCase { @@ -18,7 +18,7 @@ public class DefaultAddTodoUseCase: AddTodoUseCase { self.repository = repository } - public func execute(todo: Todo) async -> Result { + public func callAsFunction(todo: Todo) async -> Result { await repository.add(todo: todo) } } diff --git a/CleanApp/Modules/DomainLayer/Sources/UseCases/CompleteTodoUseCase.swift b/CleanApp/Modules/DomainLayer/Sources/UseCases/CompleteTodoUseCase.swift index 6345b43..06dc3dc 100644 --- a/CleanApp/Modules/DomainLayer/Sources/UseCases/CompleteTodoUseCase.swift +++ b/CleanApp/Modules/DomainLayer/Sources/UseCases/CompleteTodoUseCase.swift @@ -8,7 +8,7 @@ import Foundation public protocol CompleteTodoUseCase { - func execute(id: Int) async -> Result + func callAsFunction(id: Int) async -> Result } public class DefaultCompleteTodoUseCase: CompleteTodoUseCase { @@ -18,7 +18,7 @@ public class DefaultCompleteTodoUseCase: CompleteTodoUseCase { self.repository = repository } - public func execute(id: Int) async -> Result { + public func callAsFunction(id: Int) async -> Result { await repository.complete(id: id) } } diff --git a/CleanApp/Modules/DomainLayer/Sources/UseCases/GetAllTodosUseCase.swift b/CleanApp/Modules/DomainLayer/Sources/UseCases/GetAllTodosUseCase.swift index 105b10d..4b31401 100644 --- a/CleanApp/Modules/DomainLayer/Sources/UseCases/GetAllTodosUseCase.swift +++ b/CleanApp/Modules/DomainLayer/Sources/UseCases/GetAllTodosUseCase.swift @@ -8,7 +8,7 @@ import Foundation public protocol GetAllTodosUseCase { - func execute() async -> Result<[Todo], Error> + func callAsFunction() async -> Result<[Todo], Error> } public class DefaultGetAllTodosUseCase: GetAllTodosUseCase { @@ -18,7 +18,7 @@ public class DefaultGetAllTodosUseCase: GetAllTodosUseCase { self.repository = repository } - public func execute() async -> Result<[Todo], Error> { + public func callAsFunction() async -> Result<[Todo], Error> { await repository.todos() } } diff --git a/CleanApp/Modules/DomainLayer/Tests/UseCases/DefaultAddTodoUseCaseTests.swift b/CleanApp/Modules/DomainLayer/Tests/UseCases/DefaultAddTodoUseCaseTests.swift index d840225..0e950e1 100644 --- a/CleanApp/Modules/DomainLayer/Tests/UseCases/DefaultAddTodoUseCaseTests.swift +++ b/CleanApp/Modules/DomainLayer/Tests/UseCases/DefaultAddTodoUseCaseTests.swift @@ -16,7 +16,7 @@ final class DefaultAddTodoUseCaseTests: XCTestCase { let sut = DefaultAddTodoUseCase(repository: repository) // Act - let result = try await sut.execute(todo: expected).get() + let result = try await sut(todo: expected).get() // Assert XCTAssertEqual(result, expected) diff --git a/CleanApp/Modules/DomainLayer/Tests/UseCases/DefaultCompleteTodoUseCaseTests.swift b/CleanApp/Modules/DomainLayer/Tests/UseCases/DefaultCompleteTodoUseCaseTests.swift index 5aaf838..c9f542a 100644 --- a/CleanApp/Modules/DomainLayer/Tests/UseCases/DefaultCompleteTodoUseCaseTests.swift +++ b/CleanApp/Modules/DomainLayer/Tests/UseCases/DefaultCompleteTodoUseCaseTests.swift @@ -16,7 +16,7 @@ final class DefaultCompleteTodoUseCaseTests: XCTestCase { let sut = DefaultCompleteTodoUseCase(repository: repository) // Act - let result = try await sut.execute(id: expected.id!).get() + let result = try await sut(id: expected.id!).get() // Assert XCTAssertEqual(result, expected) diff --git a/CleanApp/Modules/DomainLayer/Tests/UseCases/GetAllTodosUseCaseTests.swift b/CleanApp/Modules/DomainLayer/Tests/UseCases/GetAllTodosUseCaseTests.swift index e90aa5f..8836859 100644 --- a/CleanApp/Modules/DomainLayer/Tests/UseCases/GetAllTodosUseCaseTests.swift +++ b/CleanApp/Modules/DomainLayer/Tests/UseCases/GetAllTodosUseCaseTests.swift @@ -16,7 +16,7 @@ final class DefaultGetAllTodosUseCaseTests: XCTestCase { let sut = DefaultGetAllTodosUseCase(repository: repository) // Act - let result = try await sut.execute().get() + let result = try await sut().get() // Assert XCTAssertEqual(result, expected) diff --git a/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewAddTodoUseCase.swift b/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewAddTodoUseCase.swift index b51b4c2..a08859a 100644 --- a/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewAddTodoUseCase.swift +++ b/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewAddTodoUseCase.swift @@ -9,7 +9,7 @@ import DomainLayer import Foundation final class PreviewAddTodoUseCase: AddTodoUseCase { - func execute(todo: Todo) async -> Result { + func callAsFunction(todo: Todo) async -> Result { fatalError() } } diff --git a/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewCompleteTodoUseCase.swift b/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewCompleteTodoUseCase.swift index b36ce82..e07356b 100644 --- a/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewCompleteTodoUseCase.swift +++ b/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewCompleteTodoUseCase.swift @@ -9,7 +9,7 @@ import DomainLayer import Foundation final class PreviewCompleteTodoUseCase: CompleteTodoUseCase { - func execute(id: Int) async -> Result { + func callAsFunction(id: Int) async -> Result { fatalError() } } diff --git a/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewGetAllTodosUseCase.swift b/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewGetAllTodosUseCase.swift index faa4885..bff15c1 100644 --- a/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewGetAllTodosUseCase.swift +++ b/CleanApp/Modules/PresentationLayer/Sources/Preview Content/UseCases/PreviewGetAllTodosUseCase.swift @@ -9,7 +9,7 @@ import DomainLayer import Foundation final class PreviewGetAllTodosUseCase: GetAllTodosUseCase { - func execute() async -> Result<[Todo], Error> { + func callAsFunction() async -> Result<[Todo], Error> { .success([ .init(id: 1, title: "Preview #1", completed: false), .init(id: 2, title: "Preview #2", completed: true), diff --git a/CleanApp/Modules/PresentationLayer/Sources/ViewModels/TodoListViewModel.swift b/CleanApp/Modules/PresentationLayer/Sources/ViewModels/TodoListViewModel.swift index 3f3c4f8..310809a 100644 --- a/CleanApp/Modules/PresentationLayer/Sources/ViewModels/TodoListViewModel.swift +++ b/CleanApp/Modules/PresentationLayer/Sources/ViewModels/TodoListViewModel.swift @@ -30,7 +30,7 @@ public class TodoListViewModel: ObservableObject { @MainActor @Sendable public func loadTodos() async { - _ = await getAllTodosUseCase.execute() + _ = await getAllTodosUseCase() .map { self.todos = $0 } @@ -45,7 +45,7 @@ public class TodoListViewModel: ObservableObject { addNewTodoIsDisabled = true let todo = Todo(title: newTodoText) - let result = await addTodoUseCase.execute(todo: todo) + let result = await addTodoUseCase(todo: todo) if case .success(let newTodo) = result { todos.append(newTodo) newTodoText = "" @@ -56,7 +56,7 @@ public class TodoListViewModel: ObservableObject { @MainActor public func completeTodo(id: Int) async { - let result = await completeTodoUseCase.execute(id: id) + let result = await completeTodoUseCase(id: id) if case .success(let completedTodo) = result, let index = todos.firstIndex(where: { $0.id == completedTodo.id }) { todos.remove(at: index) diff --git a/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockAddTodoUseCase.swift b/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockAddTodoUseCase.swift index e361281..40ac7d9 100644 --- a/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockAddTodoUseCase.swift +++ b/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockAddTodoUseCase.swift @@ -16,7 +16,7 @@ final class MockAddTodoUseCase: AddTodoUseCase { self.result = result } - func execute(todo: Todo) async -> Result { + func callAsFunction(todo: Todo) async -> Result { executeCalled = true return result } diff --git a/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockCompleteTodoUseCase.swift b/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockCompleteTodoUseCase.swift index 7636bf6..02e381a 100644 --- a/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockCompleteTodoUseCase.swift +++ b/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockCompleteTodoUseCase.swift @@ -16,7 +16,7 @@ final class MockCompleteTodoUseCase: CompleteTodoUseCase { self.result = result } - func execute(id: Int) async -> Result { + func callAsFunction(id: Int) async -> Result { executeCalledId = id return result } diff --git a/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockGetAllTodosUseCase.swift b/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockGetAllTodosUseCase.swift index d1eb49f..1c4c033 100644 --- a/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockGetAllTodosUseCase.swift +++ b/CleanApp/Modules/PresentationLayer/Tests/Mocks/UseCases/MockGetAllTodosUseCase.swift @@ -16,7 +16,7 @@ final class MockGetAllTodosUseCase: GetAllTodosUseCase { self.result = result } - func execute() async -> Result<[Todo], Error> { + func callAsFunction() async -> Result<[Todo], Error> { executeCalled = true return result }