Skip to content

Commit

Permalink
feat: show quick panel after first sync
Browse files Browse the repository at this point in the history
Refactored the note listing and synchronization process to enhance user experience.

- Simplified the quick panel display for notes, providing a cleaner interface.
- Improved note synchronization by handling first-sync scenarios and incorporating a callback mechanism for post-sync actions.
- Introduced a "sync_note_number" setting to control the number of notes fetched during synchronization, enhancing efficiency.

This change streamlines the note management process, providing a more intuitive and efficient workflow for users.
  • Loading branch information
RedAtman committed Aug 1, 2024
1 parent 6519a8d commit e44b613
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 64 deletions.
55 changes: 9 additions & 46 deletions commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ._config import CONFIG
from .lib.core import start
from .lib.gui import clear_orphaned_filepaths, close_view, on_note_changed, open_view, show_message
from .lib.gui import close_view, on_note_changed, open_view, show_message, show_quick_panel
from .lib.models import Note
from .lib.operations import NoteCreator, NoteDeleter, NotesIndicator, NoteUpdater, OperationManager

Expand All @@ -34,7 +34,6 @@ class SimplenoteViewCommand(sublime_plugin.EventListener):

@cached_property
def autosave_debounce_time(self) -> int:
logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))
settings = sublime.load_settings(CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH)
_autosave_debounce_time = settings.get("autosave_debounce_time", 1)
if not isinstance(_autosave_debounce_time, int):
Expand Down Expand Up @@ -123,53 +122,12 @@ def on_post_save(self, view: sublime.View):

class SimplenoteListCommand(sublime_plugin.ApplicationCommand):

def on_select(self, selected_index: int):
if selected_index == -1:
return
note_id = self.list__modificationDate[selected_index]
selected_note = Note.tree.find(note_id)
if not isinstance(selected_note, Note):
show_message("Note not found: note id(%s), Please restart simplenote or sublime text." % note_id)
return
filepath = selected_note.open()
selected_note.flush()
view = open_view(filepath)

def run(self):
global SIMPLENOTE_STARTED
if not SIMPLENOTE_STARTED:
if not start():
return

if Note.tree.count <= 0:
show_message(
"No notes found. Please wait for the synchronization to complete, or press [super+shift+s, super+shift+c] to create a note."
)
self.list__modificationDate: List[float] = []
self.list__title: List[str] = []
list__filename: List[str] = []
for note in Note.tree.iter(reverse=True):
if not isinstance(note, Note):
raise Exception("note is not a Note: %s" % type(note))
if note.d.deleted == True:
continue
self.list__modificationDate.append(note.d.modificationDate)
self.list__title.append(note.title)
list__filename.append(note.filename)

# TODO: Maybe doesn't need to run every time
clear_orphaned_filepaths(list__filename)

def show_panel():
sublime.active_window().show_quick_panel(
self.list__title,
self.on_select,
flags=sublime.KEEP_OPEN_ON_FOCUS_LOST,
# on_highlight=self.on_select,
placeholder="Select Note press key 'enter' to open",
)

sublime.set_timeout(show_panel, 50)
show_quick_panel()


class SimplenoteSyncCommand(sublime_plugin.ApplicationCommand):
Expand All @@ -179,14 +137,19 @@ def merge_note(self, updated_notes: List[Note]):
if note.need_flush:
on_note_changed(note)

def run(self):
def callback(self, updated_notes: List[Note], first_sync: bool = False):
self.merge_note(updated_notes)
if first_sync:
show_quick_panel(first_sync)

def run(self, first_sync: bool = False):
settings = sublime.load_settings(CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH)
sync_note_number = settings.get("sync_note_number", 1000)
if not isinstance(sync_note_number, int):
show_message("`sync_note_number` must be an integer. Please check settings file.")
return
note_indicator = NotesIndicator(sync_note_number=sync_note_number)
note_indicator.set_callback(self.merge_note)
note_indicator.set_callback(self.callback, {"first_sync": first_sync})
OperationManager().add_operation(note_indicator)


Expand Down
10 changes: 5 additions & 5 deletions lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

logger = logging.getLogger()
SIMPLENOTE_STARTED = False
manager = OperationManager()


def sync():
manager = OperationManager()
def sync(first_sync: bool = False):
if not manager.running:
sublime.run_command("simplenote_sync")
sublime.run_command("simplenote_sync", {"first_sync": first_sync})
else:
logger.debug("Sync omitted")

