@OOPer
//
// ViewController.swift
// Todoist
//
// Created by David Im on 1/14/21.
//
import UIKit
import SwipeCellKit
import StrikethroughLabel
class ViewController: UIViewController {
private var todoTableView = UITableView()
private var searchBar = UISearchBar()
private var dummyData = [Data(item: "Go to shopping", done: false),
Data(item: "Go to school", done: false),
Data(item: "Buy milk", done: false),
Data(item: "Do homework", done: false),
Data(item: "Clean room", done: false),
Data(item: "Wash car", done: false)]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
title = "Todoist"
setupViews()
}
private func setupViews() {
view.addSubview(todoTableView)
view.addSubview(searchBar)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addItem))
searchBar.translatesAutoresizingMaskIntoConstraints = false
searchBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
searchBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
searchBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
todoTableView.delegate = self
todoTableView.dataSource = self
todoTableView.register(TodoTableViewCell.self, forCellReuseIdentifier: "todoCell")
//todoTableView.allowsSelection = true
todoTableView.rowHeight = 70
//todoTableView.separatorStyle = .none
todoTableView.translatesAutoresizingMaskIntoConstraints = false
todoTableView.topAnchor.constraint(equalTo: searchBar.bottomAnchor, constant: 0).isActive = true
todoTableView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0).isActive = true
todoTableView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0).isActive = true
todoTableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
}
@objc private func addItem() {
var dataFromTextField:UITextField?
var addButton:UIAlertAction!
let addItemBox = UIAlertController(title: "Add Item", message: .none, preferredStyle: .alert)
addItemBox.addTextField { (item) in
dataFromTextField = item
}
addButton = UIAlertAction(title: "Add", style: .cancel) { (action) in
if let data = dataFromTextField?.text, !data.isEmpty {
self.dummyData.append(Data(item: data, done: false))
}else{
self.dismiss(animated: true, completion: nil)
}
DispatchQueue.main.async {
self.todoTableView.reloadData()
}
}
addItemBox.addAction(addButton)
present(addItemBox, animated: true, completion: nil)
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate, SwipeTableViewCellDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dummyData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let todoCell = tableView.dequeueReusableCell(withIdentifier: "todoCell") as! TodoTableViewCell
todoCell.dataLabel.text = dummyData[indexPath.row].item
if (dummyData[indexPath.row].done == false) {
todoCell.dataLabel.hideStrikeTextLayer()
}else{
todoCell.dataLabel.strikeThroughText()
}
todoCell.delegate = self
return todoCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let index = IndexPath(row: indexPath.row, section: 0)
if dummyData[indexPath.row].done == false {
dummyData[indexPath.row].done = true
todoTableView.reloadRows(at: [index], with: .none)
}else {
dummyData[indexPath.row].done = false
todoTableView.reloadRows(at: [index], with: .none)
}
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
// handle action by updating model with deletion
self.dummyData.remove(at: indexPath.row)
DispatchQueue.main.async {
self.todoTableView.reloadData()
}
}
// customize the action appearance
deleteAction.image = UIImage(named: "delete")
return [deleteAction]
}
func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
var options = SwipeOptions()
options.expansionStyle = .destructive
options.transitionStyle = .border
return options
}
}