From 135fcbf284e941a312567d22af80fe69d49cbd89 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 23 Apr 2018 23:22:18 +0300 Subject: Make time-based room list sorting persistent (ref #11) --- ui/room-list.go | 24 +++++++++++++++++++++--- ui/view-main.go | 4 ++++ 2 files changed, 25 insertions(+), 3 deletions(-) (limited to 'ui') 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 { -- cgit v1.2.3