add so interfaces can see task feedback

This commit is contained in:
Robin Olsen
2026-02-17 13:04:03 +01:00
parent 235275d911
commit 68af436a3f
3 changed files with 90 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package task
import (
"context"
"fmt"
"time"
"github.com/LazyBachelor/LazyPM/internal/service"
tea "github.com/charmbracelet/bubbletea"
@@ -18,6 +19,10 @@ type Task struct {
dbStateFunc DbStateFunc
svc *service.Services
feedbackChan chan ValidationFeedback
doneChan chan bool
quitChan chan bool
}
func NewTask(svc *service.Services, aboutScreen tea.Model, questionnaire tea.Model) *Task {
@@ -93,3 +98,43 @@ func (t *Task) SetDbStateFunc(fn DbStateFunc) {
func (t *Task) SetValidateFunc(fn ValidateFunc) {
t.validateFunc = fn
}
func (t *Task) SetChannels(feedbackChan chan ValidationFeedback, doneChan chan bool, quitChan chan bool) {
t.feedbackChan = feedbackChan
t.doneChan = doneChan
t.quitChan = quitChan
}
func (t *Task) StartValidationLoop(ctx context.Context) {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
ok, err := t.Validate(ctx)
feedback := ValidationFeedback{
Timestamp: time.Now(),
}
if ok {
feedback.Success = true
feedback.Message = "Task completed successfully!"
t.feedbackChan <- feedback
t.doneChan <- true
return
} else {
feedback.Success = false
if err != nil {
feedback.Message = err.Error()
} else {
feedback.Message = "Task not yet complete"
}
t.feedbackChan <- feedback
}
case <-t.quitChan:
return
case <-ctx.Done():
return
}
}
}

View File

@@ -3,6 +3,7 @@ package task
import (
"context"
"errors"
"time"
"github.com/LazyBachelor/LazyPM/internal/service"
)
@@ -18,3 +19,19 @@ type Interface interface {
type ConfigFunc func() TaskConfig
type ValidateFunc func(context.Context, *service.Services) (ok bool, err error)
type DbStateFunc func(context.Context, *service.Services) error
type ValidationFeedback struct {
Success bool
Message string
Timestamp time.Time
}
type ValidationObserver interface {
OnValidationUpdate(feedback ValidationFeedback)
OnTaskComplete()
}
type ValidatedInterface interface {
Interface
SetChannels(feedbackChan chan ValidationFeedback, quitChan chan bool)
}