Skip to content

Commit

Permalink
feature: Add service in foreground mobile app setting
Browse files Browse the repository at this point in the history
  • Loading branch information
TRtomasz committed Dec 5, 2024
1 parent 6fb40de commit b66d794
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 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
68 changes: 68 additions & 0 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,29 @@ 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),
);
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 @@ -616,6 +645,7 @@ mod test {
},
runtime::{
env::{
migrate_storage_schema_to_v16,
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,
Expand Down Expand Up @@ -1150,4 +1180,42 @@ 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
}
});

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 serverInForegroundSet 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 @@ -38,6 +38,7 @@ 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
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -77,6 +78,7 @@ impl Default for Settings {
pause_on_minimize: false,
surround_sound: false,
streaming_server_warning_dismissed: None,
server_in_foreground: false
}
}
}
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 @@ -374,7 +374,7 @@ impl DefaultTokens for Settings {
vec![
Token::Struct {
name: "Settings",
len: 27,
len: 28,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand Down Expand Up @@ -435,6 +435,8 @@ impl DefaultTokens for Settings {
Token::Bool(false),
Token::Str("streamingServerWarningDismissed"),
Token::None,
Token::Str("serverInForeground"),
Token::Bool(false),
Token::StructEnd,
]
}
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 @@ -36,11 +36,12 @@ fn settings() {
streaming_server_warning_dismissed: Some(
Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap(),
),
server_in_foreground: false
},
&[
Token::Struct {
name: "Settings",
len: 27,
len: 28,
},
Token::Str("interfaceLanguage"),
Token::Str("interface_language"),
Expand Down Expand Up @@ -105,6 +106,8 @@ fn settings() {
Token::Str("streamingServerWarningDismissed"),
Token::Some,
Token::Str("2021-01-01T00:00:00Z"),
Token::Str("serverInForeground"),
Token::Bool(false),
Token::StructEnd,
],
);
Expand All @@ -117,7 +120,7 @@ fn settings_de() {
&[
Token::Struct {
name: "Settings",
len: 22,
len: 23,
},
Token::Str("interfaceLanguage"),
Token::Str("eng"),
Expand Down Expand Up @@ -174,6 +177,8 @@ fn settings_de() {
Token::Bool(false),
Token::Str("streamingServerWarningDismissed"),
Token::None,
Token::Str("serverInForeground"),
Token::Bool(false),
Token::StructEnd,
],
);
Expand Down

0 comments on commit b66d794

Please sign in to comment.