Skip to content

Commit

Permalink
Add data manager for storing app data and add input for adding user id
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Shanks committed Nov 19, 2023
1 parent 2ed8397 commit 7a4d6cc
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 73 deletions.
11 changes: 8 additions & 3 deletions appKeyMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import "github.com/charmbracelet/bubbles/key"

type KeyMap struct {
// Global keys
Help key.Binding
Quit key.Binding
Help key.Binding
Quit key.Binding
ToggleUserIdInput key.Binding

// View keys
Trending key.Binding
Expand Down Expand Up @@ -36,7 +37,7 @@ func (k KeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Up, k.Down, k.Top, k.Bottom},
{k.HalfPageUp, k.HalfPageDown, k.PageUp, k.PageDown},
{k.Search, k.Help, k.Quit},
{k.ToggleUserIdInput, k.Search, k.Help, k.Quit},
}
}

Expand Down Expand Up @@ -98,6 +99,10 @@ var AppKeyMap = KeyMap{
key.WithKeys("Q"),
key.WithHelp("Q", "queue"),
),
ToggleUserIdInput: key.NewBinding(
key.WithKeys("="),
key.WithHelp("=", "enter user id"),
),
Search: key.NewBinding(
key.WithKeys("S", "/"),
// key.WithHelp("S", "search"),
Expand Down
Binary file added build/app1
Binary file not shown.
6 changes: 3 additions & 3 deletions common/TracksTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

var (
tableActiveContainerStyle = BorderContainer()
tableInactiveContainerStyle = BorderContainer().BorderForeground(lipgloss.Color("243"))
tableInactiveContainerStyle = BorderContainer().BorderForeground(Inactive)
)

var DefaultTracksTableColumns = []table.Column{
Expand Down Expand Up @@ -49,7 +49,7 @@ func NewTracksTable(
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(true)
s.Selected = s.Selected.Bold(false).Foreground(lipgloss.Color("#FFFFFF"))
s.Selected = s.Selected.Bold(false).Foreground(White)
t.SetStyles(s)

tt := TracksTable{table: t, isLoading: false, focused: false, onSelect: onSelect}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (tt *TracksTable) Blur() {
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(true)
s.Selected = s.Selected.Bold(false).Foreground(lipgloss.Color("#FFFFFF"))
s.Selected = s.Selected.Bold(false).Foreground(White)
tt.table.SetStyles(s)
}

Expand Down
7 changes: 4 additions & 3 deletions common/TracksTableView.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ type errMsg struct{ error }

func (e errMsg) Error() string { return e.error.Error() }

func (tt *TracksTableView) fetchTracksCmd() tea.Cmd {
func (tt *TracksTableView) FetchTracksCmd() tea.Cmd {
return func() tea.Msg {
tracks, err := tt.fetchTracks()

if err != nil {
return errMsg{err}
// return errMsg{err}
return LogCmd(err.Error())
}

return tracksResponseMsg{
Expand Down Expand Up @@ -56,7 +57,7 @@ func NewTracksTableView(viewTitle string, fetchTracks func() ([]Track, error)) T
}

func (tt TracksTableView) Init() tea.Cmd {
return tt.fetchTracksCmd()
return tt.FetchTracksCmd()
}

func (tt TracksTableView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand Down
80 changes: 80 additions & 0 deletions common/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// File for storing, reading, and deleting data for the app
package common

import (
"encoding/json"
"errors"
"os"
)

var AppDataManager = NewDataManger()

type AppData struct {
UserId string `json:"userId"`
}

type DataManager struct {
// Path to file storing app data
path string
}

func NewDataManger() DataManager {
return DataManager{
path: "data.json",
}
}

func (dm DataManager) FileCheck() {
if _, err := os.Stat(dm.path); errors.Is(err, os.ErrNotExist) {
dm.SetData(AppData{})
}
}

func (dm DataManager) GetData() (AppData, error) {
dm.FileCheck()

bytes, err := os.ReadFile(dm.path)
if err != nil {
return AppData{}, err
}

var data AppData
err = json.Unmarshal([]byte(bytes), &data)

if err != nil {
return AppData{}, err
}

return data, nil
}

func (dm DataManager) SetData(data AppData) error {
dataBytes, err := json.Marshal(data)
if err != nil {
return err
}

os.WriteFile(dm.path, dataBytes, 0644)
return nil
}

// - Property Methods -
func (dm DataManager) GetUserId() (string, error) {
data, err := dm.GetData()
if err != nil {
return "", err
}

return data.UserId, nil
}

func (dm DataManager) SetUserId(id string) error {
data, err := dm.GetData()
if err != nil {
return err
}

data.UserId = id

return dm.SetData(data)
}
25 changes: 25 additions & 0 deletions common/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Logging and debugging things
package common

import (
"os"
)

// TODO: Make a logging manager or something

func log(string string) {
f, err := os.OpenFile("debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}

defer f.Close()

if _, err := f.WriteString(string + "\n"); err != nil {
panic(err)
}
}

func errorLog(string string) {
log("ERROR: " + string)
}
119 changes: 85 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,32 @@ type App struct {
keyMap KeyMap
/* App help text component */
help help.Model

/* Toggle input for entering user id */
userIdInputVisible bool
userIdInput textinput.Model
}

func getMyFavs() ([]common.Track, error) {
return api.GetUserFavoriteTracks("PWVbN")
userId, err := common.AppDataManager.GetUserId()
if err != nil || userId == "" {
return []common.Track{}, err
}

return api.GetUserFavoriteTracks(userId)
}

/* Create and initialize a new instance of the App */
func NewApp() App {
h := help.New()
h.Styles.ShortDesc = h.Styles.ShortDesc.Foreground(lipgloss.Color("#555"))
h.Styles.FullDesc = h.Styles.FullDesc.Foreground(lipgloss.Color("#555"))
h.Styles.ShortKey = h.Styles.ShortKey.Foreground(lipgloss.Color("#777"))
h.Styles.FullKey = h.Styles.FullKey.Foreground(lipgloss.Color("#777"))
h.Styles.ShortDesc = h.Styles.ShortDesc.Foreground(common.Grey2)
h.Styles.FullDesc = h.Styles.FullDesc.Foreground(common.Grey2)
h.Styles.ShortKey = h.Styles.ShortKey.Foreground(common.Grey3)
h.Styles.FullKey = h.Styles.FullKey.Foreground(common.Grey3)

idInput := textinput.New()
idInput.Placeholder = "Enter ID"
idInput.Focus()

app := App{
view: trendingView,
Expand All @@ -81,8 +94,10 @@ func NewApp() App {
queueView: queue.NewQueueView(),
searchView: search.NewSearchView(),

keyMap: AppKeyMap,
help: h,
keyMap: AppKeyMap,
help: h,
userIdInputVisible: false,
userIdInput: idInput,
}
app.trendingView.Focus()

Expand Down Expand Up @@ -133,30 +148,54 @@ func (a App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

case tea.KeyMsg:
switch {
case key.Matches(msg, a.keyMap.Help):
if !searchInputFocused {
a.help.ShowAll = !a.help.ShowAll
}
case key.Matches(msg, a.keyMap.Quit):
// Handle app quit
if key.Matches(msg, a.keyMap.Quit) {
cmds = append(cmds, tea.Quit)
case key.Matches(msg, a.keyMap.Underground):
if !searchInputFocused {
}

// Exit early if in search input
if searchInputFocused {
updateRes, cmd := a.searchView.Update(msg)
a.searchView = updateRes.(search.SearchView)
cmds = append(cmds, cmd)

return a, tea.Batch(cmds...)
} else if a.userIdInputVisible {
if msg.String() == "enter" {
cmds = append(cmds, common.LogCmd("Setting User ID: "+a.userIdInput.Value()))
common.AppDataManager.SetUserId(a.userIdInput.Value())
a.userIdInput.Reset()
a.userIdInputVisible = false

// Refetch favorites
cmds = append(cmds, a.favoritesView.FetchTracksCmd())
a.updateViewFocus(favoritesView)
} else if key.Matches(msg, a.keyMap.ToggleUserIdInput) {
a.userIdInputVisible = false
} else {
a.userIdInput, cmd = a.userIdInput.Update(msg)
cmds = append(cmds, cmd)
}

return a, tea.Batch(cmds...)
} else {
// Handle app level key bindings
switch {
case key.Matches(msg, a.keyMap.Help):
a.help.ShowAll = !a.help.ShowAll
case key.Matches(msg, a.keyMap.ToggleUserIdInput):
// a.help.ShowAll = !a.help.ShowAll
a.userIdInputVisible = true
case key.Matches(msg, a.keyMap.Underground):
a.updateViewFocus(undergroundView)
return a, tea.Batch(cmds...)
}
case key.Matches(msg, a.keyMap.Trending):
if !searchInputFocused {
case key.Matches(msg, a.keyMap.Trending):
a.updateViewFocus(trendingView)
return a, tea.Batch(cmds...)
}
case key.Matches(msg, a.keyMap.Favorites):
if !searchInputFocused {
case key.Matches(msg, a.keyMap.Favorites):
a.updateViewFocus(favoritesView)
return a, tea.Batch(cmds...)
}
case key.Matches(msg, a.keyMap.Search):
if !searchInputFocused {
case key.Matches(msg, a.keyMap.Search):
a.updateViewFocus(searchView)
if msg.String() == "/" {
a.searchView.FocusInput()
Expand All @@ -165,16 +204,6 @@ func (a App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return a, tea.Batch(cmds...)
}
}

// Exit early if in search input
// TODO: Find a better way to handle this
if searchInputFocused {
updateRes, cmd := a.searchView.Update(msg)
a.searchView = updateRes.(search.SearchView)
cmds = append(cmds, cmd)

return a, tea.Batch(cmds...)
}
}

var updateRes tea.Model
Expand Down Expand Up @@ -209,6 +238,28 @@ func (a App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (a App) View() string {
if a.userIdInputVisible {
userIdInputHeader := common.ActiveHeader()
headerContainer := lipgloss.NewStyle().
Align(lipgloss.Center).
MarginTop(1).
Render(
userIdInputHeader.Render("User ID Input"),
)

return lipgloss.JoinVertical(
lipgloss.Left,
headerContainer,
common.BorderContainer().
BorderForeground(common.Primary).
Width(32).
Padding(0, 2).
Render(
a.userIdInput.View(),
),
)
}

var mainView tea.Model

trendingHeader := common.Header().MarginLeft(1)
Expand Down
Loading

0 comments on commit 7a4d6cc

Please sign in to comment.