refactor(updater): 重构 Go 版本更新器
- 更新项目名称为 AUTO_MAA_Go_Updater - 重构代码结构,优化函数命名和逻辑 - 移除 CDK 相关的冗余代码 - 调整版本号为 git commit hash - 更新构建配置和脚本 - 优化 API 客户端实现
This commit is contained in:
@@ -2,15 +2,15 @@ package gui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/app"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/dialog"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
)
|
||||
|
||||
// UpdateStatus represents the current status of the update process
|
||||
// UpdateStatus 表示更新过程的当前状态
|
||||
type UpdateStatus int
|
||||
|
||||
const (
|
||||
@@ -22,16 +22,15 @@ const (
|
||||
StatusError
|
||||
)
|
||||
|
||||
// Config represents the configuration structure for the GUI
|
||||
// Config 表示 GUI 的配置结构
|
||||
type Config struct {
|
||||
ResourceID string
|
||||
CurrentVersion string
|
||||
CDK string
|
||||
UserAgent string
|
||||
BackupURL string
|
||||
}
|
||||
|
||||
// GUIManager interface defines the methods for GUI management
|
||||
// GUIManager 定义 GUI 管理的接口方法
|
||||
type GUIManager interface {
|
||||
ShowMainWindow()
|
||||
UpdateStatus(status UpdateStatus, message string)
|
||||
@@ -41,7 +40,7 @@ type GUIManager interface {
|
||||
Close()
|
||||
}
|
||||
|
||||
// Manager implements the GUIManager interface
|
||||
// Manager 实现 GUIManager 接口
|
||||
type Manager struct {
|
||||
app fyne.App
|
||||
window fyne.Window
|
||||
@@ -59,12 +58,12 @@ type Manager struct {
|
||||
func NewManager() *Manager {
|
||||
a := app.New()
|
||||
a.SetIcon(theme.ComputerIcon())
|
||||
|
||||
w := a.NewWindow("轻量级更新器")
|
||||
|
||||
w := a.NewWindow("AUTO_MAA_Go_Updater")
|
||||
w.Resize(fyne.NewSize(500, 400))
|
||||
w.SetFixedSize(false)
|
||||
w.CenterOnScreen()
|
||||
|
||||
|
||||
return &Manager{
|
||||
app: a,
|
||||
window: w,
|
||||
@@ -81,10 +80,10 @@ func (m *Manager) SetCallbacks(onCheckUpdate, onCancel func()) {
|
||||
func (m *Manager) ShowMainWindow() {
|
||||
// Create UI components
|
||||
m.createUIComponents()
|
||||
|
||||
|
||||
// Create main layout
|
||||
content := m.createMainLayout()
|
||||
|
||||
|
||||
m.window.SetContent(content)
|
||||
m.window.ShowAndRun()
|
||||
}
|
||||
@@ -94,19 +93,19 @@ func (m *Manager) createUIComponents() {
|
||||
// Status label
|
||||
m.statusLabel = widget.NewLabel("准备检查更新...")
|
||||
m.statusLabel.Alignment = fyne.TextAlignCenter
|
||||
|
||||
|
||||
// Progress bar
|
||||
m.progressBar = widget.NewProgressBar()
|
||||
m.progressBar.Hide()
|
||||
|
||||
|
||||
// Version label
|
||||
m.versionLabel = widget.NewLabel("当前版本: 未知")
|
||||
m.versionLabel.TextStyle = fyne.TextStyle{Italic: true}
|
||||
|
||||
|
||||
// Release notes
|
||||
m.releaseNotes = widget.NewRichText()
|
||||
m.releaseNotes.Hide()
|
||||
|
||||
|
||||
// Action button
|
||||
m.actionButton = widget.NewButton("检查更新", func() {
|
||||
if m.onCheckUpdate != nil {
|
||||
@@ -117,25 +116,25 @@ func (m *Manager) createUIComponents() {
|
||||
}
|
||||
|
||||
// createMainLayout creates the main window layout
|
||||
func (m *Manager) createMainLayout() *container.VBox {
|
||||
func (m *Manager) createMainLayout() *fyne.Container {
|
||||
// Header section
|
||||
header := container.NewVBox(
|
||||
widget.NewCard("", "", container.NewVBox(
|
||||
widget.NewLabelWithStyle("轻量级更新器", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
|
||||
widget.NewLabelWithStyle("AUTO_MAA_Go_Updater", fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
|
||||
m.versionLabel,
|
||||
)),
|
||||
)
|
||||
|
||||
|
||||
// Status section
|
||||
statusSection := container.NewVBox(
|
||||
m.statusLabel,
|
||||
m.progressBar,
|
||||
)
|
||||
|
||||
|
||||
// Release notes section
|
||||
releaseNotesCard := widget.NewCard("更新日志", "", container.NewScroll(m.releaseNotes))
|
||||
releaseNotesCard.Hide()
|
||||
|
||||
|
||||
// Button section
|
||||
buttonSection := container.NewHBox(
|
||||
widget.NewButton("配置", func() {
|
||||
@@ -144,7 +143,7 @@ func (m *Manager) createMainLayout() *container.VBox {
|
||||
widget.NewSpacer(),
|
||||
m.actionButton,
|
||||
)
|
||||
|
||||
|
||||
// Main layout
|
||||
return container.NewVBox(
|
||||
header,
|
||||
@@ -160,33 +159,33 @@ func (m *Manager) createMainLayout() *container.VBox {
|
||||
func (m *Manager) UpdateStatus(status UpdateStatus, message string) {
|
||||
m.currentStatus = status
|
||||
m.statusLabel.SetText(message)
|
||||
|
||||
|
||||
switch status {
|
||||
case StatusChecking:
|
||||
m.actionButton.SetText("检查中...")
|
||||
m.actionButton.Disable()
|
||||
m.progressBar.Hide()
|
||||
|
||||
|
||||
case StatusUpdateAvailable:
|
||||
m.actionButton.SetText("开始更新")
|
||||
m.actionButton.Enable()
|
||||
m.progressBar.Hide()
|
||||
|
||||
|
||||
case StatusDownloading:
|
||||
m.actionButton.SetText("下载中...")
|
||||
m.actionButton.Disable()
|
||||
m.progressBar.Show()
|
||||
|
||||
|
||||
case StatusInstalling:
|
||||
m.actionButton.SetText("安装中...")
|
||||
m.actionButton.Disable()
|
||||
m.progressBar.Show()
|
||||
|
||||
|
||||
case StatusCompleted:
|
||||
m.actionButton.SetText("完成")
|
||||
m.actionButton.Enable()
|
||||
m.progressBar.Hide()
|
||||
|
||||
|
||||
case StatusError:
|
||||
m.actionButton.SetText("重试")
|
||||
m.actionButton.Enable()
|
||||
@@ -202,7 +201,7 @@ func (m *Manager) ShowProgress(percentage float64) {
|
||||
if percentage > 100 {
|
||||
percentage = 100
|
||||
}
|
||||
|
||||
|
||||
m.progressBar.SetValue(percentage / 100.0)
|
||||
m.progressBar.Show()
|
||||
}
|
||||
@@ -222,34 +221,30 @@ func (m *Manager) showConfigDialog() (*Config, error) {
|
||||
// Create form entries
|
||||
resourceIDEntry := widget.NewEntry()
|
||||
resourceIDEntry.SetPlaceHolder("例如: M9A")
|
||||
|
||||
|
||||
versionEntry := widget.NewEntry()
|
||||
versionEntry.SetPlaceHolder("例如: v1.0.0")
|
||||
|
||||
cdkEntry := widget.NewPasswordEntry()
|
||||
cdkEntry.SetPlaceHolder("输入您的CDK(可选)")
|
||||
|
||||
|
||||
userAgentEntry := widget.NewEntry()
|
||||
userAgentEntry.SetText("LightweightUpdater/1.0")
|
||||
|
||||
userAgentEntry.SetText("AUTO_MAA_Go_Updater/1.0")
|
||||
|
||||
backupURLEntry := widget.NewEntry()
|
||||
backupURLEntry.SetPlaceHolder("备用下载地址(可选)")
|
||||
|
||||
|
||||
// Create form
|
||||
form := &widget.Form{
|
||||
Items: []*widget.FormItem{
|
||||
{Text: "资源ID:", Widget: resourceIDEntry},
|
||||
{Text: "当前版本:", Widget: versionEntry},
|
||||
{Text: "CDK:", Widget: cdkEntry},
|
||||
{Text: "用户代理:", Widget: userAgentEntry},
|
||||
{Text: "备用下载地址:", Widget: backupURLEntry},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
// Create result channel
|
||||
resultChan := make(chan *Config, 1)
|
||||
errorChan := make(chan error, 1)
|
||||
|
||||
|
||||
// Create dialog
|
||||
configDialog := dialog.NewCustomConfirm(
|
||||
"配置设置",
|
||||
@@ -261,11 +256,10 @@ func (m *Manager) showConfigDialog() (*Config, error) {
|
||||
config := &Config{
|
||||
ResourceID: resourceIDEntry.Text,
|
||||
CurrentVersion: versionEntry.Text,
|
||||
CDK: cdkEntry.Text,
|
||||
UserAgent: userAgentEntry.Text,
|
||||
BackupURL: backupURLEntry.Text,
|
||||
}
|
||||
|
||||
|
||||
// Basic validation
|
||||
if config.ResourceID == "" {
|
||||
errorChan <- fmt.Errorf("资源ID不能为空")
|
||||
@@ -275,7 +269,7 @@ func (m *Manager) showConfigDialog() (*Config, error) {
|
||||
errorChan <- fmt.Errorf("当前版本不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
resultChan <- config
|
||||
} else {
|
||||
errorChan <- fmt.Errorf("用户取消了配置")
|
||||
@@ -283,30 +277,27 @@ func (m *Manager) showConfigDialog() (*Config, error) {
|
||||
},
|
||||
m.window,
|
||||
)
|
||||
|
||||
|
||||
// Add help text
|
||||
helpText := widget.NewRichTextFromMarkdown(`
|
||||
**配置说明:**
|
||||
- **资源ID**: Mirror酱服务中的资源标识符
|
||||
- **当前版本**: 当前软件的版本号
|
||||
- **CDK**: Mirror酱服务的访问密钥(可选,提供更好的下载体验)
|
||||
- **用户代理**: HTTP请求的用户代理字符串
|
||||
- **备用下载地址**: 当Mirror酱服务不可用时的备用下载地址
|
||||
|
||||
如需获取CDK,请访问 [Mirror酱官网](https://mirrorchyan.com)
|
||||
`)
|
||||
|
||||
|
||||
// Create container with help text
|
||||
dialogContent := container.NewVBox(
|
||||
form,
|
||||
widget.NewSeparator(),
|
||||
helpText,
|
||||
)
|
||||
|
||||
|
||||
configDialog.SetContent(dialogContent)
|
||||
configDialog.Resize(fyne.NewSize(600, 500))
|
||||
configDialog.Show()
|
||||
|
||||
|
||||
// Wait for result
|
||||
select {
|
||||
case config := <-resultChan:
|
||||
@@ -340,17 +331,17 @@ func (m *Manager) ShowReleaseNotes(notes string) {
|
||||
// UpdateStatusWithDetails updates status with detailed information
|
||||
func (m *Manager) UpdateStatusWithDetails(status UpdateStatus, message string, details map[string]string) {
|
||||
m.UpdateStatus(status, message)
|
||||
|
||||
|
||||
// Update version info if provided
|
||||
if version, ok := details["version"]; ok {
|
||||
m.SetVersionInfo(version)
|
||||
}
|
||||
|
||||
|
||||
// Show release notes if provided
|
||||
if notes, ok := details["release_notes"]; ok {
|
||||
m.ShowReleaseNotes(notes)
|
||||
}
|
||||
|
||||
|
||||
// Update progress if provided
|
||||
if progress, ok := details["progress"]; ok {
|
||||
if p, err := fmt.Sscanf(progress, "%f", new(float64)); err == nil && p == 1 {
|
||||
@@ -364,14 +355,14 @@ func (m *Manager) UpdateStatusWithDetails(status UpdateStatus, message string, d
|
||||
// ShowProgressWithSpeed shows progress with download speed information
|
||||
func (m *Manager) ShowProgressWithSpeed(percentage float64, speed int64, eta string) {
|
||||
m.ShowProgress(percentage)
|
||||
|
||||
|
||||
// Update status with speed and ETA information
|
||||
speedText := m.formatSpeed(speed)
|
||||
statusText := fmt.Sprintf("下载中... %.1f%% (%s)", percentage, speedText)
|
||||
if eta != "" {
|
||||
statusText += fmt.Sprintf(" - 剩余时间: %s", eta)
|
||||
}
|
||||
|
||||
|
||||
m.statusLabel.SetText(statusText)
|
||||
}
|
||||
|
||||
@@ -403,18 +394,18 @@ func (m *Manager) ShowUpdateAvailableDialog(currentVersion, newVersion, releaseN
|
||||
widget.NewLabel(fmt.Sprintf("当前版本: %s", currentVersion)),
|
||||
widget.NewSeparator(),
|
||||
)
|
||||
|
||||
|
||||
if releaseNotes != "" {
|
||||
notesWidget := widget.NewRichText()
|
||||
notesWidget.ParseMarkdown(releaseNotes)
|
||||
|
||||
|
||||
notesScroll := container.NewScroll(notesWidget)
|
||||
notesScroll.SetMinSize(fyne.NewSize(400, 200))
|
||||
|
||||
|
||||
content.Add(widget.NewLabel("更新内容:"))
|
||||
content.Add(notesScroll)
|
||||
}
|
||||
|
||||
|
||||
dialog.ShowCustomConfirm(
|
||||
"发现新版本",
|
||||
"立即更新",
|
||||
@@ -519,4 +510,4 @@ func (m *Manager) Close() {
|
||||
if m.window != nil {
|
||||
m.window.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user