2019-01-08 21:02:11 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/jroimartin/gocui"
|
2019-03-11 18:49:18 +01:00
|
|
|
"github.com/status-im/status-console-client/protocol/client"
|
2019-01-08 21:02:11 +01:00
|
|
|
)
|
|
|
|
|
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-03-11 18:49:18 +01:00
|
|
|
case client.ContactPublicChat:
|
2019-01-08 21:02:11 +01:00
|
|
|
return fmt.Sprintf("#%s", c.Name)
|
2019-03-11 18:49:18 +01:00
|
|
|
case client.ContactPrivateChat:
|
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
|
2019-03-11 18:49:18 +01:00
|
|
|
messenger *client.Messenger
|
|
|
|
contacts []client.Contact
|
2019-01-08 21:02:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContactsViewController returns a new contact view controller.
|
2019-03-11 18:49:18 +01:00
|
|
|
func NewContactsViewController(vm *ViewController, m *client.Messenger) *ContactsViewController {
|
|
|
|
return &ContactsViewController{ViewController: vm, messenger: m}
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-11 18:49:18 +01:00
|
|
|
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 {
|
2019-03-11 18:49:18 +01:00
|
|
|
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 {
|
|
|
|
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.
|
2019-03-11 18:49:18 +01:00
|
|
|
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-03-11 18:49:18 +01:00
|
|
|
}
|
|
|
|
|
2019-04-19 13:22:18 +02:00
|
|
|
// Remove removes a contact from the list.
|
2019-03-11 18:49:18 +01:00
|
|
|
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
|
|
|
}
|