removed app, reorganized logic into modules

This commit is contained in:
2025-03-24 15:09:27 -05:00
parent 44480bd30a
commit 1458f99c3d
11 changed files with 116 additions and 109 deletions
-73
View File
@@ -1,73 +0,0 @@
package main
import (
"context"
"fmt"
"log/slog"
"yellowjacket/backend/config"
"yellowjacket/backend/library"
"yellowjacket/backend/logging"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
config *config.Config
library *library.Library
logger *slog.Logger
}
// NewApp creates a new App application struct
func NewApp(conf *config.Config) *App {
if conf == nil {
// TODO proper error handling
panic(fmt.Errorf("conf was nil"))
}
return &App{
config: conf,
}
}
// 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
runtime.LogInfo(a.ctx, fmt.Sprintf("starting app with config %s", logging.PrettyJSON(*a.config)))
musicLib, err := library.GetNewLibrary(ctx, a.config.Library)
if err != nil {
panic(fmt.Errorf("could not initialize library: %w", err))
}
// config might have been updated, so lets propagate that to the app
a.config.Library = musicLib.Conf
runtime.LogInfo(a.ctx, fmt.Sprintf("using library %s", logging.PrettyJSON(musicLib)))
a.library = musicLib
}
// trying to make a function
func (a *App) DirectoryPicker() (string, error) {
runtime.LogInfo(a.ctx, "selecting a directory")
dir, err := runtime.OpenDirectoryDialog(
a.ctx,
runtime.OpenDialogOptions{})
if err != nil {
return "", fmt.Errorf("could not open directory dialog\n%w", err)
}
if dir == "" {
return "No Library Directory Selected", nil
}
// we got a directory, lets do something with it
runtime.LogInfo(a.ctx, fmt.Sprintf("got directory %s assigning to library directory config %s", dir, logging.PrettyJSON(*a.config)))
a.config.Library.DirectoryPath = dir
a.config.WriteConfig()
return dir, nil
}
// trying to make a function
func (a *App) GetLibraryDir() (string, error) {
return a.config.Library.DirectoryPath, nil
}
@@ -0,0 +1,38 @@
package frontendbindings
import (
"context"
"fmt"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// FrontendBindings contain any Go functions that are specific to the frontend only and need to be bound
type FrontendBindings struct {
ctx context.Context
}
func NewFrontendBindings() (*FrontendBindings, error) {
return &FrontendBindings{}, nil
}
func (fe *FrontendBindings) Init(ctx context.Context) error {
fe.ctx = ctx
return nil
}
// Open a directory picker
func (fe *FrontendBindings) DirectoryPicker() (string, error) {
runtime.LogInfo(fe.ctx, "selecting a directory")
dir, err := runtime.OpenDirectoryDialog(
fe.ctx,
runtime.OpenDialogOptions{})
if err != nil {
return "", fmt.Errorf("could not open directory dialog\n%w", err)
}
if dir == "" {
return "No Library Directory Selected", nil
}
return dir, nil
}
+20 -10
View File
@@ -3,32 +3,42 @@ package library
import ( import (
"context" "context"
"fmt" "fmt"
"yellowjacket/backend/logging"
"github.com/wailsapp/wails/v2/pkg/runtime"
) )
type Config struct { type Config struct {
DirectoryPath string DirectoryPath string
} }
func (c *Config) validate() error {
return nil
}
var DefaultConfig *Config = &Config{ var DefaultConfig *Config = &Config{
DirectoryPath: "", DirectoryPath: "",
} }
type Library struct { type Library struct {
ctx context.Context ctx context.Context
Conf *Config conf *Config
} }
func GetNewLibrary(ctx context.Context, conf *Config) (*Library, error) { func NewLibrary(conf *Config) (*Library, error) {
if conf == nil { if conf == nil {
runtime.LogInfo(ctx, fmt.Sprintf("library config is nil, using default config: %s", logging.PrettyJSON(DefaultConfig))) return nil, fmt.Errorf("nil config for library")
conf = DefaultConfig }
if err := conf.validate(); err != nil {
return nil, fmt.Errorf("invalid library config %s\n%w", conf, err)
} }
runtime.LogInfo(ctx, fmt.Sprintf("creating new library with config: %#v", conf))
return &Library{ return &Library{
ctx: ctx, conf: conf,
Conf: conf,
}, nil }, nil
} }
func (l *Library) Init(ctx context.Context) error {
l.ctx = ctx
return nil
}
func (l *Library) GetDir() (string, error) {
return l.conf.DirectoryPath, nil
}
+2 -2
View File
@@ -23,8 +23,8 @@ const (
Stopped Stopped
) )
func NewPlayer() *Player { func NewPlayer() (*Player, error) {
return &Player{} return &Player{}, nil
} }
func (p *Player) Init(ctx context.Context) { func (p *Player) Init(ctx context.Context) {
+3 -3
View File
@@ -1,4 +1,5 @@
import { DirectoryPicker, GetLibraryDir} from '../../../wailsjs/go/main/App'; import { DirectoryPicker } from '../../../wailsjs/go/frontendbindings/FrontendBindings';
import { GetDir as GetLibraryDir } from '../../../wailsjs/go/library/Library';
import { Play } from '../../../wailsjs/go/player/Player'; import { Play } from '../../../wailsjs/go/player/Player';
@@ -15,7 +16,7 @@ window.onPlayPauseButtonClick = function() {
try { try {
Play().then((result) => { Play().then((result) => {
console.log(result) console.log(result)
playPauseButton.setAttribute("src", "/src/assets/images/icons/music/pause-solid.svg") playPauseButtonIcon.setAttribute("src", "/src/assets/images/icons/music/pause-solid.svg")
}).catch((err) => { }).catch((err) => {
console.log("There is an error with playing") console.log("There is an error with playing")
console.error(err); console.error(err);
@@ -27,7 +28,6 @@ window.onPlayPauseButtonClick = function() {
} }
window.dirPicker = function() { window.dirPicker = function() {
var dir;
try { try {
DirectoryPicker() DirectoryPicker()
.then((result) => { window.updateLibraryDirLabel(result); }) .then((result) => { window.updateLibraryDirLabel(result); })
@@ -1,6 +1,7 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
import {context} from '../models';
export function DirectoryPicker():Promise<string>; export function DirectoryPicker():Promise<string>;
export function GetLibraryDir():Promise<string>; export function Init(arg1:context.Context):Promise<void>;
+11
View File
@@ -0,0 +1,11 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function DirectoryPicker() {
return window['go']['frontendbindings']['FrontendBindings']['DirectoryPicker']();
}
export function Init(arg1) {
return window['go']['frontendbindings']['FrontendBindings']['Init'](arg1);
}
+7
View File
@@ -0,0 +1,7 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {context} from '../models';
export function GetDir():Promise<string>;
export function Init(arg1:context.Context):Promise<void>;
+11
View File
@@ -0,0 +1,11 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function GetDir() {
return window['go']['library']['Library']['GetDir']();
}
export function Init(arg1) {
return window['go']['library']['Library']['Init'](arg1);
}
-11
View File
@@ -1,11 +0,0 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function DirectoryPicker() {
return window['go']['main']['App']['DirectoryPicker']();
}
export function GetLibraryDir() {
return window['go']['main']['App']['GetLibraryDir']();
}
+22 -9
View File
@@ -5,6 +5,8 @@ import (
"embed" "embed"
"fmt" "fmt"
"yellowjacket/backend/config" "yellowjacket/backend/config"
"yellowjacket/backend/frontendbindings"
"yellowjacket/backend/library"
"yellowjacket/backend/player" "yellowjacket/backend/player"
"github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2"
@@ -21,13 +23,24 @@ import (
var assets embed.FS var assets embed.FS
func main() { func main() {
config, err := config.GetCurrentConfig() yjConf, err := config.GetCurrentConfig()
if err != nil { if err != nil {
panic(fmt.Errorf("could not get config: %w", err)) panic(fmt.Errorf("could not get config: %w", err))
} }
app := NewApp(config) // Create objects that will be bound on the frontend
player := player.NewPlayer() frontendBindings, err := frontendbindings.NewFrontendBindings()
if err != nil {
panic(fmt.Errorf("could not create frontendBindings obj: %w", err))
}
player, err := player.NewPlayer()
if err != nil {
panic(fmt.Errorf("could not create player obj: %w", err))
}
library, err := library.NewLibrary(yjConf.Library)
if err != nil {
panic(fmt.Errorf("could not create library obj: %w", err))
}
// Create application with options // Create application with options
err = wails.Run(&options.App{ err = wails.Run(&options.App{
@@ -42,11 +55,13 @@ func main() {
LogLevel: logger.TRACE, LogLevel: logger.TRACE,
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1}, BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
OnStartup: func(ctx context.Context) { OnStartup: func(ctx context.Context) {
app.startup(ctx) frontendBindings.Init(ctx)
library.Init(ctx)
player.Init(ctx) player.Init(ctx)
}, },
Bind: []interface{}{ Bind: []any{
app, frontendBindings,
library,
player, player,
}, },
DisableResize: false, DisableResize: false,
@@ -63,15 +78,13 @@ func main() {
Logger: nil, Logger: nil,
LogLevelProduction: 0, LogLevelProduction: 0,
OnDomReady: func(ctx context.Context) { OnDomReady: func(ctx context.Context) {
return
}, },
OnShutdown: func(ctx context.Context) { OnShutdown: func(ctx context.Context) {
return
}, },
OnBeforeClose: func(ctx context.Context) bool { OnBeforeClose: func(ctx context.Context) bool {
return false return false
}, },
EnumBind: []interface{}{}, EnumBind: []any{},
WindowStartState: 0, WindowStartState: 0,
ErrorFormatter: nil, ErrorFormatter: nil,
EnableDefaultContextMenu: false, EnableDefaultContextMenu: false,