-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJournalListViewController.swift
More file actions
111 lines (96 loc) · 4.34 KB
/
JournalListViewController.swift
File metadata and controls
111 lines (96 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// ViewController.swift
// JRNL
//
// Created by iOS17Programming on 25/09/2023.
//
import UIKit
class JournalListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
// MARK: - Properties
@IBOutlet var tableView: UITableView!
let search = UISearchController(searchResultsController: nil)
var filteredTableData: [JournalEntry] = []
override func viewDidLoad() {
super.viewDidLoad()
SharedData.shared.loadJournalEntriesData()
search.searchResultsUpdater = self
search.obscuresBackgroundDuringPresentation = false
search.searchBar.placeholder = "Search titles"
navigationItem.searchController = search
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.search.isActive {
return self.filteredTableData.count
} else {
return SharedData.shared.numberOfJournalEntries()
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let journalCell = tableView.dequeueReusableCell(withIdentifier: "journalCell", for: indexPath) as! JournalListTableViewCell
let journalEntry: JournalEntry
if self.search.isActive {
journalEntry = filteredTableData[indexPath.row]
} else {
journalEntry = SharedData.shared.getJournalEntry(index: indexPath.row)
}
if let photoData = journalEntry.photoData {
journalCell.photoImageView.image = UIImage(data: photoData)
}
journalCell.dateLabel.text = journalEntry.dateString
journalCell.titleLabel.text = journalEntry.entryTitle
return journalCell
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
if self.search.isActive {
let selectedJournalEntry = filteredTableData[indexPath.row]
filteredTableData.remove(at: indexPath.row)
SharedData.shared.removeSelectedJournalEntry(selectedJournalEntry)
} else {
SharedData.shared.removeJournalEntry(index: indexPath.row)
}
SharedData.shared.saveJournalEntriesData()
tableView.reloadData()
}
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
guard segue.identifier == "entryDetail" else {
return
}
guard let journalEntryDetailViewController = segue.destination as? JournalEntryDetailViewController, let selectedJournalEntryCell = sender as? JournalListTableViewCell, let indexPath = tableView.indexPath(for: selectedJournalEntryCell) else {
fatalError("Could not get indexPath")
}
let selectedJournalEntry: JournalEntry
if self.search.isActive {
selectedJournalEntry = filteredTableData[indexPath.row]
} else {
selectedJournalEntry = SharedData.shared.getJournalEntry(index: indexPath.row)
}
journalEntryDetailViewController.selectedJournalEntry = selectedJournalEntry
}
// MARK: - Search
func updateSearchResults(for searchController: UISearchController) {
guard let searchBarText = searchController.searchBar.text else { return }
filteredTableData.removeAll()
for journalEntry in SharedData.shared.getAllJournalEntries() {
if journalEntry.entryTitle.lowercased().contains(searchBarText.lowercased()) {
filteredTableData.append(journalEntry)
}
}
self.tableView.reloadData()
}
// MARK: - Methods
@IBAction func unwindNewEntryCancel(segue: UIStoryboardSegue) {
}
@IBAction func unwindNewEntrySave(segue: UIStoryboardSegue) {
if let sourceViewController = segue.source as? AddJournalEntryViewController, let newJournalEntry = sourceViewController.newJournalEntry {
SharedData.shared.addJournalEntry(newJournalEntry: newJournalEntry)
SharedData.shared.saveJournalEntriesData()
tableView.reloadData()
}
}
}