-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from isabelroses/state-tracking
- Loading branch information
Showing
9 changed files
with
164 additions
and
27 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package lib | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"os" | ||
|
||
"github.com/adrg/xdg" | ||
) | ||
|
||
func ToggleRead(feeds Feeds, feedID int, postID int) Feeds { | ||
postr := &feeds[feedID].Posts[postID] | ||
postr.Read = !postr.Read | ||
return feeds | ||
} | ||
|
||
func ReadAll(feeds Feeds, feedID int) Feeds { | ||
for i := range feeds[feedID].Posts { | ||
feeds[feedID].Posts[i].Read = true | ||
} | ||
return feeds | ||
} | ||
|
||
func MarkRead(feeds Feeds, feedID int, postID int) Feeds { | ||
postr := &feeds[feedID].Posts[postID] | ||
postr.Read = true | ||
return feeds | ||
} | ||
|
||
func (feeds Feeds) WriteTracking() error { | ||
json, err := json.Marshal(feeds) | ||
if err != nil { | ||
return err | ||
} | ||
return os.WriteFile(getSateFile(), json, 0644) | ||
} | ||
|
||
// Read from JSON file | ||
func (feeds Feeds) ReadTracking() (Feeds, error) { | ||
fileStr := getSateFile() | ||
if _, err := os.Stat(fileStr); os.IsNotExist(err) { | ||
err := feeds.WriteTracking() | ||
if err != nil { | ||
log.Fatalf("could not write tracking file: %v", err) | ||
} | ||
} | ||
|
||
file, err := os.ReadFile(fileStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = json.Unmarshal(file, &feeds) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return feeds, nil | ||
} | ||
|
||
func getSateFile() string { | ||
stateFile, err := xdg.StateFile("izrss/tracking.json") | ||
if err != nil { | ||
log.Fatalf("could not find state file: %v", err) | ||
} | ||
return stateFile | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters