mirror of
https://github.com/status-im/status-console-client.git
synced 2025-02-24 00:28:18 +00:00
Refresh contacts periodically and compare contacts correctly (#40)
This commit is contained in:
parent
7cedfa8c10
commit
a17396e618
4
chat.go
4
chat.go
@ -93,7 +93,8 @@ func (c *ChatViewController) readEventsLoop(contact client.Contact) {
|
||||
case client.EventWithError:
|
||||
c.onError(ev.GetError())
|
||||
case client.EventWithContact:
|
||||
if ev.GetContact() != contact {
|
||||
log.Printf("[ChatViewController::readEventsLoops] selected contact %v, msg contact %v\n", contact, ev.GetContact())
|
||||
if !ev.GetContact().Equal(contact) {
|
||||
continue
|
||||
}
|
||||
msgev, ok := ev.(client.EventWithMessage)
|
||||
@ -104,6 +105,7 @@ func (c *ChatViewController) readEventsLoop(contact client.Contact) {
|
||||
continue
|
||||
}
|
||||
msg := msgev.GetMessage()
|
||||
log.Printf("[ChatViewController::readEventsLoops] received message current clock %v - msg clock %v\n", clock, msg.Clock)
|
||||
if msg.Clock < clock {
|
||||
inorder = false
|
||||
continue
|
||||
|
25
contacts.go
25
contacts.go
@ -2,11 +2,17 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/jroimartin/gocui"
|
||||
"github.com/status-im/status-console-client/protocol/client"
|
||||
)
|
||||
|
||||
const (
|
||||
refreshInterval = time.Second
|
||||
)
|
||||
|
||||
// contactToString returns a string representation.
|
||||
func contactToString(c client.Contact) string {
|
||||
switch c.Type {
|
||||
@ -24,11 +30,14 @@ type ContactsViewController struct {
|
||||
*ViewController
|
||||
messenger *client.MessengerV2
|
||||
contacts []client.Contact
|
||||
|
||||
quit chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// NewContactsViewController returns a new contact view controller.
|
||||
func NewContactsViewController(vm *ViewController, m *client.MessengerV2) *ContactsViewController {
|
||||
return &ContactsViewController{ViewController: vm, messenger: m}
|
||||
return &ContactsViewController{ViewController: vm, messenger: m, quit: make(chan struct{})}
|
||||
}
|
||||
|
||||
// refresh repaints the current list of contacts.
|
||||
@ -61,6 +70,20 @@ func (c *ContactsViewController) load() error {
|
||||
|
||||
// LoadAndRefresh loads contacts from the storage and refreshes the view.
|
||||
func (c *ContactsViewController) LoadAndRefresh() error {
|
||||
c.once.Do(func() {
|
||||
go func() {
|
||||
ticker := time.Tick(refreshInterval)
|
||||
for {
|
||||
select {
|
||||
case <-ticker:
|
||||
_ = c.LoadAndRefresh()
|
||||
case <-c.quit:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}()
|
||||
})
|
||||
if err := c.load(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -61,6 +61,11 @@ func (c Contact) String() string {
|
||||
return c.Name
|
||||
}
|
||||
|
||||
// Equal returns true if contacts have same name and same type.
|
||||
func (c Contact) Equal(other Contact) bool {
|
||||
return c.Name == other.Name && c.Type == other.Type
|
||||
}
|
||||
|
||||
func (c Contact) MarshalJSON() ([]byte, error) {
|
||||
type ContactAlias Contact
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user