Skip to content

Commit

Permalink
refactor into struct composition (inheritance)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicanordlc committed Jun 8, 2024
1 parent fc53d70 commit 411ef03
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 211 deletions.
43 changes: 12 additions & 31 deletions backend/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"runtime"

"github.com/cabaalexander/save-manager/backend/models"
"github.com/cabaalexander/save-manager/backend/utils"
rt "github.com/wailsapp/wails/v2/pkg/runtime"
)
Expand All @@ -15,30 +16,23 @@ type JsonApp struct {
}
}

// App struct
type App struct {
ctx context.Context
filename string
Settings *Settings
JsonApp
models.Json[JsonApp]
}

// 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
a.filename = "app.json"
utils.CreateConfigJsonIfNoExists[JsonApp](a.filename)
a.Filename = "app.json"
a.CreateConfigJsonIfNoExists()

app, err := a.ReadApp()
app, err := a.ReadData()
if err != nil {
panic(err)
}
if a.Size == struct {
Width int
Height int
}{} {
a.Size = app.Size
if app.Size.Width != 0 && app.Size.Height != 0 {
a.JsonData.Size = app.Size
rt.WindowSetSize(a.ctx, app.Size.Width, app.Size.Height)
}
}
Expand All @@ -52,11 +46,11 @@ func (a *App) ToggleFullScreen() {
}

func (a *App) OpenDialogDirApp() (string, error) {
return utils.OpenDialogDir(a.ctx, a.Settings.DefaultSavePath, false)
return utils.OpenDialogDir(a.ctx, a.Settings.JsonData.DefaultSavePath, false)
}

func (a *App) OpenDialogFileApp() (string, error) {
return utils.OpenDialogFile(a.ctx, a.Settings.DefaultSavePath, true)
return utils.OpenDialogFile(a.ctx, a.Settings.JsonData.DefaultSavePath, true)
}

