Skip to content

Commit

Permalink
Change profile picture when changed in Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
saertna committed Sep 1, 2024
1 parent 7d852bc commit 5dfe25a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/GamificationMediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export interface GamificationMediator {
acquireIngredients(chance:number, min:number, max:number): void;

saveSettings(): Promise<void>;

updateProfileLeaf(): void;

updateProfileLeafPic(): void;
}
17 changes: 14 additions & 3 deletions src/GamificationMediatorImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@ import { GamificationMediator } from './GamificationMediator';
import {decryptBoolean, decryptNumber, decryptString, encryptBoolean, encryptNumber, encryptString} from "./encryption";
import {Badge} from "./badges";
import {debugLogs, elements, listOfUseableIngredientsToBeShown, mil2sec} from "./constants";
import {Notice, Plugin} from 'obsidian';
import {Notice} from 'obsidian';
import {concatenateStrings} from "./Utils";
import {defaultSettings} from "./settings";
import gamification from "./main";

export class GamificationMediatorImpl implements GamificationMediator {
private settings: any;
private plugin: Plugin;
private plugin: gamification;

constructor(settings: any, plugin: Plugin) {
constructor(settings: any, plugin: gamification) {
this.settings = settings;
this.plugin = plugin;
}

updateProfileLeaf() {
this.plugin.actualizeProfileLeave();
}

updateProfileLeafPic() {
this.plugin.profileLeafUpdatePicture();
}

getSettingString(key: string): string {
const decryptedValue = this.settings[key] !== undefined ? this.settings[key].toString() : ''
//if(debugLogs) console.debug(`String: decrypted ${key} is ${decryptString(decryptedValue)}`)
Expand Down Expand Up @@ -104,4 +113,6 @@ export class GamificationMediatorImpl implements GamificationMediator {
return Math.floor(Math.random() * (max - min + 1)) + min;
}



}
38 changes: 31 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export default class gamification extends Plugin {
// Perform operations that require the layout to be ready
this.resetDailyGoals();
this.updateStatusBar(this.statusbarGamification);
this.actualizeProfileLeave();
//this.actualizeProfileLeave();
this.mediator.updateProfileLeaf();
} catch (error) {
console.error('Error during post-layout initialization:', error);
}
Expand Down Expand Up @@ -189,7 +190,7 @@ export default class gamification extends Plugin {
return true; // Successfully updated
}

private async actualizeProfileLeave(){
async actualizeProfileLeave(){
const newPoints = this.mediator.getSettingNumber('statusPoints')
const level = getLevelForPoints(newPoints);
this.profileLeafUpdateLevel(this.mediator.getSettingNumber('statusLevel'),this.mediator.getSettingNumber('statusPoints'),this.mediator.getSettingNumber('xpForNextLevel'),level.points,level.pointsNext)
Expand All @@ -200,6 +201,7 @@ export default class gamification extends Plugin {
this.profileLeafUpdateuUdateMajurityList()
}


private registerCommands(){

this.addRibbonIcon("target", "gamification side overview", () => {
Expand All @@ -217,7 +219,7 @@ export default class gamification extends Plugin {

if (this.mediator.getSettingBoolean('debug')){
this.addRibbonIcon("accessibility", "Crafting", async () => {

console.log('Debug Help Funktion accessibility is called')
//this.mediator.acquireIngredients(1,400,500);
//this.resetDailyGoals();
//this.mediator.setSettingString('weeklyNoteCreationDate', window.moment().subtract(1, 'day').format('DD.MM.YYYY'))
Expand Down Expand Up @@ -276,6 +278,10 @@ export default class gamification extends Plugin {

//this.setBadgeSave(getBadgeDetails('Brainiac Trailblazer'),'23-09-07', 'level 20');
//this.setBadgeSave(getBadgeDetails('Savvy Scholar'), '23-08-15', 'level 15');

//this.mediator.updateProfileLeaf();
this.actualizeProfileLeave();

});

this.addRibbonIcon("chevrons-right", "boost", async () => {
Expand Down Expand Up @@ -534,6 +540,28 @@ export default class gamification extends Plugin {
}
}

async profileLeafUpdatePicture() {
const { workspace } = this.app;
let leaf = null;
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_GAMIFICATION_PROFILE);

if (leaves.length > 0) {
leaf = leaves[0];
} else {
leaf = workspace.getRightLeaf(false);
// @ts-ignore
await leaf.setViewState({ type: VIEW_TYPE_GAMIFICATION_PROFILE, active: true });
}

// @ts-ignore
const view = leaf.view;
if (view instanceof GamifiedPkmProfileView) {
view.updateProfilePicture()
}else {
console.log('gamified-pkm-profile is not loaded yet.');
}
}

async profileLeafUpdatePoints(newPoints:number, nextLevel: number) {
const { workspace } = this.app;
let leaf = null;
Expand Down Expand Up @@ -1777,10 +1805,6 @@ export default class gamification extends Plugin {
}






async writeBadgeCSV(newBadge: Badge, date: string, level: string){
// check first if badge is already in
const badgeDict = parseBadgeCSV2Dict(this.mediator.getSettingString('receivedBadges'));
Expand Down
21 changes: 2 additions & 19 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ export interface ISettings extends DynamicSettings{


export class GamificationPluginSettings extends PluginSettingTab {
//private readonly plugin: gamification;
private readonly plugin: gamification;
private readonly mediator: GamificationMediator;
public settings: ISettings;
Expand Down Expand Up @@ -296,24 +295,7 @@ export class GamificationPluginSettings extends PluginSettingTab {
constructor(app: App, plugin: gamification, mediator: GamificationMediator) {
super(app, plugin as any);
this.mediator = mediator;

// let settings = Object.assign({}, defaultSettings);

/*
for (const key in settings) {
if (settings.hasOwnProperty(key)) {
if(typeof key === 'number'){
settings[key] = encryptNumber(settings[key] as number)
} else if (typeof key === 'string'){
settings[key] = encryptString(settings[key] as string)
} else if (typeof key === 'boolean'){
settings[key] = encryptBoolean(settings[key] as boolean)
}
}
}
*/
// Save `settings` with encrypted values to your storage

this.plugin = plugin
}

public display(): void {
Expand Down Expand Up @@ -375,6 +357,7 @@ export class GamificationPluginSettings extends PluginSettingTab {
.onChange(async (value) => {
this.mediator.setSettingString('avatarPicture', value);
await this.mediator.saveSettings();
this.mediator.updateProfileLeafPic();
}),
);

Expand Down
17 changes: 17 additions & 0 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,22 @@ export class GamifiedPkmProfileView extends ItemView {
}
}

updateProfilePicture() {
const container = this.containerEl.children[1];
const profileContainer = container.querySelector('.avatar-profile');

if (profileContainer) {
const imagePath = this.mediator.getSettingString('avatarPicture'); // Fetch the updated setting
const avatarImage = profileContainer.querySelector('.avatar-image') as HTMLImageElement;

if (avatarImage) {
avatarImage.src = this.app.vault.adapter.getResourcePath(imagePath);
} else {
// Create a new image element if it doesn't exist
const newAvatarImage = profileContainer.createEl('img', { cls: 'avatar-image' });
newAvatarImage.src = this.app.vault.adapter.getResourcePath(imagePath);
}
}
}

}

0 comments on commit 5dfe25a

Please sign in to comment.