Skip to content

Commit

Permalink
Changed sync choice msg in syncScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
grrlopes committed Aug 26, 2023
1 parent c8d1065 commit 7219056
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
2 changes: 0 additions & 2 deletions entity/cmdmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package entity
import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
Expand All @@ -26,7 +25,6 @@ type CmdModel struct {
ActiveSyncScreen bool
ActiveFinderScreen bool
StatusSyncScreen bool
ProgressSync progress.Model
Finder textinput.Model
FinderFilter string
Store []Commands
Expand Down
7 changes: 2 additions & 5 deletions ui/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
Expand Down Expand Up @@ -50,15 +49,14 @@ func NewHome(m *entity.CmdModel) *ModelHome {
p.SetTotalPages(count)
p.KeyMap.NextPage = helper.HotKeysHome.PageNext
p.KeyMap.PrevPage = helper.HotKeysHome.PagePrev
pro := progress.New(progress.WithDefaultGradient())
txt := textinput.New()
txt.Placeholder = "type..."
txt.CharLimit = 156
txt.Width = 50
txt.Prompt = "Finder: "
h := help.New()
spin := spinner.New()
spin.Spinner = spinner.Jump
spin := spinner.New()
spin.Spinner = spinner.Jump

home := ModelHome{
home: entity.CmdModel{
Expand All @@ -70,7 +68,6 @@ func NewHome(m *entity.CmdModel) *ModelHome {
Count: &count,
ActiveSyncScreen: false,
StatusSyncScreen: false,
ProgressSync: pro,
Ftotal: ftotal,
Finder: txt,
HomeKeys: helper.HotKeysHome,
Expand Down
58 changes: 43 additions & 15 deletions ui/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import (

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/grrlopes/storydb/usecase/fhistory"
)

const (
padding = 2
maxWidth = 80
)

var choiceEntered bool = false
var choiceEntered string = "notenter"

type tickMsg time.Time
type (
tickMsg time.Time
spinJumpMsg struct{}
)

func syncUpdate(msg tea.Msg, m ModelHome) (*ModelHome, tea.Cmd) {
var cmd tea.Cmd
var (
cmd tea.Cmd
cmds []tea.Cmd
)

switch msg := msg.(type) {
case tea.KeyMsg:
Expand All @@ -33,18 +40,19 @@ func syncUpdate(msg tea.Msg, m ModelHome) (*ModelHome, tea.Cmd) {
return &m, nil
case "enter":
if m.home.StatusSyncScreen {
choiceEntered = true
cmd = syncTickCmd()
choiceEntered = "syncing"
cmds = append(cmds, syncTickCmd())
// cmds = append(cmds, spinJump())
} else {
choiceEntered = false
choiceEntered = "notenter"
}
m.home.StatusSyncScreen = false
return &m, cmd
return &m, tea.Batch(cmds...)
case "q":
if m.home.ActiveSyncScreen {
m.home.ActiveSyncScreen = false
m.home.Viewport.SetContent(m.GetDataView())
choiceEntered = false
choiceEntered = "notenter"
return &m, nil
}
}
Expand All @@ -54,8 +62,16 @@ func syncUpdate(msg tea.Msg, m ModelHome) (*ModelHome, tea.Cmd) {
m.home.Viewport.SetContent(syncView(&m))
case tickMsg:
cmd = m.home.Spinner.Tick
cmds = append(cmds, cmd)
cmds = append(cmds, spinJump())
cmds = append(cmds, syncTickCmd())
m.home.Viewport.SetContent(syncView(&m))
return &m, tea.Batch(syncTickCmd(), cmd)
return &m, tea.Batch(cmds...)
case spinJumpMsg:
cmd = usecaseHistory.Execute()
return &m, cmd
case fhistory.SyncMsg:
choiceEntered = "synced"
}
m.home.Spinner, cmd = m.home.Spinner.Update(msg)
return &m, cmd
Expand All @@ -64,13 +80,13 @@ func syncUpdate(msg tea.Msg, m ModelHome) (*ModelHome, tea.Cmd) {
func syncView(m *ModelHome) string {
var okButton, cancelButton string

if m.home.StatusSyncScreen && !choiceEntered {
if m.home.StatusSyncScreen && choiceEntered == "notenter" {
okButton = ActiveButtonStyle.Render("Yes")
cancelButton = ButtonStyle.Render("No, take me back")
} else if choiceEntered {
} else if choiceEntered == "enter" {
okButton = ButtonDisableStyle.Render("Yes")
cancelButton = ButtonDisableStyle.Render("No, take me back")
} else if !choiceEntered {
} else if choiceEntered == "notenter" {
okButton = ButtonStyle.Render("Yes")
cancelButton = ActiveButtonStyle.Render("No, take me back")
}
Expand All @@ -97,12 +113,24 @@ func syncView(m *ModelHome) string {
}

func syncProgressView(m *ModelHome) string {
sync := fmt.Sprintf("%s Syncing .....", m.home.Spinner.View())
return sync
switch choiceEntered {
case "syncing":
return fmt.Sprintf("%s Syncing .....", m.home.Spinner.View())
case "synced":
return "been synced!!!"
default:
return "Not syncing yet...."
}
}

func syncTickCmd() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return tea.Tick(time.Duration(40)*time.Millisecond, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}

func spinJump() tea.Cmd {
return func() tea.Msg {
return spinJumpMsg{}
}
}
12 changes: 9 additions & 3 deletions usecase/fhistory/fhistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"bufio"
"log"

tea "github.com/charmbracelet/bubbletea"
"github.com/grrlopes/storydb/repositories"
)

type SyncMsg int

type InputBoundary interface {
Execute() int
Execute() tea.Cmd
}

type execute struct {
Expand All @@ -23,7 +26,7 @@ func NewFHistory(frepo repositories.IFileParsedRepository, srepo repositories.IS
}
}

func (e execute) Execute() int {
func (e execute) Execute() tea.Cmd {
fresult := e.frepository.All()

scanner := bufio.NewScanner(fresult)
Expand All @@ -37,5 +40,8 @@ func (e execute) Execute() int {
log.Fatal(err)
}

return fcount
cmd := func() tea.Msg {
return SyncMsg(fcount)
}
return cmd
}

0 comments on commit 7219056

Please sign in to comment.