status-console-client/contacts.go

117 lines
2.5 KiB
Go
Raw Normal View History

2019-01-08 21:02:11 +01:00
package main
import (
"fmt"
"sync"
"time"
2019-01-08 21:02:11 +01:00
"github.com/jroimartin/gocui"
"github.com/status-im/status-console-client/protocol/client"
2019-01-08 21:02:11 +01:00
)
const (
refreshInterval = time.Second
)
2019-04-19 13:22:18 +02:00
// contactToString returns a string representation.
func contactToString(c client.Contact) string {
2019-01-08 21:02:11 +01:00
switch c.Type {
2019-04-30 10:24:33 +02:00
case client.ContactPublicRoom:
2019-01-08 21:02:11 +01:00
return fmt.Sprintf("#%s", c.Name)
2019-04-30 10:24:33 +02:00
case client.ContactPublicKey:
2019-01-24 12:39:23 +01:00
return fmt.Sprintf("@%s", c.Name)
2019-01-08 21:02:11 +01:00
default:
return c.Name
}
}
// ContactsViewController manages contacts view.
type ContactsViewController struct {
*ViewController
messenger *client.MessengerV2
contacts []client.Contact
quit chan struct{}
once sync.Once
2019-01-08 21:02:11 +01:00
}
// NewContactsViewController returns a new contact view controller.
func NewContactsViewController(vm *ViewController, m *client.MessengerV2) *ContactsViewController {
return &ContactsViewController{ViewController: vm, messenger: m, quit: make(chan struct{})}
2019-01-08 21:02:11 +01:00
}
2019-04-19 13:22:18 +02:00
// refresh repaints the current list of contacts.
func (c *ContactsViewController) refresh() {
2019-01-08 21:02:11 +01:00
c.g.Update(func(*gocui.Gui) error {
if err := c.Clear(); err != nil {
return err
}
for _, contact := range c.contacts {
2019-04-19 13:22:18 +02:00
if _, err := fmt.Fprintln(c.ViewController, contactToString(contact)); err != nil {
2019-01-08 21:02:11 +01:00
return err
}
}
return nil
})
}
2019-02-25 08:29:35 +01:00
2019-04-19 13:22:18 +02:00
// load loads contacts from the storage.
func (c *ContactsViewController) load() error {
contacts, err := c.messenger.Contacts()
if err != nil {
return err
}
c.contacts = contacts
return nil
}
2019-04-19 13:22:18 +02:00
// 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
}
}
}()
})
2019-04-19 13:22:18 +02:00
if err := c.load(); err != nil {
return err
}
c.refresh()
return nil
}
// ContactByIdx allows to retrieve a contact for a given index.
func (c *ContactsViewController) ContactByIdx(idx int) (client.Contact, bool) {
if idx > -1 && idx < len(c.contacts) {
return c.contacts[idx], true
}
return client.Contact{}, false
}
// Add adds a new contact to the list.
func (c *ContactsViewController) Add(contact client.Contact) error {
2019-04-19 13:22:18 +02:00
if err := c.messenger.AddContact(contact); err != nil {
return err
}
return c.LoadAndRefresh()
}
2019-04-19 13:22:18 +02:00
// Remove removes a contact from the list.
func (c *ContactsViewController) Remove(contact client.Contact) error {
2019-04-19 13:22:18 +02:00
if err := c.messenger.RemoveContact(contact); err != nil {
return err
}
return c.LoadAndRefresh()
2019-02-25 08:29:35 +01:00
}