shared/domain/module/body/
poster.rs
use crate::domain::module::{
body::{Body, BodyConvert, BodyExt, ModeExt, StepExt, ThemeId, _groups::design::*},
ModuleKind,
};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use super::Audio;
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct ModuleData {
pub content: Option<Content>,
}
impl BodyExt<Mode, Step> for ModuleData {
fn as_body(&self) -> Body {
Body::Poster(self.clone())
}
fn is_complete(&self) -> bool {
self.content.is_some()
}
fn kind() -> ModuleKind {
ModuleKind::Poster
}
fn new_with_mode_and_theme(mode: Mode, theme: ThemeId) -> Self {
ModuleData {
content: Some(Content {
mode,
base: BaseContent {
theme,
..Default::default()
},
..Default::default()
}),
}
}
fn mode(&self) -> Option<Mode> {
self.content.as_ref().map(|c| c.mode.clone())
}
fn requires_choose_mode(&self) -> bool {
self.content.is_none()
}
fn set_editor_state_step(&mut self, step: Step) {
if let Some(content) = self.content.as_mut() {
content.editor_state.step = step;
}
}
fn set_editor_state_steps_completed(&mut self, steps_completed: HashSet<Step>) {
if let Some(content) = self.content.as_mut() {
content.editor_state.steps_completed = steps_completed;
}
}
fn get_editor_state_step(&self) -> Option<Step> {
self.content
.as_ref()
.map(|content| content.editor_state.step)
}
fn get_editor_state_steps_completed(&self) -> Option<HashSet<Step>> {
self.content
.as_ref()
.map(|content| content.editor_state.steps_completed.clone())
}
fn set_theme(&mut self, theme_id: ThemeId) {
if let Some(content) = self.content.as_mut() {
content.base.theme = theme_id;
}
}
fn get_theme(&self) -> Option<ThemeId> {
self.content.as_ref().map(|content| content.base.theme)
}
}
impl BodyConvert for ModuleData {}
impl TryFrom<Body> for ModuleData {
type Error = &'static str;
fn try_from(body: Body) -> Result<Self, Self::Error> {
match body {
Body::Poster(data) => Ok(data),
_ => Err("cannot convert body to poster!"),
}
}
}
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct Content {
pub editor_state: EditorState,
pub mode: Mode,
#[serde(default)]
pub audio: Option<Audio>,
pub base: BaseContent,
#[serde(default)]
pub play_settings: PlaySettings,
}
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct EditorState {
pub step: Step,
pub steps_completed: HashSet<Step>,
}
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq, Hash)]
pub enum Mode {
Printables,
TalkingPictures,
TeachAWord,
StoryTime,
Map,
Poster,
HearASong,
}
impl Default for Mode {
fn default() -> Self {
Self::Poster
}
}
impl ModeExt for Mode {
fn get_list() -> Vec<Self> {
vec![
Self::Printables,
Self::TalkingPictures,
Self::TeachAWord,
Self::StoryTime,
Self::Map,
Self::Poster,
Self::HearASong,
]
}
fn as_str_id(&self) -> &'static str {
match self {
Self::Printables => "printables",
Self::TalkingPictures => "talking-pictures",
Self::TeachAWord => "teach-a-word",
Self::StoryTime => "story-time",
Self::Map => "map",
Self::Poster => "poster",
Self::HearASong => "hear-a-song",
}
}
fn label(&self) -> &'static str {
const STR_PRINTABLES_LABEL: &'static str = "Printables";
const STR_TALKING_PICTURES_LABEL: &'static str = "Talking Picture";
const STR_TEACH_A_WORD_LABEL: &'static str = "Teach a Word";
const STR_STORY_TIME_LABEL: &'static str = "Storytime";
const STR_MAP_LABEL: &'static str = "Map";
const STR_POSTER_LABEL: &'static str = "Poster";
const STR_HEAR_A_SONG_LABEL: &'static str = "Hear a song";
match self {
Self::Printables => STR_PRINTABLES_LABEL,
Self::TalkingPictures => STR_TALKING_PICTURES_LABEL,
Self::TeachAWord => STR_TEACH_A_WORD_LABEL,
Self::StoryTime => STR_STORY_TIME_LABEL,
Self::Map => STR_MAP_LABEL,
Self::Poster => STR_POSTER_LABEL,
Self::HearASong => STR_HEAR_A_SONG_LABEL,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Step {
One,
Two,
Three,
Four,
}
impl Default for Step {
fn default() -> Self {
Self::One
}
}
impl StepExt for Step {
fn next(&self) -> Option<Self> {
match self {
Self::One => Some(Self::Two),
Self::Two => Some(Self::Three),
Self::Three => Some(Self::Four),
Self::Four => None,
}
}
fn as_number(&self) -> usize {
match self {
Self::One => 1,
Self::Two => 2,
Self::Three => 3,
Self::Four => 4,
}
}
fn label(&self) -> &'static str {
const STR_DESIGN: &'static str = "Design";
const STR_CONTENT: &'static str = "Content";
const STR_SETTINGS: &'static str = "Settings";
const STR_PREVIEW: &'static str = "Preview";
match self {
Self::One => STR_DESIGN,
Self::Two => STR_CONTENT,
Self::Three => STR_SETTINGS,
Self::Four => STR_PREVIEW,
}
}
fn get_list() -> Vec<Self> {
vec![Self::One, Self::Two, Self::Three, Self::Four]
}
fn get_preview() -> Self {
Self::Four
}
}
#[derive(Clone, Default, Serialize, Deserialize, Debug)]
pub struct PlaySettings {
pub next: Next,
}
#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub enum Next {
AfterAudio,
ClickNext,
}
impl Default for Next {
fn default() -> Self {
Self::ClickNext
}
}