Skip to content

Commit

Permalink
Merge pull request #1243 from ebkr/vuex-active-profile
Browse files Browse the repository at this point in the history
Duplicate active profile in Vuex
  • Loading branch information
MythicManiac authored Mar 5, 2024
2 parents ad01b03 + 4f64064 commit fcb21a7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
50 changes: 23 additions & 27 deletions src/pages/Profiles.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</template>
<template v-if="addingProfile && importUpdateSelection === 'UPDATE'">
<button class="button is-danger" v-if="!doesProfileExist(selectedProfile)">Update profile: {{ selectedProfile }}</button>
<button class="button is-info" v-else @click="updateProfile(selectedProfile)">Update profile: {{ selectedProfile }}</button>
<button class="button is-info" v-else @click="updateProfile()">Update profile: {{ selectedProfile }}</button>
</template>
<template v-if="renamingProfile">
<button class="button is-danger" v-if="doesProfileExist(newProfileName)">Rename</button>
Expand Down Expand Up @@ -186,7 +186,7 @@
</div>
</div>
<div v-for="(profileName) of profileList" :key="profileName">
<a @click="selectProfile(profileName)">
<a @click="selectedProfile = profileName">
<div class="container">
<div class="border-at-bottom">
<div class="card is-shadowless">
Expand Down Expand Up @@ -278,8 +278,6 @@ export default class Profiles extends Vue {
private profileList: string[] = ['Default'];
private selectedProfile: string = '';
private addingProfile: boolean = false;
private newProfileName: string = '';
private addingProfileType: string = 'Create';
Expand All @@ -301,6 +299,14 @@ export default class Profiles extends Vue {
private activeGame!: Game;
get selectedProfile(): string {
return this.$store.getters['profile/activeProfileName'];
}
set selectedProfile(profileName: string) {
this.$store.dispatch('profile/updateActiveProfile', profileName);
}
get appName(): string {
return ManagerInformation.APP_NAME;
}
Expand Down Expand Up @@ -336,14 +342,11 @@ export default class Profiles extends Vue {
);
this.closeNewProfileModal();
await this.updateProfileList();
this.selectedProfile = newName;
}
selectProfile(profile: string) {
new Profile(profile);
this.selectedProfile = profile;
settings.setProfile(profile);
}
// Open modal for entering a name for a new profile. Triggered
// either through user action or profile importing via file or code.
newProfile(type: string, nameOverride: string | undefined) {
this.newProfileName = nameOverride || '';
this.addingProfile = true;
Expand All @@ -355,18 +358,20 @@ export default class Profiles extends Vue {
});
}
// User confirmed creation of a new profile with a name that didn't exist before.
// The profile can be either empty or populated via importing.
createProfile(profile: string) {
const safeName = this.makeProfileNameSafe(profile);
if (safeName === '') {
return;
}
new Profile(safeName);
this.profileList.push(safeName);
this.selectedProfile = Profile.getActiveProfile().getProfileName();
this.selectedProfile = safeName;
this.addingProfile = false;
document.dispatchEvent(new CustomEvent("created-profile", {detail: safeName}));
}
// User confirmed updating an existing profile via importing.
updateProfile() {
this.addingProfile = false;
document.dispatchEvent(new CustomEvent("created-profile", {detail: this.selectedProfile}));
Expand All @@ -375,9 +380,6 @@ export default class Profiles extends Vue {
closeNewProfileModal() {
this.addingProfile = false;
this.renamingProfile = false;
if (this.addingProfile) {
document.dispatchEvent(new CustomEvent("created-profile", {detail: ''}));
}
}
removeProfile() {
Expand All @@ -404,12 +406,7 @@ export default class Profiles extends Vue {
}
}
}
new Profile('Default');
this.selectedProfile = Profile.getActiveProfile().getProfileName();
const settings = await ManagerSettings.getSingleton(this.activeGame);
await settings.setProfile(Profile.getActiveProfile().getProfileName());
this.selectedProfile = 'Default';
this.closeRemoveProfileModal();
}
Expand All @@ -426,7 +423,6 @@ export default class Profiles extends Vue {
// flashing on the screen while a new profile's list is loaded.
await this.$store.dispatch('profile/updateModList', []);
await settings.setProfile(Profile.getActiveProfile().getProfileName());
await this.$router.push({name: 'manager.installed'});
}
Expand Down Expand Up @@ -537,7 +533,9 @@ export default class Profiles extends Vue {
await FileUtils.emptyDirectory(path.join(Profile.getDirectory(), profileName));
await fs.rmdir(path.join(Profile.getDirectory(), profileName));
}
new Profile(profileName);
// Use commit instead of dispatch so _profile_update is not
// saved to persistent storage if something goes wrong.
this.$store.commit('profile/setActiveProfile', profileName);
}
if (parsed.getMods().length > 0) {
this.importingProfile = true;
Expand Down Expand Up @@ -569,7 +567,7 @@ export default class Profiles extends Vue {
}
}
if (this.importUpdateSelection === 'UPDATE') {
new Profile(event.detail);
this.selectedProfile = event.detail;
try {
await FileUtils.emptyDirectory(path.join(Profile.getDirectory(), event.detail));
} catch (e) {
Expand Down Expand Up @@ -652,15 +650,13 @@ export default class Profiles extends Vue {
}
async created() {
this.activeGame = GameManager.activeGame;
fs = FsProvider.instance;
settings = await ManagerSettings.getSingleton(this.activeGame);
await settings.load();
this.selectedProfile = settings.getContext().gameSpecific.lastSelectedProfile;
new Profile(this.selectedProfile);
await this.$store.dispatch('profile/loadLastSelectedProfile');
// Set default paths
if (settings.getContext().gameSpecific.gameDirectory === null) {
Expand Down
42 changes: 42 additions & 0 deletions src/store/modules/ProfileModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import ProfileModList from '../../r2mm/mods/ProfileModList';
import SearchUtils from '../../utils/SearchUtils';

interface State {
activeProfile: Profile | null;
modList: ManifestV2[];
order?: SortNaming;
direction?: SortDirection;
Expand All @@ -31,6 +32,7 @@ export default {
namespaced: true,

state: (): State => ({
activeProfile: null,
modList: [],
order: undefined,
direction: undefined,
Expand All @@ -39,6 +41,26 @@ export default {
}),

getters: <GetterTree<State, RootState>>{
activeProfile(state) {
if (state.activeProfile !== null) {
return state.activeProfile;
}

console.warn("Called profile/activeProfile but profile is not set. Falling back to Profile provider.");
const profile = Profile.getActiveProfile();

if (profile !== undefined) {
return profile;
}

console.warn("Called Profile.getActiveProfile but profile is not set. Falling back to Default profile.");
return new Profile('Default');
},

activeProfileName(_state, getters) {
return getters.activeProfile.getProfileName();
},

modsWithUpdates(state, _getters, rootState): ThunderstoreCombo[] {
return ThunderstoreDownloaderProvider.instance.getLatestOfAllToUpdate(
state.modList,
Expand Down Expand Up @@ -78,6 +100,15 @@ export default {
},

mutations: {
// Use updateActiveProfile action to ensure the persistent
// settings are updated.
setActiveProfile(state: State, profileName: string) {
// Stores the active profile in Profile.ts.
const profile = new Profile(profileName);

state.activeProfile = profile;
},

// Avoid calling this directly, prefer updateModList action to
// ensure TSMM specific code gets called.
setModList(state: State, list: ManifestV2[]) {
Expand Down Expand Up @@ -175,13 +206,24 @@ export default {
}
},

async loadLastSelectedProfile({commit, rootGetters}): Promise<string> {
const profileName = rootGetters['settings'].getContext().gameSpecific.lastSelectedProfile;
commit('setActiveProfile', profileName);
return profileName;
},

async loadOrderingSettings({commit, rootGetters}) {
const settings: ManagerSettings = rootGetters['settings'];
commit('setOrder', settings.getInstalledSortBy());
commit('setDirection', settings.getInstalledSortDirection());
commit('setDisabledPosition', settings.getInstalledDisablePosition());
},

async updateActiveProfile({commit, rootGetters}, profileName: string) {
commit('setActiveProfile', profileName);
rootGetters['settings'].setProfile(profileName);
},

async updateDirection({commit, rootGetters}, value: SortDirection) {
commit('setDirection', value);
rootGetters['settings'].setInstalledSortDirection(value);
Expand Down

0 comments on commit fcb21a7

Please sign in to comment.