basically refactored the email viewer
This commit is contained in:
@@ -2,47 +2,41 @@ import asyncio
|
||||
import logging
|
||||
|
||||
from textual import work
|
||||
from textual.logging import TextualHandler
|
||||
from textual.widgets import ListView
|
||||
|
||||
logging.basicConfig(
|
||||
level="NOTSET",
|
||||
handlers=[TextualHandler()],
|
||||
)
|
||||
from apis.himalaya import client as himalaya_client
|
||||
|
||||
|
||||
@work(exclusive=False)
|
||||
async def archive_current(app) -> None:
|
||||
"""Archive the current email message."""
|
||||
try:
|
||||
index = app.current_message_index
|
||||
logging.info("Archiving message ID: " + str(app.current_message_id))
|
||||
process = await asyncio.create_subprocess_shell(
|
||||
f"himalaya message move Archives {app.current_message_id}",
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = await process.communicate()
|
||||
app.show_status(f"{stdout.decode()}", "info")
|
||||
logging.info(stdout.decode())
|
||||
if process.returncode == 0:
|
||||
# Remove the item from the ListView
|
||||
app.query_one(ListView).pop(index)
|
||||
@work(exclusive=True)
|
||||
async def archive_current(app):
|
||||
"""Archive the current message."""
|
||||
if not app.current_message_id:
|
||||
app.show_status("No message selected to archive.", "error")
|
||||
return
|
||||
|
||||
# Find the next message to display using the MessageStore
|
||||
next_id, next_idx = app.message_store.find_next_valid_id(index)
|
||||
# Store the current message ID and index
|
||||
current_message_id = app.current_message_id
|
||||
current_index = app.current_message_index
|
||||
|
||||
# Show the next available message
|
||||
if next_id is not None and next_idx is not None:
|
||||
# Set ListView index first to ensure UI is synchronized
|
||||
app.query_one(ListView).index = next_idx
|
||||
# Now update the current_message_id to trigger content update
|
||||
app.current_message_id = next_id
|
||||
else:
|
||||
# No messages left, just update ListView
|
||||
app.query_one(ListView).index = 0
|
||||
app.reload_needed = True
|
||||
# Find the next message to display after archiving
|
||||
next_id, next_idx = app.message_store.find_next_valid_id(current_index)
|
||||
if next_id is None or next_idx is None:
|
||||
# If there's no next message, try to find a previous one
|
||||
next_id, next_idx = app.message_store.find_prev_valid_id(current_index)
|
||||
|
||||
# Archive the message using our Himalaya client module
|
||||
success = await himalaya_client.archive_message(current_message_id)
|
||||
|
||||
if success:
|
||||
app.show_status(f"Message {current_message_id} archived.", "success")
|
||||
app.message_store.remove_envelope(current_message_id)
|
||||
app.refresh_list_view()
|
||||
|
||||
# Select the next available message if it exists
|
||||
if next_id is not None and next_idx is not None:
|
||||
app.current_message_id = next_id
|
||||
app.current_message_index = next_idx
|
||||
else:
|
||||
app.show_status(f"Error archiving message: {stderr.decode()}", "error")
|
||||
except Exception as e:
|
||||
app.show_status(f"Error: {e}", "error")
|
||||
# If there are no other messages, reset the UI
|
||||
app.current_message_id = 0
|
||||
app.show_status("No more messages available.", "warning")
|
||||
else:
|
||||
app.show_status(f"Failed to archive message {current_message_id}.", "error")
|
||||
|
||||
Reference in New Issue
Block a user