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

Feature/add new mobile app settings #748

Merged
merged 5 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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 @@ -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
77 changes: 75 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,33 @@ 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(false),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serde_json::Value::Bool(false),
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 +652,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 +1184,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": false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"sendCrashReports": 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: false,
}
}
}
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(false),
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: false,
},
&[
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(false),
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(false),
Token::StructEnd,
],
);
Expand Down
Loading