changeset 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 969a3e8c79c5
children 65df2813d0de
files include/core/anime.h src/core/anime.cc src/core/anime_db.cc src/core/anime_season.cc src/services/anilist.cc
diffstat 5 files changed, 49 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/include/core/anime.h	Wed May 08 16:20:37 2024 -0400
+++ b/include/core/anime.h	Wed May 08 16:21:05 2024 -0400
@@ -118,11 +118,7 @@
 
 struct SeriesInformation {
 	int id;
-	struct {
-		std::string romaji;
-		std::string english;
-		std::string native;
-	} title;
+	std::map<TitleLanguage, std::string> titles;
 	std::vector<std::string> synonyms;
 	int episodes = 0;
 	SeriesStatus status = SeriesStatus::Unknown;
@@ -164,9 +160,7 @@
 
 	/* Series data */
 	int GetId() const;
-	std::string GetRomajiTitle() const;
-	std::string GetEnglishTitle() const;
-	std::string GetNativeTitle() const;
+	std::optional<std::string> GetTitle(TitleLanguage language) const;
 	std::vector<std::string> GetTitleSynonyms() const;
 	int GetEpisodes() const;
 	SeriesStatus GetAiringStatus() const;
@@ -182,9 +176,7 @@
 	std::string GetServiceUrl() const;
 
 	void SetId(int id);
-	void SetRomajiTitle(std::string const& title);
-	void SetEnglishTitle(std::string const& title);
-	void SetNativeTitle(std::string const& title);
+	void SetTitle(TitleLanguage language, const std::string& title);
 	void SetTitleSynonyms(std::vector<std::string> const& synonyms);
 	void AddTitleSynonym(std::string const& synonym);
 	void SetEpisodes(int episodes);
--- a/src/core/anime.cc	Wed May 08 16:20:37 2024 -0400
+++ b/src/core/anime.cc	Wed May 08 16:21:05 2024 -0400
@@ -170,19 +170,16 @@
 	return info_.id;
 }
 
