From 439aac2702d99435e4bb23c720da2c66c518ef2f Mon Sep 17 00:00:00 2001
From: doc-code <bellini.francescob@gmail.com>
Date: Wed, 28 Aug 2024 19:26:46 +0200
Subject: [PATCH] Fix preference persistence by marking preferences as dirty,
 Used poll to disable operators instead of hiding them

---
 addon/classes/ots.py | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/addon/classes/ots.py b/addon/classes/ots.py
index af9a8fb..65aa024 100644
--- a/addon/classes/ots.py
+++ b/addon/classes/ots.py
@@ -33,8 +33,7 @@ class CustomTemplatesPreferences(AddonPreferences):
         col.menu("CT_MT_export", icon='DOWNARROW_HLT', text="")
         col.separator()
         col.operator("ct.add", icon='ADD', text="")
-        if self.projects:
-            col.operator("ct.remove", icon='REMOVE', text="")
+        col.operator("ct.remove", icon='REMOVE', text="")
         col.separator()
         col.operator("ct.move_up", icon='TRIA_UP', text="")
         col.operator("ct.move_down", icon='TRIA_DOWN', text="")
@@ -85,6 +84,10 @@ class CT_OT_export_templates(bpy.types.Operator):
     def invoke(self, context, event):
         context.window_manager.fileselect_add(self)
         return {'RUNNING_MODAL'}
+    
+    @classmethod
+    def poll(cls, context):
+        return len(context.preferences.addons[base_package].preferences.projects) > 0
 
 class CT_OT_import_templates(bpy.types.Operator):
     bl_idname = "ct.import_templates"
@@ -106,6 +109,7 @@ class CT_OT_import_templates(bpy.types.Operator):
                 item.name = project["name"]
                 item.path = project["path"]
             prefs.active_template_index = 0
+            context.preferences.is_dirty = True
 
             self.report({'INFO'}, f"Projects imported from {self.filepath}")
         else:
@@ -126,6 +130,7 @@ class CT_OT_add(Operator):
         prefs = context.preferences.addons[base_package].preferences
         prefs.projects.add()
         prefs.active_template_index = len(prefs.projects) - 1
+        context.preferences.is_dirty = True
         return {'FINISHED'}
 
 class CT_OT_remove(Operator):
@@ -138,7 +143,12 @@ class CT_OT_remove(Operator):
         prefs.projects.remove(prefs.active_template_index)
         prefs.active_template_index = min(
             max(0, prefs.active_template_index - 1), len(prefs.projects) - 1)
+        context.preferences.is_dirty = True
         return {'FINISHED'}
+    
+    @classmethod
+    def poll(cls, context):
+        return len(context.preferences.addons[base_package].preferences.projects) > 0
 
 class CT_OT_move_up(Operator):
     bl_idname = "ct.move_up"
@@ -152,10 +162,14 @@ class CT_OT_move_up(Operator):
         if index > 0:
             prefs.projects.move(index, index - 1)
             prefs.active_template_index -= 1
+            context.preferences.is_dirty = True
         else:
             self.report({'WARNING'}, "Template is already at the top")
-
         return {'FINISHED'}
+    
+    @classmethod
+    def poll(cls, context):
+        return len(context.preferences.addons[base_package].preferences.projects) > 0
 
 class CT_OT_move_down(Operator):
     bl_idname = "ct.move_down"
@@ -169,10 +183,14 @@ class CT_OT_move_down(Operator):
         if index < len(prefs.projects) - 1:
             prefs.projects.move(index, index + 1)
             prefs.active_template_index += 1
+            context.preferences.is_dirty = True
         else:
             self.report({'WARNING'}, "Template is already at the bottom")
-
         return {'FINISHED'}
+    
+    @classmethod
+    def poll(cls, context):
+        return len(context.preferences.addons[base_package].preferences.projects) > 0
 
 # Popups of File > Defaults
 def already_present(self, prefs, path):
@@ -199,6 +217,7 @@ class CT_OT_add_template_popup(Operator):
                 new_project = prefs.projects.add()
                 new_project.name = self.project_name
                 new_project.path = current_file_path
+                context.preferences.is_dirty = True
             else:
                 self.report({'ERROR'}, "Current file is not saved on disk.")
         return {'FINISHED'}
@@ -220,6 +239,7 @@ class CT_OT_select_template_popup(Operator):
             new_project = prefs.projects.add()
             new_project.name = self.project_name
             new_project.path = self.project_path
+            context.preferences.is_dirty = True
         return {'FINISHED'}
 
     def invoke(self, context, event):