mirror of
https://github.com/immich-app/immich.git
synced 2025-01-01 08:31:59 +00:00
feat(web): uniform random asset selection in slideshow mode (#4953)
* Implement weighted asset selection * Rename totalAssets -> bucketCount * Remove redundant check
This commit is contained in:
parent
0bf55d8e32
commit
55e3605ca4
1 changed files with 15 additions and 8 deletions
|
@ -32,6 +32,7 @@ export class AssetBucket {
|
||||||
*/
|
*/
|
||||||
bucketHeight!: number;
|
bucketHeight!: number;
|
||||||
bucketDate!: string;
|
bucketDate!: string;
|
||||||
|
bucketCount!: number;
|
||||||
assets!: AssetResponseDto[];
|
assets!: AssetResponseDto[];
|
||||||
cancelToken!: AbortController | null;
|
cancelToken!: AbortController | null;
|
||||||
position!: BucketPosition;
|
position!: BucketPosition;
|
||||||
|
@ -158,6 +159,7 @@ export class AssetStore {
|
||||||
return {
|
return {
|
||||||
bucketDate: bucket.timeBucket,
|
bucketDate: bucket.timeBucket,
|
||||||
bucketHeight: height,
|
bucketHeight: height,
|
||||||
|
bucketCount: bucket.count,
|
||||||
assets: [],
|
assets: [],
|
||||||
cancelToken: null,
|
cancelToken: null,
|
||||||
position: BucketPosition.Unknown,
|
position: BucketPosition.Unknown,
|
||||||
|
@ -274,6 +276,7 @@ export class AssetStore {
|
||||||
bucket = {
|
bucket = {
|
||||||
bucketDate: timeBucket,
|
bucketDate: timeBucket,
|
||||||
bucketHeight: THUMBNAIL_HEIGHT,
|
bucketHeight: THUMBNAIL_HEIGHT,
|
||||||
|
bucketCount: 0,
|
||||||
assets: [],
|
assets: [],
|
||||||
cancelToken: null,
|
cancelToken: null,
|
||||||
position: BucketPosition.Unknown,
|
position: BucketPosition.Unknown,
|
||||||
|
@ -313,16 +316,17 @@ export class AssetStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRandomAsset(): Promise<AssetResponseDto | null> {
|
async getRandomAsset(): Promise<AssetResponseDto | null> {
|
||||||
const bucket = this.buckets[Math.floor(Math.random() * this.buckets.length)] || null;
|
let index = Math.floor(Math.random() * this.buckets.reduce((acc, bucket) => acc + bucket.bucketCount, 0));
|
||||||
if (!bucket) {
|
for (const bucket of this.buckets) {
|
||||||
return null;
|
if (index < bucket.bucketCount) {
|
||||||
}
|
|
||||||
|
|
||||||
if (bucket.assets.length === 0) {
|
|
||||||
await this.loadBucket(bucket.bucketDate, BucketPosition.Unknown);
|
await this.loadBucket(bucket.bucketDate, BucketPosition.Unknown);
|
||||||
|
return bucket.assets[index] || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return bucket.assets[Math.floor(Math.random() * bucket.assets.length)] || null;
|
index -= bucket.bucketCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateAsset(_asset: AssetResponseDto, recalculate = false) {
|
updateAsset(_asset: AssetResponseDto, recalculate = false) {
|
||||||
|
@ -412,6 +416,9 @@ export class AssetStore {
|
||||||
const assetToBucket: Record<string, AssetLookup> = {};
|
const assetToBucket: Record<string, AssetLookup> = {};
|
||||||
for (let i = 0; i < this.buckets.length; i++) {
|
for (let i = 0; i < this.buckets.length; i++) {
|
||||||
const bucket = this.buckets[i];
|
const bucket = this.buckets[i];
|
||||||
|
if (bucket.assets.length !== 0) {
|
||||||
|
bucket.bucketCount = bucket.assets.length;
|
||||||
|
}
|
||||||
for (let j = 0; j < bucket.assets.length; j++) {
|
for (let j = 0; j < bucket.assets.length; j++) {
|
||||||
const asset = bucket.assets[j];
|
const asset = bucket.assets[j];
|
||||||
assetToBucket[asset.id] = { bucket, bucketIndex: i, assetIndex: j };
|
assetToBucket[asset.id] = { bucket, bucketIndex: i, assetIndex: j };
|
||||||
|
|
Loading…
Reference in a new issue