Skip to content

Commit

Permalink
Fixed creation and deletion (added new extra fields for note)
Browse files Browse the repository at this point in the history
  • Loading branch information
scbtest committed May 4, 2014
1 parent 85c4c1a commit a722545
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions quick_simplenote.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def handle_open_filename_change(old_file_path, updated_note):
close_view(view)
open_note(updated_note)
sublime.active_window().focus_view(old_active)
try:
remove(old_file_path)
except OSError as e:
pass

def close_view(view):
view.set_scratch(True)
Expand All @@ -125,6 +129,19 @@ def update_note(existing_note, updated_note):
existing_note['needs_update'] = False
existing_note['filename'] = get_filename_for_note(existing_note)

def load_notes():
notes = []
try:
with open(path.join(package_path, 'note_cache'),'rb') as cache_file:
notes = pickle.load(cache_file)
except (EOFError, IOError) as e:
pass
return notes

def save_notes(notes):
cache_file = open(path.join(package_path, 'note_cache'),'w+b')
pickle.dump(notes, cache_file)

class OperationManager:
_instance = None
def __new__(cls, *args, **kwargs):
Expand Down Expand Up @@ -191,6 +208,7 @@ def handle_note_changed(self, modified_note_resume):
handle_open_filename_change(self.old_file_path, note)
break
notes.sort(key=cmp_to_key(sort_notes), reverse=True)
save_notes(notes)

def on_post_save(self, view):
self.current_view = view
Expand Down Expand Up @@ -230,19 +248,6 @@ def run(self):
import pickle
class StartQuickSimplenoteCommand(sublime_plugin.ApplicationCommand):

def load_notes(self):
notes = []
try:
with open(path.join(package_path, 'note_cache'),'rb') as cache_file:
notes = pickle.load(cache_file)
except (EOFError, IOError) as e:
pass
return notes

def save_notes(self, notes):
cache_file = open(path.join(package_path, 'note_cache'),'w+b')
pickle.dump(notes, cache_file)

def set_result(self, new_notes):
global notes
notes = new_notes
Expand Down Expand Up @@ -288,7 +293,7 @@ def merge_delta(self, updated_note_resume, existing_notes):
for deleted_note in deleted_notes:
existing_notes.remove(deleted_note)

self.save_notes(existing_notes)
save_notes(existing_notes)
self.notes_synch(existing_notes)

def notes_synch(self, notes):
Expand Down Expand Up @@ -393,7 +398,7 @@ def merge_notes(self, updated_notes, existing_notes):
if note['key'] == updated_note['key']:
update_note(note, updated_note)

self.save_notes(existing_notes)
save_notes(existing_notes)
self.set_result(existing_notes)

def run(self):
Expand All @@ -402,7 +407,7 @@ def run(self):
if not path.exists(temp_path):
makedirs(temp_path)

existing_notes = self.load_notes()
existing_notes = load_notes()

for f in listdir(temp_path):
remove(path.join(temp_path, f))
Expand All @@ -417,8 +422,10 @@ class CreateQuickSimplenoteNoteCommand(sublime_plugin.ApplicationCommand):
def handle_new_note(self, result):
if result:
global notes
update_note(result, result)
notes.append(result)
notes.sort(key=cmp_to_key(sort_notes), reverse=True)
save_notes(notes)
show_message('QuickSimplenote: Done')
sublime.set_timeout(remove_status, 2000)
open_note(result)
Expand All @@ -433,7 +440,11 @@ class DeleteQuickSimplenoteNoteCommand(sublime_plugin.ApplicationCommand):
def handle_deletion(self, result):
global notes
notes.remove(self.note)
remove(get_path_for_note(self.note))
save_notes(notes)
try:
remove(get_path_for_note(self.note))
except OSError as e:
pass
close_view(self.note_view)

def run(self):
Expand Down

0 comments on commit a722545

Please sign in to comment.