Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // NotesTableViewController.swift
- // MyNotes
- //
- // Created by Vladimir Orlov on 23.09.17.
- // Copyright © 2017 Vladimir Orlov. All rights reserved.
- //
- import UIKit
- import SwipeCellKit
- class NotesTableViewController: UITableViewController {
- var notes = [Note]() {
- didSet {
- tableView.reloadData()
- }
- }
- var filteredNotes: [Note] = []
- let manager = NoteManager.shared
- let searchController = UISearchController(searchResultsController: nil)
- // MARK: - Lifecycle
- override func viewDidLoad() {
- super.viewDidLoad()
- let nib = UINib(nibName: NoteCell.nibName, bundle: nil)
- tableView.register(nib, forCellReuseIdentifier: NoteCell.reuseId)
- setupNavigationBar()
- notes.append(Note(title: "First note", body: "First body", createDate: "24-09-2017"))
- }
- // MARK: - Helpers
- func setupNavigationBar() {
- navigationController?.navigationBar.prefersLargeTitles = true
- navigationItem.searchController = searchController
- searchController.searchBar.placeholder = "Поиск"
- searchController.searchBar.searchBarStyle = .minimal
- searchController.searchBar.showsCancelButton = false
- searchController.searchResultsUpdater = self
- searchController.dimsBackgroundDuringPresentation = false
- definesPresentationContext = true
- }
- func searchBarIsEmpty() -> Bool {
- // Returns true if the text is empty or nil
- return searchController.searchBar.text?.isEmpty ?? true
- }
- func filterContentForSearchText(_ searchText: String, scope: String = "All") {
- filteredNotes = notes.filter { ($0.title?.lowercased().contains(searchText.lowercased()))! }
- tableView.reloadData()
- }
- func isFiltering() -> Bool {
- return searchController.isActive && !searchBarIsEmpty()
- }
- // MARK: - Table view data source
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- if isFiltering() {
- return filteredNotes.count
- }
- return notes.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: NoteCell.reuseId, for: indexPath) as! NoteCell
- let note: Note
- if isFiltering() {
- note = filteredNotes[indexPath.row]
- } else {
- note = notes[indexPath.row]
- }
- cell.titleLabel.text = note.title
- cell.lastUpdateLabel.text = note.lastUpdate
- cell.bodyLabel.text = note.body
- cell.delegate = self
- return cell
- }
- // MARK: - Table view delegate
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- tableView.deselectRow(at: indexPath, animated: true)
- // navigationItem.backBarButtonItem?.tintColor = navigationItem.rightBarButtonItem?.tintColor
- performSegue(withIdentifier: Segue.editNote, sender: notes[indexPath.row])
- }
- // MARK: - Navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- guard notes.count != 0, let destination = segue.destination as? NoteViewController else { return }
- destination.delegate = self
- if segue.identifier == Segue.editNote {
- destination.note = sender as? Note
- }
- }
- }
- extension NotesTableViewController: NoteViewControllerDelegate {
- func noteViewController(_ controller: NoteViewController, didAddNote note: Note) {
- notes.append(note)
- }
- func noteViewController(_ controller: NoteViewController, didEditNote note: Note, at indexPath: IndexPath) {
- }
- }
- extension NotesTableViewController: SwipeTableViewCellDelegate {
- func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
- guard orientation == .right else { return nil }
- let deleteAction = SwipeAction(style: .destructive, title: "Удалить") { action, indexPath in
- action.image = UIImage(contentsOfFile: "garbage.png")
- self.notes.remove(at: indexPath.row)
- }
- let lockAction = SwipeAction(style: .default, title: "Заблок.") { action, indexPath in
- action.image = UIImage(named: "locked")
- let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
- alert.addAction(UIAlertAction(title: "Ввод пароля", style: .default, handler: { action in
- let passwordAlert = UIAlertController(title: "Блокировка заметки", message: "Введите пароль для блокировки этой заметки", preferredStyle: .alert)
- passwordAlert.addTextField(configurationHandler: { textField in
- textField.returnKeyType = .done
- })
- }))
- }
- return [deleteAction, lockAction]
- }
- }
- extension NotesTableViewController: UISearchResultsUpdating, UISearchBarDelegate {
- func updateSearchResults(for searchController: UISearchController) {
- filterContentForSearchText(searchController.searchBar.text!)
- }
- func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
- searchController.searchBar.showsCancelButton = true
- return true
- }
- func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
- self.view.endEditing(true)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement