This is a sample Task Manager app built with UIKit to demonstrate Protocol-Oriented Programming (POP) in Swift. The app allows users to:
- Add tasks with a title and optional description.
- Mark tasks as completed.
- Delete tasks by swiping.
- View task details.
- Persist tasks using
UserDefaults.
This project follows Protocol-Oriented Programming (POP) principles by using:
protocol TaskRepresentable {
var title: String { get }
var description: String? { get }
var isCompleted: Bool { get set }
}protocol TaskPersistable {
func saveTasks(_ tasks: [Task])
func loadTasks() -> [Task]
}protocol TaskManagerProtocol {
var tasks: [Task] { get set }
func addTask(_ task: Task)
func updateTask(_ task: Task)
func filterTasks(by completionStatus: Bool) -> [Task]
}struct Task: TaskRepresentable, Codable {
var title: String
var description: String?
var isCompleted: Bool
}Handles task storage and retrieval using UserDefaults.
class TaskManager: TaskManagerProtocol, TaskPersistable {
var tasks: [Task] = []
func addTask(_ task: Task) {
tasks.append(task)
saveTasks(tasks)
}
func updateTask(_ task: Task) {
if let index = tasks.firstIndex(where: { $0.title == task.title }) {
tasks[index] = task
saveTasks(tasks)
}
}
func filterTasks(by completionStatus: Bool) -> [Task] {
return tasks.filter { $0.isCompleted == completionStatus }
}
func saveTasks(_ tasks: [Task]) {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(tasks) {
UserDefaults.standard.set(encoded, forKey: "tasks")
}
}
func loadTasks() -> [Task] {
if let savedTasks = UserDefaults.standard.data(forKey: "tasks") {
let decoder = JSONDecoder()
if let loadedTasks = try? decoder.decode([Task].self, from: savedTasks) {
tasks = loadedTasks
return loadedTasks
}
}
return []
}
}- Displays tasks in a table view.
- Allows users to mark tasks as completed.
- Supports swipe-to-delete.
- Users enter a title and description.
- Task is added and saved.
- Shows task details when a task is tapped.
- Clone the repository.
- Open
TaskManager.xcodeprojin Xcode. - Run the app on a simulator or device.
- Implement filtering (e.g., show only completed tasks).
- Use CoreData instead of
UserDefaultsfor persistence. - Add unit tests for
TaskManager.
This project showcases how Protocol-Oriented Programming (POP) helps in creating modular, reusable, and scalable code. The use of protocols promotes composition over inheritance, making the codebase cleaner and more maintainable.