update lib item when add/remove it from lib
@@ -12,6 +12,7 @@ use crate::types::profile::AuthKey;
use futures::future::Either;
use futures::{future, FutureExt, TryFutureExt};
use std::collections::HashMap;
+use std::marker::PhantomData;
pub fn update_library<E: Env + 'static>(
library: &mut LibraryBucket,
@@ -30,25 +31,13 @@ pub fn update_library<E: Env + 'static>(
}
}
Msg::Action(Action::Ctx(ActionCtx::AddToLibrary(meta_preview))) => {
- let mut library_item = LibraryItem {
- id: meta_preview.id.to_owned(),
- r#type: meta_preview.r#type.to_owned(),
- name: meta_preview.name.to_owned(),
- poster: meta_preview.poster.to_owned(),
- poster_shape: meta_preview.poster_shape.to_owned(),
- behavior_hints: meta_preview.behavior_hints.to_owned(),
- removed: false,
- temp: false,
- mtime: E::now(),
- ctime: Some(E::now()),
- state: LibraryItemState::default(),
- };
- if let Some(LibraryItem { ctime, state, .. }) = library.items.get(&meta_preview.id) {
- library_item.state = state.to_owned();
- if let Some(ctime) = ctime {
- library_item.ctime = Some(ctime.to_owned());
- };
+ let mut library_item = match library.items.get(&meta_preview.id) {
+ Some(library_item) => LibraryItem::from((meta_preview, library_item)),
+ _ => LibraryItem::from((meta_preview, PhantomData::<E>)),
};
+ library_item.removed = false;
+ library_item.temp = false;
+ library_item.mtime = E::now();
Effects::msg(Msg::Internal(Internal::UpdateLibraryItem(library_item)))
.join(Effects::msg(Msg::Event(Event::LibraryItemAdded {
id: meta_preview.id.to_owned(),
@@ -59,6 +48,8 @@ pub fn update_library<E: Env + 'static>(
Some(library_item) => {
let mut library_item = library_item.to_owned();
library_item.removed = true;
+ library_item.temp = false;
+ library_item.mtime = E::now();
Effects::msg(Msg::Internal(Internal::UpdateLibraryItem(library_item)))
.join(Effects::msg(Msg::Event(Event::LibraryItemRemoved {
id: id.to_owned(),
