33import aiohttp
44import webbrowser
55from customtkinter import *
6- import requests
7- from PIL import Image , ImageTk , ImageSequence
6+ from PIL import Image , ImageSequence
87import os
98
109from ...libs import lib
1110from ...libs .lib import CONFIG_FILE
1211
1312
13+ async def update_async (latest_release ):
14+ # Scarica l'aggiornamento dal repository su GitHub
15+ async with aiohttp .ClientSession () as session :
16+ async with session .get (latest_release ['zipball_url' ]) as response :
17+ with open ('latest_release.zip' , 'wb' ) as f :
18+ f .write (await response .read ())
19+
20+ # Aggiorna il file di configurazione locale con la nuova versione
21+ lib .update_key_json (CONFIG_FILE , "version" , latest_release ['tag_name' ])
22+
23+ # Avvia l'applicazione aggiornata
24+ os .startfile ('latest_release.zip' )
25+
26+
1427class UpdatePage (CTkFrame ):
15- def __init__ (self , master , app ):
16- self .app = app
28+ def __init__ (self , master , app , loop : asyncio .AbstractEventLoop ):
29+ from ..main_app import App
30+ self .loop = loop
31+ self .app : App = app
1732 super ().__init__ (master )
1833
1934 self .grid_rowconfigure ((0 , 1 , 2 , 3 ), weight = 1 )
@@ -22,7 +37,7 @@ def __init__(self, master, app):
2237 self .check_updates_button = CTkButton (
2338 self ,
2439 text = "Controlla aggiornamenti" ,
25- command = lambda : asyncio . ensure_future ( self .check_for_updates ())
40+ command = self .check_for_updates_thread
2641 )
2742 self .check_updates_button .grid (row = 0 , column = 0 , padx = 20 , pady = 10 )
2843
@@ -34,34 +49,43 @@ def __init__(self, master, app):
3449 self .version .grid (
3550 row = 1 , column = 0 , columnspan = 2 , padx = 5 ,)
3651
37- # Crea un'immagine GIF animata e ottieni il frame iniziale
3852 self .loading_gif = Image .open (lib .get_image_path ('loading.gif' ))
39- self .loading_gif_frames = [ImageTk .PhotoImage (
40- frame ) for frame in ImageSequence .Iterator (self .loading_gif )]
53+ self .loading_gif_frames = [
54+ Image .frombytes (
55+ self .loading_gif .mode , self .loading_gif .size , frame .tobytes ())
56+ for frame in ImageSequence .Iterator (self .loading_gif )
57+ ]
58+
59+ def animate_loading_gif (self ):
60+ self .check_updates_button .configure (image = CTkImage (
61+ light_image = self .loading_gif_frames [self .loading_gif_index ],
62+ size = (30 , 30 )
63+ ))
64+ self .loading_gif_index = (
65+ self .loading_gif_index + 1 ) % len (self .loading_gif_frames )
66+ self .loading_gif_animation_id = self .after (
67+ 100 , self .animate_loading_gif )
4168
4269 def update_button_state (self , state ):
4370 if state == "loading" :
44- self .check_updates_button .config (
71+ self .check_updates_button .configure (
4572 text = "Caricamento in corso..." , state = "disabled" )
46- self .loading_gif_index = 0
73+ self .loading_gif_index = 1
4774 self .animate_loading_gif ()
4875 elif state == "loaded" :
49- self .check_updates_button .config (
76+ self .check_updates_button .configure (
5077 text = "Controlla aggiornamenti" , state = "normal" )
5178 self .after_cancel (self .loading_gif_animation_id )
5279
53- def animate_loading_gif (self ):
54- self .check_updates_button .config (
55- image = self .loading_gif_frames [self .loading_gif_index ])
56- self .loading_gif_index = (
57- self .loading_gif_index + 1 ) % len (self .loading_gif_frames )
58- self .loading_gif_animation_id = self .after (
59- 100 , self .animate_loading_gif )
60-
6180 async def open_github (self ):
6281 url = f'https://api.github.com/repos/{ lib .get_key_value_json (CONFIG_FILE , "repo_owner" )} /{ lib .get_key_value_json (CONFIG_FILE , "repo_name" )} /releases/latest'
6382 webbrowser .open_new_tab (url )
6483
84+ def check_for_updates_thread (self ):
85+ print ("check_for_updates_thread" )
86+ # self.loop.create_task(self.check_for_updates())
87+ self .loop .run_until_complete (self .check_for_updates ())
88+
6589 async def check_for_updates (self ):
6690 current_version = lib .get_key_value_json (CONFIG_FILE , "version" )
6791
@@ -82,28 +106,17 @@ async def check_for_updates(self):
82106 self ,
83107 text = "Aggiorna ora" ,
84108 command = lambda : asyncio .ensure_future (
85- self . update_async (latest_release ))
109+ update_async (latest_release ))
86110 )
87111 self .update_button .grid (
88112 row = 2 , column = 0 , padx = 20 , pady = 10 )
89113
90114 else :
115+
91116 # Gestione dell'errore
92117 messagebox .showerror (
93118 "Error" , "Nessun aggiornamento trovato." )
119+ self .update_button_state ("loaded" )
94120 # Riabilita il bottone di aggiornamento e nasconde l'icona animata
95- self .check_updates_button .config (state = 'normal' )
96- self .check_updates_button .config (image = '' )
97-
98- async def update_async (self , latest_release ):
99- # Scarica l'aggiornamento dal repository su GitHub
100- async with aiohttp .ClientSession () as session :
101- async with session .get (latest_release ['zipball_url' ]) as response :
102- with open ('latest_release.zip' , 'wb' ) as f :
103- f .write (await response .read ())
104-
105- # Aggiorna il file di configurazione locale con la nuova versione
106- lib .update_key_json (CONFIG_FILE , "version" , latest_release ['tag_name' ])
107-
108- # Avvia l'applicazione aggiornata
109- os .startfile ('latest_release.zip' )
121+ self .check_updates_button .configure (state = 'normal' )
122+ self .check_updates_button .configure (image = '' )
0 commit comments