Skip to content

Commit

Permalink
Add history field to submission handler
Browse files Browse the repository at this point in the history
  • Loading branch information
bensadeh committed Jul 22, 2021
1 parent bb33636 commit 0bf8528
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"clx/endpoints"
"clx/favorites"
"clx/handler"
"clx/history"
"strconv"
"time"

Expand All @@ -23,8 +24,9 @@ var addCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
id := args[0]
fav := favorites.Initialize()
his := history.Initialize(0)
sh := new(handler.StoryHandler)
sh.Init(fav)
sh.Init(fav, his)

item := new(endpoints.Story)
item.ID, _ = strconv.Atoi(id)
Expand Down
5 changes: 3 additions & 2 deletions cmd/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var cacheCmd = &cobra.Command{
Use: "cache",
Short: "cache",
Run: func(cmd *cobra.Command, args []string) {
c := new(history.History)
c.Initialize(1)
_ = history.Initialize(0)

println("OK")
},
}
4 changes: 3 additions & 1 deletion constructors/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"clx/core"
"clx/favorites"
"clx/handler"
"clx/history"
"clx/screen"
"clx/utils/vim"

Expand Down Expand Up @@ -34,8 +35,9 @@ func NewScreenController() *core.ScreenController {
sc.MainView.Panels.AddPanel(panels.StoriesPanel, sc.Articles, true, true)

fav := favorites.Initialize()
his := history.Initialize(0)
sc.StoryHandler = new(handler.StoryHandler)
sc.StoryHandler.Init(fav)
sc.StoryHandler.Init(fav, his)

sc.VimRegister = new(vim.Register)

Expand Down
7 changes: 5 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"clx/favorites"
"clx/file"
"clx/header"
"clx/history"
"clx/utils/filter"
"clx/utils/formatter"
"clx/utils/http"
Expand All @@ -28,7 +29,8 @@ const (
)

type StoryHandler struct {
sc []*storyCategory
sc []*storyCategory
history *history.History
}

type storyCategory struct {
Expand Down Expand Up @@ -95,7 +97,7 @@ func getOverriddenYCJobsStatus(visibleStories int, hideYCJobs bool) bool {
return hideYCJobs
}

func (r *StoryHandler) Init(fav *favorites.Favorites) {
func (r *StoryHandler) Init(fav *favorites.Favorites, his *history.History) {
r.sc = make([]*storyCategory, totalNumberOfCategories)

r.sc[categories.FrontPage] = new(storyCategory)
Expand All @@ -111,6 +113,7 @@ func (r *StoryHandler) Init(fav *favorites.Favorites) {
r.sc[categories.Favorites].maxPages = favoritesMaxPages

r.sc[categories.Favorites].stories = fav.Items
r.history = his
}

func (r *StoryHandler) Reset() {
Expand Down
14 changes: 9 additions & 5 deletions history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ type History struct {
mode int
}

func (h *History) Initialize(historyMode int) {
h.visitedStories = hashset.New()
h.mode = historyMode
func Initialize(historyMode int) *History {
h := &History{
visitedStories: hashset.New(),
mode: historyMode,
}

if h.mode == disableHistory {
return
return h
}

fullPath, dirPath, fileName := getCacheFilePaths()

if !exists(fullPath) {
writeToDisk(h, dirPath, fileName)

return
return h
}

historyFileContent, readErr := os.ReadFile(fullPath)
Expand All @@ -42,6 +44,8 @@ func (h *History) Initialize(historyMode int) {
if deserializationErr != nil {
panic(deserializationErr)
}

return h
}

func writeToDisk(h *History, dirPath string, fileName string) {
Expand Down

0 comments on commit 0bf8528

Please sign in to comment.