Fix First(), Last(), Next() and Previous() of RoomList

This commit is contained in:
Tulir Asokan 2018-04-24 22:46:16 +03:00
parent 9ccceb48b9
commit 6aceb4f8d2
2 changed files with 28 additions and 19 deletions

View File

@ -23,6 +23,7 @@ import (
"strings" "strings"
"time" "time"
"maunium.net/go/gomuks/debug"
"maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/gomuks/ui/widget" "maunium.net/go/gomuks/ui/widget"
"maunium.net/go/tcell" "maunium.net/go/tcell"
@ -57,6 +58,7 @@ func NewRoomList() *RoomList {
return &RoomList{ return &RoomList{
Box: tview.NewBox(), Box: tview.NewBox(),
items: make(map[string][]*rooms.Room), items: make(map[string][]*rooms.Room),
tags: []string{"m.favourite", "im.vector.fake.direct", "", "m.lowpriority"},
mainTextColor: tcell.ColorWhite, mainTextColor: tcell.ColorWhite,
selectedTextColor: tcell.ColorWhite, selectedTextColor: tcell.ColorWhite,
@ -86,16 +88,17 @@ func (list *RoomList) CheckTag(tag string) {
items, ok := list.items[tag] items, ok := list.items[tag]
if len(items) == 0 { if ok && len(items) == 0 {
delete(list.items, tag) delete(list.items, tag)
ok = false ok = false
} }
if ok && index == -1 { if ok && index == -1 {
list.tags = append(list.tags, tag) list.tags = append(list.tags, tag)
} else if index != -1 { }/* TODO this doesn't work properly
else if index != -1 {
list.tags = append(list.tags[0:index], list.tags[index+1:]...) list.tags = append(list.tags[0:index], list.tags[index+1:]...)
} }*/
} }
func (list *RoomList) AddToTag(tag string, room *rooms.Room) { func (list *RoomList) AddToTag(tag string, room *rooms.Room) {
@ -211,6 +214,7 @@ func (list *RoomList) Clear() {
func (list *RoomList) SetSelected(tag string, room *rooms.Room) { func (list *RoomList) SetSelected(tag string, room *rooms.Room) {
list.selected = room list.selected = room
list.selectedTag = tag list.selectedTag = tag
debug.Print("Selecting", room.GetTitle(), "in", tag)
} }
func (list *RoomList) HasSelected() bool { func (list *RoomList) HasSelected() bool {
@ -229,7 +233,7 @@ func (list *RoomList) First() (string, *rooms.Room) {
for _, tag := range list.tags { for _, tag := range list.tags {
items := list.items[tag] items := list.items[tag]
if len(items) > 0 { if len(items) > 0 {
return tag, items[0] return tag, items[len(items)-1]
} }
} }
return "", nil return "", nil
@ -240,7 +244,7 @@ func (list *RoomList) Last() (string, *rooms.Room) {
tag := list.tags[tagIndex] tag := list.tags[tagIndex]
items := list.items[tag] items := list.items[tag]
if len(items) > 0 { if len(items) > 0 {
return tag, items[len(items)-1] return tag, items[0]
} }
} }
return "", nil return "", nil
@ -268,15 +272,15 @@ func (list *RoomList) Previous() (string, *rooms.Room) {
return list.First() return list.First()
} else if index == len(items)-1 { } else if index == len(items)-1 {
tagIndex := list.IndexTag(list.selectedTag) tagIndex := list.IndexTag(list.selectedTag)
tagIndex++ tagIndex--
for ; tagIndex < len(list.tags); tagIndex++ { for ; tagIndex >= 0; tagIndex-- {
nextTag := list.tags[tagIndex] prevTag := list.tags[tagIndex]
nextTagItems := list.items[nextTag] prevTagItems := list.items[prevTag]
if len(nextTagItems) > 0 { if len(prevTagItems) > 0 {
return nextTag, nextTagItems[0] return prevTag, prevTagItems[0]
} }
} }
return list.First() return list.Last()
} }
return list.selectedTag, items[index+1] return list.selectedTag, items[index+1]
} }
@ -294,15 +298,15 @@ func (list *RoomList) Next() (string, *rooms.Room) {
return list.Last() return list.Last()
} else if index == 0 { } else if index == 0 {
tagIndex := list.IndexTag(list.selectedTag) tagIndex := list.IndexTag(list.selectedTag)
tagIndex-- tagIndex++
for ; tagIndex >= 0; tagIndex-- { for ; tagIndex < len(list.tags); tagIndex++ {
prevTag := list.tags[tagIndex] nextTag := list.tags[tagIndex]
prevTagItems := list.items[prevTag] nextTagItems := list.items[nextTag]
if len(prevTagItems) > 0 { if len(nextTagItems) > 0 {
return prevTag, prevTagItems[len(prevTagItems)-1] return nextTag, nextTagItems[len(nextTagItems)-1]
} }
} }
return list.Last() return list.First()
} }
return list.selectedTag, items[index-1] return list.selectedTag, items[index-1]
} }
@ -352,6 +356,8 @@ func (list *RoomList) GetTagDisplayName(tag string) string {
return "Favorites" return "Favorites"
case tag == "m.lowpriority": case tag == "m.lowpriority":
return "Low Priority" return "Low Priority"
case tag == "im.vector.fake.direct":
return "People"
case strings.HasPrefix(tag, "m."): case strings.HasPrefix(tag, "m."):
return strings.Title(strings.Replace(tag[len("m."):], "_", " ", -1)) return strings.Title(strings.Replace(tag[len("m."):], "_", " ", -1))
case strings.HasPrefix(tag, "u."): case strings.HasPrefix(tag, "u."):

View File

@ -319,6 +319,9 @@ func (view *MainView) AddRoom(roomID string) {
room := view.matrix.GetRoom(roomID) room := view.matrix.GetRoom(roomID)
view.roomList.Add(room) view.roomList.Add(room)
view.addRoomPage(room) view.addRoomPage(room)
if !view.roomList.HasSelected() {
view.SwitchRoom(view.roomList.First())
}
} }
func (view *MainView) RemoveRoom(roomID string) { func (view *MainView) RemoveRoom(roomID string) {