1+ import asyncio
12from tkinter import messagebox
3+ import aiohttp
24import webbrowser
35from customtkinter import *
46import requests
7+ from PIL import Image , ImageTk , ImageSequence
8+ import os
59
610from ...libs import lib
711from ...libs .lib import CONFIG_FILE
@@ -18,7 +22,7 @@ def __init__(self, master, app):
1822 self .check_updates_button = CTkButton (
1923 self ,
2024 text = "Controlla aggiornamenti" ,
21- command = self .check_for_updates
25+ command = lambda : asyncio . ensure_future ( self .check_for_updates ())
2226 )
2327 self .check_updates_button .grid (row = 0 , column = 0 , padx = 20 , pady = 10 )
2428
@@ -30,50 +34,76 @@ def __init__(self, master, app):
3034 self .version .grid (
3135 row = 1 , column = 0 , columnspan = 2 , padx = 5 ,)
3236
33- def open_github (self ):
37+ # Crea un'immagine GIF animata e ottieni il frame iniziale
38+ 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 )]
41+
42+ def update_button_state (self , state ):
43+ if state == "loading" :
44+ self .check_updates_button .config (
45+ text = "Caricamento in corso..." , state = "disabled" )
46+ self .loading_gif_index = 0
47+ self .animate_loading_gif ()
48+ elif state == "loaded" :
49+ self .check_updates_button .config (
50+ text = "Controlla aggiornamenti" , state = "normal" )
51+ self .after_cancel (self .loading_gif_animation_id )
52+
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+
61+ async def open_github (self ):
3462 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'
3563 webbrowser .open_new_tab (url )
3664
37- def check_for_updates (self ):
38-
65+ async def check_for_updates (self ):
3966 current_version = lib .get_key_value_json (CONFIG_FILE , "version" )
4067
68+ # Disabilita il bottone di aggiornamento e mostra l'icona animata
69+ self .update_button_state ("loading" )
70+
71+ print ("check_for_updates" )
72+
4173 # Recupera la versione più recente dalla repository su GitHub
42- response = requests .get (
43- 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' )
44- if response .status_code == 200 :
45- latest_release = response .json ()
46- latest_version = latest_release ['tag_name' ]
47- print (latest_version )
48- if latest_version != current_version :
49- self .update_button = CTkButton (
50- self ,
51- text = "Aggiorna ora" ,
52- command = lambda : self .update_app (latest_release )
53- )
54- self .check_updates_button .grid (
55- row = 2 , column = 0 , padx = 20 , pady = 10 )
56-
57- else :
58- # Gestione dell'errore
59- latest_version = current_version
60- messagebox .showerror (
61- "Error" , "Nessun aggiornamento trovato." )
62-
63- def update_app (self , latest_release ):
74+ async with aiohttp .ClientSession () as session :
75+ async with session .get (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' ) as response :
76+ if response .status == 200 :
77+ latest_release = await response .json ()
78+ latest_version = latest_release ['tag_name' ]
79+ print (latest_version )
80+ if latest_version != current_version :
81+ self .update_button = CTkButton (
82+ self ,
83+ text = "Aggiorna ora" ,
84+ command = lambda : asyncio .ensure_future (
85+ self .update_async (latest_release ))
86+ )
87+ self .update_button .grid (
88+ row = 2 , column = 0 , padx = 20 , pady = 10 )
89+
90+ else :
91+ # Gestione dell'errore
92+ messagebox .showerror (
93+ "Error" , "Nessun aggiornamento trovato." )
94+ # 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 ):
6499 # Scarica l'aggiornamento dal repository su GitHub
65- assets = latest_release ['assets' ]
66- for asset in assets :
67- if asset ['name' ].endswith ('.exe' ):
68- url = asset ['browser_download_url' ]
69- response = requests .get (url )
70- with open ('new_version.exe' , 'wb' ) as f :
71- f .write (response .content )
72-
73- # Aggiorna il file di configurazione locale con la nuova versione
74- lib .update_key_json (CONFIG_FILE , "version" ,
75- latest_release ['tag_name' ])
76-
77- # Avvia l'applicazione aggiornata
78- os .startfile ('new_version.exe' )
79- self .update_button .destroy ()
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' )
0 commit comments