mirror of
https://github.com/wisplite/parchment.git
synced 2026-06-27 21:57:08 -05:00
Refactor UI state management and improve error handling in the application. Added success and error messages to enhance user feedback during package creation and device connection status updates.
This commit is contained in:
@@ -24,7 +24,10 @@ func MonitorDevice(ctx context.Context, app *tview.Application, frame **tview.Fr
|
||||
if err != nil {
|
||||
if device.Connected {
|
||||
device.Close()
|
||||
UpdateDeviceStatus(app, *frame, state, "Cardputer disconnected", tcell.ColorRed)
|
||||
state.Error = "Cardputer disconnected"
|
||||
UpdateDeviceStatus(app, *frame, state, "Waiting for cardputer...", tcell.ColorWhite)
|
||||
} else {
|
||||
UpdateDeviceStatus(app, *frame, state, "Waiting for cardputer...", tcell.ColorWhite)
|
||||
}
|
||||
} else {
|
||||
if !device.Connected {
|
||||
@@ -34,6 +37,9 @@ func MonitorDevice(ctx context.Context, app *tview.Application, frame **tview.Fr
|
||||
UpdateDeviceStatus(app, *frame, state, "Error connecting to cardputer: "+err.Error(), tcell.ColorRed)
|
||||
} else {
|
||||
UpdateDeviceStatus(app, *frame, state, "Connected to cardputer: "+port, tcell.ColorGreen)
|
||||
if state.Error == "Cardputer disconnected" {
|
||||
state.Error = ""
|
||||
}
|
||||
}
|
||||
} else {
|
||||
UpdateDeviceStatus(app, *frame, state, "Connected to cardputer: "+port, tcell.ColorGreen)
|
||||
@@ -47,10 +53,21 @@ func main() {
|
||||
// Initialize Device manager
|
||||
device := NewDevice()
|
||||
|
||||
// Ensure cleanup happens no matter how we exit
|
||||
defer func() {
|
||||
log.Println("Cleanup: Disconnecting device...")
|
||||
if err := device.Close(); err != nil {
|
||||
log.Printf("Cleanup: Error disconnecting device: %v\n", err)
|
||||
} else {
|
||||
log.Println("Cleanup: Device disconnected successfully")
|
||||
}
|
||||
}()
|
||||
|
||||
app := tview.NewApplication()
|
||||
var currentFrame *tview.Frame
|
||||
state := &UIState{
|
||||
Page: "home",
|
||||
Page: "home",
|
||||
Error: "",
|
||||
}
|
||||
frame := AppUI(app, ¤tFrame, state)
|
||||
currentFrame = frame
|
||||
@@ -66,15 +83,9 @@ func main() {
|
||||
// Handle signals in a goroutine
|
||||
go func() {
|
||||
<-sigChan
|
||||
log.Println("Signal received, shutting down...")
|
||||
// Cancel context to stop monitoring
|
||||
cancel()
|
||||
// Disconnect device gracefully
|
||||
if device.Connected {
|
||||
log.Println("Disconnecting device...")
|
||||
if err := device.Close(); err != nil {
|
||||
log.Printf("Error disconnecting device: %v\n", err)
|
||||
}
|
||||
}
|
||||
// Stop the application
|
||||
app.Stop()
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user