Skip to content

Commit

Permalink
feat: add macro dump restore
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Apr 6, 2024
1 parent 2a7356e commit 232308c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.21.4

require (
github.com/jpillora/overseer v1.1.6
github.com/mishamyrt/nuga-lib v0.7.3
github.com/mishamyrt/nuga-lib v0.7.4
github.com/wailsapp/wails/v2 v2.8.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mishamyrt/nuga-lib v0.7.3 h1:dg0bCBmddtimdmEMRjgBFlf7SEvGtDpvE70xFnqhSNk=
github.com/mishamyrt/nuga-lib v0.7.3/go.mod h1:Zvch+eplvbZyH0ubJSIV9bWoSDLJtbjOiuwu/YEPsYA=
github.com/mishamyrt/nuga-lib v0.7.4 h1:Dx3zShk/QWE0B5IKE4c2fLkSlKw156TJIx2QZmqZ7A4=
github.com/mishamyrt/nuga-lib v0.7.4/go.mod h1:Zvch+eplvbZyH0ubJSIV9bWoSDLJtbjOiuwu/YEPsYA=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
28 changes: 28 additions & 0 deletions internal/dto/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package dto

import (
"bytes"
"fmt"

"github.com/mishamyrt/nuga-lib/features/keys"
"github.com/mishamyrt/nuga-lib/features/keys/layout"
)

// ErrMacrosLengthMismatch is returned when macros and titles have different lengths
var ErrMacrosLengthMismatch = fmt.Errorf("macros and titles must have the same length")

// KeyMap represents a map of keys.
type KeyMap layout.KeyMap

Expand Down Expand Up @@ -41,6 +45,30 @@ func (m Macros) ToDomain() keys.Macros {
return macros
}

// MacrosFromDomain converts keys.Macros to Macros
func MacrosFromDomain(k keys.Macros, titles []string) (Macros, error) {
if len(k) != len(titles) {
return nil, ErrMacrosLengthMismatch
}
m := make([]MacroWithTitle, 0, len(k))
for i, macro := range k {
m = append(m, MacroWithTitle{
Macro: macro,
Title: titles[i],
})
}
return m, nil
}

// Titles returns titles of macros
func (m Macros) Titles() []string {
titles := make([]string, 0, len(m))
for _, macro := range m {
titles = append(titles, macro.Title)
}
return titles
}

// Key ... as it names
type Key struct {
Title string `json:"title"`
Expand Down
3 changes: 2 additions & 1 deletion internal/dto/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

// StateFile represents keyboard state file
type StateFile struct {
Version string `json:"version"`
Version string `json:"version"`
MacroTitles []string `json:"macro_titles"`
*dump.State
}
23 changes: 21 additions & 2 deletions internal/usecase/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/mishamyrt/nuga-lib"
"github.com/mishamyrt/nuga-lib/dump"
"github.com/mishamyrt/nuga-lib/features/keys"
"github.com/wailsapp/wails/v2/pkg/runtime"
)

Expand Down Expand Up @@ -109,9 +110,11 @@ func (d *DeviceUsecase) SaveState() error {
if err != nil {
return err
}
titles := d.repo.Settings.GetMacros().Titles()
return d.repo.State.WriteFile(path, dto.StateFile{
Version: config.AppVersion,
State: &state,
Version: config.AppVersion,
State: &state,
MacroTitles: titles,
})
}

Expand Down Expand Up @@ -139,6 +142,22 @@ func (d *DeviceUsecase) RestoreState() error {
if state.Name != dev.Name {
return errors.ErrStateWrongDevice
}
err = dump.Restore(dev.Handle, state.State)
if err != nil {
return err
}
macros, err := keys.ParseHeadlessMacros(state.Keys.Macros)
if err != nil {
return err
}
macrosSettings, err := dto.MacrosFromDomain(macros, state.MacroTitles)
if err != nil {
return err
}
err = d.repo.Settings.SetMacros(macrosSettings)
if err != nil {
return err
}
return dump.Restore(dev.Handle, state.State)
}

Expand Down
7 changes: 7 additions & 0 deletions ui/src/entities/keys/model/stores/keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ sample({
target: keysInitiated
})

sample({
clock: anyStateRestored,
source: supportsStore,
filter: ({ keys }) => keys,
target: keysInitiated
})

// Load keys on device connect
sample({
clock: [keysInitiated, anyStateRestored, modeChanged],
Expand Down

0 comments on commit 232308c

Please sign in to comment.