probo-app/app.go

111 lines
2.4 KiB
Go
Raw Normal View History

2023-06-29 10:52:15 +02:00
package main
import (
"context"
2023-07-03 18:18:59 +02:00
"git.andreafazzi.eu/andrea/probo/client"
"git.andreafazzi.eu/andrea/probo/models"
2023-07-03 18:18:59 +02:00
"git.andreafazzi.eu/andrea/probo/store/file"
"github.com/fsnotify/fsnotify"
"github.com/wailsapp/wails/v2/pkg/runtime"
2023-06-29 10:52:15 +02:00
)
// App struct
type App struct {
ctx context.Context
store *file.FileProboCollectorStore
watcher *fsnotify.Watcher
2023-07-03 18:18:59 +02:00
}
type Quiz struct {
Text string
2023-06-29 10:52:15 +02:00
}
// NewApp creates a new App application struct
func NewApp() *App {
2023-07-03 18:18:59 +02:00
store, err := file.NewFileProboCollectorStore("./data/quizzes")
if err != nil {
panic(err)
}
return &App{
store: store,
}
2023-06-29 10:52:15 +02:00
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) onShutdown(ctx context.Context) {
a.watcher.Close()
}
func (a *App) onDomReady(ctx context.Context) {
var err error
// Create new watcher.
a.watcher, err = fsnotify.NewWatcher()
if err != nil {
runtime.LogFatal(ctx, err.Error())
}
// Start listening for events.
go func() {
runtime.LogDebug(ctx, "Filesystem watcher initialized...")
for {
select {
case event, ok := <-a.watcher.Events:
if !ok {
return
}
runtime.LogDebugf(ctx, "Event: %v", event)
2023-09-05 20:38:18 +02:00
if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) {
runtime.LogDebugf(ctx, "Modified or removed file: %v", event.Name)
runtime.EventsEmit(ctx, "fsChangeEvent")
}
case err, ok := <-a.watcher.Errors:
if !ok {
return
}
runtime.LogDebugf(ctx, "Error: %v", err)
}
}
}()
// Add a path.
runtime.LogDebugf(ctx, "Begin watching path %v", a.store.Dir)
err = a.watcher.Add(a.store.Dir)
if err != nil {
runtime.LogFatalf(ctx, "Error: %v", err)
}
}
func (a *App) ReadAllQuizzes() ([]*models.Quiz, error) {
return a.store.ReadAllQuizzes()
2023-07-03 18:18:59 +02:00
}
func (a *App) MarkdownFromQuiz(quiz *models.Quiz) (string, error) {
return file.MarkdownFromQuiz(quiz)
}
func (a *App) QuizFromMarkdown(markdown string) (*client.Quiz, error) {
return file.QuizFromMarkdown(markdown)
}
func (a *App) UpdateQuiz(quiz *client.Quiz, id string) (*models.Quiz, error) {
return a.store.UpdateQuiz(&client.CreateUpdateQuizRequest{Quiz: quiz}, id)
}
func (a *App) CreateQuiz(quiz *client.Quiz) (*models.Quiz, error) {
return a.store.CreateQuiz(&client.CreateUpdateQuizRequest{Quiz: quiz})
}
2023-09-05 20:38:18 +02:00
func (a *App) DeleteQuiz(id string) (*models.Quiz, error) {
return a.store.DeleteQuiz(&client.DeleteQuizRequest{ID: id})
}