func (a *App) GetOS() string {
Expand All @@ -65,24 +59,11 @@ func (a *App) GetOS() string {
}

func (a *App) UpdateAppSize(width, height int) error {
a.Size.Width = width
a.Size.Height = height
return a.updateJson()
}

func (a *App) ReadApp() (*JsonApp, error) {
appJson, err := utils.ReadConfigFrom[JsonApp](a.filename)
if err != nil {
return appJson, err
}
return appJson, nil
}

func (a *App) updateJson() error {
return utils.WriteStructTo(a.filename, a.JsonApp)
a.JsonData.Size.Width = width
a.JsonData.Size.Height = height
return a.UpdateJson()
}

// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
48 changes: 18 additions & 30 deletions backend/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"

"github.com/cabaalexander/save-manager/backend/models"
"github.com/cabaalexander/save-manager/backend/utils"
"github.com/google/uuid"
rt "github.com/wailsapp/wails/v2/pkg/runtime"
Expand All @@ -24,62 +25,53 @@ type JsonGame struct {
}

type Game struct {
ctx context.Context
filename string
JsonGame
ctx context.Context
models.Json[JsonGame]
}

func (g *Game) Startup(ctx context.Context) {
g.ctx = ctx
g.filename = "game.json"
utils.CreateConfigJsonIfNoExists[JsonGame](g.filename)
g.Filename = "game.json"
g.CreateConfigJsonIfNoExists()

game, err := g.ReadGames()
game, err := g.ReadData()
if err != nil {
panic(err)
}
g.JsonGame = *game
g.JsonData = *game
}

func (g *Game) AddGame(name, savePath string, isFile bool) uuid.UUID {
id := uuid.New()
game := GameSingle{ID: id, Name: name, SavePath: savePath, SavePathIsFile: isFile}
g.JsonGame.Data = append(g.JsonGame.Data, game)
g.updateJson()
g.JsonData.Data = append(g.JsonData.Data, game)
g.UpdateJson()
CreateGameDir(id)
g.logf("Created: %v", id)
return id
}

func (g *Game) RemoveGame(id uuid.UUID) error {
newList := []GameSingle{}
for _, game := range g.JsonGame.Data {
for _, game := range g.JsonData.Data {
if game.ID == id {
continue
}
newList = append(newList, game)
}
if len(newList) == len(g.JsonGame.Data) {
if len(newList) == len(g.JsonData.Data) {
errorMsg := fmt.Sprintf("no element deleted: %v", id)
return errors.New(errorMsg)
}
g.JsonGame.Data = newList
g.updateJson()
g.JsonData.Data = newList
g.UpdateJson()
g.removeGameDir(id)
g.logf("Deleted: %v", id)
return nil
}

func (g *Game) ReadGames() (*JsonGame, error) {
gameJson, err := utils.ReadConfigFrom[JsonGame](g.filename)
if err != nil {
return gameJson, err
}
return gameJson, nil
}

func (g *Game) FindGame(id uuid.UUID) GameSingle {
for _, game := range g.JsonGame.Data {
for _, game := range g.JsonData.Data {
if game.ID != id {
continue
}
Expand All @@ -89,7 +81,7 @@ func (g *Game) FindGame(id uuid.UUID) GameSingle {
}

func (g *Game) BrowseGameDir(gameID uuid.UUID) {
for _, game := range g.JsonGame.Data {
for _, game := range g.JsonData.Data {
if game.ID == gameID {
utils.BrowsePath(g.ctx, game.SavePath)
}
Expand All @@ -98,7 +90,7 @@ func (g *Game) BrowseGameDir(gameID uuid.UUID) {

func (g *Game) UpdateGame(props GameSingle) {
var gameList []GameSingle
for _, gm := range g.JsonGame.Data {
for _, gm := range g.JsonData.Data {
if gm.ID == props.ID {
if props.Name != "" {
gm.Name = props.Name
Expand All @@ -110,8 +102,8 @@ func (g *Game) UpdateGame(props GameSingle) {
}
gameList = append(gameList, gm)
}
g.JsonGame.Data = gameList
g.updateJson()
g.JsonData.Data = gameList
g.UpdateJson()
}

func (g *Game) OpenDialogDirGame(gameID uuid.UUID) (string, error) {
Expand All @@ -138,10 +130,6 @@ func (g *Game) logf(format string, args ...interface{}) {
rt.LogDebugf(g.ctx, "[Game] %v", msg)
}

func (g *Game) updateJson() error {
return utils.WriteStructTo(g.filename, g.JsonGame)
}

func CreateGameDir(gameID uuid.UUID) error {
gameDir, err := GetGameDir(gameID)
if err != nil {
Expand Down
104 changes: 104 additions & 0 deletions backend/models/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package models

import (
"encoding/json"
"errors"
"os"
"path"

"github.com/cabaalexander/save-manager/backend/utils"
)

type Json[T any] struct {
Filename string
JsonData T
}

func (j *Json[T]) CreateConfigJsonIfNoExists() (string, error) {
settingsJson, errSettingsJson := j.getConfigJson(j.Filename)
if errSettingsJson != nil {
return "", errSettingsJson
}
jsonExists, errJsonExists := utils.Exists(settingsJson)
if errJsonExists != nil {
return "", errJsonExists
}
if !jsonExists {
var data T
j.writeStructTo(j.Filename, data)
}
return settingsJson, nil
}

func (j *Json[T]) ReadData() (*T, error) {
settingsJson, err := j.readConfigFrom(j.Filename)
if err != nil {
return settingsJson, err
}
return settingsJson, nil
}

func (j *Json[T]) UpdateJson() error {
return j.writeStructTo(j.Filename, j.JsonData)
}

func (j *Json[T]) getConfigJson(filename string) (string, error) {
err := j.createConfigDirIfNoExist()
if err != nil {
return "", err
}
if filename == "" {
return "", errors.New("filename cannot be empty")
}
configDir, err := utils.GetAppConfigDir()
if err != nil {
return "", err
}
settingsJson := path.Join(configDir, filename)
return settingsJson, nil
}

func (j *Json[T]) createConfigDirIfNoExist() error {
appConfigPath, err := utils.GetAppConfigDir()
if err != nil {
return err
}
dirExists, errDirExists := utils.Exists(appConfigPath)
if errDirExists != nil {
return errDirExists
}
if !dirExists {
os.Mkdir(appConfigPath, os.ModePerm)
}
return nil
}

func (j *Json[T]) readConfigFrom(filename string) (*T, error) {
var defalutData T
path, err := j.getConfigJson(filename)
if err != nil {
return &defalutData, err
}
jsonByte, errJsonByte := os.ReadFile(path)
if errJsonByte != nil {
return &defalutData, errJsonByte
}
var data T
errData := json.Unmarshal(jsonByte, &data)
if errData != nil {
return &defalutData, errData
}
return &data, nil
}

func (j *Json[T]) writeStructTo(filename string, jsonStruct T) error {
path, err := j.getConfigJson(filename)
if err != nil {
return err
}
jsonByte, errJsonByte := json.Marshal(jsonStruct)
if errJsonByte != nil {
return errJsonByte
}
return os.WriteFile(path, jsonByte, 0644)
}
4 changes: 2 additions & 2 deletions backend/save-quicksave.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (s *Save) GetQuickSave(gameID uuid.UUID) (bool, error) {
}

func (s *Save) AddQuicksave(gameID uuid.UUID) error {
s.updateJson()
s.UpdateJson()
s.createQuickSaveDir(gameID)
s.copyGameContentQuickSave(gameID)
s.logf("Created quicksave for: %v", gameID)
Expand All @@ -46,7 +46,7 @@ func (s *Save) LoadQuickSave(gameID uuid.UUID) error {
}

func (s *Save) RemoveQuickSave(gameID uuid.UUID) error {
s.updateJson()
s.UpdateJson()
s.logf("Deleted quicksave for: %v", gameID)
s.removeQuickSaveDir(gameID)
return nil
Expand Down
Loading

0 comments on commit 411ef03

Please sign in to comment.