Fix save confirmation bug, UX refinements
authorJakob Cornell <jakob+gpg@jcornell.net>
Mon, 13 Jan 2020 01:46:25 +0000 (19:46 -0600)
committerJakob Cornell <jakob+gpg@jcornell.net>
Mon, 13 Jan 2020 01:46:25 +0000 (19:46 -0600)
main.py

diff --git a/main.py b/main.py
index 8ceae69093b156e15b96f81a3f54eaf4fe7d9d7a..14e9c26686909e953def5c13d518ff5e846547a6 100644 (file)
--- a/main.py
+++ b/main.py
@@ -129,7 +129,7 @@ class MainModel(Model):
                self.state_mgr = state_mgr
                state = self.state_mgr.load()
                self.state = state or new_app_state()
-               if self.state['student'] is not None and self.state['student'] not in self.data:
+               if self.state['student'] not in self.data:
                        self.state['student'] = None
 
                self.saved = False
@@ -137,6 +137,8 @@ class MainModel(Model):
 
        def on_linked(self):
                self.view.set_student_list(self.view_names())
+               if self.state['student'] is None and self.data:
+                       self.select_auto()
                self.view.set_student(self.state['student'])
 
        def view_names(self):
@@ -146,14 +148,14 @@ class MainModel(Model):
                if name in self.data:
                        raise ValueError()
                else:
-                       self.data[name] = []
                        self.dirty = True
+                       self.data[name] = []
                        self.view.set_student_list(self.view_names())
                        self.view.update_controls()
 
        def delete_student(self, name):
-               del self.data[name]
                self.dirty = True
+               del self.data[name]
                self.view.set_student_list(self.view_names())
                if self.state['student'] == name:
                        self.select_auto()
@@ -161,6 +163,7 @@ class MainModel(Model):
 
        def rename_student(self, curr_name, new_name):
                if new_name != curr_name:
+                       self.dirty = True
                        if new_name in self.data:
                                raise ValueError("Name already in use")
                        else:
@@ -207,14 +210,23 @@ class MainView(View):
                self.root.title("Tutoring Tool")
                self.root.resizable(False, False)
 
+               def on_save_err():
+                       messagebox.showerror("Error", "Error saving data")
+
                def on_close():
+                       self.controller.save_state()
                        if self.editor_views:
                                next(iter(self.editor_views.values())).root.lift()
                        elif self.controller.dirty:
                                result = messagebox.askyesnocancel("Confirm", "You have unsaved changes. Save before exiting?")
                                if result is True:
-                                       self.controller.save_state()
-                               if result is not None:
+                                       try:
+                                               self.controller.save_data()
+                                       except:
+                                               on_save_err()
+                                       else:
+                                               self.root.destroy()
+                               elif result is False:
                                        self.root.destroy()
                        else:
                                self.root.destroy()
@@ -224,7 +236,7 @@ class MainView(View):
                        try:
                                self.controller.save_data()
                        except:
-                               messagebox.showerror("Error", "Error saving data")
+                               on_save_err()
                menu = tk.Menu(self.root)
                self.root.config(menu = menu)
                file_menu = tk.Menu(menu, tearoff = 0)
@@ -243,7 +255,7 @@ class MainView(View):
                self.student_sel.configure(width = 25)
 
                def on_student_add():
-                       result = simpledialog.askstring("New student", "Name")
+                       result = simpledialog.askstring("New student", "Name:")
                        if result:
                                try:
                                        self.controller.add_student(result)
@@ -253,7 +265,10 @@ class MainView(View):
 
                def on_student_rename():
                        curr = self.controller.current_student()
-                       name = simpledialog.askstring("Rename student", "New name for {}".format(curr))
+                       name = simpledialog.askstring(
+                               "Rename student", "New name for {}:".format(curr),
+                               initialvalue = curr,
+                       )
                        if name:
                                try:
                                        self.controller.rename_student(curr, name)
@@ -568,8 +583,9 @@ class DataEditView(View):
 
 class DataEditController(Controller):
        def update_data(self, records):
+               differ = records != self.base_model.data[self.student_name]
                self.base_model.data[self.student_name] = records
-               self.base_model.dirty = True
+               self.base_model.dirty = differ
 
 
 class VertScrollingFrame(tk.Frame):