Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add subtitles_opacity to profile settings #636

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub const NOTIFICATION_ITEMS_COUNT: usize = 100;
pub const WATCHED_THRESHOLD_COEF: f64 = 0.7;
pub const CREDITS_THRESHOLD_COEF: f64 = 0.9;
/// The latest migration scheme version
pub const SCHEMA_VERSION: u32 = 13;
pub const SCHEMA_VERSION: u32 = 14;
pub const IMDB_LINK_CATEGORY: &str = "imdb";
pub const GENRES_LINK_CATEGORY: &str = "Genres";
pub const CINEMETA_TOP_CATALOG_ID: &str = "top";
Expand Down
70 changes: 68 additions & 2 deletions src/runtime/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ pub trait Env {
.await?;
schema_version = 13;
}
if schema_version == 13 {
migrate_storage_schema_to_v14::<Self>()
.map_err(|error| EnvError::StorageSchemaVersionUpgrade(Box::new(error)))
.await?;
schema_version = 14;
}
if schema_version != SCHEMA_VERSION {
panic!(
"Storage schema version must be upgraded from {} to {}",
Expand Down Expand Up @@ -563,6 +569,29 @@ fn migrate_storage_schema_to_v13<E: Env>() -> TryEnvFuture<()> {
.boxed_env()
}

fn migrate_storage_schema_to_v14<E: Env>() -> TryEnvFuture<()> {
E::get_storage::<serde_json::Value>(PROFILE_STORAGE_KEY)
.and_then(|mut profile| {
match profile
.as_mut()
.and_then(|profile| profile.as_object_mut())
.and_then(|profile| profile.get_mut("settings"))
.and_then(|settings| settings.as_object_mut())
{
Some(settings) => {
settings.insert(
"subtitlesOpacity".to_owned(),
serde_json::Value::Number(100.into()),
);
E::set_storage(PROFILE_STORAGE_KEY, Some(&profile))
}
_ => E::set_storage::<()>(PROFILE_STORAGE_KEY, None),
}
})
.and_then(|_| E::set_storage(SCHEMA_VERSION_STORAGE_KEY, Some(&14)))
.boxed_env()
}

#[cfg(test)]
mod test {
use serde_json::{json, Value};
Expand All @@ -575,8 +604,9 @@ mod test {
env::{
migrate_storage_schema_to_v10, migrate_storage_schema_to_v11,
migrate_storage_schema_to_v12, migrate_storage_schema_to_v13,
migrate_storage_schema_to_v6, migrate_storage_schema_to_v7,
migrate_storage_schema_to_v8, migrate_storage_schema_to_v9,
migrate_storage_schema_to_v14, migrate_storage_schema_to_v6,
migrate_storage_schema_to_v7, migrate_storage_schema_to_v8,
migrate_storage_schema_to_v9,
},
Env,
},
Expand Down Expand Up @@ -628,6 +658,18 @@ mod test {
);
}

fn assert_storage_profile(profile: Value) {
let storage = STORAGE.read().expect("Should lock");

assert_eq!(
&profile.to_string(),
storage
.get(PROFILE_STORAGE_KEY)
.expect("Should have the schema set"),
"Profile should match"
);
}

#[tokio::test]
async fn test_migration_to_latest_version() {
{
Expand Down Expand Up @@ -1055,4 +1097,28 @@ mod test {
);
}
}

#[tokio::test]
async fn test_migration_from_13_to_14() {
let _test_env_guard = TestEnv::reset().expect("Should lock TestEnv");

let init_profile = json!({
"settings": {}
});

let migrated_profile = json!({
"settings": {
"subtitlesOpacity": 100
}
});

set_profile_and_schema_version(&init_profile, 13);

migrate_storage_schema_to_v14::<TestEnv>()
.await
.expect("Should migrate");

assert_storage_schema_version(14);
assert_storage_profile(migrated_profile);
}
}
2 changes: 2 additions & 0 deletions src/types/profile/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Settings {
pub subtitles_text_color: String,
pub subtitles_background_color: String,
pub subtitles_outline_color: String,
pub subtitles_opacity: u8,
/// Whether or not the Escape key should exists from the app when in Full screen.
pub esc_exit_fullscreen: bool,
/// The Seek time duration (in milliseconds) is when using the Arrow keys
Expand Down Expand Up @@ -69,6 +70,7 @@ impl Default for Settings {
subtitles_text_color: "#FFFFFFFF".to_owned(),
subtitles_background_color: "#00000000".to_owned(),
subtitles_outline_color: "#000000".to_owned(),
subtitles_opacity: 100,
esc_exit_fullscreen: true,
seek_time_duration: 10000,
seek_short_time_duration: 3000,
Expand Down
4 changes: 3 additions & 1 deletion src/unit_tests/serde/default_tokens_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ impl DefaultTokens for Settings {
vec![
Token::Struct {
name: "Settings",
len: 26,
len: 27,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand Down Expand Up @@ -416,6 +416,8 @@ impl DefaultTokens for Settings {
Token::Str("#00000000"),
Token::Str("subtitlesOutlineColor"),
Token::Str("#000000"),
Token::Str("subtitlesOpacity"),
Token::U8(100),
Token::Str("escExitFullscreen"),
Token::Bool(true),
Token::Str("seekTimeDuration"),
Expand Down
9 changes: 7 additions & 2 deletions src/unit_tests/serde/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn settings() {
subtitles_text_color: "subtitles_text_color".to_owned(),
subtitles_background_color: "subtitles_background_color".to_owned(),
subtitles_outline_color: "subtitles_outline_color".to_owned(),
subtitles_opacity: 1,
esc_exit_fullscreen: true,
seek_time_duration: 10,
seek_short_time_duration: 3,
Expand All @@ -39,7 +40,7 @@ fn settings() {
&[
Token::Struct {
name: "Settings",
len: 26,
len: 27,
},
Token::Str("interfaceLanguage"),
Token::Str("interface_language"),
Expand Down Expand Up @@ -87,6 +88,8 @@ fn settings() {
Token::Str("subtitles_background_color"),
Token::Str("subtitlesOutlineColor"),
Token::Str("subtitles_outline_color"),
Token::Str("subtitlesOpacity"),
Token::U8(1),
Token::Str("escExitFullscreen"),
Token::Bool(true),
Token::Str("seekTimeDuration"),
Expand All @@ -112,7 +115,7 @@ fn settings_de() {
&[
Token::Struct {
name: "Settings",
len: 21,
len: 22,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand Down Expand Up @@ -153,6 +156,8 @@ fn settings_de() {
Token::Str("#00000000"),
Token::Str("subtitlesOutlineColor"),
Token::Str("#000000"),
Token::Str("subtitlesOpacity"),
Token::U8(100),
Token::Str("escExitFullscreen"),
Token::Bool(true),
Token::Str("seekTimeDuration"),
Expand Down