From 0bf85284a75f08a09c61ad4eca9c571daac7e9c8 Mon Sep 17 00:00:00 2001 From: Ben Sadeh <701096+bensadeh@users.noreply.github.com> Date: Thu, 22 Jul 2021 11:12:30 +0200 Subject: [PATCH] Add history field to submission handler --- cmd/add.go | 4 +++- cmd/cache.go | 5 +++-- constructors/constructors.go | 4 +++- handler/handler.go | 7 +++++-- history/history.go | 14 +++++++++----- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 3e2ab259..9fb22dbf 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -5,6 +5,7 @@ import ( "clx/endpoints" "clx/favorites" "clx/handler" + "clx/history" "strconv" "time" @@ -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) diff --git a/cmd/cache.go b/cmd/cache.go index 523f6716..07fbccff 100644 --- a/cmd/cache.go +++ b/cmd/cache.go @@ -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") }, } diff --git a/constructors/constructors.go b/constructors/constructors.go index c4731a68..0bcc8fd7 100644 --- a/constructors/constructors.go +++ b/constructors/constructors.go @@ -6,6 +6,7 @@ import ( "clx/core" "clx/favorites" "clx/handler" + "clx/history" "clx/screen" "clx/utils/vim" @@ -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) diff --git a/handler/handler.go b/handler/handler.go index a5315728..c1ecd3f7 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -6,6 +6,7 @@ import ( "clx/favorites" "clx/file" "clx/header" + "clx/history" "clx/utils/filter" "clx/utils/formatter" "clx/utils/http" @@ -28,7 +29,8 @@ const ( ) type StoryHandler struct { - sc []*storyCategory + sc []*storyCategory + history *history.History } type storyCategory struct { @@ -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) @@ -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() { diff --git a/history/history.go b/history/history.go index 1956354b..32d8ebfe 100644 --- a/history/history.go +++ b/history/history.go @@ -17,12 +17,14 @@ 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() @@ -30,7 +32,7 @@ func (h *History) Initialize(historyMode int) { if !exists(fullPath) { writeToDisk(h, dirPath, fileName) - return + return h } historyFileContent, readErr := os.ReadFile(fullPath) @@ -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) {