Skip to content

Commit

Permalink
feat: adding and removing feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Jan 3, 2023
1 parent 2207026 commit e032c23
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 22 deletions.
11 changes: 9 additions & 2 deletions internal/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,18 @@ type NewItemMessage struct {

// NewItem is a function to tell the main model that a new item
// needs to be added to the list
func NewItem(itemType ItemType, fields ...string) tea.Cmd {
func NewItem(itemType ItemType) tea.Cmd {
return func() tea.Msg {
var textFields []string
if itemType == Category {
textFields = []string{"Name", "Description"}
} else {
textFields = []string{"Name", "URL"}
}

return NewItemMessage{
Type: itemType,
Fields: fields,
Fields: textFields,
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions internal/model/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,25 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case backend.DeleteItemMessage:
// Delete the item
m.message = fmt.Sprintf("Deleted item %s", msg.Key)
m.message = fmt.Sprintf("Deleting item %s", msg.Key)

// Check the type of the item
if msg.Type == backend.Category {
err := m.backend.Rss().RemoveCategory(msg.Key)
if err != nil {
m.message = fmt.Sprintf("Error deleting category %s - %s", msg.Key, err.Error())
}
}

// Fetch the categories again to update the list
if m.createItem.Type == backend.Category {
// Refresh the categories
return m, m.backend.FetchCategories()
}

// Delete the feed
err := m.backend.Rss().RemoveFeed(m.tabs[m.activeTab].Title(), msg.Key)
if err != nil {
m.message = fmt.Sprintf("Error deleting feed %s - %s", msg.Key, err.Error())
}

// Fetch the feeds again to update the list
return m, m.backend.FetchFeeds(m.tabs[m.activeTab].Title())

Expand Down
38 changes: 33 additions & 5 deletions internal/rss/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,41 @@ func (rss *Rss) AddFeed(category string, name string, url string) error {

// RemoveCategory will remove a category from the Rss structure
func (rss *Rss) RemoveCategory(name string) error {
// Remove the category
for i, cat := range rss.Categories {
if cat.Name == name {
rss.Categories = append(rss.Categories[:i], rss.Categories[i+1:]...)
// Check if the category matches
if cat.Name != name {
continue
}

// Remove the category
rss.Categories = append(rss.Categories[:i], rss.Categories[i+1:]...)
return nil
}

// Return no errors
return nil
// We couldn't remove the category
return ErrNotFound
}

// RemoveFeed will remove a feed from the Rss structure
func (rss *Rss) RemoveFeed(category string, name string) error {
for i, cat := range rss.Categories {
// Check if the category matches
if cat.Name != category {
continue
}

for j, feed := range cat.Subscriptions {
// Check if the feed matches
if feed.Name != name {
continue
}

// Remove the feed
rss.Categories[i].Subscriptions = append(rss.Categories[i].Subscriptions[:j], rss.Categories[i].Subscriptions[j+1:]...)
return nil
}
}

// We couldn't remove the feed
return ErrNotFound
}
34 changes: 27 additions & 7 deletions internal/tab/category/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,47 @@ func (c RSSCategoryTab) Update(msg tea.Msg) (tab.Tab, tea.Cmd) {
// The data fetch was successful
if !c.loaded {
c.list = list.NewList(c.title, style.WindowHeight-5)
c.list.SetItems(msg.Items)
c.loaded = true
return c, nil
}

// Set the items of the list
c.list.SetItems(msg.Items)
return c, nil

case tea.KeyMsg:
// If the tab is not loaded, return
if !c.loaded {
return c, nil
}

// Handle the list messages
switch msg.String() {
case "enter":

// If it isnt a number, check if it is an enter
if !c.list.IsEmpty() {
return c, tab.NewTab(c.list.SelectedItem().FilterValue(), tab.Feed)
}

case "n":
// Add a new category
return c, backend.NewItem(backend.Feed)

case "d":
// Delete the selected category
if !c.list.IsEmpty() {
return c, backend.DeleteItem(
backend.Feed,
c.list.SelectedItem().FilterValue(),
)
}
}

// Check if the user opened a tab using the number pad
if index, ok := c.list.HasItem(msg.String()); ok {
return c, tab.NewTab(c.list.GetItem(index).FilterValue(), tab.Feed)
}

// If it isnt a number, check if it is an enter
if msg.String() == "enter" && !c.list.IsEmpty() {
return c, tab.NewTab(c.list.SelectedItem().FilterValue(), tab.Feed)
}

default:
if !c.loaded {
c.loadingSpinner, cmd = c.loadingSpinner.Update(msg)
Expand Down
5 changes: 1 addition & 4 deletions internal/tab/welcome/welcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ func (w Welcome) Update(msg tea.Msg) (tab.Tab, tea.Cmd) {

case "n":
// Add a new category
cmds = append(cmds, backend.NewItem(
backend.Category,
"name", "desc",
))
cmds = append(cmds, backend.NewItem(backend.Category))

case "d":
// Delete the selected category
Expand Down

0 comments on commit e032c23

Please sign in to comment.