-std::string Anime::GetRomajiTitle() const {
-	return info_.title.romaji;
-}
+/* note: this should use std::optional */
+std::optional<std::string> Anime::GetTitle(TitleLanguage language) const {
+	if (info_.titles.find(language) == info_.titles.end())
+		return std::nullopt;
 
-std::string Anime::GetEnglishTitle() const {
-	return info_.title.english;
-}
-
-std::string Anime::GetNativeTitle() const {
-	return info_.title.native;
+	return info_.titles.at(language);
 }
 
 std::vector<std::string> Anime::GetTitleSynonyms() const {
+	/* mainly for the GUI */
 	std::vector<std::string> result;
 
 	auto add_to_synonyms = [this](std::vector<std::string>& vec, std::string key) {
@@ -190,9 +187,9 @@
 			vec.push_back(key);
 	};
 
-	add_to_synonyms(result, info_.title.english);
-	add_to_synonyms(result, info_.title.romaji);
-	add_to_synonyms(result, info_.title.native);
+	for (const auto& lang : TitleLanguages)
+		if (info_.titles.find(lang) != info_.titles.end())
+			add_to_synonyms(result, info_.titles.at(lang));
 
 	for (auto& synonym : info_.synonyms)
 		add_to_synonyms(result, synonym);
@@ -257,16 +254,8 @@
 	info_.id = id;
 }
 
-void Anime::SetRomajiTitle(std::string const& title) {
-	info_.title.romaji = title;
-}
-
-void Anime::SetEnglishTitle(std::string const& title) {
-	info_.title.english = title;
-}
-
-void Anime::SetNativeTitle(std::string const& title) {
-	info_.title.native = title;
+void Anime::SetTitle(TitleLanguage language, const std::string& title) {
+	info_.titles[language] = title;
 }
 
 void Anime::SetTitleSynonyms(std::vector<std::string> const& synonyms) {
@@ -318,12 +307,16 @@
 }
 
 std::string Anime::GetUserPreferredTitle() const {
-	switch (session.config.anime_list.language) {
-		case TitleLanguage::Native: return (GetNativeTitle().empty()) ? GetRomajiTitle() : GetNativeTitle();
-		case TitleLanguage::Romaji: return (GetEnglishTitle().empty()) ? GetRomajiTitle() : GetEnglishTitle();
-		default: break;
-	}
-	return GetRomajiTitle();
+	std::optional<std::string> title = GetTitle(session.config.anime_list.language);
+	if (title.has_value())
+		return title.value();
+
+	title = GetTitle(TitleLanguage::Romaji);
+	if (title.has_value())
+		return title.value();
+
+	/* what? */
+	return std::string();
 }
 
 } // namespace Anime
--- a/src/core/anime_db.cc	Wed May 08 16:20:37 2024 -0400
+++ b/src/core/anime_db.cc	Wed May 08 16:21:05 2024 -0400
@@ -157,11 +157,6 @@
 	// clang-format off
 	json = {
 		{"id", anime.GetId()},
-		{"title", {
-			{"native", anime.GetNativeTitle()},
-			{"romaji", anime.GetRomajiTitle()},
-			{"english", anime.GetEnglishTitle()}
-		}},
 		{"synonyms", anime.GetTitleSynonyms()},
 		{"episodes", anime.GetEpisodes()},
 		{"airing_status", Translate::ToString(anime.GetAiringStatus())},
@@ -177,6 +172,13 @@
 	};
 	// clang-format on
 
+	/* now for dynamically-filled stuff */
+	for (const auto& lang : TitleLanguages) {
+		std::optional<std::string> title = anime.GetTitle(lang);
+		if (title.has_value())
+			json["title"][Strings::ToLower(Translate::ToString(lang))] = title.value();
+	}
+
 	nlohmann::json user;
 	if (GetListDataAsJSON(anime, user))
 		json.push_back({"list_data", user});
@@ -236,9 +238,12 @@
 	Anime& anime = database.items[id];
 
 	anime.SetId(id);
-	anime.SetNativeTitle(JSON::GetString<std::string>(json, "/title/native"_json_pointer, ""));
-	anime.SetRomajiTitle(JSON::GetString<std::string>(json, "/title/romaji"_json_pointer, ""));
-	anime.SetEnglishTitle(JSON::GetString<std::string>(json, "/title/english"_json_pointer, ""));
+	for (const auto& lang : TitleLanguages) {
+		nlohmann::json::json_pointer p("/title/" + Strings::ToLower(Translate::ToString(lang)));
+
+		if (json.contains(p) && json[p].is_string())
+			anime.SetTitle(lang, json[p].get<std::string>());
+	}
 	anime.SetTitleSynonyms(JSON::GetArray<std::vector<std::string>>(json, "/synonyms"_json_pointer, {}));
 	anime.SetEpisodes(JSON::GetNumber(json, "/episodes"_json_pointer, 0));
 	anime.SetAiringStatus(
--- a/src/core/anime_season.cc	Wed May 08 16:20:37 2024 -0400
+++ b/src/core/anime_season.cc	Wed May 08 16:21:05 2024 -0400
@@ -7,6 +7,7 @@
 SeriesSeason GetSeasonForMonth(Date::Month month) {
 	switch (month) {
 		case Date::Month::Jan: case Date::Month::Feb: case Date::Month::Mar:
+		default:
 			return SeriesSeason::Winter;
 		case Date::Month::Apr: case Date::Month::May: case Date::Month::Jun:
 			return SeriesSeason::Spring;
--- a/src/services/anilist.cc	Wed May 08 16:20:37 2024 -0400
+++ b/src/services/anilist.cc	Wed May 08 16:21:05 2024 -0400
@@ -110,9 +110,17 @@
 }
 
 void ParseTitle(const nlohmann::json& json, Anime::Anime& anime) {
-	anime.SetNativeTitle(JSON::GetString<std::string>(json, "/native"_json_pointer, ""));
-	anime.SetEnglishTitle(JSON::GetString<std::string>(json, "/english"_json_pointer, ""));
-	anime.SetRomajiTitle(JSON::GetString<std::string>(json, "/romaji"_json_pointer, ""));
+	nlohmann::json::json_pointer g = "/native"_json_pointer;
+	if (json.contains(g) && json[g].is_string())
+		anime.SetTitle(Anime::TitleLanguage::Native, json[g]);
+
+	g = "/english"_json_pointer;
+	if (json.contains(g) && json[g].is_string())
+		anime.SetTitle(Anime::TitleLanguage::English, json[g]);
+
+	g = "/romaji"_json_pointer;
+	if (json.contains(g) && json[g].is_string())
+		anime.SetTitle(Anime::TitleLanguage::Romaji, json[g]);
 }
 
 int ParseMediaJson(const nlohmann::json& json) {