Skip to content

Commit

Permalink
Merge pull request #748 from Stremio/feature/add-new-mobile-app-settings
Browse files Browse the repository at this point in the history
Feature/add new mobile app settings
  • Loading branch information
TRtomasz authored Dec 6, 2024
2 parents 6fb40de + 798742e commit 9fa57ed
Show file tree
Hide file tree
Showing 5 changed files with 94 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 @@ -39,7 +39,7 @@ pub const CALENDAR_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 = 15;
pub const SCHEMA_VERSION: u32 = 16;
pub const IMDB_LINK_CATEGORY: &str = "imdb";
pub const GENRES_LINK_CATEGORY: &str = "Genres";
pub const CINEMETA_TOP_CATALOG_ID: &str = "top";
Expand Down
74 changes: 72 additions & 2 deletions src/runtime/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ pub trait Env {
.await?;
schema_version = 15;
}
if schema_version == 15 {
migrate_storage_schema_to_v16::<Self>()
.map_err(|error| EnvError::StorageSchemaVersionUpgrade(Box::new(error)))
.await?;
schema_version = 16;
}
if schema_version != SCHEMA_VERSION {
panic!(
"Storage schema version must be upgraded from {} to {}",
Expand Down Expand Up @@ -606,6 +612,30 @@ fn migrate_storage_schema_to_v15<E: Env>() -> TryEnvFuture<()> {
.boxed_env()
}

fn migrate_storage_schema_to_v16<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(
"serverInForeground".to_owned(),
serde_json::Value::Bool(false),
);
settings.insert("sendCrashReports".to_owned(), serde_json::Value::Bool(true));
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(&16)))
.boxed_env()
}

#[cfg(test)]
mod test {
use serde_json::{json, Value};
Expand All @@ -619,8 +649,9 @@ mod test {
migrate_storage_schema_to_v10, migrate_storage_schema_to_v11,
migrate_storage_schema_to_v12, migrate_storage_schema_to_v13,
migrate_storage_schema_to_v14, migrate_storage_schema_to_v15,
migrate_storage_schema_to_v6, migrate_storage_schema_to_v7,
migrate_storage_schema_to_v8, migrate_storage_schema_to_v9,
migrate_storage_schema_to_v16, 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 @@ -1150,4 +1181,43 @@ mod test {
assert_storage_schema_version(15);
}
}

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

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

let migrated_profile = json!({
"settings": {
"serverInForeground": false,
"sendCrashReports": true
}
});

set_profile_and_schema_version(&init_profile, 15);

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

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

assert_eq!(
&16.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"
);
}
}
4 changes: 4 additions & 0 deletions src/types/profile/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct Settings {
pub pause_on_minimize: bool,
pub surround_sound: bool,
pub streaming_server_warning_dismissed: Option<DateTime<Utc>>,
pub server_in_foreground: bool,
pub send_crash_reports: bool,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -77,6 +79,8 @@ impl Default for Settings {
pause_on_minimize: false,
surround_sound: false,
streaming_server_warning_dismissed: None,
server_in_foreground: false,
send_crash_reports: true,
}
}
}
6 changes: 5 additions & 1 deletion src/unit_tests/serde/default_tokens_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl DefaultTokens for Settings {
vec![
Token::Struct {
name: "Settings",
len: 27,
len: 29,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand Down Expand Up @@ -435,6 +435,10 @@ impl DefaultTokens for Settings {
Token::Bool(false),
Token::Str("streamingServerWarningDismissed"),
Token::None,
Token::Str("serverInForeground"),
Token::Bool(false),
Token::Str("sendCrashReports"),
Token::Bool(true),
Token::StructEnd,
]
}
Expand Down
14 changes: 12 additions & 2 deletions src/unit_tests/serde/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ fn settings() {
streaming_server_warning_dismissed: Some(
Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap(),
),
server_in_foreground: false,
send_crash_reports: true,
},
&[
Token::Struct {
name: "Settings",
len: 27,
len: 29,
},
Token::Str("interfaceLanguage"),
Token::Str("interface_language"),
Expand Down Expand Up @@ -105,6 +107,10 @@ fn settings() {
Token::Str("streamingServerWarningDismissed"),
Token::Some,
Token::Str("2021-01-01T00:00:00Z"),
Token::Str("serverInForeground"),
Token::Bool(false),
Token::Str("sendCrashReports"),
Token::Bool(true),
Token::StructEnd,
],
);
Expand All @@ -117,7 +123,7 @@ fn settings_de() {
&[
Token::Struct {
name: "Settings",
len: 22,
len: 24,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand Down Expand Up @@ -174,6 +180,10 @@ fn settings_de() {
Token::Bool(false),
Token::Str("streamingServerWarningDismissed"),
Token::None,
Token::Str("serverInForeground"),
Token::Bool(false),
Token::Str("sendCrashReports"),
Token::Bool(true),
Token::StructEnd,
],
);
Expand Down

0 comments on commit 9fa57ed

Please sign in to comment.