-1

Im trying to create a map that remembers its values when the page is recalled. I declared it outside the function so it would remain the same but the map still initializes to the defauly=t values when the page is recalled. How can i get it to remeber its values?

var rememberExpand =  make(map[int]bool{})

func (c *CollapsibleWithOption) Layout(gtx layout.Context, header, body func(C) D, more func(C), wallet_ID int) layout.Dimensions {
    fmt.Println(rememberExpand)
    for c.button.Clicked() {
        c.isExpanded = !c.isExpanded
    }

    rememberExpand[wallet_ID] = c.isExpanded
    fmt.Println(rememberExpand)

    icon := c.collapsedIcon
    if c.isExpanded {
        icon = c.expandedIcon
    }


                        
                    )
                })
            }),
            layout.Rigid(func(gtx C) D {
                if rememberExpand[wallet_ID] {
                    return body(gtx)
                }
                return D{}
            }),
        )
    })
}
jcezetah
  • 3
  • 1
  • 1
    Firstly, `make(map[int]bool{})` does not compile as `bool{}` is not a type - I think you mean `make(map[int]bool)` – colm.anseo Apr 13 '22 at 17:18
  • Secondly if you are allowing concurrent access to your map (from page requests) - you need to put a lock around any map read/writes. – colm.anseo Apr 13 '22 at 17:19
  • Back to "firstly", did you mean to paste `var rememberExpand = map[int]bool{}` - which does initialize the map – colm.anseo Apr 13 '22 at 17:23
  • @colm.anseo for the first part its meant to be "var rememberExpand map[int]bool", which should initiate the map. it needs to be outside as if its in the function it does not hold its old values and its reinitialized when the page is called which I dont want to happen. It doesn't need a lock I believe because its only one function accessing it. The answer below completely solved my issue. – jcezetah Apr 14 '22 at 17:13
  • `var rememberExpand = map[int]bool{}` will initialize the map for you, so there's no need for a `nil` check – colm.anseo Apr 14 '22 at 17:50

1 Answers1

0

Try defining the map as `var rememberExpand map[int]bool.

This however, leads to a new problem. The first time you reference this map, it will be nil, because it won't have been made yet. We can fix this fairly easily by checking if the map is equal to nil, and if it is, create a new map:

var rememberExpand map[int]bool

func (c *CollapsibleWithOption) Layout(gtx layout.Context, header, body func(C) D, more func(C), wallet_ID int) layout.Dimensions {
    if rememberExpand == nil { // Check if the map has been initialized yet
      rememberExpand = make(map[int]bool) // If not, create a new map
    }

    for c.button.Clicked() {
        c.isExpanded = !c.isExpanded
    }

    rememberExpand[wallet_ID] = c.isExpanded
    fmt.Println(rememberExpand)

    icon := c.collapsedIcon
    if c.isExpanded {
        icon = c.expandedIcon
    }


                        
                    )
                })
            }),
            layout.Rigid(func(gtx C) D {
                if rememberExpand[wallet_ID] {
                    return body(gtx)
                }
                return D{}
            }),
        )
    })
}
Thijs van der Heijden
  • 1,147
  • 1
  • 10
  • 25
  • 2
    `var rememberExpand = map[int]bool{}` initializes the map once at program startup - it does not reset the map "every time you reference rememberExpand". Also you need `sync.Mutex` around any changes to `rememberExpand` to avoid a data-race. – colm.anseo Apr 13 '22 at 17:24
  • @colm.anseo Okay, I did not know that, Go is smarter than I expected... Would you also need a mutex if only one routine is used? – Thijs van der Heijden Apr 13 '22 at 17:30
  • concurrent access means 2 or more goroutines. So if only one then no mutex would be needed. However, since this for page requests - with each page in its own goroutine - then access much be controlled. – colm.anseo Apr 13 '22 at 17:32
  • @colm.anseo Not sure if Gioui (which is the UI framework being used here) uses routines, but you make a good point. I will remove my answer as it is not correct, but I honestly don't see the issue in the given code, it probably has something to do with the render function not being called again after an update, therefore making it seem like the map did not update? – Thijs van der Heijden Apr 13 '22 at 17:35
  • @jcezetah glad I could help, not sure how my answer did, as it was not correct :P – Thijs van der Heijden Apr 13 '22 at 17:40