Skip to content

Commit

Permalink
Add searching by title and content
Browse files Browse the repository at this point in the history
  • Loading branch information
cognophile committed Jul 9, 2019
1 parent ed65355 commit 1771a42
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 31 deletions.
4 changes: 4 additions & 0 deletions NotaFresca.xcodeproj/project.pbxproj
Expand Up @@ -26,6 +26,7 @@
0D746AD722D11C2D007A86DF /* NSTableViewExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D746AD622D11C2D007A86DF /* NSTableViewExtension.swift */; };
0D746ADC22D2939D007A86DF /* NoteEditorSyncDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D746ADB22D2939D007A86DF /* NoteEditorSyncDelegate.swift */; };
0D746ADE22D29CFA007A86DF /* DialogHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D746ADD22D29CFA007A86DF /* DialogHelper.swift */; };
0D746AE022D542D9007A86DF /* BaseRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D746ADF22D542D9007A86DF /* BaseRepository.swift */; };
0DF085D622B3025D00EBF9EC /* AppWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DF085D522B3025D00EBF9EC /* AppWindowController.swift */; };
1A8F683441A1656430E862F7 /* Pods_NotaFresca.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 300FF06F0B1E95E215A205E2 /* Pods_NotaFresca.framework */; };
3EC9C87C0DF45E468B3558E1 /* Pods_NotaFrescaUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F94469192A384B2DCA418483 /* Pods_NotaFrescaUITests.framework */; };
Expand Down Expand Up @@ -77,6 +78,7 @@
0D746AD622D11C2D007A86DF /* NSTableViewExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSTableViewExtension.swift; sourceTree = "<group>"; };
0D746ADB22D2939D007A86DF /* NoteEditorSyncDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoteEditorSyncDelegate.swift; sourceTree = "<group>"; };
0D746ADD22D29CFA007A86DF /* DialogHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DialogHelper.swift; sourceTree = "<group>"; };
0D746ADF22D542D9007A86DF /* BaseRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseRepository.swift; sourceTree = "<group>"; };
0DF085D522B3025D00EBF9EC /* AppWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppWindowController.swift; sourceTree = "<group>"; };
0E3717C642A812CD6406AE72 /* Pods-NotaFresca.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NotaFresca.release.xcconfig"; path = "Target Support Files/Pods-NotaFresca/Pods-NotaFresca.release.xcconfig"; sourceTree = "<group>"; };
300FF06F0B1E95E215A205E2 /* Pods_NotaFresca.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_NotaFresca.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -157,6 +159,7 @@
isa = PBXGroup;
children = (
0D00F59722BC505900420EDD /* NoteRepository.swift */,
0D746ADF22D542D9007A86DF /* BaseRepository.swift */,
);
path = Repositories;
sourceTree = "<group>";
Expand Down Expand Up @@ -505,6 +508,7 @@
0D746ACF22CD7730007A86DF /* DatabaseHelper.swift in Sources */,
0D746ADE22D29CFA007A86DF /* DialogHelper.swift in Sources */,
0D00F58F22BC2E4800420EDD /* NoteBrowserViewController.swift in Sources */,
0D746AE022D542D9007A86DF /* BaseRepository.swift in Sources */,
0D746AD522D0FEEA007A86DF /* NoteDataStruct.swift in Sources */,
0D00F58D22BC2D6700420EDD /* NoteModel.swift in Sources */,
0D746ADC22D2939D007A86DF /* NoteEditorSyncDelegate.swift in Sources */,
Expand Down
5 changes: 3 additions & 2 deletions NotaFresca/Base.lproj/Main.storyboard
Expand Up @@ -838,7 +838,7 @@
</searchFieldCell>
</searchField>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="qM6-Zf-82n">
<rect key="frame" x="2" y="5" width="47" height="32"/>
<rect key="frame" x="2" y="5" width="39" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" bezelStyle="rounded" image="NSAddTemplate" imagePosition="overlaps" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="3vz-cf-DcU">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -849,7 +849,7 @@
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="d4w-jt-nIu">
<rect key="frame" x="175" y="5" width="47" height="32"/>
<rect key="frame" x="183" y="5" width="39" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" bezelStyle="rounded" image="NSRemoveTemplate" imagePosition="overlaps" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="P1p-cM-05E">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -865,6 +865,7 @@
<outlet property="browser" destination="V32-IE-4KG" id="KcG-gw-CMn"/>
<outlet property="create" destination="CWj-q7-wez" id="Qmc-Ut-gjt"/>
<outlet property="remove" destination="d4w-jt-nIu" id="Fyr-3X-qi2"/>
<outlet property="search" destination="Bm8-SX-Xkl" id="ZY8-z6-Iju"/>
</connections>
</viewController>
<customObject id="PV2-i2-GUd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
Expand Down
2 changes: 1 addition & 1 deletion NotaFresca/Helpers/DatabaseHelper.swift
Expand Up @@ -45,7 +45,7 @@ class DatabaseHelper {
public func update(model: BaseModel, index: Int, query: [Setter]) -> Row? {
guard let statement = model.table?.filter(model.id == index) else { return nil }

if let record = try! self.connection?.run(statement.update(query)) {
if (try! self.connection?.run(statement.update(query))) != nil {
if let updated = try! self.connection?.pluck(statement) {
return updated
}
Expand Down
2 changes: 1 addition & 1 deletion NotaFresca/Info.plist
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.5.2</string>
<string>1.0.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
Expand Down
30 changes: 30 additions & 0 deletions NotaFresca/Repositories/BaseRepository.swift
@@ -0,0 +1,30 @@

import Foundation

class BaseRepository {
let database = DatabaseHelper()

func readAll() -> Array<BaseModel>? {
return nil
}

func readOne() -> BaseModel? {
return nil
}

func save() -> BaseModel? {
return nil
}

func update() -> BaseModel? {
return nil
}

func delete() -> Bool? {
return false
}

func find() -> Array<BaseModel>? {
return nil
}
}
54 changes: 36 additions & 18 deletions NotaFresca/Repositories/NoteRepository.swift
Expand Up @@ -2,30 +2,26 @@
import Foundation
import SQLite

class NoteRepository {
class NoteRepository: BaseRepository {
private var data = [NoteModel]()
private var model = NoteModel()
private let database = DatabaseHelper()

init() {
override init() {
super.init()
self.database.createTable(model: self.model)
}

public func readAll() -> Array<NoteModel> {
let records = Array(self.database.selectAll(model: self.model)!)
var notes = [NoteModel]()

for line in records {
let note = self.rowToObject(record: line)
notes.append(note)
}
let notes = self.transformMultipleRows(rows: records)

return notes
}

public func readOne(target: Int) -> NoteModel {
let record = self.database.selectOne(model: self.model, index: target)
let note = self.rowToObject(record: record!)
let note = self.transformRow(row: record!)

return note
}

Expand Down Expand Up @@ -54,22 +50,44 @@ class NoteRepository {
}

public func delete(target: Int) -> Bool {
if let deletedId = self.database.delete(model: self.model, index: target) {
if self.database.delete(model: self.model, index: target) != nil {
return true
}

return false
}

private func rowToObject(record: Row) -> NoteModel {
public func find(term: String, notes: [NoteModel]) -> Array<NoteModel> {
let notes = Array(self.database.selectAll(model: self.model)!)

let results = notes.filter { item in return
(item[self.model.title]?.lowercased().contains(term.lowercased()))! ||
(item[self.model.body]?.lowercased().contains(term.lowercased()))!
}

return self.transformMultipleRows(rows: results)
}

private func transformMultipleRows(rows: [Row]) -> [NoteModel] {
var notes = [NoteModel]()

for line in rows {
let noteObject = self.transformRow(row: line)
notes.append(noteObject)
}

return notes
}

private func transformRow(row: Row) -> NoteModel {
let note = NoteModel()

note.record = record
note.data.id = record[note.id]
note.data.title = record[note.title]
note.data.body = record[note.body]
note.data.created = record[note.created]
note.data.updated = record[note.updated]
note.record = row
note.data.id = row[note.id]
note.data.title = row[note.title]
note.data.body = row[note.body]
note.data.created = row[note.created]
note.data.updated = row[note.updated]

return note
}
Expand Down
47 changes: 40 additions & 7 deletions NotaFresca/Views/Browser/NoteBrowserViewController.swift
Expand Up @@ -2,14 +2,15 @@
import Foundation
import Cocoa

class NoteBrowserViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate, NSTableViewClickableDelegate, NoteEditorSyncDelegate {
class NoteBrowserViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate, NSSearchFieldDelegate, NSTableViewClickableDelegate, NoteEditorSyncDelegate {
@IBOutlet weak var browser: NSTableView?
@IBOutlet weak var search: NSSearchField?
@IBOutlet var create: NSButton!
@IBOutlet weak var remove: NSButton!

var notes: Array<NoteModel>?
var selectedIndex: Int?
var isSearching: Bool = false
var activeNote: NoteModel?
var repository: NoteRepository?
var editor: NoteEditorViewController?
Expand All @@ -18,20 +19,25 @@ class NoteBrowserViewController: NSViewController, NSTableViewDataSource, NSTabl
super.viewDidLoad()
self.repository = NoteRepository()

// NotificationCenter.default.addObserver(self, selector: #selector(refresh), name: NSNotification.Name(rawValue: "refresh"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(refresh), name: NSNotification.Name(rawValue: "refresh"), object: nil)

self.browser?.delegate = self
self.browser?.dataSource = self
self.search?.delegate = self
self.editor?.syncDelegate = self

self.refreshBrowserSelection()
}

@objc func refresh(_ notification: NSNotification? = nil) {
self.notes = self.repository?.readAll()
self.browser?.reloadData()

let defaultIndex = 0

if !self.isSearching {
self.notes = self.repository?.readAll()
}

if let selected = self.selectedIndex {
self.browser?.reloadData()
self.browser?.selectRowIndexes(NSIndexSet(index: selected) as IndexSet, byExtendingSelection: false)
self.browser?.scrollRowToVisible(selected)

Expand All @@ -41,10 +47,10 @@ class NoteBrowserViewController: NSViewController, NSTableViewDataSource, NSTabl
}
}
else {
let defaultIndex = 0
self.browser?.reloadData()
self.browser?.selectRowIndexes(NSIndexSet(index: defaultIndex) as IndexSet, byExtendingSelection: false)
self.browser?.scrollRowToVisible(defaultIndex)
}
}
}

@IBAction func createNoteButton(_ sender: NSButton) {
Expand Down Expand Up @@ -120,6 +126,33 @@ class NoteBrowserViewController: NSViewController, NSTableViewDataSource, NSTabl
}
}

func controlTextDidChange(_ obj: Notification) {
if obj.object as? NSSearchField == self.search {
self.searchNotes(self.search?.stringValue ?? "")
}
}

private func searchNotes(_ searchTerm: String) {
var results = [NoteModel]()

if searchTerm.count > 0 {
self.isSearching = true
results = (self.repository?.find(term: searchTerm, notes: self.notes!))!

if results.count <= 0 {
self.editor?.empty()
}

self.notes = results
}
else {
self.isSearching = false
}

self.browser?.reloadData()
self.refreshBrowserSelection(indexToHighlight: 0)
}

private func refreshBrowserSelection(indexToHighlight: Int? = nil) {
if let selected = indexToHighlight {
self.selectedIndex = selected
Expand Down
4 changes: 2 additions & 2 deletions NotaFresca/Views/Editor/NoteEditorViewController.swift
Expand Up @@ -10,7 +10,7 @@ class NoteEditorViewController: NSViewController, NSTextViewDelegate, NSTextFiel
@IBOutlet weak var titlePane: NSTextField!
@IBOutlet weak var updatedLabel: NSTextField!

private let TIME_DELAY_FACTOR: Double = 1.0
private let TIME_DELAY_FACTOR: Double = 0.8

var activeNote: NoteModel?
var repository: NoteRepository?
Expand Down Expand Up @@ -53,7 +53,7 @@ class NoteEditorViewController: NSViewController, NSTextViewDelegate, NSTextFiel
}

public func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView, self.bodyTextView.identifier == textView.identifier else {
guard let textView = notification.object as? NSTextView, self.bodyTextView.identifier == textView.identifier, self.bodyTextView.string == textView.string else {
return
}

Expand Down

0 comments on commit 1771a42

Please sign in to comment.