Skip to content

Commit

Permalink
refactor: replace the old input model with the new category popup
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Jul 4, 2023
1 parent 6db4293 commit dab7a88
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 239 deletions.
115 changes: 11 additions & 104 deletions internal/model/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/TypicalAM/goread/internal/backend"
"github.com/TypicalAM/goread/internal/config"
"github.com/TypicalAM/goread/internal/model/input"
"github.com/TypicalAM/goread/internal/model/tab"
"github.com/TypicalAM/goread/internal/model/tab/category"
"github.com/TypicalAM/goread/internal/model/tab/feed"
Expand All @@ -33,10 +32,6 @@ type Model struct {
windowWidth int
windowHeight int

// creating items
newItem bool
input input.Model

// other
message string
quitting bool
Expand Down Expand Up @@ -68,11 +63,6 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.waitForSize(msg)
}

// If we are creating new items, we need to update the inputs
if m.newItem {
return m.updateItemCreation(msg)
}

switch msg := msg.(type) {
case backend.FetchErrorMessage:
// If there is an error, display it on the status bar
Expand All @@ -83,17 +73,23 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.tabs[m.activeTab], _ = m.tabs[m.activeTab].Update(msg)
return m, nil

case popup.ChosenCategoryMsg:
m.message = fmt.Sprintf("Chosen category: %s", msg.Name)
m.popupShown = false
return m, nil

case tab.NewTabMessage:
// Create the new tab
m.createNewTab(msg.Title, msg.Type)
m.message = ""
return m, m.tabs[m.activeTab].Init()

case backend.NewItemMessage:
// Initialize the new Item model
m.input = input.New(msg.Type, msg.New, msg.Fields, msg.ItemPath, msg.OldFields)
m.newItem = true
return m, m.input.Init()
// Open a new popup
bg := lipgloss.NewStyle().Width(m.windowWidth).Height((m.windowHeight))
m.popup = popup.New(m.style.colors, bg.Render(m.View()), m.windowWidth/2, m.windowHeight/2+m.windowHeight/4)
m.popupShown = true
return m, m.popup.Init()

case backend.DeleteItemMessage:
// Delete the item
Expand Down Expand Up @@ -162,12 +158,6 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.message = fmt.Sprintf("Closed tab - %s", m.tabs[m.activeTab].Title())
return m, nil

case "g":
// Open a new popup
bg := lipgloss.NewStyle().Width(m.windowWidth).Height((m.windowHeight))
m.popup = popup.New(m.style.colors, bg.Render(m.View()), m.windowWidth/2, m.windowHeight/2+m.windowHeight/4)
m.popupShown = true

case "ctrl+h":
// View the help page
return m.showHelp()
Expand Down Expand Up @@ -217,16 +207,8 @@ func (m Model) View() string {
// Render the status bar
sections = append(sections, m.renderStatusBar())

// If we are typing, shift the focus onto the text-field
var messageBar string
if m.newItem {
messageBar = m.input.View()
} else {
messageBar = m.message
}

// Render the message bar
sections = append(sections, messageBar)
sections = append(sections, m.message)

// Join all the sections
return lipgloss.JoinVertical(lipgloss.Top, sections...)
Expand Down Expand Up @@ -259,31 +241,6 @@ func (m Model) waitForSize(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}

// updateItemCreation updates the child model for creating items
func (m Model) updateItemCreation(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)

// If the child model is done, add the item
switch m.input.State {
case input.NotEnoughText:
m.message = "Fields cannot be blank!"
m.newItem = false
return m, cmd

case input.Cancel:
m.message = "Cancelled adding or editing item"
m.newItem = false
return m, cmd

case input.Finished:
return m.addItem()

default:
return m, cmd
}
}

// createNewTab bootstraps the new tab and adds it to the model
func (m *Model) createNewTab(title string, tabType tab.Type) {
// Create and add the new tab
Expand Down Expand Up @@ -338,56 +295,6 @@ func (m *Model) createNewTab(title string, tabType tab.Type) {
m.activeTab++
}

// addItem gets the data from the child model and adds it to the rss
func (m Model) addItem() (tea.Model, tea.Cmd) {
// End creating new items
m.newItem = false
values := m.input.GetValues()

// Check if we are creating or editing the item
if m.input.Creating {
m.message = "Adding an item - " + strings.Join(values, " ")
} else {
m.message = "Editing an item - " + strings.Join(values, " ")
}

// Check if the values are valid
if m.input.Type == backend.Category {
var err error
if m.input.Creating {
err = m.config.Backend.Rss.AddCategory(values[0], values[1])
} else {
err = m.config.Backend.Rss.UpdateCategory(m.input.Path[0], values[0], values[1])
}

// Check if there was an error
if err != nil {
m.message = "Error adding or updating category: " + err.Error()
return m, nil
}

// Refresh the categories
return m, m.config.Backend.FetchCategories()
}

// Check if the feed already exists
var err error
if m.input.Creating {
err = m.config.Backend.Rss.AddFeed(m.tabs[m.activeTab].Title(), values[0], values[1])
} else {
err = m.config.Backend.Rss.UpdateFeed(m.input.Path[0], m.input.Path[1], values[0], values[1])
}

// Check if there was an error
if err != nil {
m.message = "Error adding or updating feed: " + err.Error()
return m, nil
}

// Refresh the feeds
return m, m.config.Backend.FetchFeeds(m.tabs[m.activeTab].Title())
}

// deleteItem deletes the focused item from the backend
func (m Model) deleteItem(msg backend.DeleteItemMessage) (tea.Model, tea.Cmd) {
m.message = fmt.Sprintf("Deleting item %s", msg.Key)
Expand Down
130 changes: 0 additions & 130 deletions internal/model/input/input.go

This file was deleted.

34 changes: 29 additions & 5 deletions internal/popup/popup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import (
"strings"

"github.com/TypicalAM/goread/internal/colorscheme"
"github.com/TypicalAM/goread/internal/rss"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/ansi"
)

// ChosenCategoryMsg is the message displayed when a category is successfully chosen.
type ChosenCategoryMsg struct {
Name string
}

// focusedField is the field that is currently focused.
type focusedField int

Expand Down Expand Up @@ -88,20 +94,34 @@ func (p Popup) Update(msg tea.Msg) (Popup, tea.Cmd) {
p.focused = downloadedField
case downloadedField:
p.focused = newCategoryField
cmds = append(cmds, p.textInput.Focus())
case newCategoryField:
p.focused = allField
cmds = append(cmds, p.textInput.Focus())
}

case "up", "k":
switch p.focused {
case allField:
p.focused = newCategoryField
cmds = append(cmds, p.textInput.Focus())
case downloadedField:
p.focused = allField
case newCategoryField:
p.focused = downloadedField
}

case "enter":
switch p.focused {
case allField:
return p, confirmCategory(rss.AllFeedsName)

case downloadedField:
return p, confirmCategory(rss.DownloadedFeedsName)

case newCategoryField:
// TODO: Validate the name
return p, confirmCategory(p.textInput.Value())
}
}
}

Expand Down Expand Up @@ -158,11 +178,8 @@ func (p Popup) View() string {
}

if p.focused == newCategoryField {
renderedChoices[2] = selectedChoiceStyle.Render(lipgloss.JoinVertical(lipgloss.Top, choices[2], descriptions[2]))
renderedChoices[2] = selectedChoiceStyle.Render(lipgloss.JoinVertical(lipgloss.Top, choices[2], p.textInput.View()))
} else {
if p.textInput.Focused() {
renderedChoices[2] = choiceStyle.Render(lipgloss.JoinVertical(lipgloss.Top, choices[2], "focused", p.textInput.View()))
}
renderedChoices[2] = choiceStyle.Render(lipgloss.JoinVertical(lipgloss.Top, choices[2], p.textInput.View()))
}

Expand Down Expand Up @@ -194,3 +211,10 @@ func findPrintableIndex(str string, index int) int {
}
return -1
}

// confirmCategory returns a tea.Cmd which relays the message to the browser.
func confirmCategory(name string) tea.Cmd {
return func() tea.Msg {
return ChosenCategoryMsg{Name: name}
}
}

0 comments on commit dab7a88

Please sign in to comment.