Skip to content

Commit

Permalink
Improve database schema and usability
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaublitz committed Sep 14, 2024
1 parent 9c9925d commit 2772ee7
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 160 deletions.
6 changes: 3 additions & 3 deletions language-practice
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ Inflection charts are pulled from wiktionary.
import argparse
import sys

from language_practice.gui import GuiApplication
from language_practice.sqlite import SqliteHandle
from language_practice.terminal import TerminalApplication


class Once(argparse.Action):
Expand Down Expand Up @@ -53,9 +51,11 @@ def main():
try:
all_sets = handle.get_all_sets()
if args.gui:
gui = GuiApplication(handle, all_sets)
from language_practice.gui import GuiApplication # pylint: disable=import-outside-toplevel
gui = GuiApplication(handle, all_sets, application_id="me.jbaublitz.LanguagePractice")
gui.run()
else:
from language_practice.terminal import TerminalApplication # pylint: disable=import-outside-toplevel
tui = TerminalApplication(handle, all_sets)
tui.run()
except Exception as err: # pylint: disable=broad-exception-caught
Expand Down
4 changes: 2 additions & 2 deletions language_practice/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(
aspect: str | None,
usage: str | None,
part_of_speech: str | None,
charts: list[list[list[str]]] | None,
charts: list[list[str]] | None,
repetition: WordRepetition,
):
self.word = word
Expand Down Expand Up @@ -72,7 +72,7 @@ def get_part_of_speech(self) -> str | None:
"""
return self.part_of_speech

def get_charts(self) -> list[list[list[str]]] | None:
def get_charts(self) -> list[list[str]] | None:
"""
Get charts.
"""
Expand Down
76 changes: 48 additions & 28 deletions language_practice/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# pylint: disable=too-few-public-methods

import asyncio
from sqlite3 import IntegrityError
import tomllib
from typing import Self

Expand Down Expand Up @@ -62,10 +63,8 @@ def __init__(

self.flashcard_set_grid = FlashcardSetGrid()
for flashcard_set in flashcard_sets:
delete_button = Gtk.Button(label="Delete")
delete_button.connect("clicked", self.delete_flashcard_set)
self.flashcard_set_grid.add_row(
Gtk.CheckButton(), Gtk.Label.new(flashcard_set), delete_button
Gtk.CheckButton(), Gtk.Label.new(flashcard_set)
)
scrollable = Gtk.ScrolledWindow()
scrollable.set_size_request(700, 600)
Expand All @@ -75,6 +74,9 @@ def __init__(
import_button = Gtk.Button(label="Import")
import_button.connect("clicked", self.import_button)
button_hbox.append(import_button)
delete_button = Gtk.Button(label="Delete")
delete_button.connect("clicked", self.delete_flashcard_set)
button_hbox.append(delete_button)
select_all_button = Gtk.Button(label="Select all")
select_all_button.connect("clicked", self.flashcard_set_grid.select_all)
button_hbox.append(select_all_button)
Expand All @@ -96,12 +98,18 @@ def import_button(self, button: Gtk.Button):
file_dialog = Gtk.FileDialog()
file_dialog.open_multiple(callback=self.handle_files)

# pylint: disable=unused-argument
def delete_flashcard_set(self, button: Gtk.Button):
"""
Handle deleting flashcard set on button press.
"""
self.handle.delete_set(button.get_prev_sibling().get_text())
self.flashcard_set_grid.delete_row(button)
selected = self.flashcard_set_grid.get_selected()
selected.sort(reverse=True, key=lambda info: info[1])
for text, row in selected:
set_id = self.handle.get_id_from_file_name(text)
if set_id is not None:
self.handle.delete_set(set_id)
self.flashcard_set_grid.delete_row(row)

def handle_files(self, dialog: Gtk.FileDialog, task: Gio.Task):
"""
Expand All @@ -111,20 +119,34 @@ def handle_files(self, dialog: Gtk.FileDialog, task: Gio.Task):
for current_import in self.imports:
try:
toml = TomlConfig(current_import)
except tomllib.TOMLDecodeError:
except tomllib.TOMLDecodeError as err:
dialog = Gtk.AlertDialog()
dialog.set_message(f"{current_import}: {err}")
dialog.set_modal(True)
dialog.choose()
continue

self.handle.import_set(
current_import,
toml,
asyncio.run(scrape(toml.get_words(), toml.get_lang())),
)
try:
new = self.handle.import_set(
current_import,
toml,
asyncio.run(scrape(toml.get_words(), toml.get_lang())),
)
except IntegrityError as err:
dialog = Gtk.AlertDialog()
dialog.set_message(f"{current_import}: {err}")
dialog.set_modal(True)
dialog.choose()
set_id = self.handle.get_id_from_file_name(current_import)
if set_id is not None:
self.handle.delete_set(set_id)
continue

delete_button = Gtk.Button(label="Delete")
delete_button.connect("clicked", self.delete_flashcard_set)
self.flashcard_set_grid.add_row(
Gtk.CheckButton(), Gtk.Label.new(current_import), delete_button
)
if new:
self.flashcard_set_grid.add_row(
Gtk.CheckButton(),
Gtk.Label.new(current_import),
)
self.imports = []

# pylint: disable=unused-argument
Expand All @@ -134,11 +156,11 @@ def handle_start(self, button: Gtk.Button):
"""
files = self.flashcard_set_grid.get_selected()
config = None
for file in files:
for text, _ in files:
if config is None:
config = self.handle.load_config(file)
config = self.handle.load_config(text)
else:
config = config.extend(self.handle.load_config(file))
config = config.extend(self.handle.load_config(text))

if config is not None:
self.flashcard = Flashcard(self.handle, config.get_words())
Expand Down Expand Up @@ -167,23 +189,20 @@ def __init__(self, *args, **kwargs):
self.set_column_spacing(10)
self.num_rows = 0

def add_row(
self, checkbox: Gtk.CheckButton, label: Gtk.Label, delete_button: Gtk.Button
):
def add_row(self, checkbox: Gtk.CheckButton, label: Gtk.Label):
"""
Add a row to the grid.
"""
self.attach(checkbox, 0, self.num_rows, 1, 1)
self.attach(label, 1, self.num_rows, 1, 1)
self.attach(delete_button, 2, self.num_rows, 1, 1)
self.num_rows += 1

def delete_row(self, contains_child: Gtk.Button):
def delete_row(self, row):
"""
Delete a row from the grid.
"""
info = self.query_child(contains_child)
self.remove_row(info.row)
self.remove_row(row)
self.num_rows -= 1

# pylint: disable=unused-argument
def select_all(self, button: Gtk.Button):
Expand All @@ -194,14 +213,14 @@ def select_all(self, button: Gtk.Button):
self.get_child_at(0, row).set_active(True)

# pylint: disable=unused-argument
def get_selected(self) -> list[str]:
def get_selected(self) -> list[tuple[str, int]]:
"""
Get all selected flashcard sets.
"""
files = []
for row in range(self.num_rows):
if self.get_child_at(0, row).get_active():
files.append(self.get_child_at(1, row).get_text())
files.append((self.get_child_at(1, row).get_text(), row))

return files

Expand All @@ -213,6 +232,7 @@ class StudyWindow(Gtk.ApplicationWindow):

def __init__(self, flashcard: Flashcard, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_title("Language Practice")
self.flashcard = flashcard

(self.peek, self.is_review) = self.flashcard.current()
Expand Down
Loading

0 comments on commit 2772ee7

Please sign in to comment.