Skip to content

Commit

Permalink
Refactor pomodoro (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
padilo authored Apr 24, 2022
1 parent a879059 commit 5ca1233
Show file tree
Hide file tree
Showing 16 changed files with 423 additions and 507 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ workbench
pomaquet
.idea/
assets
.vscode/
44 changes: 44 additions & 0 deletions pkg/core/tui/window/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package window

import (
tea "github.com/charmbracelet/bubbletea"
tui_pomodoro "github.com/padilo/pomaquet/pkg/pomodoro/tui"
tui_task "github.com/padilo/pomaquet/pkg/task/tui"
"github.com/padilo/pomaquet/pkg/task/tui/crud"
)

type LeftWindow int64

const (
Task LeftWindow = iota
CrudTask
)

type model struct {
pomodoroModel tea.Model
taskModel tea.Model
crudModel tea.Model
leftWindow LeftWindow
height int
width int
modeTaskCrud bool
}

func NewModel() model {
m := model{
pomodoroModel: tui_pomodoro.NewModel(),
taskModel: tui_task.NewModel(),
crudModel: crud.NewModel(),
modeTaskCrud: false,
}
return m
}

func (m model) Init() tea.Cmd {
var cmds []tea.Cmd
cmds = append(cmds, m.pomodoroModel.Init())
cmds = append(cmds, m.taskModel.Init())
cmds = append(cmds, m.crudModel.Init())

return tea.Batch(cmds...)
}
116 changes: 116 additions & 0 deletions pkg/core/tui/window/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package window

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/padilo/pomaquet/pkg/pomodoro/app/core"
)

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd

switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.height = msg.Height
m.width = msg.Width
dim := m.getDimensions()

cmds = m.updateModels(msg)

leftMsg := core.DimensionChangeMsg{
Dimension: dim.Left,
ScreenSize: dim.Screen,
}
rightMsg := core.DimensionChangeMsg{
Dimension: dim.Right,
ScreenSize: dim.Screen,
}
var cmd tea.Cmd
m.crudModel, cmd = m.crudModel.Update(leftMsg)
cmds = append(cmds, cmd)
m.taskModel, cmd = m.taskModel.Update(leftMsg)
cmds = append(cmds, cmd)
m.pomodoroModel, cmd = m.pomodoroModel.Update(rightMsg)
cmds = append(cmds, cmd)

case core.SwitchToTaskCrudMsg:
m.leftWindow = CrudTask

case core.SwitchToTaskMsg:
m.leftWindow = Task

case core.CrudOkMsg, core.CrudCancelMsg:
var cmd tea.Cmd
m.taskModel, cmd = m.taskModel.Update(msg)
cmds = append(cmds, cmd)

case core.SetTaskMsg:
var cmd tea.Cmd
m.crudModel, cmd = m.crudModel.Update(msg)
cmds = append(cmds, cmd)

default:
cmds = m.updateModels(msg)
}

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

func (m *model) updateModels(msg tea.Msg) []tea.Cmd {
return []tea.Cmd{
m.updateLeft(msg),
m.updateRight(msg),
}
}

func (m *model) updateRight(msg tea.Msg) tea.Cmd {
var cmd tea.Cmd
m.pomodoroModel, cmd = m.pomodoroModel.Update(msg)
return cmd
}

func (m *model) updateLeft(msg tea.Msg) tea.Cmd {
var cmd tea.Cmd

// TODO: Fix this, must be another way to do what I'm trying to do
switch m.leftWindow {
case Task:
m.taskModel, cmd = m.taskModel.Update(msg)
case CrudTask:
m.crudModel, cmd = m.crudModel.Update(msg)
}
return cmd
}

func (m model) getLeftModel() tea.Model {
switch m.leftWindow {
case Task:
return m.taskModel
case CrudTask:
return m.crudModel
}

return nil
}

