Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
glushchenko committed Mar 26, 2018
1 parent f585c43 commit ebf4b96
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 63 deletions.
3 changes: 2 additions & 1 deletion FSNotes/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var mainWindowController: MainWindowController?
var storage = Storage.sharedInstance()

var appTitle: String {
let name = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String
Expand Down Expand Up @@ -70,7 +71,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func application(_ application: NSApplication, open urls: [URL]) {
if let url = urls.first {
let name = url.lastPathComponent
if let note = Storage.instance.getBy(title: name),
if let note = storage.getBy(title: name),
let window = NSApplication.shared.windows.first,
let controller = window.contentViewController as? ViewController {
controller.updateTable(filter: name) {
Expand Down
3 changes: 2 additions & 1 deletion FSNotes/EditTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Highlightr
class EditTextView: NSTextView {
public static var note: Note?
var isHighlighted: Bool = false
let storage = Storage.sharedInstance()

class UndoInfo: NSObject {
let text: String
Expand Down Expand Up @@ -48,7 +49,7 @@ class EditTextView: NSTextView {

let nsString = string as NSString
let chars = nsString.substring(with: charRange)
if let notes = Storage.instance.getBy(startWith: chars) {
if let notes = storage.getBy(startWith: chars) {
let titles = notes.map{ $0.title }
return titles
}
Expand Down
4 changes: 2 additions & 2 deletions FSNotes/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.3</string>
<string>1.3.1</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
Expand All @@ -38,7 +38,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>117</string>
<string>118</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.productivity</string>
<key>LSMinimumSystemVersion</key>
Expand Down
20 changes: 6 additions & 14 deletions FSNotes/Model/Note+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Note: NSManagedObject {
var syncDate: Date?
var creationDate: Date? = Date()
var isCached = false
var sharedStorage = Storage.sharedInstance()

convenience init(name: String) {
let context = CoreDataManager.instance.context
Expand Down Expand Up @@ -51,7 +52,7 @@ public class Note: NSManagedObject {
func initWith(url: URL, fileName: String) {
storage = CoreDataManager.instance.fetchGeneralStorage()

self.url = Storage.instance.getBaseURL().appendingPathComponent(fileName)
self.url = sharedStorage.getBaseURL().appendingPathComponent(fileName)
parseURL()

let options = getDocOptions()
Expand Down Expand Up @@ -109,15 +110,6 @@ public class Note: NSManagedObject {
try FileManager.default.moveItem(at: url, to: to)
print("File moved from \"\(url.deletingPathExtension().lastPathComponent)\" to \"\(to.deletingPathExtension().lastPathComponent)\"")
} catch {}

#if os(iOS)
Storage.instance.removeNotes(notes: [self]) {
let note = CoreDataManager.instance.make()
note.storage = CoreDataManager.instance.fetchStorageItemBy(fileUrl: to)
note.load(to)
note.save(cloudSync: true)
}
#endif
}

func getNewURL(name: String) -> URL {
Expand Down Expand Up @@ -229,7 +221,7 @@ public class Note: NSManagedObject {
name = defaultName
}

var fileUrl = Storage.instance.getBaseURL()
var fileUrl = sharedStorage.getBaseURL()
fileUrl.appendPathComponent(name)
fileUrl.appendPathExtension(type.rawValue)

Expand All @@ -252,7 +244,7 @@ public class Note: NSManagedObject {
}

func addPin() {
Storage.pinned += 1
sharedStorage.pinned += 1
isPinned = true
CoreDataManager.instance.save()

Expand All @@ -265,7 +257,7 @@ public class Note: NSManagedObject {

func removePin() {
if isPinned {
Storage.pinned -= 1
sharedStorage.pinned -= 1
isPinned = false

#if CLOUDKIT || os(iOS)
Expand Down Expand Up @@ -361,7 +353,7 @@ public class Note: NSManagedObject {
return
}

Storage.instance.saveNote(note: self, userInitiated: false, cloudSync: cloudSync)
sharedStorage.saveNote(note: self, userInitiated: false, cloudSync: cloudSync)
}

func getFileAttributes() -> [FileAttributeKey: Any] {
Expand Down
34 changes: 18 additions & 16 deletions FSNotes/Model/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import Foundation
import Highlightr

class Storage {
static let instance = Storage()
static var instance: Storage? = nil

var noteList = [Note]()
var notesDict: [String: Note] = [:]
var generalUrl: URL?

static var generalUrl: URL?
static var pinned: Int = 0
static var allowedExtensions = ["md", "markdown", "txt", "rtf", "fountain", UserDefaultsManagement.storageExtension]
var allowedExtensions = ["md", "markdown", "txt", "rtf", "fountain", UserDefaultsManagement.storageExtension]

public static var fsImportIsAvailable = true
var pinned: Int = 0

#if os(iOS)
let initialFiles = [
Expand All @@ -35,6 +34,14 @@ class Storage {
]
#endif

public static func sharedInstance() -> Storage {
guard let storage = self.instance else {
self.instance = Storage()
return self.instance!
}
return storage
}

func loadDocuments(tryCount: Int = 0) {
noteList.removeAll()

Expand Down Expand Up @@ -167,7 +174,7 @@ class Storage {
}

if note.isPinned {
Storage.pinned += 1
pinned += 1
}

noteList.append(note)
Expand All @@ -182,7 +189,8 @@ class Storage {
try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: [.contentModificationDateKey, .creationDateKey], options:.skipsHiddenFiles)

return
directoryFiles.filter {Storage.allowedExtensions.contains($0.pathExtension)}.map{
directoryFiles.filter {
allowedExtensions.contains($0.pathExtension)}.map{
url in (
url,
(try? url.resourceValues(forKeys: [.contentModificationDateKey])
Expand All @@ -206,21 +214,16 @@ class Storage {

func removeBy(note: Note) {
if let i = noteList.index(of: note) {
note.isRemoved = true
noteList.remove(at: i)
}
}

func remove(id: Int) {
noteList[id].isRemoved = true
}

func getNextId() -> Int {
return noteList.count
}

func checkFirstRun() -> Bool {
let destination = Storage.instance.getBaseURL()
let destination = getBaseURL()
let path = destination.path

if !FileManager.default.fileExists(atPath: path) {
Expand Down Expand Up @@ -314,16 +317,15 @@ class Storage {

func getBaseURL() -> URL {
#if os(OSX)
if let gu = Storage.generalUrl {
if let gu = generalUrl {
return gu
}

guard let storage = CoreDataManager.instance.fetchGeneralStorage(), let path = storage.path, let url = URL(string: path) else {
return UserDefaultsManagement.storageUrl
}

Storage.generalUrl = url

generalUrl = url
return url
#else
return UserDefaultsManagement.documentDirectory
Expand Down
10 changes: 10 additions & 0 deletions FSNotes/NotesTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class NotesTableView: NSTableView, NSTableViewDataSource,
var noteList = [Note]()
var defaultCell = NoteCellView()
var pinnedCell = NoteCellView()
var storage = Storage.sharedInstance()

override func draw(_ dirtyRect: NSRect) {
self.dataSource = self
Expand Down Expand Up @@ -176,4 +177,13 @@ class NotesTableView: NSTableView, NSTableViewDataSource,
}
}

func removeByNotes(notes: [Note]) {
for note in notes {
if let i = noteList.index(of: note) {
let indexSet = IndexSet(integer: i)
removeRows(at: indexSet, withAnimation: .effectFade)
}
}
}

}
5 changes: 3 additions & 2 deletions FSNotes/Preferences/PrefsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PrefsViewController: NSViewController {
@IBOutlet weak var restoreCursorButton: NSButton!

let viewController = NSApplication.shared.windows.first!.contentViewController as! ViewController
let storage = Storage.sharedInstance()

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -131,7 +132,7 @@ class PrefsViewController: NSViewController {
// reset instantiated storage
if selected != nil && selected?.label == "general" {
CoreDataManager.instance.setDefaultStorage(storage: storage)
Storage.generalUrl = nil
self.storage.generalUrl = nil
}

CoreDataManager.instance.save()
Expand Down Expand Up @@ -283,7 +284,7 @@ class PrefsViewController: NSViewController {
}

func reloadStorage() {
Storage.instance.loadDocuments()
storage.loadDocuments()

self.storageTableView.reload()
self.viewController.updateTable(filter: "") {
Expand Down
3 changes: 2 additions & 1 deletion FSNotes/Preferences/StorageTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import CoreData
class StorageTableView: NSTableView, NSTableViewDataSource,
NSTableViewDelegate {

let storage = Storage.sharedInstance()
var list = [StorageItem]()
let viewController = NSApplication.shared.windows.first!.contentViewController as! ViewController

Expand Down Expand Up @@ -59,7 +60,7 @@ NSTableViewDelegate {
func reload() {
list = CoreDataManager.instance.fetchStorageList()
reloadData()
Storage.instance.loadDocuments()
storage.loadDocuments()
viewController.notesTableView.reloadData()
}

Expand Down
Loading

0 comments on commit ebf4b96

Please sign in to comment.