diff --git a/main.go b/main.go index 7f4ee3ea7d1f90d2575be5c1e9888dd53390a951..a8cc2284213ec80fc79533019f77cbcc438f83a1 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,12 @@ type mainModel struct { // newInitialModel now returns a pointer, which is crucial for state management. func newInitialModel(cfg *config.Config) *mainModel { + // If config is nil, it means we're starting from the login screen. + if cfg == nil { + return &mainModel{ + current: tui.NewLogin(), + } + } return &mainModel{ current: tui.NewChoice(), config: cfg, @@ -61,6 +67,22 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } // --- Custom Messages for switching views --- + case tui.Credentials: // This is a struct, not a pointer + cfg := &config.Config{ + ServiceProvider: msg.Provider, + Name: msg.Name, + Email: msg.Email, + Password: msg.Password, + } + if err := config.SaveConfig(cfg); err != nil { + // TODO: Show an error message to the user + log.Printf("could not save config: %v", err) + return m, tea.Quit + } + m.config = cfg + m.current = tui.NewChoice() + cmds = append(cmds, m.current.Init()) + case tui.GoToInboxMsg: m.current = tui.NewStatus("Fetching emails...") return m, tea.Batch(m.current.Init(), fetchEmails(m.config)) @@ -127,13 +149,15 @@ func fetchEmails(cfg *config.Config) tea.Cmd { func main() { cfg, err := config.LoadConfig() + var initialModel *mainModel if err != nil { // If the config doesn't exist, we can guide the user to create one. - // For now, we'll just log a fatal error. - log.Fatalf("could not load config: %v", err) + initialModel = newInitialModel(nil) + } else { + initialModel = newInitialModel(cfg) } - p := tea.NewProgram(newInitialModel(cfg), tea.WithAltScreen()) + p := tea.NewProgram(initialModel, tea.WithAltScreen()) if _, err := p.Run(); err != nil { fmt.Printf("Alas, there's been an error: %v", err) diff --git a/tui/login.go b/tui/login.go index 343ffcb71fcb0dddb6262371a25c2e064ebe6790..ec7b67fafd17263dd5d6211e5bcc9a8f8d3150ff 100644 --- a/tui/login.go +++ b/tui/login.go @@ -12,10 +12,10 @@ type Login struct { inputs []textinput.Model } -// NewLogin creates a new login model. This function was missing. +// NewLogin creates a new login model. func NewLogin() tea.Model { m := Login{ - inputs: make([]textinput.Model, 2), + inputs: make([]textinput.Model, 4), // Increased to 4 for provider, name, email, and password } var t textinput.Model @@ -26,10 +26,16 @@ func NewLogin() tea.Model { switch i { case 0: - t.Placeholder = "Email" + t.Placeholder = "Provider (gmail or icloud)" t.Focus() - t.Prompt = "✉️ > " + t.Prompt = "☁️ > " case 1: + t.Placeholder = "Name" + t.Prompt = "👤 > " + case 2: + t.Placeholder = "Email" + t.Prompt = "✉️ > " + case 3: t.Placeholder = "Password" t.EchoMode = textinput.EchoPassword t.Prompt = "🔑 > " @@ -55,8 +61,10 @@ func (m Login) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.focusIndex == len(m.inputs)-1 { return m, func() tea.Msg { return Credentials{ - Email: m.inputs[0].Value(), - Password: m.inputs[1].Value(), + Provider: m.inputs[0].Value(), + Name: m.inputs[1].Value(), + Email: m.inputs[2].Value(), + Password: m.inputs[3].Value(), } } } @@ -103,6 +111,8 @@ func (m Login) View() string { "Please enter your credentials.", m.inputs[0].View(), m.inputs[1].View(), + m.inputs[2].View(), + m.inputs[3].View(), helpStyle.Render("\nenter: submit • tab: next field • esc: quit"), ) } \ No newline at end of file diff --git a/tui/messages.go b/tui/messages.go index 71dd2d9ae6d7de1889deeabe24b791a064b71e7a..4662a525b5cf8b3aa8014a42df0c70a2e19e7eb2 100644 --- a/tui/messages.go +++ b/tui/messages.go @@ -16,6 +16,8 @@ type SendEmailMsg struct { // A message to indicate that the user has entered their credentials. type Credentials struct { + Provider string + Name string Email string Password string }