diff --git a/Tasks.xcodeproj/project.pbxproj b/Tasks.xcodeproj/project.pbxproj index bc905e5..1651794 100644 --- a/Tasks.xcodeproj/project.pbxproj +++ b/Tasks.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + 8BD0818F26F894270008ED22 /* Task.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BD0818E26F894270008ED22 /* Task.swift */; }; + 8BD0819326F89DED0008ED22 /* TaskViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BD0819226F89DED0008ED22 /* TaskViewController.swift */; }; + 8BD0819526F8A9A80008ED22 /* Tasks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BD0819426F8A9A80008ED22 /* Tasks.swift */; }; 8BF3739F26F1F00C0017A890 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BF3739E26F1F00C0017A890 /* AppDelegate.swift */; }; 8BF373A126F1F00C0017A890 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BF373A026F1F00C0017A890 /* SceneDelegate.swift */; }; 8BF373A326F1F00C0017A890 /* AllTasksViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BF373A226F1F00C0017A890 /* AllTasksViewController.swift */; }; @@ -17,6 +20,9 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 8BD0818E26F894270008ED22 /* Task.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Task.swift; sourceTree = ""; }; + 8BD0819226F89DED0008ED22 /* TaskViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaskViewController.swift; sourceTree = ""; }; + 8BD0819426F8A9A80008ED22 /* Tasks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tasks.swift; sourceTree = ""; }; 8BF3739B26F1F00C0017A890 /* Tasks.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Tasks.app; sourceTree = BUILT_PRODUCTS_DIR; }; 8BF3739E26F1F00C0017A890 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 8BF373A026F1F00C0017A890 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -39,6 +45,15 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 8BD0819626F8E0150008ED22 /* Models */ = { + isa = PBXGroup; + children = ( + 8BD0818E26F894270008ED22 /* Task.swift */, + 8BD0819426F8A9A80008ED22 /* Tasks.swift */, + ); + path = Models; + sourceTree = ""; + }; 8BF3739226F1F00C0017A890 = { isa = PBXGroup; children = ( @@ -58,6 +73,7 @@ 8BF3739D26F1F00C0017A890 /* Tasks */ = { isa = PBXGroup; children = ( + 8BD0819626F8E0150008ED22 /* Models */, 8BF373B326F1F5150017A890 /* Controllers */, 8BF373B226F1F1D40017A890 /* Resources */, 8BF3739E26F1F00C0017A890 /* AppDelegate.swift */, @@ -82,6 +98,7 @@ children = ( 8BF373A226F1F00C0017A890 /* AllTasksViewController.swift */, 8BF373B426F1F5300017A890 /* AddTaskViewController.swift */, + 8BD0819226F89DED0008ED22 /* TaskViewController.swift */, ); path = Controllers; sourceTree = ""; @@ -157,7 +174,10 @@ buildActionMask = 2147483647; files = ( 8BF373A326F1F00C0017A890 /* AllTasksViewController.swift in Sources */, + 8BD0819326F89DED0008ED22 /* TaskViewController.swift in Sources */, 8BF373B526F1F5300017A890 /* AddTaskViewController.swift in Sources */, + 8BD0818F26F894270008ED22 /* Task.swift in Sources */, + 8BD0819526F8A9A80008ED22 /* Tasks.swift in Sources */, 8BF3739F26F1F00C0017A890 /* AppDelegate.swift in Sources */, 8BF373A126F1F00C0017A890 /* SceneDelegate.swift in Sources */, ); diff --git a/Tasks/AppDelegate.swift b/Tasks/AppDelegate.swift index fa58b12..6438503 100644 --- a/Tasks/AppDelegate.swift +++ b/Tasks/AppDelegate.swift @@ -14,6 +14,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. + let task1 = Task(description: "Task 1", main: true) + let task2 = Task(description: "Task 2", main: true) + let task3 = Task(description: "Task 3", main: true) + let task4 = Task(description: "Task 4", main: true) + let task5 = Task(description: "Task 5", main: true) + let task6 = Task(description: "Task 6", main: true) + let task7 = Task(description: "Task 7", main: false) + let task8 = Task(description: "Task 8", main: false) + let task9 = Task(description: "Task 9", main: false) + let task10 = Task(description: "Task 10", main: false) + task1.tasks = [task8, task9, task10] + task10.tasks = [task7] + + Tasks.shared.tasks = [task1, task2, task3, task4, task5, task6, task7, task8, task9, task10] return true } diff --git a/Tasks/Controllers/AddTaskViewController.swift b/Tasks/Controllers/AddTaskViewController.swift index 3bfa394..94dcbca 100644 --- a/Tasks/Controllers/AddTaskViewController.swift +++ b/Tasks/Controllers/AddTaskViewController.swift @@ -9,7 +9,23 @@ import UIKit class AddTaskViewController: UIViewController { + @IBOutlet weak var taskTextView: UITextView! + + var parentTask: Task? + override func viewDidLoad() { super.viewDidLoad() } + + @IBAction func saveTaskTapped(_ sender: Any) { + guard let description = taskTextView.text else { return } + let main = parentTask == nil + + let newTask = Task(description: description, main: main) + + Tasks.shared.tasks.append(newTask) + parentTask?.tasks.append(newTask) + + navigationController?.popViewController(animated: true) + } } diff --git a/Tasks/Controllers/AllTasksViewController.swift b/Tasks/Controllers/AllTasksViewController.swift index ef98631..4f638ab 100644 --- a/Tasks/Controllers/AllTasksViewController.swift +++ b/Tasks/Controllers/AllTasksViewController.swift @@ -9,8 +9,61 @@ import UIKit class AllTasksViewController: UIViewController { + @IBOutlet weak var tasksTableView: UITableView! + + var tasks: [Task] = [] + override func viewDidLoad() { super.viewDidLoad() + + tasksTableView.delegate = self + tasksTableView.dataSource = self + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + // TODO: Сделать хранение задач в Realm (например) + self.tasks = [] + + for task in Tasks.shared.tasks { + if task.main { + self.tasks.append(task) + } + } + + tasksTableView.reloadData() + } +} + +extension AllTasksViewController: UITableViewDelegate, UITableViewDataSource { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return tasks.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + return tableView.dequeueReusableCell(withIdentifier: "TaskCellIdentifier", for: indexPath) + } + + func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { + let task = tasks[indexPath.row] + cell.textLabel?.text = task.description + cell.detailTextLabel?.text = String(task.tasks.count) + } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + if segue.identifier == "showTaskSegue" { + guard let taskViewController = segue.destination as? TaskViewController else { return } + + guard let indexPath = sender as? IndexPath else { return } + let task = tasks[indexPath.row] + + taskViewController.currentTask = task + } + } + + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + performSegue(withIdentifier: "showTaskSegue", sender: indexPath) } } diff --git a/Tasks/Controllers/TaskViewController.swift b/Tasks/Controllers/TaskViewController.swift new file mode 100644 index 0000000..e95a221 --- /dev/null +++ b/Tasks/Controllers/TaskViewController.swift @@ -0,0 +1,60 @@ +// +// TaskViewController.swift +// Tasks +// +// Created by Кирилл Копытин on 20.09.2021. +// + +import UIKit + +class TaskViewController: UIViewController { + + var currentTask: Task! + + @IBOutlet weak var descriptionTaskLabel: UILabel! + @IBOutlet weak var tasksTableView: UITableView! + + override func viewDidLoad() { + super.viewDidLoad() + + descriptionTaskLabel.text = currentTask.description + + tasksTableView.delegate = self + tasksTableView.dataSource = self + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + + tasksTableView.reloadData() + } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + if segue.identifier == "showAddTaskSegue" { + guard let addTaskViewController = segue.destination as? AddTaskViewController else { return } + addTaskViewController.parentTask = currentTask + } + } +} + +extension TaskViewController: UITableViewDelegate, UITableViewDataSource { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return currentTask.tasks.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + return tableView.dequeueReusableCell(withIdentifier: "TaskCellIdentifier", for: indexPath) + } + + func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { + let task = currentTask.tasks[indexPath.row] + cell.textLabel?.text = task.description + cell.detailTextLabel?.text = String(task.tasks.count) + } + + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + self.currentTask = currentTask.tasks[indexPath.row] + descriptionTaskLabel.text = currentTask.description + tableView.reloadData() + } +} diff --git a/Tasks/Models/Task.swift b/Tasks/Models/Task.swift new file mode 100644 index 0000000..5e9eb25 --- /dev/null +++ b/Tasks/Models/Task.swift @@ -0,0 +1,20 @@ +// +// Task.swift +// Tasks +// +// Created by Кирилл Копытин on 20.09.2021. +// + +import Foundation + +class Task { + let description: String + let main: Bool + + var tasks: [Task] = [] + + init(description: String, main: Bool) { + self.description = description + self.main = main + } +} diff --git a/Tasks/Models/Tasks.swift b/Tasks/Models/Tasks.swift new file mode 100644 index 0000000..c59fdc4 --- /dev/null +++ b/Tasks/Models/Tasks.swift @@ -0,0 +1,16 @@ +// +// Tasks.swift +// Tasks +// +// Created by Кирилл Копытин on 20.09.2021. +// + +import Foundation + +final class Tasks { + static let shared = Tasks() + + private init() {} + + var tasks: [Task] = [] +} diff --git a/Tasks/Resources/Base.lproj/Main.storyboard b/Tasks/Resources/Base.lproj/Main.storyboard index bb2fc01..8fdfdea 100644 --- a/Tasks/Resources/Base.lproj/Main.storyboard +++ b/Tasks/Resources/Base.lproj/Main.storyboard @@ -20,7 +20,7 @@ - + @@ -28,16 +28,16 @@ -