Skip to content

Commit

Permalink
Refactor populating the attributes of GameSelectionScreen pre-mount
Browse files Browse the repository at this point in the history
Extract any logic out of the component, only setting the attributes by
calling helper methods. Clean up unnecessary code and attempt to
simplify the implementation.

Refs TS-1138
  • Loading branch information
anttimaki committed Oct 17, 2022
1 parent 9ebe31d commit afb3957
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
6 changes: 6 additions & 0 deletions src/model/game/GameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,10 @@ export default class GameManager {
public static unsetGame(): Game {
return this._gameList.find(value => value.internalFolderName === "RiskOfRain2")!;
}

public static findByFolderName(name?: string|null) {
return name
? this._gameList.find((game) => game.internalFolderName === name)
: undefined;
}
}
46 changes: 13 additions & 33 deletions src/pages/GameSelectionScreen.vue
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ import Hero from '../components/Hero.vue';
import PathResolver from '../r2mm/manager/PathResolver';
import * as path from 'path';
import FileUtils from '../utils/FileUtils';
import * as ManagerUtils from '../utils/ManagerUtils';
import ManagerSettings from '../r2mm/manager/ManagerSettings';
import { StorePlatform } from '../model/game/StorePlatform';
import { GameSelectionDisplayMode } from '../model/game/GameSelectionDisplayMode';
Expand Down Expand Up @@ -389,41 +390,20 @@ export default class GameSelectionScreen extends Vue {
await this.$store.dispatch('checkMigrations');
this.runningMigration = false;
ManagerSettings.getSingleton(GameManager.unsetGame()).then(settings => {
const lastSelectedGame = settings.getContext().global.lastSelectedGame;
const savedViewMode = settings.getContext().global.gameSelectionViewMode;
switch (savedViewMode) {
case "List": this.viewMode = GameSelectionViewMode.LIST; break;
case "Card":
case undefined:
this.viewMode = GameSelectionViewMode.CARD;
break;
}
if (lastSelectedGame !== null) {
const game = GameManager.gameList.find(value => value.internalFolderName === lastSelectedGame);
if (game !== undefined) {
this.selectedGame = game;
}
}
});
ManagerSettings.getSingleton(GameManager.unsetGame()).then(value => {
this.settings = value;
this.favourites = value.getContext().global.favouriteGames || [];
if (value.getContext().global.defaultGame !== undefined) {
if (value.getContext().global.defaultStore !== undefined) {
const game = GameManager.gameList
.find(value1 => value1.internalFolderName === value.getContext().global.defaultGame)!;
const platform = game.storePlatformMetadata.find(value1 => value1.storePlatform === value.getContext().global.defaultStore)!;
this.settings = await ManagerSettings.getSingleton(GameManager.unsetGame());
const globalSettings = this.settings.getContext().global;
this.viewMode = globalSettings.gameSelectionViewMode;
this.favourites = globalSettings.favouriteGames ?? [];
this.selectedGame = GameManager.findByFolderName(globalSettings.lastSelectedGame) ?? null;
this.selectedGame = game;
this.selectedPlatform = platform.storePlatform;
// Skip game selection view if valid default game & platform are set.
const {defaultGame, defaultPlatform} = ManagerUtils.getDefaults(this.settings);
this.proceed();
return;
}
}
});
if (defaultGame && defaultPlatform) {
this.selectedGame = defaultGame;
this.selectedPlatform = defaultPlatform;
this.proceed();
}
}
toggleViewMode() {
Expand Down
2 changes: 1 addition & 1 deletion src/r2mm/manager/SettingsDexieStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export interface ManagerSettingsInterfaceGlobal_V2 {
favouriteGames: string[] | undefined;
defaultGame: string | undefined;
defaultStore: StorePlatform | undefined;
gameSelectionViewMode: string | undefined;
gameSelectionViewMode: GameSelectionViewMode;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/utils/ManagerUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import GameManager from "../model/game/GameManager";
import ManagerSettings from "../r2mm/manager/ManagerSettings";


/**
* Return default game selection needed to skip the game selection screen.
*/
export const getDefaults = (settings: ManagerSettings) => {
const globals = settings.getContext().global;
const defaultGame = GameManager.findByFolderName(globals.defaultGame);
const platforms = defaultGame ? defaultGame.storePlatformMetadata : [];
const defaultPlat = platforms.find(x => x.storePlatform === globals.defaultStore);

return {
defaultGame,
defaultPlatform: defaultPlat ? defaultPlat.storePlatform : undefined
};
}

0 comments on commit afb3957

Please # to comment.