Skip to content

Commit

Permalink
Migrate preference in bindings after cloud changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jul 29, 2021
1 parent 9bb3fc0 commit 185fd13
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions data/binding/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ func (b *prefBound{{ .Name }}) checkForChange() {
b.trigger()
}
func (b *prefBound{{ .Name }}) replaceProvider(p fyne.Preferences) {
b.p = p
}
`

const toStringTemplate = `
Expand Down
24 changes: 24 additions & 0 deletions data/binding/pref_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func (b *preferenceBindings) setItem(key string, item preferenceItem) {
type preferencesMap struct {
lock sync.RWMutex
prefs map[fyne.Preferences]*preferenceBindings

appPrefs fyne.Preferences // the main application prefs, to check if it changed...
}

func newPreferencesMap() *preferencesMap {
Expand Down Expand Up @@ -74,6 +76,14 @@ func (m *preferencesMap) ensurePreferencesAttached(p fyne.Preferences) *preferen

func (m *preferencesMap) getBindings(p fyne.Preferences) *preferenceBindings {
m.lock.RLock()
if p == fyne.CurrentApp().Preferences() {
if m.appPrefs == nil {
m.appPrefs = p
} else if m.appPrefs != p {
m.migratePreferences(m.appPrefs, p)
}
}

binds := m.prefs[p]
m.lock.RUnlock()
return binds
Expand All @@ -88,3 +98,17 @@ func (m *preferencesMap) preferencesChanged(p fyne.Preferences) {
item.checkForChange()
}
}

func (m *preferencesMap) migratePreferences(p1, p2 fyne.Preferences) {
m.prefs[p2] = m.prefs[p1]
delete(m.prefs, p1)
m.appPrefs = p2

for _, b := range m.prefs[p2].list() {
if backed, ok := b.(interface{ replaceProvider(fyne.Preferences) }); ok {
backed.replaceProvider(p2)
}
}

m.preferencesChanged(p2)
}
16 changes: 16 additions & 0 deletions data/binding/preference.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (b *prefBoundBool) checkForChange() {
b.trigger()
}

func (b *prefBoundBool) replaceProvider(p fyne.Preferences) {
b.p = p
}

type prefBoundFloat struct {
base
key string
Expand Down Expand Up @@ -127,6 +131,10 @@ func (b *prefBoundFloat) checkForChange() {
b.trigger()
}

func (b *prefBoundFloat) replaceProvider(p fyne.Preferences) {
b.p = p
}

type prefBoundInt struct {
base
key string
Expand Down Expand Up @@ -185,6 +193,10 @@ func (b *prefBoundInt) checkForChange() {
b.trigger()
}

func (b *prefBoundInt) replaceProvider(p fyne.Preferences) {
b.p = p
}

type prefBoundString struct {
base
key string
Expand Down Expand Up @@ -242,3 +254,7 @@ func (b *prefBoundString) checkForChange() {

b.trigger()
}

func (b *prefBoundString) replaceProvider(p fyne.Preferences) {
b.p = p
}
1 change: 1 addition & 0 deletions data/binding/preference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func TestPreferenceBindingCopies(t *testing.T) {
func TestPreferenceBindingTriggers(t *testing.T) {
a := test.NewApp()
p := a.Preferences()
prefBinds.appPrefs = nil // reset app migration checks
key := "test-string"

p.SetString(key, "aString")
Expand Down

0 comments on commit 185fd13

Please sign in to comment.