Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] Steam and Oculus are not at the top of the versions list #323

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/renderer/components/nav-bar/nav-bar.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function NavBar() {

if (downloadingVersion){ versions.push(downloadingVersion); }

const sorted = versions.sort((a, b) => +b.ReleaseDate - +a.ReleaseDate);
const sorted = BSVersionManagerService.sortVersions(versions);

return BSVersionManagerService.removeDuplicateVersions(sorted);
}
Expand Down
28 changes: 15 additions & 13 deletions src/renderer/services/bs-version-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ModalExitCode, ModalService } from "./modale.service";
import { NotificationService } from "./notification.service";
import { ProgressBarService } from "./progress-bar.service";
import { EditVersionModal } from "renderer/components/modal/modal-types/edit-version-modal.component";
import { swapElements } from "shared/helpers/array.helpers";
import { popElement } from "shared/helpers/array.helpers";

export class BSVersionManagerService {
private static instance: BSVersionManagerService;
Expand Down Expand Up @@ -34,18 +34,7 @@ export class BSVersionManagerService {
}

public setInstalledVersions(versions: BSVersion[]) {
const sorted: BSVersion[] = versions.sort((a, b) => +b.ReleaseDate - +a.ReleaseDate);

const steamIndex = sorted.findIndex(v => v.steam);
const oculusIndex = sorted.findIndex(v => v.oculus);

if (steamIndex > 0) {
swapElements(steamIndex, 0, sorted);
}
if (oculusIndex > 0) {
swapElements(oculusIndex, steamIndex >= 0 ? 1 : 0, sorted);
}

const sorted = BSVersionManagerService.sortVersions(versions);
this.installedVersions$.next(BSVersionManagerService.removeDuplicateVersions(sorted));
}

Expand Down Expand Up @@ -123,6 +112,19 @@ export class BSVersionManagerService {
return this.ipcService.sendV2("get-version-full-path", { args: version });
}

public static sortVersions(versions: BSVersion[]): BSVersion[] {
const steamVersion = popElement(v => v.steam, versions);
const oculusVersion = popElement(v => v.oculus, versions);

const compare = (a: BSVersion, b: BSVersion) => ( +b.ReleaseDate - +a.ReleaseDate )

const sorted: BSVersion[] = versions.sort(compare);

sorted.unshift(...[steamVersion, oculusVersion].filter(Boolean).sort(compare));

return sorted;
}

public static removeDuplicateVersions(versions: BSVersion[]): BSVersion[] {
return Array.from(new Map(versions.map(version => ([
JSON.stringify([
Expand Down
8 changes: 8 additions & 0 deletions src/shared/helpers/array.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ export function swapElements<T = unknown>(from: number, to: number, arr: T[]): T
[arr[from], arr[to]] = [arr[to], arr[from]];
return arr;
}

export function popElement<T = unknown>(func: (element: T) => boolean, arr: T[]): T {
const index = arr.findIndex(func);
if (index === -1) {
return null;
}
return arr.splice(index, 1)[0];
}
Loading