comparison src/core/anime.cc @ 284:e66ffc338d82

anime: refactor title structure to a map
author Paper <paper@paper.us.eu.org>
date Wed, 08 May 2024 16:21:05 -0400
parents 9b6e12c14a1e
children 53e3c015a973
comparison
equal deleted inserted replaced
283:969a3e8c79c5 284:e66ffc338d82
168 /* Series data */ 168 /* Series data */
169 int Anime::GetId() const { 169 int Anime::GetId() const {
170 return info_.id; 170 return info_.id;
171 } 171 }
172 172
173 std::string Anime::GetRomajiTitle() const { 173 /* note: this should use std::optional */
174 return info_.title.romaji; 174 std::optional<std::string> Anime::GetTitle(TitleLanguage language) const {
175 } 175 if (info_.titles.find(language) == info_.titles.end())
176 176 return std::nullopt;
177 std::string Anime::GetEnglishTitle() const { 177
178 return info_.title.english; 178 return info_.titles.at(language);
179 }
180
181 std::string Anime::GetNativeTitle() const {
182 return info_.title.native;
183 } 179 }
184 180
185 std::vector<std::string> Anime::GetTitleSynonyms() const { 181 std::vector<std::string> Anime::GetTitleSynonyms() const {
182 /* mainly for the GUI */
186 std::vector<std::string> result; 183 std::vector<std::string> result;
187 184
188 auto add_to_synonyms = [this](std::vector<std::string>& vec, std::string key) { 185 auto add_to_synonyms = [this](std::vector<std::string>& vec, std::string key) {
189 if (!key.empty() && !std::count(vec.begin(), vec.end(), key) && key != GetUserPreferredTitle()) 186 if (!key.empty() && !std::count(vec.begin(), vec.end(), key) && key != GetUserPreferredTitle())
190 vec.push_back(key); 187 vec.push_back(key);
191 }; 188 };
192 189
193 add_to_synonyms(result, info_.title.english); 190 for (const auto& lang : TitleLanguages)
194 add_to_synonyms(result, info_.title.romaji); 191 if (info_.titles.find(lang) != info_.titles.end())
195 add_to_synonyms(result, info_.title.native); 192 add_to_synonyms(result, info_.titles.at(lang));
196 193
197 for (auto& synonym : info_.synonyms) 194 for (auto& synonym : info_.synonyms)
198 add_to_synonyms(result, synonym); 195 add_to_synonyms(result, synonym);
199 196
200 return result; 197 return result;
255 252
256 void Anime::SetId(int id) { 253 void Anime::SetId(int id) {
257 info_.id = id; 254 info_.id = id;
258 } 255 }
259 256
260 void Anime::SetRomajiTitle(std::string const& title) { 257 void Anime::SetTitle(TitleLanguage language, const std::string& title) {
261 info_.title.romaji = title; 258 info_.titles[language] = title;
262 }
263
264 void Anime::SetEnglishTitle(std::string const& title) {
265 info_.title.english = title;
266 }
267
268 void Anime::SetNativeTitle(std::string const& title) {
269 info_.title.native = title;
270 } 259 }
271 260
272 void Anime::SetTitleSynonyms(std::vector<std::string> const& synonyms) { 261 void Anime::SetTitleSynonyms(std::vector<std::string> const& synonyms) {
273 info_.synonyms = synonyms; 262 info_.synonyms = synonyms;
274 } 263 }
316 void Anime::SetPosterUrl(std::string url) { 305 void Anime::SetPosterUrl(std::string url) {
317 info_.poster_url = url; 306 info_.poster_url = url;
318 } 307 }
319 308
320 std::string Anime::GetUserPreferredTitle() const { 309 std::string Anime::GetUserPreferredTitle() const {
321 switch (session.config.anime_list.language) { 310 std::optional<std::string> title = GetTitle(session.config.anime_list.language);
322 case TitleLanguage::Native: return (GetNativeTitle().empty()) ? GetRomajiTitle() : GetNativeTitle(); 311 if (title.has_value())
323 case TitleLanguage::Romaji: return (GetEnglishTitle().empty()) ? GetRomajiTitle() : GetEnglishTitle(); 312 return title.value();
324 default: break; 313
325 } 314 title = GetTitle(TitleLanguage::Romaji);
326 return GetRomajiTitle(); 315 if (title.has_value())
316 return title.value();
317
318 /* what? */
319 return std::string();
327 } 320 }
328 321
329 } // namespace Anime 322 } // namespace Anime