Skip to content

Commit

Permalink
Prevent active tasks from being deleted.
Browse files Browse the repository at this point in the history
Show projects with active tasks on home view.
  • Loading branch information
landonepps committed Dec 11, 2019
1 parent a8a9a2b commit 57b073e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
4 changes: 3 additions & 1 deletion TimeCrumbs/Custom Cells/LogItemTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class LogItemTableViewCell: UITableViewCell {
dateLabel.text = format(date: date)
}

if let taskName = task.name?.trimmingCharacters(in: .whitespacesAndNewlines),
if task.isActive {
taskNameLabel.text = "In Progress"
} else if let taskName = task.name?.trimmingCharacters(in: .whitespacesAndNewlines),
taskName.isEmpty == false {
taskNameLabel.text = task.name
} else {
Expand Down
18 changes: 15 additions & 3 deletions TimeCrumbs/Custom Cells/ProjectCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import UIKit
import CoreData

protocol ProjectCollectionViewCellDelegate: class {
func logTimeButtonTapped(cell: ProjectCollectionViewCell)
Expand Down Expand Up @@ -82,15 +83,26 @@ class ProjectCollectionViewCell: UICollectionViewCell {
}

private func setupViews() {
projectLabel.text = project?.name
if let projectColorName = project?.color,
guard let project = project else { return }

projectLabel.text = project.name
if let projectColorName = project.color,
let projectColor = UIColor(named: projectColorName)
{
projectColorView.backgroundColor = projectColor
logTimeButton.tintColor = projectColor
startButton.tintColor = projectColor
}


let request: NSFetchRequest<Task> = Task.fetchRequest()
request.predicate = NSPredicate(format: "(project = %@) AND (isActive = YES)", project)
if let startedTasks = try? project.managedObjectContext?.fetch(request),
startedTasks.count > 0 {

startLabel.text = "RESUME"
} else {
startLabel.text = "START"
}
}

}
4 changes: 3 additions & 1 deletion TimeCrumbs/Custom Cells/ProjectDetailTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class ProjectDetailTableViewCell: UITableViewCell {
func update(with task: Task) {
let taskDuration = format(duration: task.duration)

if let taskName = task.name?.trimmingCharacters(in: .whitespacesAndNewlines),
if task.isActive {
taskNameLabel.text = "In Progress"
} else if let taskName = task.name?.trimmingCharacters(in: .whitespacesAndNewlines),
taskName.isEmpty == false {
taskNameLabel.text = task.name
} else {
Expand Down
9 changes: 9 additions & 0 deletions TimeCrumbs/View Controllers/LogTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class LogTableViewController: UITableViewController {
return cell
}

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
if let task = fetchedResultsController?.object(at: indexPath) {
if task.isActive {
return .none
}
}
return .delete
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
if let task = fetchedResultsController?.object(at: indexPath) {
Expand Down
20 changes: 20 additions & 0 deletions TimeCrumbs/View Controllers/ProjectDetailTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class ProjectDetailTableViewController: UITableViewController {

return cell
}

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
if let task = fetchedResultsController?.object(at: indexPath) {
if task.isActive {
return .none
}
}
return .delete
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
Expand All @@ -96,7 +105,18 @@ class ProjectDetailTableViewController: UITableViewController {
}
}

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
guard let task = fetchedResultsController?.object(at: indexPath) else { return nil }

if task.isActive {
return nil
}

return indexPath
}

// MARK: - Navigation

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toEditProject" {
guard let destination = segue.destination as? AddProjectViewController,
Expand Down

0 comments on commit 57b073e

Please sign in to comment.