-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pagination): added pagination to queue
- Loading branch information
1 parent
3310947
commit b90bfa6
Showing
4 changed files
with
104 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod pagination; | ||
pub mod spotify; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::collections::VecDeque; | ||
|
||
use crate::state::Track; | ||
|
||
pub struct PaginatedQueue<'a> { | ||
queue: &'a VecDeque<Track>, | ||
page: usize, | ||
limit: usize, | ||
total: usize, | ||
} | ||
|
||
impl<'a> PaginatedQueue<'a> { | ||
pub fn new(queue: &'a VecDeque<Track>, total: usize, page: usize) -> Self { | ||
Self { | ||
queue, | ||
page, | ||
limit: 10, | ||
total, | ||
} | ||
} | ||
|
||
fn start_idx(&self) -> usize { | ||
(self.page - 1) * self.limit | ||
} | ||
|
||
fn end_idx(&self) -> usize { | ||
(self.start_idx() + self.limit).min(self.total) | ||
} | ||
|
||
pub fn total_pages(&self) -> usize { | ||
(self.total + self.limit - 1) / self.limit | ||
} | ||
|
||
pub fn get_fields(&self) -> impl IntoIterator<Item = (String, String, bool)> + '_ { | ||
let start = self.start_idx(); | ||
let end = self.end_idx(); | ||
self.queue | ||
.iter() | ||
.skip(start) | ||
.take(end - start) | ||
.enumerate() | ||
.map(move |(index, song)| { | ||
if index == 0 { | ||
( | ||
format!( | ||
"{}. {} - {}[{}] ⬅️", | ||
index + 1 + start, | ||
song.artist, | ||
song.name, | ||
song.duration | ||
), | ||
"".to_string(), | ||
false, | ||
) | ||
} else { | ||
( | ||
format!( | ||
"{}. {} - {}[{}]", | ||
index + 1 + start, | ||
song.artist, | ||
song.name, | ||
song.duration | ||
), | ||
"".to_string(), | ||
false, | ||
) | ||
} | ||
}) | ||
} | ||
} |