diff --git a/discordo.go b/discordo.go index 19af472..83b8387 100644 --- a/discordo.go +++ b/discordo.go @@ -28,7 +28,6 @@ var ( messageInputField *tview.InputField mainFlex *tview.Flex - loginVia string kr keyring.Keyring config *util.Config discordSession *session.Session @@ -55,7 +54,6 @@ func main() { config = util.NewConfig() app = ui.NewApp(onAppInputCapture) - loginModal = ui.NewLoginModal(onLoginModalDone) guildsDropDown = ui.NewGuildsDropDown(onGuildsDropDownSelected, config.Theme) channelsTreeNode = tview.NewTreeNode("") channelsTreeView = ui.NewChannelsTreeView(channelsTreeNode, onChannelsTreeViewSelected, config.Theme) @@ -70,7 +68,8 @@ func main() { discordSession = newSession("", "", token) } else { - app.SetRoot(loginModal, true) + loginForm = ui.NewLoginForm(onLoginFormLoginButtonSelected) + app.SetRoot(loginForm, true) } if err := app.Run(); err != nil { @@ -95,18 +94,6 @@ func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey { return e } -func onLoginModalDone(i int, label string) { - if label == ui.LoginViaEmailPasswordLoginModalButton { - loginVia = "emailpassword" - loginForm = ui.NewLoginForm(loginVia, onLoginFormLoginButtonSelected) - app.SetRoot(loginForm, true) - } else if label == ui.LoginViaTokenLoginModalButton { - loginVia = "token" - loginForm = ui.NewLoginForm(loginVia, onLoginFormLoginButtonSelected) - app.SetRoot(loginForm, true) - } -} - func newSession(email string, password string, token string) *session.Session { api.UserAgent = "" + "Mozilla/5.0 (X11; Linux x86_64) " + @@ -224,28 +211,17 @@ func onChannelsTreeViewSelected(n *tview.TreeNode) { } func onLoginFormLoginButtonSelected() { - if loginVia == "emailpassword" { - email := loginForm.GetFormItemByLabel("Email").(*tview.InputField).GetText() - password := loginForm.GetFormItemByLabel("Password").(*tview.InputField).GetText() - if email == "" || password == "" { - return - } - - discordSession = newSession(email, password, "") - - go util.SetItem(kr, "token", discordSession.Token) - } else if loginVia == "token" { - token := loginForm.GetFormItemByLabel("Token").(*tview.InputField).GetText() - if token == "" { - return - } - - discordSession = newSession("", "", token) - - go util.SetItem(kr, "token", token) + email := loginForm.GetFormItemByLabel("Email").(*tview.InputField).GetText() + password := loginForm.GetFormItemByLabel("Password").(*tview.InputField).GetText() + if email == "" || password == "" { + return } app. SetRoot(mainFlex, true). SetFocus(guildsDropDown) + + discordSession = newSession(email, password, "") + + go util.SetItem(kr, "token", discordSession.Token) } diff --git a/ui/forms.go b/ui/forms.go index e42a3e1..9dee34b 100644 --- a/ui/forms.go +++ b/ui/forms.go @@ -5,23 +5,16 @@ import ( "github.com/rivo/tview" ) -func NewLoginForm(via string, onLoginFormLoginButtonSelected func()) *tview.Form { - loginForm := tview.NewForm() - - loginForm. +func NewLoginForm(onLoginFormLoginButtonSelected func()) *tview.Form { + f := tview.NewForm() + f. + AddInputField("Email", "", 0, nil, nil). + AddPasswordField("Password", "", 0, 0, nil). AddButton("Login", onLoginFormLoginButtonSelected). SetButtonsAlign(tview.AlignCenter). SetButtonBackgroundColor(tcell.GetColor("#5865F2")). SetBorder(true). SetBorderPadding(0, 0, 1, 1) - if via == "token" { - loginForm.AddPasswordField("Token", "", 0, 0, nil) - } else if via == "emailpassword" { - loginForm. - AddInputField("Email", "", 0, nil, nil). - AddPasswordField("Password", "", 0, 0, nil) - } - - return loginForm + return f } diff --git a/ui/modals.go b/ui/modals.go deleted file mode 100644 index af89470..0000000 --- a/ui/modals.go +++ /dev/null @@ -1,22 +0,0 @@ -package ui - -import ( - "github.com/rivo/tview" -) - -const ( - LoginViaTokenLoginModalButton = "Login via token" - LoginViaEmailPasswordLoginModalButton = "Login via email and password" -) - -func NewLoginModal(onLoginModalDone func(buttonIndex int, buttonLabel string)) *tview.Modal { - m := tview.NewModal() - m. - SetText("Choose a login method:"). - AddButtons([]string{LoginViaTokenLoginModalButton, LoginViaEmailPasswordLoginModalButton}). - SetDoneFunc(onLoginModalDone). - SetBorder(true). - SetBorderPadding(0, 0, 1, 1) - - return m -}