Skip to content

Commit

Permalink
Merge pull request #636 from Stremio/feat/profile-settings-subtitles-…
Browse files Browse the repository at this point in the history
…opacity

feat: add subtitles_opacity to profile settings
  • Loading branch information
elpiel authored Feb 2, 2024
2 parents 9ba36f6 + 24f3c12 commit fde8815
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
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
72 changes: 70 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 @@ -1055,4 +1085,42 @@ 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");

let storage = STORAGE.read().expect("Should lock");

assert_eq!(
&14.to_string(),
storage
.get(SCHEMA_VERSION_STORAGE_KEY)
.expect("Should have the schema set"),
"Scheme version should now be updated"
);
assert_eq!(
&migrated_profile.to_string(),
storage
.get(PROFILE_STORAGE_KEY)
.expect("Should have the profile set"),
"Profile should match"
);
}
}
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 @@ -418,6 +418,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 @@ -89,6 +90,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 @@ -114,7 +117,7 @@ fn settings_de() {
&[
Token::Struct {
name: "Settings",
len: 21,
len: 22,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand Down Expand Up @@ -157,6 +160,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

0 comments on commit fde8815

Please sign in to comment.