Skip to content

Commit

Permalink
Merge pull request #34 from Heatmanofurioso/feature/addDisableSrcCach…
Browse files Browse the repository at this point in the history
…eSupport

Feature/add disable src cache support
  • Loading branch information
Heatmanofurioso authored Jan 24, 2022
2 parents de5d35a + 025402e commit b4eae7c
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# [1.3.0] - 2022-01-24
- Added ability to disable src cache
- Added missing postinstall hook for Husky in package.json

# [1.2.1] - 2022-01-24
- Add Husky Git Hooks
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"start": "ng serve",
"build": "ng build --configuration production",
"postbuild": "node scripts/copy-artifacts.js",
"postinstall": "ngcc && husky install",
"deploy": "cd dist/ngx-avatars && npm publish",
"test": "ng test",
"lint": "ng lint",
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-avatars/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ngx-avatars",
"description": "A universal avatar component for Angular applications that fetches / generates avatar based on the information you have about the user.",
"version": "1.2.1",
"version": "1.3.0",
"keywords": [
"angular",
"avatar",
Expand Down
25 changes: 24 additions & 1 deletion projects/ngx-avatars/src/lib/avatar-config.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AvatarConfigService } from './avatar-config.service';
import { AvatarSource } from './sources/avatar-source.enum';
import { AvatarConfig } from './avatar-config';
import { defaultSources, defaultColors } from './avatar.service';
import { defaultSources, defaultColors, defaultDisableSrcCache } from './avatar.service';

describe('AvatarConfigService', () => {
describe('AvatarSources', () => {
Expand Down Expand Up @@ -105,3 +105,26 @@ describe('AvatarConfigService', () => {
});
});
});

describe('AvatarDisableCache', () => {
it('should return the user\'s disable custom source cache settings when provided in the avatar configuration', () => {
const userDisableSrcCache = true;
const userConfig: AvatarConfig = {
disableSrcCache: userDisableSrcCache
};

const avatarConfigService = new AvatarConfigService(userConfig);

expect(avatarConfigService.getDisableSrcCache(defaultDisableSrcCache)).toBe(
userDisableSrcCache
);
});

it('should return the default disable custom source cache settings when no settings are provided in the avatar configuration', () => {
const avatarConfigService = new AvatarConfigService({});

expect(avatarConfigService.getDisableSrcCache(defaultDisableSrcCache)).toBe(
defaultDisableSrcCache
);
});
});
10 changes: 9 additions & 1 deletion projects/ngx-avatars/src/lib/avatar-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AvatarSource } from './sources/avatar-source.enum';
import { AVATAR_CONFIG } from './avatar-config.token';
import { AvatarConfig } from './avatar-config';

@Injectable()
@Injectable({providedIn: 'root'})
export class AvatarConfigService {
constructor(
@Optional()
Expand Down Expand Up @@ -39,4 +39,12 @@ export class AvatarConfigService {
defaultColors
);
}

public getDisableSrcCache(defaultDisableSrcCache: boolean): boolean {
if (this.userConfig == null || this.userConfig.disableSrcCache == null) {
return defaultDisableSrcCache;
} else {
return this.userConfig.disableSrcCache;
}
}
}
5 changes: 5 additions & 0 deletions projects/ngx-avatars/src/lib/avatar-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ export interface AvatarConfig {
* The order in which the avatar sources will be used.
*/
sourcePriorityOrder?: AvatarSource[];

/**
* Disable custom source (for custom images) cache.
*/
disableSrcCache?: boolean;
}
7 changes: 6 additions & 1 deletion projects/ngx-avatars/src/lib/avatar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ export const defaultColors = [
'#7f8c8d'
];

/**
* Default disable custom source cache settings
*/
export const defaultDisableSrcCache = false;

/**
* Provides utilities methods related to Avatar component
*/
@Injectable()
@Injectable({providedIn: 'root'})
export class AvatarService {
public avatarSources: AvatarSource[] = defaultSources;
public avatarColors: string[] = defaultColors;
Expand Down
18 changes: 18 additions & 0 deletions projects/ngx-avatars/src/lib/sources/custom-no-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {Source} from './source';
import {AvatarSource} from './avatar-source.enum';

/**
* Custom source implementation (with no cache).
* return custom image as an avatar
*
*/
export class CustomNoCache implements Source {
readonly sourceType: AvatarSource = AvatarSource.CUSTOM;

constructor(public sourceId: string) {}

public getAvatar(): string {
const urlSuffix = Math.random();
return `${this.sourceId}${this.sourceId.indexOf('?') > -1 ? '&' : '?'}_=${urlSuffix}`;
}
}
10 changes: 7 additions & 3 deletions projects/ngx-avatars/src/lib/sources/source.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,28 @@ import { Vkontakte } from './vkontakte';
import { Github } from './github';
import { SourceCreator } from './source.creator';
import { AvatarSource } from './avatar-source.enum';
import { AvatarConfigService } from '../avatar-config.service';
import { defaultDisableSrcCache } from '../avatar.service';
import { CustomNoCache } from './custom-no-cache';

/**
* Factory class that implements factory method pattern.
* Used to create Source implementation class based
* on the source Type
*/
@Injectable()
@Injectable({providedIn: 'root'})
export class SourceFactory {
private sources: { [key: string]: SourceCreator } = {};

constructor() {
constructor(avatarConfigService: AvatarConfigService) {
const disableSrcCache = avatarConfigService.getDisableSrcCache(defaultDisableSrcCache);
this.sources[AvatarSource.FACEBOOK] = Facebook;
this.sources[AvatarSource.TWITTER] = Twitter;
this.sources[AvatarSource.GOOGLE] = Google;
this.sources[AvatarSource.INSTAGRAM] = Instagram;
this.sources[AvatarSource.SKYPE] = Skype;
this.sources[AvatarSource.GRAVATAR] = Gravatar;
this.sources[AvatarSource.CUSTOM] = Custom;
this.sources[AvatarSource.CUSTOM] = disableSrcCache ? CustomNoCache : Custom;
this.sources[AvatarSource.INITIALS] = Initials;
this.sources[AvatarSource.VALUE] = Value;
this.sources[AvatarSource.VKONTAKTE] = Vkontakte;
Expand Down

0 comments on commit b4eae7c

Please sign in to comment.