Skip to content

Commit

Permalink
Add Remove to data binding lists and trees
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Nov 15, 2023
1 parent 4128839 commit c8e06cd
Show file tree
Hide file tree
Showing 3 changed files with 483 additions and 0 deletions.
232 changes: 232 additions & 0 deletions data/binding/bindlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type BoolList interface {
Get() ([]bool, error)
GetValue(index int) (bool, error)
Prepend(value bool) error
Remove(value bool) error
Set(list []bool) error
SetValue(index int, value bool) error
}
Expand Down Expand Up @@ -106,6 +107,34 @@ func (l *boundBoolList) Reload() error {
return l.doReload()
}

func (l *boundBoolList) Remove(val bool) error {
l.lock.Lock()
defer l.lock.Unlock()

if len(*l.val) == 0 {
return nil
}
if (*l.val)[0] == val {
*l.val = (*l.val)[1:]
} else if (*l.val)[len(*l.val)-1] == val {
*l.val = (*l.val)[:len(*l.val)]
} else {
id := -1
for i, v := range *l.val {
if v == val {
id = i
}
}

if id == -1 {
return nil
}
*l.val = append((*l.val)[:id], (*l.val)[id+1:]...)
}

return l.doReload()
}

func (l *boundBoolList) Set(v []bool) error {
l.lock.Lock()
defer l.lock.Unlock()
Expand Down Expand Up @@ -241,6 +270,7 @@ type BytesList interface {
Get() ([][]byte, error)
GetValue(index int) ([]byte, error)
Prepend(value []byte) error
Remove(value []byte) error
Set(list [][]byte) error
SetValue(index int, value []byte) error
}
Expand Down Expand Up @@ -328,6 +358,34 @@ func (l *boundBytesList) Reload() error {
return l.doReload()
}

func (l *boundBytesList) Remove(val []byte) error {
l.lock.Lock()
defer l.lock.Unlock()

if len(*l.val) == 0 {
return nil
}
if bytes.Equal((*l.val)[0], val) {
*l.val = (*l.val)[1:]
} else if bytes.Equal((*l.val)[len(*l.val)-1], val) {
*l.val = (*l.val)[:len(*l.val)]
} else {
id := -1
for i, v := range *l.val {
if bytes.Equal(v, val) {
id = i
}
}

if id == -1 {
return nil
}
*l.val = append((*l.val)[:id], (*l.val)[id+1:]...)
}

return l.doReload()
}

func (l *boundBytesList) Set(v [][]byte) error {
l.lock.Lock()
defer l.lock.Unlock()
Expand Down Expand Up @@ -463,6 +521,7 @@ type FloatList interface {
Get() ([]float64, error)
GetValue(index int) (float64, error)
Prepend(value float64) error
Remove(value float64) error
Set(list []float64) error
SetValue(index int, value float64) error
}
Expand Down Expand Up @@ -550,6 +609,34 @@ func (l *boundFloatList) Reload() error {
return l.doReload()
}

func (l *boundFloatList) Remove(val float64) error {
l.lock.Lock()
defer l.lock.Unlock()

if len(*l.val) == 0 {
return nil
}
if (*l.val)[0] == val {
*l.val = (*l.val)[1:]
} else if (*l.val)[len(*l.val)-1] == val {
*l.val = (*l.val)[:len(*l.val)]
} else {
id := -1
for i, v := range *l.val {
if v == val {
id = i
}
}

if id == -1 {
return nil
}
*l.val = append((*l.val)[:id], (*l.val)[id+1:]...)
}

return l.doReload()
}

func (l *boundFloatList) Set(v []float64) error {
l.lock.Lock()
defer l.lock.Unlock()
Expand Down Expand Up @@ -685,6 +772,7 @@ type IntList interface {
Get() ([]int, error)
GetValue(index int) (int, error)
Prepend(value int) error
Remove(value int) error
Set(list []int) error
SetValue(index int, value int) error
}
Expand Down Expand Up @@ -772,6 +860,34 @@ func (l *boundIntList) Reload() error {
return l.doReload()
}

func (l *boundIntList) Remove(val int) error {
l.lock.Lock()
defer l.lock.Unlock()

if len(*l.val) == 0 {
return nil
}
if (*l.val)[0] == val {
*l.val = (*l.val)[1:]
} else if (*l.val)[len(*l.val)-1] == val {
*l.val = (*l.val)[:len(*l.val)]
} else {
id := -1
for i, v := range *l.val {
if v == val {
id = i
}
}

if id == -1 {
return nil
}
*l.val = append((*l.val)[:id], (*l.val)[id+1:]...)
}

return l.doReload()
}

func (l *boundIntList) Set(v []int) error {
l.lock.Lock()
defer l.lock.Unlock()
Expand Down Expand Up @@ -907,6 +1023,7 @@ type RuneList interface {
Get() ([]rune, error)
GetValue(index int) (rune, error)
Prepend(value rune) error
Remove(value rune) error
Set(list []rune) error
SetValue(index int, value rune) error
}
Expand Down Expand Up @@ -994,6 +1111,34 @@ func (l *boundRuneList) Reload() error {
return l.doReload()
}

func (l *boundRuneList) Remove(val rune) error {
l.lock.Lock()
defer l.lock.Unlock()

if len(*l.val) == 0 {
return nil
}
if (*l.val)[0] == val {
*l.val = (*l.val)[1:]
} else if (*l.val)[len(*l.val)-1] == val {
*l.val = (*l.val)[:len(*l.val)]
} else {
id := -1
for i, v := range *l.val {
if v == val {
id = i
}
}

if id == -1 {
return nil
}
*l.val = append((*l.val)[:id], (*l.val)[id+1:]...)
}

return l.doReload()
}

func (l *boundRuneList) Set(v []rune) error {
l.lock.Lock()
defer l.lock.Unlock()
Expand Down Expand Up @@ -1129,6 +1274,7 @@ type StringList interface {
Get() ([]string, error)
GetValue(index int) (string, error)
Prepend(value string) error
Remove(value string) error
Set(list []string) error
SetValue(index int, value string) error
}
Expand Down Expand Up @@ -1216,6 +1362,34 @@ func (l *boundStringList) Reload() error {
return l.doReload()
}

func (l *boundStringList) Remove(val string) error {
l.lock.Lock()
defer l.lock.Unlock()

if len(*l.val) == 0 {
return nil
}
if (*l.val)[0] == val {
*l.val = (*l.val)[1:]
} else if (*l.val)[len(*l.val)-1] == val {
*l.val = (*l.val)[:len(*l.val)]
} else {
id := -1
for i, v := range *l.val {
if v == val {
id = i
}
}

if id == -1 {
return nil
}
*l.val = append((*l.val)[:id], (*l.val)[id+1:]...)
}

return l.doReload()
}

func (l *boundStringList) Set(v []string) error {
l.lock.Lock()
defer l.lock.Unlock()
Expand Down Expand Up @@ -1351,6 +1525,7 @@ type UntypedList interface {
Get() ([]interface{}, error)
GetValue(index int) (interface{}, error)
Prepend(value interface{}) error
Remove(value interface{}) error
Set(list []interface{}) error
SetValue(index int, value interface{}) error
}
Expand Down Expand Up @@ -1438,6 +1613,34 @@ func (l *boundUntypedList) Reload() error {
return l.doReload()
}

func (l *boundUntypedList) Remove(val interface{}) error {
l.lock.Lock()
defer l.lock.Unlock()

if len(*l.val) == 0 {
return nil
}
if (*l.val)[0] == val {
*l.val = (*l.val)[1:]
} else if (*l.val)[len(*l.val)-1] == val {
*l.val = (*l.val)[:len(*l.val)]
} else {
id := -1
for i, v := range *l.val {
if v == val {
id = i
}
}

if id == -1 {
return nil
}
*l.val = append((*l.val)[:id], (*l.val)[id+1:]...)
}

return l.doReload()
}

func (l *boundUntypedList) Set(v []interface{}) error {
l.lock.Lock()
defer l.lock.Unlock()
Expand Down Expand Up @@ -1573,6 +1776,7 @@ type URIList interface {
Get() ([]fyne.URI, error)
GetValue(index int) (fyne.URI, error)
Prepend(value fyne.URI) error
Remove(value fyne.URI) error
Set(list []fyne.URI) error
SetValue(index int, value fyne.URI) error
}
Expand Down Expand Up @@ -1660,6 +1864,34 @@ func (l *boundURIList) Reload() error {
return l.doReload()
}

func (l *boundURIList) Remove(val fyne.URI) error {
l.lock.Lock()
defer l.lock.Unlock()

if len(*l.val) == 0 {
return nil
}
if compareURI((*l.val)[0], val) {
*l.val = (*l.val)[1:]
} else if compareURI((*l.val)[len(*l.val)-1], val) {
*l.val = (*l.val)[:len(*l.val)]
} else {
id := -1
for i, v := range *l.val {
if compareURI(v, val) {
id = i
}
}

if id == -1 {
return nil
}
*l.val = append((*l.val)[:id], (*l.val)[id+1:]...)
}

return l.doReload()
}

func (l *boundURIList) Set(v []fyne.URI) error {
l.lock.Lock()
defer l.lock.Unlock()
Expand Down
Loading

0 comments on commit c8e06cd

Please sign in to comment.