Expand All @@ -36,11 +36,11 @@ def start():
password = settings.get("password")

if username and password:
sync()
sync(first_sync=True)
SIMPLENOTE_STARTED = True
else:
edit_settings()
show_message("Simplenote: Please configure username/password, Please check settings file.")
show_message("Simplenote: Please configure username/password in settings file.")
sublime.set_timeout(remove_status, 2000)
SIMPLENOTE_STARTED = False
return SIMPLENOTE_STARTED
51 changes: 51 additions & 0 deletions lib/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"close_view",
"clear_orphaned_filepaths",
"on_note_changed",
"show_quick_panel",
]


Expand Down Expand Up @@ -121,3 +122,53 @@ def on_note_changed(note: Note):
old_window.focus_view(old_active_view)

sublime.set_timeout(partial(new_view.run_command, "revert"), 0)


def on_select(list__modificationDate: List[float], selected_index: int):
if selected_index == -1:
return
note_id = list__modificationDate[selected_index]
selected_note = Note.tree.find(note_id)
if not isinstance(selected_note, Note):
show_message("Note not found: note id(%s), Please restart simplenote or sublime text." % note_id)
return
filepath = selected_note.open()
selected_note.flush()
view = open_view(filepath)


def show_quick_panel(first_sync: bool = False):
logger.warning("show_quick_panel")
if Note.tree.count <= 0:
show_message(
"No notes found. Please wait for the synchronization to complete, or press [super+shift+s, super+shift+c] to create a note."
)
list__modificationDate: List[float] = []
list__title: List[str] = []
list__filename: List[str] = []
for note in Note.tree.iter(reverse=True):
if not isinstance(note, Note):
raise Exception("note is not a Note: %s" % type(note))
if note.d.deleted == True:
continue
list__modificationDate.append(note.d.modificationDate)
list__title.append(note.title)
list__filename.append(note.filename)

# TODO: Maybe doesn't need to run every time
clear_orphaned_filepaths(list__filename)

placeholder = "Select Note press key 'enter' to open"
if first_sync:
placeholder = "Sync complete. Press [super+shift+s] [super+shift+l] to display the note list again."

def show_panel():
sublime.active_window().show_quick_panel(
list__title,
partial(on_select, list__modificationDate),
flags=sublime.MONOSPACE_FONT,
# on_highlight=self.on_select,
placeholder=placeholder,
)

sublime.set_timeout(show_panel, 500)
25 changes: 12 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import functools
import logging

import sublime

from ._config import CONFIG
from .lib.core import start
from .lib.core import start, sync
from .lib.models import Note


Expand All @@ -12,7 +12,7 @@
SIMPLENOTE_RELOAD_CALLS = -1


def reload_if_needed():
def reload_if_needed(autostart: bool = True):
# global SIMPLENOTE_RELOAD_CALLS

# # Sublime calls this twice for some reason :(
Expand All @@ -21,28 +21,27 @@ def reload_if_needed():
# logger.debug("Simplenote Reload call %s" % SIMPLENOTE_RELOAD_CALLS)
# return

logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))
settings = sublime.load_settings("Simplenote.sublime-settings")
autostart = settings.get("autostart")
if bool(autostart):
autostart = True
logger.debug(("Simplenote Reloading", autostart))
start()
if autostart:
sublime.set_timeout(start, 2000)
sublime.set_timeout_async(sync, 2000)
logger.debug("Auto Starting")


def plugin_loaded():
# load_notes()
logger.debug(("Loaded notes number: ", Note.tree.count))

logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))
settings = sublime.load_settings("Simplenote.sublime-settings")
# logger.debug(("SETTINGS.__dict__: ", SETTINGS.__dict__))
# logger.debug(("SETTINGS.username: ", SETTINGS.get("username")))
autostart = settings.get("autostart", True)
if not isinstance(autostart, bool):
autostart = True
callback = functools.partial(reload_if_needed, autostart)
settings.clear_on_change("username")
settings.clear_on_change("password")
settings.add_on_change("username", reload_if_needed)
settings.add_on_change("password", reload_if_needed)
settings.add_on_change("username", callback)
settings.add_on_change("password", callback)

reload_if_needed()
reload_if_needed(autostart=autostart)

0 comments on commit e44b613

Please sign in to comment.