Skip to content
This repository has been archived by the owner on Dec 24, 2024. It is now read-only.

Calendar model #117

Closed
wants to merge 11 commits into from
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ default = []
log-trace = []

[dependencies]
stremio-core = { git = "https://github.com/Stremio/stremio-core", features = ["derive", "analytics"], branch = "development" }
stremio-core = { git = "https://github.com/Stremio/stremio-core", features = ["derive", "analytics"], branch = "feat/calendar-model" }
serde = { version = "1.0.*", features = ["derive"] }
serde_json = "1.0.*"
futures = "0.3.*"
Expand Down
18 changes: 18 additions & 0 deletions src/model/deep_links_ext/calendar_deep_links.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::model::deep_links_ext::DeepLinksExt;
use stremio_core::deep_links::{CalendarDeepLinks, CalendarItemDeepLinks};

impl DeepLinksExt for CalendarDeepLinks {
fn into_web_deep_links(self) -> Self {
Self {
calendar: self.calendar.replace("stremio://", "#"),
}
}
}

impl DeepLinksExt for CalendarItemDeepLinks {
fn into_web_deep_links(self) -> Self {
Self {
meta_details_streams: self.meta_details_streams.replace("stremio://", "#"),
}
}
}
1 change: 1 addition & 0 deletions src/model/deep_links_ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod deep_links_ext;
pub use deep_links_ext::*;

mod addons_deep_links;
mod calendar_deep_links;
mod discover_deep_links;
mod library_deep_links;
mod library_item_deep_links;
Expand Down
3 changes: 3 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub mod deep_links_ext;

mod serialize_calendar;
use serialize_calendar::*;

mod serialize_catalogs_with_extra;
use serialize_catalogs_with_extra::*;

Expand Down
7 changes: 7 additions & 0 deletions src/model/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use wasm_bindgen::JsValue;
use stremio_core::{
models::{
addon_details::AddonDetails,
calendar::Calendar,
catalog_with_filters::CatalogWithFilters,
catalogs_with_extra::CatalogsWithExtra,
continue_watching_preview::ContinueWatchingPreview,
Expand Down Expand Up @@ -39,6 +40,8 @@ use crate::{
},
};

use super::serialize_calendar;

#[derive(Model, Clone)]
#[cfg_attr(debug_assertions, derive(Serialize))]
#[model(WebEnv)]
Expand All @@ -51,6 +54,7 @@ pub struct WebModel {
pub discover: CatalogWithFilters<MetaItemPreview>,
pub library: LibraryWithFilters<NotRemovedFilter>,
pub continue_watching: LibraryWithFilters<ContinueWatchingFilter>,
pub calendar: Calendar,
pub search: CatalogsWithExtra,
/// Pre-loaded results for local search
pub local_search: LocalSearch,
Expand Down Expand Up @@ -84,6 +88,7 @@ impl WebModel {
InstalledAddonsWithFilters::new(&profile);
let (streaming_server, streaming_server_effects) = StreamingServer::new::<WebEnv>(&profile);
let (local_search, local_search_effects) = LocalSearch::new::<WebEnv>();

let model = WebModel {
ctx: Ctx::new(
profile,
Expand All @@ -101,6 +106,7 @@ impl WebModel {
discover,
library: library_,
continue_watching,
calendar: Default::default(),
search: Default::default(),
meta_details: Default::default(),
remote_addons,
Expand Down Expand Up @@ -150,6 +156,7 @@ impl WebModel {
"continuewatching".to_owned(),
),
WebModelField::Search => serialize_catalogs_with_extra(&self.search, &self.ctx),
WebModelField::Calendar => serialize_calendar(&self.calendar),
WebModelField::LocalSearch => serialize_local_search(&self.local_search),
WebModelField::MetaDetails => {
serialize_meta_details(&self.meta_details, &self.ctx, &self.streaming_server)
Expand Down
99 changes: 99 additions & 0 deletions src/model/serialize_calendar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use gloo_utils::format::JsValueSerdeExt;
use itertools::Itertools;
use serde::Serialize;
use stremio_core::{
deep_links::{CalendarDeepLinks, CalendarItemDeepLinks},
models::calendar::{Date, MonthInfo, Selected},
types::resource::SeriesInfo,
};
use url::Url;
use wasm_bindgen::JsValue;

use crate::model::deep_links_ext::DeepLinksExt;

mod model {
use super::*;

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CalendarContentItem<'a> {
pub id: &'a String,
pub name: &'a String,
pub poster: &'a Option<Url>,
pub title: &'a String,
#[serde(flatten)]
pub series_info: &'a Option<SeriesInfo>,
pub deep_links: CalendarItemDeepLinks,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CalendarItem<'a> {
pub date: &'a Date,
pub items: Vec<CalendarContentItem<'a>>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectableDate<'a> {
#[serde(flatten)]
pub date: &'a Date,
pub deep_links: CalendarDeepLinks,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Selectable<'a> {
pub prev: SelectableDate<'a>,
pub next: SelectableDate<'a>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Calendar<'a> {
pub selected: &'a Option<Selected>,
pub selectable: Selectable<'a>,
pub month_info: &'a MonthInfo,
pub items: &'a Vec<CalendarItem<'a>>,
}
}

pub fn serialize_calendar(calendar: &stremio_core::models::calendar::Calendar) -> JsValue {
<JsValue as JsValueSerdeExt>::from_serde(&model::Calendar {
selected: &calendar.selected,
selectable: model::Selectable {
prev: model::SelectableDate {
date: &calendar.selectable.prev,
deep_links: CalendarDeepLinks::from(&calendar.selectable.prev)
.into_web_deep_links(),
},
next: model::SelectableDate {
date: &calendar.selectable.next,
deep_links: CalendarDeepLinks::from(&calendar.selectable.next)
.into_web_deep_links(),
},
},
month_info: &calendar.month_info,
items: &calendar
.items
.iter()
.map(|item| model::CalendarItem {
date: &item.date,
items: item
.items
.iter()
.map(|item| model::CalendarContentItem {
id: &item.video.id,
name: &item.meta_item.preview.name,
poster: &item.meta_item.preview.poster,
title: &item.video.title,
series_info: &item.video.series_info,
deep_links: CalendarItemDeepLinks::from((&item.meta_item, &item.video))
.into_web_deep_links(),
})
.collect_vec(),
})
.collect_vec(),
})
.expect("JsValue from model::Calendar")
}