Skip to content

Commit

Permalink
fix: rework pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
lostb1t committed Aug 13, 2023
1 parent 70a990d commit 838ffdb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 32 deletions.
64 changes: 51 additions & 13 deletions src/plex_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,31 +161,69 @@ impl PlexClient {
let mut c = self
.get_collection_children(id, Some(offset), Some(limit))
.await?;
// dbg!(limit);
// dbg!(c.media_container.children_mut())

if !config.include_watched {
// dbg!(c.media_container.children_mut().len());
let original_size = c.media_container.size.unwrap();
//let children =
c.media_container.children_mut().retain(|x| !x.is_watched());
// dbg!(c.media_container.children_mut().len());
// dbg!("-----");

let children_lenght = c.media_container.children_mut().len() as i32;
let total_size = c.media_container.total_size.unwrap();
if children_lenght < original_limit

// dbg!("checking", original_size, children_lenght, offset, total_size, limit, original_limit);
// if original_size != children_lenght {
// dbg!("checking", original_size, children_lenght, offset, total_size, limit, original_limit);
// }
// take into account watched, reload if we are under the requested limit
if (children_lenght < original_limit
&& total_size > offset + limit
&& offset < total_size
&& offset < total_size)
|| (children_lenght < original_limit
&& total_size > offset + original_size)
{
// load more
return self
let mut children = c.media_container.children();
// dbg!("recursive");
// dbg!("checking", original_size, children_lenght, offset, total_size, limit);
let new_limit = match limit {
x if x < 25 => 25 + x,
x if x > 25 => x * 2,
_ => 25 + limit,
};
let mut r = self
.load_collection_children_recursive(
id,
offset,
limit + 10,
offset + children_lenght,
new_limit,
// limit + 10,
original_limit,
)
.await;
.await.unwrap();
children.append(r.media_container.children_mut());
c.media_container.set_children(children);
// return self
// .load_collection_children_recursive(
// id,
// offset,
// new_limit,
// // limit + 10,
// original_limit,
// )
// .await;
}
}
// dbg!(c.media_container.children_mut().len());
// dbg!(limit);
c.media_container
.children_mut()
.truncate(original_limit as usize);

// for (pos, child) in c.media_container.children().iter().enumerate() {
// if child.title == "Plane" {
// dbg!(pos);
// }
// }
// dbg!(c.media_container.children_mut().len());
// dbg!("-----");
// dbg!("checking", offset, c.media_container.total_size.unwrap(), limit, original_limit, c.media_container.children_mut().len());
Ok(c)
}

Expand Down
51 changes: 32 additions & 19 deletions src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ impl Transform for LibraryMixTransform {
let collection_ids_len = self.collection_ids.clone().len() as i32;
let mut total_size = 0;
let mut collections_total_sizes: HashMap<u32, i32> = HashMap::with_capacity(collection_ids_len as usize);
let collection_offset =
(self.offset as f32 / collection_ids_len as f32).ceil() as i32;
let collection_limit =
(self.limit as f32 / collection_ids_len as f32).ceil() as i32;

for id in self.collection_ids.clone() {
// We need this for the total count. In theory it shouldnt have mattered.
Expand Down Expand Up @@ -469,27 +473,31 @@ impl Transform for LibraryMixTransform {
}

for id in self.collection_ids.clone() {
let percentage = {
let mut b = 0;
for s in collections_total_sizes.clone().into_iter() {
b += s.1;
}
let size = collections_total_sizes.get(&id);
// b / size
// dbg!(&b);
// dbg!(&size);
let x = ((size.unwrap() * 100 / b * 100) / 100) as f32;
x / 100.00
};
// dbg!(percentage);
// let percentage = {
// let mut b = 0;
// for s in collections_total_sizes.clone().into_iter() {
// b += s.1;
// }
// let size = collections_total_sizes.get(&id);
// // b / size
// // dbg!(&b);
// // dbg!(&size);
// let x = ((size.unwrap() * 100 / b * 100) / 100) as f32;
// // let x = (100 -((size.unwrap() * 100 / b * 100) / 100) as f32;
// x / 100.00
// };

let mut c = plex_client
.clone()
.get_cached(
plex_client.load_collection_children_recursive(
id,
(self.offset as f32 * percentage).ceil() as i32,
(self.limit as f32 * percentage).ceil() as i32,
(self.limit as f32 * percentage).ceil() as i32,
collection_offset,
collection_limit,
collection_limit,
// (self.offset as f32 * percentage).ceil() as i32,
// (self.limit as f32 * percentage).ceil() as i32,
// (self.limit as f32 * percentage).ceil() as i32,
),
format!(
"load_collection_children_recursive:{}:{}:{}",
Expand All @@ -498,7 +506,8 @@ impl Transform for LibraryMixTransform {
)
.await
.unwrap();

// dbg!(c.media_container.children())
// dbg!(c)
// if !config.include_watched {
// // We need this for the total count. In theory it shouldnt have mattered.
// // But IOS doesnt listen to changes in total size. Picks it from the first request.
Expand Down Expand Up @@ -540,7 +549,8 @@ impl Transform for LibraryMixTransform {
// } else {
// total_size += c.media_container.total_size.unwrap();
// }

// let local_c = c.media_container.children().truncate();
// children.truncate(self.limit as usize);
match children.is_empty() {
false => {
children = children
Expand All @@ -551,7 +561,10 @@ impl Transform for LibraryMixTransform {
true => children.append(&mut c.media_container.children()),
}
}

// children.truncate(self.limit as usize);

// dbg!("DONE");
// dbg!("------");
item.total_size = Some(total_size);
// always metadata library
item.metadata = children;
Expand Down

0 comments on commit 838ffdb

Please sign in to comment.