1 /*
2  * Copyright (C) 2018 Emeric Poupon
3  *
4  * This file is part of LMS.
5  *
6  * LMS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * LMS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with LMS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include <optional>
23 #include <unordered_map>
24 
25 #include <Wt/WContainerWidget.h>
26 #include <Wt/WTemplate.h>
27 
28 #include "database/Types.hpp"
29 #include "PlayQueueAction.hpp"
30 
31 namespace Database
32 {
33 	class Track;
34 }
35 
36 namespace UserInterface {
37 
38 class Filters;
39 class Tracks : public Wt::WTemplate
40 {
41 	public:
42 		Tracks(Filters* filters);
43 
44 		PlayQueueActionSignal tracksAction;
45 
46 	private:
47 
48 		enum class Mode
49 		{
50 			Random,
51 			Starred,
52 			RecentlyPlayed,
53 			RecentlyAdded,
54 			MostPlayed,
55 			All
56 		};
57 
58 		void refreshView();
59 		void refreshView(Mode mode);
60 		void displayLoadingIndicator();
61 		void hideLoadingIndicator();
62 		void addSome();
63 
64 		std::vector<Wt::Dbo::ptr<Database::Track>> getRandomTracks(std::optional<Database::Range> range, bool& moreResults);
65 		std::vector<Wt::Dbo::ptr<Database::Track>> getTracks(std::optional<Database::Range> range, bool& moreResults);
66 		std::vector<Database::IdType> getAllTracks();
67 
68 		static constexpr Mode defaultMode {Mode::Random};
69 		static constexpr std::size_t batchSize {20};
70 		static inline std::unordered_map<Mode, std::optional<std::size_t>> maxItemsPerMode
71 		{
72 			{Mode::Random, batchSize * 20},
73 			{Mode::RecentlyPlayed, batchSize * 10},
74 			{Mode::RecentlyAdded, batchSize * 10},
75 			{Mode::MostPlayed, batchSize * 10},
76 			{Mode::All, batchSize * 50},
77 		};
78 
79 		Mode _mode {defaultMode};
80 		Filters* _filters {};
81 		std::vector<Database::IdType> _randomTracks;
82 		Wt::WContainerWidget* _tracksContainer {};
83 		Wt::WTemplate* _loadingIndicator {};
84 };
85 
86 } // namespace UserInterface
87 
88