Skip to content

Commit

Permalink
Improve updater mechanism, remove obsolete and unused code for better…
Browse files Browse the repository at this point in the history
… maintainability
  • Loading branch information
canack committed Dec 17, 2024
1 parent 8efeab6 commit 1460ed0
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 180 deletions.
41 changes: 16 additions & 25 deletions internal/terminal/handler/ghinformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
gu "github.com/termkit/gama/internal/github/usecase"
Expand Down Expand Up @@ -33,12 +34,10 @@ type ModelInfo struct {

// keymap
keys githubInformationKeyMap

updateSelfChan chan selfUpdateMsg
}

func SetupModelInfo(skeleton *skeleton.Skeleton, githubUseCase gu.UseCase, version pkgversion.Version) *ModelInfo {
modelStatus := status.SetupModelStatus(skeleton)
func SetupModelInfo(sk *skeleton.Skeleton, githubUseCase gu.UseCase, version pkgversion.Version) *ModelInfo {
modelStatus := status.SetupModelStatus(sk)

const (
releaseURL = "https://github.com/termkit/gama/releases"
Expand All @@ -53,7 +52,7 @@ func SetupModelInfo(skeleton *skeleton.Skeleton, githubUseCase gu.UseCase, versi
)

return &ModelInfo{
skeleton: skeleton,
skeleton: sk,
releaseURL: releaseURL,
logo: applicationName,
github: githubUseCase,
Expand All @@ -64,35 +63,25 @@ func SetupModelInfo(skeleton *skeleton.Skeleton, githubUseCase gu.UseCase, versi
}
}

func (m *ModelInfo) selfUpdate() {
m.updateSelfChan <- selfUpdateMsg{}
}

func (m *ModelInfo) selfListen() tea.Cmd {
return func() tea.Msg {
return <-m.updateSelfChan
}
}

func (m *ModelInfo) Init() tea.Cmd {
m.applicationDescription = fmt.Sprintf("Github Actions Manager (%s)", m.version.CurrentVersion())

go m.checkUpdates(context.Background())
go m.testConnection(context.Background())

return tea.Batch(tea.EnterAltScreen, tea.SetWindowTitle("GitHub Actions Manager (GAMA)"), m.selfListen())
return tea.Batch(tea.EnterAltScreen, tea.SetWindowTitle("GitHub Actions Manager (GAMA)"))
}

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

switch msg := msg.(type) {
case selfUpdateMsg:
_ = msg
cmds = append(cmds, m.selfListen())
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.Quit):
return m, tea.Quit
}
}

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

func (m *ModelInfo) View() string {
Expand All @@ -112,11 +101,13 @@ func (m *ModelInfo) View() string {

infoDoc.WriteString(strings.Repeat("\n", max(0, requiredNewlinesForPadding)))

return lipgloss.JoinVertical(lipgloss.Center, infoDoc.String(), m.status.View(), helpWindowStyle.Render(m.ViewHelp()))
str := lipgloss.JoinVertical(lipgloss.Center, infoDoc.String(), m.status.View(), helpWindowStyle.Render(m.ViewHelp()))

return str
}

func (m *ModelInfo) checkUpdates(ctx context.Context) {
defer m.selfUpdate()
defer m.skeleton.TriggerUpdate()

isUpdateAvailable, version, err := m.version.IsUpdateAvailable(ctx)
if err != nil {
Expand All @@ -132,7 +123,7 @@ func (m *ModelInfo) checkUpdates(ctx context.Context) {
}

func (m *ModelInfo) testConnection(ctx context.Context) {
defer m.selfUpdate()
defer m.skeleton.TriggerUpdate()

m.status.SetProgressMessage("Checking your token...")
m.skeleton.LockTabs()
Expand Down
44 changes: 12 additions & 32 deletions internal/terminal/handler/ghrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ type ModelGithubRepository struct {
modelTabOptions *taboptions.Options

textInput textinput.Model

updateSelfChan chan selfUpdateMsg
}

func SetupModelGithubRepository(skeleton *skeleton.Skeleton, githubUseCase gu.UseCase) *ModelGithubRepository {
func SetupModelGithubRepository(sk *skeleton.Skeleton, githubUseCase gu.UseCase) *ModelGithubRepository {
var tableRowsGithubRepository []table.Row

tableGithubRepository := table.New(
Expand Down Expand Up @@ -101,16 +99,16 @@ func SetupModelGithubRepository(skeleton *skeleton.Skeleton, githubUseCase gu.Us

ti := textinput.New()
ti.Blur()
ti.CharLimit = 72
ti.CharLimit = 128
ti.Placeholder = "Type to search repository"
ti.ShowSuggestions = false // disable suggestions, it will be enabled future.

// setup models
modelStatus := status.SetupModelStatus(skeleton)
tabOptions := taboptions.NewOptions(&modelStatus)
modelStatus := status.SetupModelStatus(sk)
tabOptions := taboptions.NewOptions(sk, &modelStatus)

return &ModelGithubRepository{
skeleton: skeleton,
skeleton: sk,
help: help.New(),
Keys: githubRepositoryKeys,
github: githubUseCase,
Expand All @@ -121,17 +119,6 @@ func SetupModelGithubRepository(skeleton *skeleton.Skeleton, githubUseCase gu.Us
textInput: ti,
syncRepositoriesContext: context.Background(),
cancelSyncRepositories: func() {},
updateSelfChan: make(chan selfUpdateMsg),
}
}

func (m *ModelGithubRepository) selfUpdate() {
m.updateSelfChan <- selfUpdateMsg{}
}

func (m *ModelGithubRepository) selfListen() tea.Cmd {
return func() tea.Msg {
return <-m.updateSelfChan
}
}

Expand All @@ -150,8 +137,10 @@ func (m *ModelGithubRepository) Init() tea.Cmd {
}

m.modelTabOptions.AddOption("Open in browser", openInBrowser)

go m.syncRepositories(m.syncRepositoriesContext)
return tea.Batch(m.modelTabOptions.Init(), m.SelfUpdater())

return tea.Batch(m.modelTabOptions.Init())
}

func (m *ModelGithubRepository) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -175,8 +164,6 @@ func (m *ModelGithubRepository) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.searchTableGithubRepository.GotoTop()
m.searchTableGithubRepository.SetCursor(0)
}
case selfUpdateMsg:
cmds = append(cmds, m.SelfUpdater())
}

m.textInput, cmd = m.textInput.Update(textInputMsg)
Expand All @@ -190,11 +177,11 @@ func (m *ModelGithubRepository) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.searchTableGithubRepository, cmd = m.searchTableGithubRepository.Update(msg)
cmds = append(cmds, cmd)

m.handleTableInputs(m.syncRepositoriesContext)

m.modelTabOptions, cmd = m.modelTabOptions.Update(msg)
cmds = append(cmds, cmd)

m.handleTableInputs(m.syncRepositoriesContext)

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

Expand Down Expand Up @@ -223,14 +210,9 @@ func (m *ModelGithubRepository) View() string {
m.modelTabOptions.View(), m.status.View(), helpWindowStyle.Render(m.ViewHelp()))
}

func (m *ModelGithubRepository) SelfUpdater() tea.Cmd {
return func() tea.Msg {
return <-m.updateSelfChan
}
}

func (m *ModelGithubRepository) syncRepositories(ctx context.Context) {
defer m.selfUpdate()
defer m.skeleton.TriggerUpdate()
defer m.modelTabOptions.SetStatus(taboptions.OptionIdle)

m.status.Reset() // reset previous errors
m.modelTabOptions.SetStatus(taboptions.OptionWait)
Expand Down Expand Up @@ -293,8 +275,6 @@ func (m *ModelGithubRepository) handleTableInputs(_ context.Context) {
m.selectedRepository.RepositoryName = selectedRow[0]
m.selectedRepository.BranchName = selectedRow[1]
}

m.modelTabOptions.SetStatus(taboptions.OptionIdle)
}

func (m *ModelGithubRepository) viewSearchBar() string {
Expand Down
39 changes: 11 additions & 28 deletions internal/terminal/handler/ghtrigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ type ModelGithubTrigger struct {
status *status.ModelStatus
textInput textinput.Model
tableTrigger table.Model

updateSelfChan chan any
}

func SetupModelGithubTrigger(skeleton *skeleton.Skeleton, githubUseCase gu.UseCase) *ModelGithubTrigger {
func SetupModelGithubTrigger(sk *skeleton.Skeleton, githubUseCase gu.UseCase) *ModelGithubTrigger {
var tableRowsTrigger []table.Row

tableTrigger := table.New(
Expand All @@ -80,9 +78,9 @@ func SetupModelGithubTrigger(skeleton *skeleton.Skeleton, githubUseCase gu.UseCa
ti.Blur()
ti.CharLimit = 72

modelStatus := status.SetupModelStatus(skeleton)
modelStatus := status.SetupModelStatus(sk)
return &ModelGithubTrigger{
skeleton: skeleton,
skeleton: sk,
help: help.New(),
Keys: githubTriggerKeys,
github: githubUseCase,
Expand All @@ -92,22 +90,11 @@ func SetupModelGithubTrigger(skeleton *skeleton.Skeleton, githubUseCase gu.UseCa
textInput: ti,
syncWorkflowContext: context.Background(),
cancelSyncWorkflow: func() {},
updateSelfChan: make(chan any),
}
}

func (m *ModelGithubTrigger) selfUpdate() {
m.updateSelfChan <- selfUpdateMsg{}
}

func (m *ModelGithubTrigger) selfListen() tea.Cmd {
return func() tea.Msg {
return <-m.updateSelfChan
}
}

func (m *ModelGithubTrigger) Init() tea.Cmd {
return tea.Batch(textinput.Blink, m.selfListen())
return tea.Batch(textinput.Blink)
}

func (m *ModelGithubTrigger) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand Down Expand Up @@ -183,8 +170,6 @@ func (m *ModelGithubTrigger) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
go m.triggerWorkflow()
}
}
case selfUpdateMsg:
cmds = append(cmds, m.selfListen())
}

m.tableTrigger, cmd = m.tableTrigger.Update(msg)
Expand Down Expand Up @@ -395,7 +380,7 @@ func (m *ModelGithubTrigger) inputController(_ context.Context) {
}

func (m *ModelGithubTrigger) syncWorkflowContent(ctx context.Context) {
defer m.selfUpdate()
defer m.skeleton.TriggerUpdate()

m.status.Reset()
m.status.SetProgressMessage(
Expand Down Expand Up @@ -579,9 +564,8 @@ func (m *ModelGithubTrigger) triggerWorkflow() {
m.fillEmptyValuesWithDefault()
}

m.status.SetProgressMessage(
fmt.Sprintf("[%s@%s]:[%s] Triggering workflow...",
m.selectedRepository.RepositoryName, m.selectedRepository.BranchName, m.selectedWorkflow))
m.status.SetProgressMessage(fmt.Sprintf("[%s@%s]:[%s] Triggering workflow...",
m.selectedRepository.RepositoryName, m.selectedRepository.BranchName, m.selectedWorkflow))

if m.workflowContent == nil {
m.status.SetErrorMessage("Workflow contents cannot be empty")
Expand Down Expand Up @@ -610,10 +594,9 @@ func (m *ModelGithubTrigger) triggerWorkflow() {
m.status.SetSuccessMessage(fmt.Sprintf("[%s@%s]:[%s] Workflow triggered.",
m.selectedRepository.RepositoryName, m.selectedRepository.BranchName, m.selectedWorkflow))

time.Sleep(1 * time.Second)
m.selfUpdate()
m.skeleton.TriggerUpdate()
m.status.SetProgressMessage("Switching to workflow history tab...")
time.Sleep(1500 * time.Millisecond)
time.Sleep(2000 * time.Millisecond)

// move these operations under new function named "resetTabSettings"
m.workflowContent = nil // reset workflow content
Expand All @@ -622,8 +605,8 @@ func (m *ModelGithubTrigger) triggerWorkflow() {
m.optionValues = nil // reset option values
m.selectedRepositoryName = "" // reset selected repository name

UpdateWorkflowHistory(time.Second * 3) // update workflow history
m.skeleton.SetActivePage("history") // switch tab to workflow history
m.skeleton.TriggerUpdateWithMsg(workflowHistoryUpdateMsg{time.Second * 3}) // update workflow history
m.skeleton.SetActivePage("history") // switch tab to workflow history
}

func (m *ModelGithubTrigger) emptySelector() string {
Expand Down
29 changes: 5 additions & 24 deletions internal/terminal/handler/ghworkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ type ModelGithubWorkflow struct {
help help.Model
tableTriggerableWorkflow table.Model
status *status.ModelStatus

updateSelfChan chan any
}

func SetupModelGithubWorkflow(skeleton *skeleton.Skeleton, githubUseCase gu.UseCase) *ModelGithubWorkflow {
func SetupModelGithubWorkflow(sk *skeleton.Skeleton, githubUseCase gu.UseCase) *ModelGithubWorkflow {
var tableRowsTriggerableWorkflow []table.Row

tableTriggerableWorkflow := table.New(
Expand All @@ -65,10 +63,10 @@ func SetupModelGithubWorkflow(skeleton *skeleton.Skeleton, githubUseCase gu.UseC
Bold(false)
tableTriggerableWorkflow.SetStyles(s)

modelStatus := status.SetupModelStatus(skeleton)
modelStatus := status.SetupModelStatus(sk)

return &ModelGithubWorkflow{
skeleton: skeleton,
skeleton: sk,
help: help.New(),
keys: githubWorkflowKeys,
github: githubUseCase,
Expand All @@ -77,34 +75,17 @@ func SetupModelGithubWorkflow(skeleton *skeleton.Skeleton, githubUseCase gu.UseC
selectedRepository: hdltypes.NewSelectedRepository(),
syncTriggerableWorkflowsContext: context.Background(),
cancelSyncTriggerableWorkflows: func() {},
updateSelfChan: make(chan any),
}
}

func (m *ModelGithubWorkflow) selfUpdate() {
m.updateSelfChan <- selfUpdateMsg{}
}

func (m *ModelGithubWorkflow) selfListen() tea.Cmd {
return func() tea.Msg {
return <-m.updateSelfChan
}
}

func (m *ModelGithubWorkflow) Init() tea.Cmd {
return tea.Batch(m.selfListen())
return nil
}

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

switch msg := msg.(type) {
case selfUpdateMsg:
_ = msg
cmds = append(cmds, m.selfListen())
}

if m.lastRepository != m.selectedRepository.RepositoryName {
m.tableReady = false // reset table ready status
m.cancelSyncTriggerableWorkflows() // cancel previous sync
Expand Down Expand Up @@ -154,7 +135,7 @@ func (m *ModelGithubWorkflow) View() string {
}

func (m *ModelGithubWorkflow) syncTriggerableWorkflows(ctx context.Context) {
defer m.selfUpdate()
defer m.skeleton.TriggerUpdate()

m.status.Reset()
m.status.SetProgressMessage(fmt.Sprintf("[%s@%s] Fetching triggerable workflows...", m.selectedRepository.RepositoryName, m.selectedRepository.BranchName))
Expand Down
Loading

0 comments on commit 1460ed0

Please sign in to comment.