Move URL handling to relevant structs and share selected song

This commit is contained in:
Rasmus Larsen
2019-05-09 16:04:07 +02:00
parent a145d73072
commit 4897d97931
7 changed files with 58 additions and 10 deletions

View File

@@ -199,6 +199,12 @@ impl ListItem for Album {
Some(AlbumView::new(queue, library, self).as_boxed_view_ext())
}
fn share_url(&self) -> Option<String> {
self.id
.clone()
.map(|id| format!("https://open.spotify.com/album/{}", id))
}
fn artist(&self) -> Option<Artist> {
Some(Artist::new(
self.artist_ids[0].clone(),

View File

@@ -193,4 +193,10 @@ impl ListItem for Artist {
fn open(&self, queue: Arc<Queue>, library: Arc<Library>) -> Option<Box<dyn ViewExt>> {
Some(ArtistView::new(queue, library, self).as_boxed_view_ext())
}
fn share_url(&self) -> Option<String> {
self.id
.clone()
.map(|id| format!("https://open.spotify.com/artist/{}", id))
}
}

View File

@@ -10,7 +10,7 @@ use clipboard::{ClipboardContext, ClipboardProvider};
use library::Library;
use queue::{Queue, RepeatSetting};
use spotify::Spotify;
use traits::ViewExt;
use traits::{ListItem, ViewExt};
use ui::layout::Layout;
type CommandCb = dyn Fn(&mut Cursive, &[String]) -> Result<Option<String>, String>;
@@ -202,14 +202,14 @@ impl CommandManager {
let queue = queue.clone();
self.register_command(
"share",
Some(Box::new(move |_, _| {
if let Some(url) = queue
.get_current()
.and_then(|t| t.id)
.map(|id| format!("https://open.spotify.com/track/{}", id))
{
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
ctx.set_contents(url).unwrap();
Some(Box::new(move |_, args| {
if let Some(url) = args.get(0).and_then(|source| match source.as_str() {
"current" => queue.get_current().and_then(|t| t.share_url()),
_ => None,
}) {
ClipboardProvider::new()
.and_then(|mut ctx: ClipboardContext| ctx.set_contents(url))
.unwrap();
}
Ok(None)
@@ -341,7 +341,8 @@ impl CommandManager {
kb.insert(",".into(), "seek -500".into());
kb.insert("r".into(), "repeat".into());
kb.insert("z".into(), "shuffle".into());
kb.insert("x".into(), "share".into());
kb.insert("x".into(), "share current".into());
kb.insert("Shift+x".into(), "share selected".into());
kb.insert("F1".into(), "focus queue".into());
kb.insert("F2".into(), "focus search".into());

View File

@@ -166,4 +166,11 @@ impl ListItem for Playlist {
fn open(&self, queue: Arc<Queue>, library: Arc<Library>) -> Option<Box<dyn ViewExt>> {
Some(PlaylistView::new(queue, library, self).as_boxed_view_ext())
}
fn share_url(&self) -> Option<String> {
Some(format!(
"https://open.spotify.com/user/{}/{}",
self.owner_id, self.id
))
}
}

View File

@@ -189,6 +189,12 @@ impl ListItem for Track {
None
}
fn share_url(&self) -> Option<String> {
self.id
.clone()
.map(|id| format!("https://open.spotify.com/track/{}", id))
}
fn album(&self, queue: Arc<Queue>) -> Option<Album> {
let spotify = queue.get_spotify();

View File

@@ -18,6 +18,7 @@ pub trait ListItem: Sync + Send + 'static {
fn queue(&mut self, queue: Arc<Queue>);
fn toggle_saved(&mut self, library: Arc<Library>);
fn open(&self, queue: Arc<Queue>, library: Arc<Library>) -> Option<Box<dyn ViewExt>>;
fn share_url(&self) -> Option<String>;
fn album(&self, _queue: Arc<Queue>) -> Option<Album> {
None

View File

@@ -9,6 +9,7 @@ use cursive::view::ScrollBase;
use cursive::{Cursive, Printer, Rect, Vec2};
use unicode_width::UnicodeWidthStr;
use clipboard::{ClipboardContext, ClipboardProvider};
use commands::CommandResult;
use library::Library;
use queue::Queue;
@@ -314,6 +315,26 @@ impl<I: ListItem + Clone> ViewExt for ListView<I> {
}
}
if cmd == "share" {
return args.get(0).map_or_else(
|| Err("wrong number of parameters".to_string()),
|source| match source.as_str() {
"selected" => {
if let Some(url) = self.content.read().ok().and_then(|content| {
content.get(self.selected).and_then(ListItem::share_url)
}) {
ClipboardProvider::new()
.and_then(|mut ctx: ClipboardContext| ctx.set_contents(url))
.unwrap();
}
Ok(CommandResult::Consumed(None))
}
_ => Ok(CommandResult::Ignored),
},
);
}
if cmd == "move" {
if let Some(dir) = args.get(0) {
let amount: usize = args