Skip to content

Commit

Permalink
Pin notes (cmd-y)
Browse files Browse the repository at this point in the history
  • Loading branch information
glushchenko committed Aug 17, 2017
1 parent 69f2ebc commit 82455af
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 14 deletions.
4 changes: 4 additions & 0 deletions FSNotes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
D79700501F3E19C600BAD94D /* DocumentAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = D797004F1F3E19C600BAD94D /* DocumentAttributes.swift */; };
D7A415141F2FBDA00099B82C /* NotesTableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A415131F2FBDA00099B82C /* NotesTableView.swift */; };
D7D3D5181F44799400D152FE /* makeNote.png in Resources */ = {isa = PBXBuildFile; fileRef = D7D3D5171F44794A00D152FE /* makeNote.png */; };
D7D3D51A1F4483F500D152FE /* pin.png in Resources */ = {isa = PBXBuildFile; fileRef = D7D3D5191F4483EA00D152FE /* pin.png */; };
D7E025081F3B6DDB00EDDA32 /* Storage.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7E025071F3B6DDB00EDDA32 /* Storage.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -81,6 +82,7 @@
D797004F1F3E19C600BAD94D /* DocumentAttributes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentAttributes.swift; sourceTree = "<group>"; };
D7A415131F2FBDA00099B82C /* NotesTableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotesTableView.swift; sourceTree = "<group>"; };
D7D3D5171F44794A00D152FE /* makeNote.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = makeNote.png; sourceTree = "<group>"; };
D7D3D5191F4483EA00D152FE /* pin.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pin.png; sourceTree = "<group>"; };
D7E025071F3B6DDB00EDDA32 /* Storage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Storage.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -208,6 +210,7 @@
children = (
D7166F871F330A64001A883F /* SourceCodePro-Regular.ttf */,
D735E5B81F2E45CC00173215 /* icons.icns */,
D7D3D5191F4483EA00D152FE /* pin.png */,
D7D3D5171F44794A00D152FE /* makeNote.png */,
);
path = Resources;
Expand Down Expand Up @@ -328,6 +331,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D7D3D51A1F4483F500D152FE /* pin.png in Resources */,
D7D3D5181F44799400D152FE /* makeNote.png in Resources */,
D7166F541F32F75E001A883F /* Main.storyboard in Resources */,
6F2592741F39913200307D71 /* SourceCodePro-Regular.ttf in Resources */,
Expand Down
9 changes: 8 additions & 1 deletion FSNotes/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@
</textFieldCell>
</textField>
<textField autoresizesSubviews="NO" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="UvD-jB-7Ok">
<rect key="frame" x="3" y="28" width="42" height="17"/>
<rect key="frame" x="36" y="28" width="42" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
<textFieldCell key="cell" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" alignment="left" title="Name" usesSingleLineMode="YES" id="y7T-Rc-etT">
<font key="font" metaFont="system"/>
Expand All @@ -1098,10 +1098,16 @@
</connections>
</textFieldCell>
</textField>
<imageView hidden="YES" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="9h5-Ob-Nej">
<rect key="frame" x="3" y="21" width="23" height="23"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" image="pin" id="MIi-Yg-aUg"/>
</imageView>
</subviews>
<connections>
<outlet property="date" destination="abw-Lq-K7d" id="k6D-i6-nTL"/>
<outlet property="name" destination="UvD-jB-7Ok" id="5Mo-lm-FHk"/>
<outlet property="pin" destination="9h5-Ob-Nej" id="vPu-Ic-vSj"/>
<outlet property="preview" destination="EXQ-Ld-D5r" id="aS2-51-odI"/>
</connections>
</tableCellView>
Expand Down Expand Up @@ -1208,5 +1214,6 @@
</scenes>
<resources>
<image name="makeNote" width="512" height="512"/>
<image name="pin" width="128" height="128"/>
</resources>
</document>
1 change: 1 addition & 0 deletions FSNotes/EditTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ class EditTextView: NSTextView {
if (!viewController.emptyEditAreaImage.isHidden) {
viewController.makeNote(NSTextField())
}
return super.mouseDown(with: event)
}
}
36 changes: 36 additions & 0 deletions FSNotes/Model/Note.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Note: NSObject {
var date: Date?
var url: URL!
var isRemoved: Bool = false
var isPinned: Bool = false

override init(){}

Expand Down Expand Up @@ -52,6 +53,10 @@ class Note: NSObject {

do {
try fileManager.trashItem(at: self.url, resultingItemURL: nil)

if (isPinned) {
removePin()
}
}
catch let error as NSError {
print("Remove went wrong: \(error)")
Expand Down Expand Up @@ -149,4 +154,35 @@ class Note: NSObject {
func isRTF() -> Bool {
return (url.pathExtension == "rtf")
}

func addPin() {
let urlString = url.absoluteString
var pinnedNotes = UserDefaultsManagement.pinnedNotes
pinnedNotes.append(urlString)
UserDefaultsManagement.pinnedNotes = pinnedNotes
Storage.pinned += 1

isPinned = true
}

func removePin() {
let urlString = url.absoluteString
var pinnedNotes = UserDefaultsManagement.pinnedNotes

if let itemToRemoveIndex = pinnedNotes.index(of: urlString) {
pinnedNotes.remove(at: itemToRemoveIndex)
UserDefaultsManagement.pinnedNotes = pinnedNotes
Storage.pinned -= 1
}

isPinned = false
}

func togglePin() {
if (!isPinned) {
addPin()
} else {
removePin()
}
}
}
7 changes: 7 additions & 0 deletions FSNotes/Model/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import Foundation
class Storage {
var noteList = [Note]()
var i: Int = 0
static var pinned: Int = 0

func loadFiles() {
let markdownFiles = readDocuments()
let pinnedNotes = UserDefaultsManagement.pinnedNotes

for (markdownPath) in markdownFiles {
let url = UserDefaultsManagement.storageUrl.appendingPathComponent(markdownPath)
Expand All @@ -29,6 +31,11 @@ class Storage {
note.load()
note.id = i

if (pinnedNotes.contains(url.absoluteString)) {
note.isPinned = true
Storage.pinned += 1
}

i += 1

noteList.append(note)
Expand Down
14 changes: 14 additions & 0 deletions FSNotes/Model/UserDefaultsManagement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class UserDefaultsManagement {
static let NewNoteKeyModifier = "newNoteKeyModifier"
static let SearchNoteKeyCode = "searchNoteKeyCode"
static let SearchNoteKeyModifier = "searchNoteKeyModifier"
static let PinListKey = "pinList"
}

static var fontName: String {
Expand Down Expand Up @@ -157,4 +158,17 @@ public class UserDefaultsManagement {
UserDefaults.standard.set(newValue.modifierFlags, forKey: Constants.SearchNoteKeyModifier)
}
}

static var pinnedNotes: [String] {
get {
if let pinList = UserDefaults.standard.object(forKey: Constants.PinListKey) {
return pinList as! [String]
} else {
return []
}
}
set {
UserDefaults.standard.set(newValue, forKey: Constants.PinListKey)
}
}
}
18 changes: 12 additions & 6 deletions FSNotes/NoteCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ class NoteCellView: NSTableCellView {
@IBOutlet var name: NSTextField!
@IBOutlet var preview: NSTextField!
@IBOutlet var date: NSTextField!
@IBOutlet var pin: NSImageView!

override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)

renderPin()

let fontName = UserDefaultsManagement.fontName
date.font = NSFont(name: fontName, size: 10)
preview.font = NSFont(name: fontName, size: 11)

name.sizeToFit()

if (UserDefaultsManagement.horizontalOrientation) {
Expand All @@ -31,7 +33,6 @@ class NoteCellView: NSTableCellView {
}

func applyVerticalConstrainst() {

preview.translatesAutoresizingMaskIntoConstraints = false
date.translatesAutoresizingMaskIntoConstraints = false
name.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -40,13 +41,13 @@ class NoteCellView: NSTableCellView {
let previewLeft = preview.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 5)
let dateRight = date.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -5)
let dateTop = date.topAnchor.constraint(equalTo: self.topAnchor, constant: 4)
let nameRight = name.rightAnchor.constraint(equalTo: date.leftAnchor, constant: -8)
let nameLeft = name.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 5)
let nameRight = name.rightAnchor.constraint(equalTo: date.leftAnchor, constant: 5)
let nameTop = name.topAnchor.constraint(equalTo: self.topAnchor, constant: 5)

date.sizeToFit()
let nameLeft = name.leftAnchor.constraint(equalTo: pin.rightAnchor, constant: 0)

NSLayoutConstraint.activate([previewTop, previewLeft, dateRight, dateTop, nameLeft, nameRight, nameTop])

date.sizeToFit()
}

func applyHorizontalConstrains() {
Expand Down Expand Up @@ -89,4 +90,9 @@ class NoteCellView: NSTableCellView {
date.textColor = lightGray
}
}

func renderPin() {
pin.isHidden = !(objectValue as! Note).isPinned
pin.frame.size.width = !pin.isHidden ? 20 : 5
}
}
11 changes: 11 additions & 0 deletions FSNotes/NotesTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,15 @@ class NotesTableView: NSTableView, NSTableViewDataSource,

return note
}

override func performKeyEquivalent(with event: NSEvent) -> Bool {
if (
event.keyCode == 16
&& event.modifierFlags.contains(.command)) {
return true
}

return super.performKeyEquivalent(with: event)
}

}
38 changes: 31 additions & 7 deletions FSNotes/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ class ViewController: NSViewController,
if (event.keyCode == 45 && event.modifierFlags.contains(.command)) {
makeNote(NSTextField())
}

// Pin note shortcut (cmd-y)
if (event.keyCode == 16 && event.modifierFlags.contains(.command)) {
let row = notesTableView.rowView(atRow: notesTableView.selectedRow, makeIfNecessary: false) as! NoteRowView
let cell = row.view(atColumn: 0) as! NoteCellView

let note = cell.objectValue as! Note
let selected = notesTableView.selectedRow

note.togglePin()
moveAtTop(id: selected)
cell.renderPin()
}
}

override var representedObject: Any? {
Expand Down Expand Up @@ -187,13 +200,8 @@ class ViewController: NSViewController,
storage.noteList[storageId].date = Date()
storage.noteList[storageId].content = content

notesTableView.noteList.remove(at: selected)
notesTableView.noteList.insert(note, at: 0)

if editArea.save(note: note) {
notesTableView.moveRow(at: selected, to: 0)
notesTableView.reloadData(forRowIndexes: [0, selected], columnIndexes: [0])
notesTableView.scrollRowToVisible(0)
moveAtTop(id: selected)
}
}
}
Expand Down Expand Up @@ -229,7 +237,12 @@ class ViewController: NSViewController,
)
)
}
.sorted(by: { $0.date! > $1.date! })
.sorted(by: {
$0.date! > $1.date!
})
.sorted(by: {
$0.isPinned && !$1.isPinned
})

notesTableView.reloadData()
}
Expand Down Expand Up @@ -290,5 +303,16 @@ class ViewController: NSViewController,
cleanSearchAndEditArea()
}

func moveAtTop(id: Int) {
let isPinned = notesTableView.noteList[id].isPinned
let position = isPinned ? 0 : Storage.pinned
let note = notesTableView.noteList.remove(at: id)

notesTableView.noteList.insert(note, at: position)
notesTableView.moveRow(at: id, to: position)
notesTableView.reloadData(forRowIndexes: [id, position], columnIndexes: [0])
notesTableView.scrollRowToVisible(0)
}

}

Binary file added Resources/pin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 82455af

Please sign in to comment.