func (m *model) getDimensions() core.Dimensions {
return core.Dimensions{
Left: core.Dimension{
Top: 0,
Left: 0,
Right: m.width/2 - 1,
Bottom: m.height - 1,
},
Right: core.Dimension{
Top: 0,
Left: m.width / 2,
Right: m.width - 1,
Bottom: m.height - 1,
},
Screen: core.Dimension{
Top: 0,
Left: 0,
Right: m.width,
Bottom: m.height,
},
}
}
151 changes: 0 additions & 151 deletions pkg/core/tui/window/view.go
Original file line number Diff line number Diff line change
@@ -1,160 +1,9 @@
package window

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/padilo/pomaquet/pkg/pomodoro/app/core"
tui_pomodoro "github.com/padilo/pomaquet/pkg/pomodoro/tui"
tui_task "github.com/padilo/pomaquet/pkg/task/tui"
"github.com/padilo/pomaquet/pkg/task/tui/crud"
)

type LeftWindow int64

const (
Task LeftWindow = iota
CrudTask
)

type model struct {
pomodoroModel tea.Model
taskModel tea.Model
crudModel tea.Model
leftWindow LeftWindow
height int
width int
modeTaskCrud bool
}

func NewModel() model {
m := model{
pomodoroModel: tui_pomodoro.NewModel(),
taskModel: tui_task.NewModel(),
crudModel: crud.NewModel(),
modeTaskCrud: false,
}
return m
}

func (m model) Init() tea.Cmd {
var cmds []tea.Cmd
cmds = append(cmds, m.pomodoroModel.Init())
cmds = append(cmds, m.taskModel.Init())
cmds = append(cmds, m.crudModel.Init())

return tea.Batch(cmds...)
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd

switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.height = msg.Height
m.width = msg.Width
dim := m.getDimensions()

cmds = m.updateModels(msg)

leftMsg := core.DimensionChangeMsg{
Dimension: dim.Left,
ScreenSize: dim.Screen,
}
rightMsg := core.DimensionChangeMsg{
Dimension: dim.Right,
ScreenSize: dim.Screen,
}
var cmd tea.Cmd
m.crudModel, cmd = m.crudModel.Update(leftMsg)
cmds = append(cmds, cmd)
m.taskModel, cmd = m.taskModel.Update(leftMsg)
cmds = append(cmds, cmd)
m.pomodoroModel, cmd = m.pomodoroModel.Update(rightMsg)
cmds = append(cmds, cmd)

case core.SwitchToTaskCrudMsg:
m.leftWindow = CrudTask

case core.SwitchToTaskMsg:
m.leftWindow = Task

case core.CrudOkMsg, core.CrudCancelMsg:
var cmd tea.Cmd
m.taskModel, cmd = m.taskModel.Update(msg)
cmds = append(cmds, cmd)

case core.SetTaskMsg:
var cmd tea.Cmd
m.crudModel, cmd = m.crudModel.Update(msg)
cmds = append(cmds, cmd)

default:
cmds = m.updateModels(msg)
}

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

func (m *model) updateModels(msg tea.Msg) []tea.Cmd {
return []tea.Cmd{
m.updateLeft(msg),
m.updateRight(msg),
}
}

func (m *model) updateRight(msg tea.Msg) tea.Cmd {
var cmd tea.Cmd
m.pomodoroModel, cmd = m.pomodoroModel.Update(msg)
return cmd
}

func (m *model) updateLeft(msg tea.Msg) tea.Cmd {
var cmd tea.Cmd

// TODO: Fix this, must be another way to do what I'm trying to do
switch m.leftWindow {
case Task:
m.taskModel, cmd = m.taskModel.Update(msg)
case CrudTask:
m.crudModel, cmd = m.crudModel.Update(msg)
}
return cmd
}

func (m model) getLeftModel() tea.Model {
switch m.leftWindow {
case Task:
return m.taskModel
case CrudTask:
return m.crudModel
}

return nil
}

func (m model) View() string {
return lipgloss.JoinHorizontal(lipgloss.Top, m.getLeftModel().View(), m.pomodoroModel.View())
}

func (m *model) getDimensions() core.Dimensions {
return core.Dimensions{
Left: core.Dimension{
Top: 0,
Left: 0,
Right: m.width/2 - 1,
Bottom: m.height - 1,
},
Right: core.Dimension{
Top: 0,
Left: m.width / 2,
Right: m.width - 1,
Bottom: m.height - 1,
},
Screen: core.Dimension{
Top: 0,
Left: 0,
Right: m.width,
Bottom: m.height,
},
}
}
Loading

0 comments on commit 5ca1233

Please sign in to comment.