aboutsummaryrefslogtreecommitdiff
path: root/screens/home.go
blob: fa88fb5ab45632d0033be04c36b56e7518ef64b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package screens

import (
	"fmt"
	"yato/config"
	"yato/lib"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
)

type HomeScreen struct {
	RecentAnimeRecommendations []lib.Recommendation
	RecentMangaRecommendations []lib.Recommendation
	imageCache                 *lib.ImageCache
	imageRenderer              *lib.ImageRenderer
}

func homeScreen() tea.Model {
	recentAnimeRecommendations, _ := lib.GetRecentAnimeRecommendations()
	recentMangaRecommendations, _ := lib.GetRecentMangaRecommendations()
	imageCache := lib.NewImageCache()
	imageRenderer := lib.NewImageRenderer()

	return HomeScreen{
		RecentAnimeRecommendations: recentAnimeRecommendations,
		RecentMangaRecommendations: recentMangaRecommendations,
		imageCache:                 imageCache,
		imageRenderer:              imageRenderer,
	}
}

func (h HomeScreen) Init() tea.Cmd {
	return nil
}

func (h HomeScreen) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	if msg, ok := msg.(tea.KeyMsg); ok {
		if msg.String() == "q" {
			return h, tea.Quit
		}
	}

	return h, nil
}

func (h HomeScreen) View() string {
	w := lipgloss.Width

	// Top bar, Content, Status bar
	topBarStyle := lipgloss.NewStyle().
		Foreground(config.Colors.Text).
		Background(config.Colors.Primary)

	mainText := topBarStyle.Padding(0, 0, 0, 1).Render(config.PrettyAppName + " | [H]ome | [A]nime | [M]anga | [S]earch | [C]ommunity | [P]rofile | [O]ptions | [Q]uit")
	userText := topBarStyle.Padding(0, 1, 0, 0).Render("User: " + globals.CurrentUser.Name + " | [L]ogout")
	separator := topBarStyle.Width(globals.width - w(mainText) - w(userText)).Render("")

	topBar := lipgloss.JoinHorizontal(
		lipgloss.Top,
		mainText,
		separator,
		userText,
	)

	content := ""
	// Top 5 recommendations
	for i, rec := range h.RecentAnimeRecommendations {
		if i == 5 {
			break
		}
		content += h.renderRecommendation("anime", rec)
	}

	for i, rec := range h.RecentMangaRecommendations {
		if i == 5 {
			break
		}
		content += h.renderRecommendation("manga", rec)
	}

	return lipgloss.JoinVertical(
		lipgloss.Top,
		topBar,
		content,
	)

}

func (h HomeScreen) renderRecommendation(mediaType string, rec lib.Recommendation) string {
	img, err := h.imageCache.GetImage(mediaType, rec.Entry[0].MALId, "small", rec.Entry[0].Images.JPG.SmallImageURL)
	if err != nil {
		return fmt.Sprintf("%s -> %s\n", rec.Entry[0].Title, rec.Entry[1].Title)
	}

	renderedImage := h.imageRenderer.RenderImage(img, 20, 30)
	return fmt.Sprintf("%s%s -> %s\n", renderedImage, rec.Entry[0].Title, rec.Entry[1].Title)
}