diff options
author | Tulir Asokan <tulir@maunium.net> | 2018-04-23 23:22:18 +0300 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2018-04-24 02:13:43 +0300 |
commit | 135fcbf284e941a312567d22af80fe69d49cbd89 (patch) | |
tree | 2ebbed34c82f73d88d5041de7a16de68b4d524d5 /ui | |
parent | 1e8705319a2bc6461d8768273aac16df4b6df4be (diff) |
Make time-based room list sorting persistent (ref #11)
Diffstat (limited to 'ui')
-rw-r--r-- | ui/room-list.go | 24 | ||||
-rw-r--r-- | ui/view-main.go | 4 |
2 files changed, 25 insertions, 3 deletions
diff --git a/ui/room-list.go b/ui/room-list.go index 7637d4f..cad1b5a 100644 --- a/ui/room-list.go +++ b/ui/room-list.go @@ -19,6 +19,7 @@ package ui import ( "fmt" "strconv" + "time" "maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/ui/widget" @@ -29,7 +30,9 @@ import ( type RoomList struct { *tview.Box - items []*rooms.Room + // The list of rooms, in reverse order. + items []*rooms.Room + // The selected room. selected *rooms.Room // The item main text color. @@ -61,7 +64,18 @@ func (list *RoomList) Contains(roomID string) bool { } func (list *RoomList) Add(room *rooms.Room) { - list.items = append(list.items, room) + list.items = append(list.items, nil) + insertAt := len(list.items) - 1 + for i := 0; i < len(list.items)-1; i++ { + if list.items[i].LastReceivedMessage.After(room.LastReceivedMessage) { + insertAt = i + break + } + } + for i := len(list.items) - 1; i > insertAt; i-- { + list.items[i] = list.items[i-1] + } + list.items[insertAt] = room } func (list *RoomList) Remove(room *rooms.Room) { @@ -91,6 +105,7 @@ func (list *RoomList) Bump(room *rooms.Room) { } } list.items[len(list.items)-1] = room + room.LastReceivedMessage = time.Now() } func (list *RoomList) Clear() { @@ -150,7 +165,10 @@ func (list *RoomList) Index(room *rooms.Room) int { } func (list *RoomList) Get(n int) *rooms.Room { - return list.items[len(list.items)-1-(n%len(list.items))] + if n < 0 || n > len(list.items)-1 { + return nil + } + return list.items[len(list.items)-1-n] } // Draw draws this primitive onto the screen. diff --git a/ui/view-main.go b/ui/view-main.go index e9350e7..d1af09d 100644 --- a/ui/view-main.go +++ b/ui/view-main.go @@ -249,6 +249,10 @@ func (view *MainView) MouseEventHandler(roomView *RoomView, event *tcell.EventMo } func (view *MainView) SwitchRoom(room *rooms.Room) { + if room == nil { + return + } + view.roomView.SwitchToPage(room.ID) roomView := view.rooms[room.ID] if roomView.MessageView().ScrollOffset == 0 { |