Refresh contacts periodically and compare contacts correctly (#40)

This commit is contained in:
Dmitry Shulyak 2019-05-17 11:06:42 +03:00 committed by GitHub
parent 7cedfa8c10
commit a17396e618
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -93,7 +93,8 @@ func (c *ChatViewController) readEventsLoop(contact client.Contact) {
case client.EventWithError: case client.EventWithError:
c.onError(ev.GetError()) c.onError(ev.GetError())
case client.EventWithContact: 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 continue
} }
msgev, ok := ev.(client.EventWithMessage) msgev, ok := ev.(client.EventWithMessage)
@ -104,6 +105,7 @@ func (c *ChatViewController) readEventsLoop(contact client.Contact) {
continue continue
} }
msg := msgev.GetMessage() msg := msgev.GetMessage()
log.Printf("[ChatViewController::readEventsLoops] received message current clock %v - msg clock %v\n", clock, msg.Clock)
if msg.Clock < clock { if msg.Clock < clock {
inorder = false inorder = false
continue continue

View File

@ -2,11 +2,17 @@ package main
import ( import (
"fmt" "fmt"
"sync"
"time"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
"github.com/status-im/status-console-client/protocol/client" "github.com/status-im/status-console-client/protocol/client"
) )
const (
refreshInterval = time.Second
)
// contactToString returns a string representation. // contactToString returns a string representation.
func contactToString(c client.Contact) string { func contactToString(c client.Contact) string {
switch c.Type { switch c.Type {
@ -24,11 +30,14 @@ type ContactsViewController struct {
*ViewController *ViewController
messenger *client.MessengerV2 messenger *client.MessengerV2
contacts []client.Contact contacts []client.Contact
quit chan struct{}
once sync.Once
} }
// NewContactsViewController returns a new contact view controller. // NewContactsViewController returns a new contact view controller.
func NewContactsViewController(vm *ViewController, m *client.MessengerV2) *ContactsViewController { 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. // 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. // LoadAndRefresh loads contacts from the storage and refreshes the view.
func (c *ContactsViewController) LoadAndRefresh() error { 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 { if err := c.load(); err != nil {
return err return err
} }

View File

@ -61,6 +61,11 @@ func (c Contact) String() string {
return c.Name 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) { func (c Contact) MarshalJSON() ([]byte, error) {
type ContactAlias Contact type ContactAlias Contact