cli: Track and use conversation active state properly

This commit is contained in:
John Brooks 2016-10-26 16:12:16 -06:00
parent db6295d5c8
commit ffe15e6f0c
2 changed files with 30 additions and 10 deletions

View File

@ -28,6 +28,7 @@ type Conversation struct {
messages []*ricochet.Message messages []*ricochet.Message
numUnread int numUnread int
active bool
} }
// Send an outbound message to the contact and add that message into the // Send an outbound message to the contact and add that message into the
@ -68,11 +69,10 @@ func (c *Conversation) AddMessage(msg *ricochet.Message, populating bool) {
} }
c.trimBacklog() c.trimBacklog()
if !populating { if !populating {
// XXX Need to do mark-as-read when displaying received messages in
// an active conversation.
// XXX Also, need to limit prints to the active conversation.
// XXX Quite possibly, more of the active conversation logic belongs here.
c.printMessage(msg) c.printMessage(msg)
if c.active {
c.MarkAsReadBefore(msg)
}
} }
} }
@ -239,6 +239,15 @@ func (c *Conversation) validateMessage(msg *ricochet.Message) error {
} }
func (c *Conversation) printMessage(msg *ricochet.Message) { func (c *Conversation) printMessage(msg *ricochet.Message) {
if !c.active {
messages := fmt.Sprintf("%d new message", c.numUnread)
if c.numUnread > 1 {
messages += "s"
}
fmt.Fprintf(Ui.Stdout, "\r\x1b[31m[[ \x1b[1;34m%s\x1b[0m from \x1b[1m%s\x1b[0m (\x1b[1m%d\x1b[0m) \x1b[31m]]\x1b[39m\n", messages, c.Contact.Data.Nickname, c.Contact.Data.Id)
return
}
// XXX actual timestamp // XXX actual timestamp
ts := "\x1b[90m" + time.Now().Format("15:04") + "\x1b[39m" ts := "\x1b[90m" + time.Now().Format("15:04") + "\x1b[39m"
@ -255,10 +264,6 @@ func (c *Conversation) printMessage(msg *ricochet.Message) {
c.Contact.Data.Nickname, c.Contact.Data.Nickname,
direction, direction,
msg.Text) msg.Text)
/*
fmt.Fprintf(ui.Input.Stdout(), "\r\x1b[31m( \x1b[39mNew message from \x1b[31m%s\x1b[39m -- \x1b[1m/%d\x1b[0m to view \x1b[31m)\x1b[39m\n", contact.Data.Nickname, contact.Data.Id)
*/
} }
func (c *Conversation) UnreadCount() int { func (c *Conversation) UnreadCount() int {
@ -273,3 +278,15 @@ func (c *Conversation) recountUnread() {
} }
} }
} }
func (c *Conversation) SetActive(active bool) {
if active == c.active {
return
}
c.active = active
if active {
c.PrintContext()
c.MarkAsRead()
}
}

View File

@ -282,12 +282,15 @@ func (ui *UI) SetCurrentContact(contact *Contact) {
return return
} }
if ui.CurrentContact != nil {
ui.CurrentContact.Conversation.SetActive(false)
}
ui.CurrentContact = contact ui.CurrentContact = contact
if ui.CurrentContact != nil { if ui.CurrentContact != nil {
ui.setupConversationPrompt() ui.setupConversationPrompt()
fmt.Fprintf(ui.Stdout, "------- \x1b[1m%s\x1b[0m is %s -------\n", contact.Data.Nickname, ColoredContactStatus(contact.Data.Status)) fmt.Fprintf(ui.Stdout, "------- \x1b[1m%s\x1b[0m is %s -------\n", contact.Data.Nickname, ColoredContactStatus(contact.Data.Status))
contact.Conversation.PrintContext() ui.CurrentContact.Conversation.SetActive(true)
contact.Conversation.MarkAsRead()
} else { } else {
ui.Input.Config.Listener.(*conversationInputConfig).Remove() ui.Input.Config.Listener.(*conversationInputConfig).Remove()
ui.Input.SetConfig(ui.baseConfig) ui.Input.SetConfig(ui.baseConfig)