Last active
May 20, 2022 10:16
-
-
Save tifoaudii/440fcfba48b7ec5ac2cd7e23baf759f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol TaskViewPresenter { | |
var contentType: TaskContentType { get } | |
func fetchTask(for contentType: TaskContentType, completion: @escaping ([PresentableTask]) -> Void) | |
func updateContentType(with contentType: TaskContentType) | |
} | |
final class TaskViewDefaultPresenter: TaskViewPresenter { | |
private let dataStore: TaskViewDataStore | |
init(dataStore: TaskViewDataStore) { | |
self.dataStore = dataStore | |
} | |
var contentType: TaskContentType = .today | |
func updateContentType(with contentType: TaskContentType) { | |
self.contentType = contentType | |
} | |
func fetchTask(for contentType: TaskContentType, completion: @escaping ([PresentableTask]) -> Void) { | |
switch contentType { | |
case .today: | |
dataStore.fetchTodayTask { [weak self] tasks in | |
if let presentableTasks = self?.createPresentableTask(from: tasks) { | |
completion(presentableTasks) | |
} | |
} | |
case .upcoming: | |
dataStore.fetchUpcomingTask { [weak self] tasks in | |
if let presentableTasks = self?.createPresentableTask(from: tasks) { | |
completion(presentableTasks) | |
} | |
} | |
case .failed: | |
dataStore.fetchFailedTask { [weak self] tasks in | |
if let presentableTasks = self?.createPresentableTask(from: tasks) { | |
completion(presentableTasks) | |
} | |
} | |
case .done: | |
dataStore.fetchFinishedTask { [weak self] tasks in | |
if let presentableTasks = self?.createPresentableTask(from: tasks) { | |
completion(presentableTasks) | |
} | |
} | |
} | |
} | |
private func createPresentableTask(from tasks: [TaskModel]) -> [PresentableTask] { | |
return tasks.map { task in | |
PresentableTask( | |
title: task.title, | |
type: task.type, | |
deadline: task.deadline, | |
color: task.color) { [weak self] in | |
self?.dataStore.finishTask(task) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment