Fixed the program stop bug. Adapted webpic script for multi-tasking + added comments

This commit is contained in:
2022-09-04 15:28:28 +02:00
parent 7002439532
commit 03a6d9b54f
6 changed files with 267 additions and 172 deletions

View File

@@ -17,14 +17,15 @@ class HomeController:
__main_controller = None
__view = None
__webpic = None
__download_task = None
# Constructor
def __init__(self, controller: MainController, webpic) -> None:
"""
Constructor
:controller: -> The main application cpntroller.
:webpic: -> The webpicdownloader instance.
* :controller: -> The main application cpntroller.
* :webpic: -> The webpicdownloader instance.
"""
# Setub variables
self.__main_controller = controller
@@ -39,7 +40,7 @@ class HomeController:
[function for view]
=> Define the view of this controller.
:view: -> The view that this controller manage.
* :view: -> The view that this controller manage.
"""
self.__view = view
self.__webpic.set_messenger_callback(view.add_log)
@@ -51,7 +52,7 @@ class HomeController:
[event function for view]
=> Call this event method when the user requests to change the window.
:frame: -> The frame we want to launch.
* :frame: -> The frame we want to launch.
"""
self.__main_controller.change_frame(frame)
@@ -60,20 +61,16 @@ class HomeController:
[event function for view]
=> Call this event method when the user requests to download
:url: -> The url of the website to use for pic-download.\n
:name: -> The name of the folder in which put pictures.
* :url: -> The url of the website to use for pic-download.
* :name: -> The name of the folder in which put pictures.
"""
# Define the download task function (to call in a AsyncTask)
def download_task():
self.__view.clear_logs()
if self.__webpic.download(url, name):
self.__view.show_success_message("The download has been successfully completed.")
else:
self.__view.show_error_message("A critical error preventing the download occurred, check the logs.")
# Verify variable and start AsyncTask
if url.strip() and name.strip() :
AsyncTask(download_task)
if url.strip() and name.strip():
self.__download_task = AsyncTask(
rcallback=self.__async_task_start,
rargs=(url, name),
qcallback=self.__async_task_stop
)
self.__download_task.start()
else:
self.__view.show_error_message("Opss, the url or folder name are not valid!")
# END View events
@@ -84,5 +81,35 @@ class HomeController:
[event function for controller]
=> Call this event when a request to exit is thrown.
"""
print("Quit... homecontroller")
# END Controller methods
self.__download_task.stop()
print("Quit... homecontroller END")
# END Controller methods
# START Task methods
def __async_task_start(self, url, name) -> None:
"""
[CallBack start function]
=> Start Callback function for asynctask, be careful once executed in asynctask this
function will keep its controller context. In short it's as if the thread was
launched in the controller and the execution never left it.
* :url: -> Url for webpic.
* :name: -> Working dir name for webpic.
"""
print("start callback called")
self.__view.clear_logs()
if self.__webpic.download(url, name):
self.__view.show_success_message("The download has been successfully completed.")
else:
self.__view.show_error_message("A critical error preventing the download occurred, check the logs.")
def __async_task_stop(self) -> None:
"""
[CallBack stop function]
=> End Callback function for asynctask, be careful once executed in asynctask this
function will keep its controller context. In short it's as if the thread was
launched in the controller and the execution never left it.
"""
print("stop callback called")
self.__webpic.stop()
# END Task methods

View File

@@ -46,11 +46,12 @@ class MainController:
def on_quite(self) -> None:
"""
[event function for view]
=> Event launch when you ask to quit the program.
=> Event launch when you ask to quit the program. This event is propagated
to the subscribers, they can eventually cancel the event
"""
for callback in self.__quite_event_subscribers:
callback()
print("on_quite") # TODO remove
if callback():
return
self.__view.close_window() # End the program
def on_check_for_update(self) -> None: