Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.29 KB | None | 0 0
  1. //
  2. //  NotesTableViewController.swift
  3. //  MyNotes
  4. //
  5. //  Created by Vladimir Orlov on 23.09.17.
  6. //  Copyright © 2017 Vladimir Orlov. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import SwipeCellKit
  11.  
  12. class NotesTableViewController: UITableViewController {
  13.  
  14.   var notes = [Note]() {
  15.     didSet {
  16.       tableView.reloadData()
  17.     }
  18.   }
  19.   var filteredNotes: [Note] = []
  20.   let manager = NoteManager.shared
  21.   let searchController = UISearchController(searchResultsController: nil)
  22.  
  23.   // MARK: - Lifecycle
  24.   override func viewDidLoad() {
  25.     super.viewDidLoad()
  26.    
  27.     let nib = UINib(nibName: NoteCell.nibName, bundle: nil)
  28.     tableView.register(nib, forCellReuseIdentifier: NoteCell.reuseId)
  29.    
  30.     setupNavigationBar()
  31.    
  32.     notes.append(Note(title: "First note", body: "First body", createDate: "24-09-2017"))
  33.   }
  34.  
  35.   // MARK: - Helpers
  36.   func setupNavigationBar() {
  37.     navigationController?.navigationBar.prefersLargeTitles = true
  38.    
  39.     navigationItem.searchController = searchController
  40.     searchController.searchBar.placeholder = "Поиск"
  41.     searchController.searchBar.searchBarStyle = .minimal
  42.     searchController.searchBar.showsCancelButton = false
  43.     searchController.searchResultsUpdater = self
  44.     searchController.dimsBackgroundDuringPresentation = false
  45.     definesPresentationContext = true
  46.   }
  47.  
  48.   func searchBarIsEmpty() -> Bool {
  49.     // Returns true if the text is empty or nil
  50.     return searchController.searchBar.text?.isEmpty ?? true
  51.   }
  52.  
  53.   func filterContentForSearchText(_ searchText: String, scope: String = "All") {
  54.     filteredNotes = notes.filter { ($0.title?.lowercased().contains(searchText.lowercased()))! }
  55.     tableView.reloadData()
  56.   }
  57.  
  58.   func isFiltering() -> Bool {
  59.     return searchController.isActive && !searchBarIsEmpty()
  60.   }
  61.  
  62.   // MARK: - Table view data source
  63.  
  64.   override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  65.     if isFiltering() {
  66.       return filteredNotes.count
  67.     }
  68.     return notes.count
  69.   }
  70.  
  71.   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  72.     let cell = tableView.dequeueReusableCell(withIdentifier: NoteCell.reuseId, for: indexPath) as! NoteCell
  73.     let note: Note
  74.    
  75.     if isFiltering() {
  76.       note = filteredNotes[indexPath.row]
  77.     } else {
  78.       note = notes[indexPath.row]
  79.     }
  80.      
  81.     cell.titleLabel.text      = note.title
  82.     cell.lastUpdateLabel.text = note.lastUpdate
  83.     cell.bodyLabel.text       = note.body
  84.     cell.delegate = self
  85.    
  86.     return cell
  87.   }
  88.  
  89.   // MARK: - Table view delegate
  90.  
  91.   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  92.     tableView.deselectRow(at: indexPath, animated: true)
  93. //    navigationItem.backBarButtonItem?.tintColor = navigationItem.rightBarButtonItem?.tintColor
  94.     performSegue(withIdentifier: Segue.editNote, sender: notes[indexPath.row])
  95.   }
  96.  
  97.   // MARK: - Navigation
  98.   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  99.     guard notes.count != 0, let destination = segue.destination as? NoteViewController else { return }
  100.     destination.delegate = self
  101.  
  102.     if segue.identifier == Segue.editNote {
  103.       destination.note = sender as? Note
  104.     }
  105.   }
  106. }
  107.  
  108. extension NotesTableViewController: NoteViewControllerDelegate {
  109.   func noteViewController(_ controller: NoteViewController, didAddNote note: Note) {
  110.     notes.append(note)
  111.   }
  112.  
  113.   func noteViewController(_ controller: NoteViewController, didEditNote note: Note, at indexPath: IndexPath) {
  114.    
  115.   }
  116. }
  117.  
  118. extension NotesTableViewController: SwipeTableViewCellDelegate {
  119.   func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
  120.     guard orientation == .right else { return nil }
  121.    
  122.     let deleteAction = SwipeAction(style: .destructive, title: "Удалить") { action, indexPath in
  123.       action.image = UIImage(contentsOfFile: "garbage.png")
  124.       self.notes.remove(at: indexPath.row)
  125.     }
  126.     let lockAction = SwipeAction(style: .default, title: "Заблок.") { action, indexPath in
  127.       action.image = UIImage(named: "locked")
  128.      
  129.       let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  130.       alert.addAction(UIAlertAction(title: "Ввод пароля", style: .default, handler: { action in
  131.         let passwordAlert = UIAlertController(title: "Блокировка заметки", message: "Введите пароль для блокировки этой заметки", preferredStyle: .alert)
  132.         passwordAlert.addTextField(configurationHandler: { textField in
  133.           textField.returnKeyType = .done
  134.         })
  135.       }))
  136.     }
  137.    
  138.     return [deleteAction, lockAction]
  139.   }
  140.  
  141. }
  142.  
  143. extension NotesTableViewController: UISearchResultsUpdating, UISearchBarDelegate {
  144.   func updateSearchResults(for searchController: UISearchController) {
  145.     filterContentForSearchText(searchController.searchBar.text!)
  146.   }
  147.  
  148.   func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
  149.     searchController.searchBar.showsCancelButton = true
  150.     return true
  151.   }
  152.  
  153.   func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  154.     self.view.endEditing(true)
  155.   }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement