diff --git a/app.go b/app.go deleted file mode 100644 index d6ae393..0000000 --- a/app.go +++ /dev/null @@ -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 -} diff --git a/backend/frontendbindings/frontendbindings.go b/backend/frontendbindings/frontendbindings.go new file mode 100644 index 0000000..b152358 --- /dev/null +++ b/backend/frontendbindings/frontendbindings.go @@ -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 +} diff --git a/backend/library/library.go b/backend/library/library.go index a1a04f9..9e64246 100644 --- a/backend/library/library.go +++ b/backend/library/library.go @@ -3,32 +3,42 @@ package library import ( "context" "fmt" - "yellowjacket/backend/logging" - - "github.com/wailsapp/wails/v2/pkg/runtime" ) type Config struct { DirectoryPath string } +func (c *Config) validate() error { + return nil +} + var DefaultConfig *Config = &Config{ DirectoryPath: "", } type Library struct { 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 { - runtime.LogInfo(ctx, fmt.Sprintf("library config is nil, using default config: %s", logging.PrettyJSON(DefaultConfig))) - conf = DefaultConfig + return nil, fmt.Errorf("nil config for library") + } + 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{ - ctx: ctx, - Conf: conf, + conf: conf, }, 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 +} diff --git a/backend/player/player.go b/backend/player/player.go index 214ce45..a066e1b 100644 --- a/backend/player/player.go +++ b/backend/player/player.go @@ -23,8 +23,8 @@ const ( Stopped ) -func NewPlayer() *Player { - return &Player{} +func NewPlayer() (*Player, error) { + return &Player{}, nil } func (p *Player) Init(ctx context.Context) { diff --git a/frontend/src/assets/scripts/main.js b/frontend/src/assets/scripts/main.js index a2421da..c497640 100644 --- a/frontend/src/assets/scripts/main.js +++ b/frontend/src/assets/scripts/main.js @@ -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'; @@ -15,7 +16,7 @@ window.onPlayPauseButtonClick = function() { try { Play().then((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) => { console.log("There is an error with playing") console.error(err); @@ -27,7 +28,6 @@ window.onPlayPauseButtonClick = function() { } window.dirPicker = function() { - var dir; try { DirectoryPicker() .then((result) => { window.updateLibraryDirLabel(result); }) diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/frontendbindings/FrontendBindings.d.ts similarity index 64% rename from frontend/wailsjs/go/main/App.d.ts rename to frontend/wailsjs/go/frontendbindings/FrontendBindings.d.ts index d1b1475..3e604f2 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/frontendbindings/FrontendBindings.d.ts @@ -1,6 +1,7 @@ // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // This file is automatically generated. DO NOT EDIT +import {context} from '../models'; export function DirectoryPicker():Promise; -export function GetLibraryDir():Promise; +export function Init(arg1:context.Context):Promise; diff --git a/frontend/wailsjs/go/frontendbindings/FrontendBindings.js b/frontend/wailsjs/go/frontendbindings/FrontendBindings.js new file mode 100755 index 0000000..20c7682 --- /dev/null +++ b/frontend/wailsjs/go/frontendbindings/FrontendBindings.js @@ -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); +} diff --git a/frontend/wailsjs/go/library/Library.d.ts b/frontend/wailsjs/go/library/Library.d.ts new file mode 100755 index 0000000..f6fc6b8 --- /dev/null +++ b/frontend/wailsjs/go/library/Library.d.ts @@ -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; + +export function Init(arg1:context.Context):Promise; diff --git a/frontend/wailsjs/go/library/Library.js b/frontend/wailsjs/go/library/Library.js new file mode 100755 index 0000000..539e50d --- /dev/null +++ b/frontend/wailsjs/go/library/Library.js @@ -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); +} diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js deleted file mode 100755 index fd828ba..0000000 --- a/frontend/wailsjs/go/main/App.js +++ /dev/null @@ -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'](); -} diff --git a/main.go b/main.go index 6119631..5ebd2fd 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "embed" "fmt" "yellowjacket/backend/config" + "yellowjacket/backend/frontendbindings" + "yellowjacket/backend/library" "yellowjacket/backend/player" "github.com/wailsapp/wails/v2" @@ -21,13 +23,24 @@ import ( var assets embed.FS func main() { - config, err := config.GetCurrentConfig() + yjConf, err := config.GetCurrentConfig() if err != nil { panic(fmt.Errorf("could not get config: %w", err)) } - app := NewApp(config) - player := player.NewPlayer() + // Create objects that will be bound on the frontend + 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 err = wails.Run(&options.App{ @@ -42,11 +55,13 @@ func main() { LogLevel: logger.TRACE, BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1}, OnStartup: func(ctx context.Context) { - app.startup(ctx) + frontendBindings.Init(ctx) + library.Init(ctx) player.Init(ctx) }, - Bind: []interface{}{ - app, + Bind: []any{ + frontendBindings, + library, player, }, DisableResize: false, @@ -63,15 +78,13 @@ func main() { Logger: nil, LogLevelProduction: 0, OnDomReady: func(ctx context.Context) { - return }, OnShutdown: func(ctx context.Context) { - return }, OnBeforeClose: func(ctx context.Context) bool { return false }, - EnumBind: []interface{}{}, + EnumBind: []any{}, WindowStartState: 0, ErrorFormatter: nil, EnableDefaultContextMenu: false,