Move LoginView to own struct

This commit is contained in:
Tulir Asokan 2018-03-22 23:03:45 +02:00
parent b93dea2e1c
commit 497ae566c0
3 changed files with 51 additions and 17 deletions

View File

@ -36,6 +36,7 @@ type GomuksUI interface {
SetView(name View) SetView(name View)
InitViews() tview.Primitive InitViews() tview.Primitive
MainView() MainView MainView() MainView
LoginView() LoginView
} }
type MainView interface { type MainView interface {
@ -51,3 +52,7 @@ type MainView interface {
ProcessMessageEvent(evt *gomatrix.Event) (*widget.RoomView, *types.Message) ProcessMessageEvent(evt *gomatrix.Event) (*widget.RoomView, *types.Message)
ProcessMembershipEvent(evt *gomatrix.Event, new bool) (*widget.RoomView, *types.Message) ProcessMembershipEvent(evt *gomatrix.Event, new bool) (*widget.RoomView, *types.Message)
} }
type LoginView interface {
}

View File

@ -28,7 +28,7 @@ type GomuksUI struct {
views *tview.Pages views *tview.Pages
mainView *MainView mainView *MainView
loginView *tview.Form loginView *LoginView
} }
func init() { func init() {
@ -63,3 +63,7 @@ func (ui *GomuksUI) InitViews() tview.Primitive {
func (ui *GomuksUI) MainView() ifc.MainView { func (ui *GomuksUI) MainView() ifc.MainView {
return ui.mainView return ui.mainView
} }
func (ui *GomuksUI) LoginView() ifc.LoginView {
return ui.loginView
}

View File

@ -17,39 +17,64 @@
package ui package ui
import ( import (
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/ui/debug" "maunium.net/go/gomuks/ui/debug"
"maunium.net/go/gomuks/ui/widget" "maunium.net/go/gomuks/ui/widget"
"maunium.net/go/tview" "maunium.net/go/tview"
) )
type LoginView struct {
*tview.Form
homeserver *widget.AdvancedInputField
username *widget.AdvancedInputField
password *widget.AdvancedInputField
matrix ifc.MatrixContainer
config *config.Config
parent *GomuksUI
}
func (ui *GomuksUI) NewLoginView() tview.Primitive { func (ui *GomuksUI) NewLoginView() tview.Primitive {
view := &LoginView{
Form: tview.NewForm(),
homeserver: widget.NewAdvancedInputField(),
username: widget.NewAdvancedInputField(),
password: widget.NewAdvancedInputField(),
matrix: ui.gmx.MatrixContainer(),
config: ui.gmx.Config(),
parent: ui,
}
hs := ui.gmx.Config().HS hs := ui.gmx.Config().HS
if len(hs) == 0 { if len(hs) == 0 {
hs = "https://matrix.org" hs = "https://matrix.org"
} }
view.homeserver.SetLabel("Homeserver").SetText(hs).SetFieldWidth(30)
view.username.SetLabel("Username").SetText(ui.gmx.Config().UserID).SetFieldWidth(30)
view.password.SetLabel("Password").SetMaskCharacter('*').SetFieldWidth(30)
homeserver := widget.NewAdvancedInputField().SetLabel("Homeserver").SetText(hs).SetFieldWidth(30) view.
username := widget.NewAdvancedInputField().SetLabel("Username").SetText(ui.gmx.Config().UserID).SetFieldWidth(30) AddFormItem(view.homeserver).AddFormItem(view.username).AddFormItem(view.password).
password := widget.NewAdvancedInputField().SetLabel("Password").SetMaskCharacter('*').SetFieldWidth(30) AddButton("Log in", view.Login).
ui.loginView = tview.NewForm()
ui.loginView.
AddFormItem(homeserver).AddFormItem(username).AddFormItem(password).
AddButton("Log in", ui.login).
AddButton("Quit", ui.gmx.Stop). AddButton("Quit", ui.gmx.Stop).
SetButtonsAlign(tview.AlignCenter). SetButtonsAlign(tview.AlignCenter).
SetBorder(true).SetTitle("Log in to Matrix") SetBorder(true).SetTitle("Log in to Matrix")
ui.loginView = view
return widget.Center(45, 11, ui.loginView) return widget.Center(45, 11, ui.loginView)
} }
func (ui *GomuksUI) login() { func (view *LoginView) Login() {
hs := ui.loginView.GetFormItem(0).(*widget.AdvancedInputField).GetText() hs := view.homeserver.GetText()
mxid := ui.loginView.GetFormItem(1).(*widget.AdvancedInputField).GetText() mxid := view.username.GetText()
password := ui.loginView.GetFormItem(2).(*widget.AdvancedInputField).GetText() password := view.password.GetText()
debug.Printf("Logging into %s as %s...", hs, mxid) debug.Printf("Logging into %s as %s...", hs, mxid)
ui.gmx.Config().HS = hs view.config.HS = hs
mx := ui.gmx.MatrixContainer() debug.Print("Connect result:", view.matrix.InitClient())
debug.Print("Connect result:", mx.InitClient()) debug.Print("Login result:", view.matrix.Login(mxid, password))
debug.Print("Login result:", mx.Login(mxid, password))
} }