Skip to content

Commit

Permalink
refactor: use the app config (#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecarn authored Feb 3, 2025
1 parent 542c6c6 commit 21f30d3
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 89 deletions.
89 changes: 89 additions & 0 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { DOCUMENT } from '@angular/common';
import { provideHttpClient } from '@angular/common/http';
import { ApplicationConfig } from '@angular/core';
import { APP_INITIALIZER, Provider, importProvidersFrom } from '@angular/core';
import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';
import {
MAT_TOOLTIP_DEFAULT_OPTIONS,
MatTooltipDefaultOptions
} from '@angular/material/tooltip';
import { BrowserModule } from '@angular/platform-browser';
import { provideAnimations } from '@angular/platform-browser/animations';
import { provideRouter } from '@angular/router';
import { ServiceWorkerModule } from '@angular/service-worker';

import { provideAuthentification } from '@igo2/auth';
import { withMicrosoftSupport } from '@igo2/auth/microsoft';
import { provideIcon } from '@igo2/common/icon';
import { IgoCoreModule } from '@igo2/core';
import { ConfigService, provideConfig } from '@igo2/core/config';
import { provideTranslation, withAsyncConfig } from '@igo2/core/language';
import { IgoMessageModule } from '@igo2/core/message';
import { RouteService } from '@igo2/core/route';
import { provideOffline } from '@igo2/geo';
import { loadTheme } from '@igo2/utils';

import { first } from 'rxjs';
import { environment } from 'src/environments';

import { PortalModule } from './pages';

const DEFAULT_THEME = 'blue-theme';

const TOOLTIP_OPTIONS: MatTooltipDefaultOptions = {
showDelay: 500,
hideDelay: 0,
touchendHideDelay: 0,
disableTooltipInteractivity: true
};

export const appConfig: ApplicationConfig = {
providers: [
importProvidersFrom(
BrowserModule,
IgoCoreModule,
IgoMessageModule,
PortalModule,
ServiceWorkerModule.register('ngsw-worker.js', {
enabled: environment.igo.app.pwa.enabled,
registrationStrategy: 'registerWithDelay:5000'
})
),
provideHttpClient(),
provideAnimations(),
provideRouter([]),
provideConfig({
default: environment.igo,
path: './config/config.json'
}),
provideTranslation(withAsyncConfig()),
provideAuthentification(
withMicrosoftSupport('add'),
withMicrosoftSupport('b2c')
),
provideOffline(environment.igo.app.offline),
provideIcon(),
provideTheme(),
RouteService,
{ provide: MAT_TOOLTIP_DEFAULT_OPTIONS, useValue: TOOLTIP_OPTIONS },
{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
useValue: { appearance: 'fill' }
}
]
};

function provideTheme(): Provider {
return {
provide: APP_INITIALIZER,
useFactory: (configService: ConfigService, document: Document) => () =>
configService.isLoaded$
.pipe(first((isLoaded) => isLoaded))
.subscribe(() => {
const theme = configService.getConfig('theme', DEFAULT_THEME);
loadTheme(document, theme);
}),
deps: [ConfigService, DOCUMENT],
multi: true
};
}
93 changes: 4 additions & 89 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,14 @@
import { DOCUMENT } from '@angular/common';
import { provideHttpClient } from '@angular/common/http';
import {
APP_INITIALIZER,
Provider,
enableProdMode,
importProvidersFrom
} from '@angular/core';
import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';
import {
MAT_TOOLTIP_DEFAULT_OPTIONS,
MatTooltipDefaultOptions
} from '@angular/material/tooltip';
import { BrowserModule, bootstrapApplication } from '@angular/platform-browser';
import { provideAnimations } from '@angular/platform-browser/animations';
import { provideRouter } from '@angular/router';
import { ServiceWorkerModule } from '@angular/service-worker';

import { provideAuthentification } from '@igo2/auth';
import { withMicrosoftSupport } from '@igo2/auth/microsoft';
import { provideIcon } from '@igo2/common/icon';
import { IgoCoreModule } from '@igo2/core';
import { ConfigService, provideConfig } from '@igo2/core/config';
import { provideTranslation, withAsyncConfig } from '@igo2/core/language';
import { IgoMessageModule } from '@igo2/core/message';
import { RouteService } from '@igo2/core/route';
import { provideOffline } from '@igo2/geo';
import { loadTheme } from '@igo2/utils';
import { enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';

import 'hammerjs';
import { first } from 'rxjs';

import { AppComponent } from './app/app.component';
import { PortalModule } from './app/pages';
import { appConfig } from './app/app.config';
import { environment } from './environments/environment';

const DEFAULT_THEME = 'blue-theme';

const TOOLTIP_OPTIONS: MatTooltipDefaultOptions = {
showDelay: 500,
hideDelay: 0,
touchendHideDelay: 0,
disableTooltipInteractivity: true
};

if (environment.production) {
enableProdMode();
}

bootstrapApplication(AppComponent, {
providers: [
importProvidersFrom(
BrowserModule,
IgoCoreModule,
IgoMessageModule,
PortalModule,
ServiceWorkerModule.register('ngsw-worker.js', {
enabled: environment.igo.app.pwa.enabled,
registrationStrategy: 'registerWithDelay:5000'
})
),
provideHttpClient(),
provideAnimations(),
provideRouter([]),
provideConfig({
default: environment.igo,
path: './config/config.json'
}),
provideTranslation(withAsyncConfig()),
provideAuthentification(
withMicrosoftSupport('add'),
withMicrosoftSupport('b2c')
),
provideOffline(environment.igo.app.offline),
provideIcon(),
provideTheme(),
RouteService,
{ provide: MAT_TOOLTIP_DEFAULT_OPTIONS, useValue: TOOLTIP_OPTIONS },
{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
useValue: { appearance: 'fill' }
}
]
}).catch((err) => console.log(err));

function provideTheme(): Provider {
return {
provide: APP_INITIALIZER,
useFactory: (configService: ConfigService, document: Document) => () =>
configService.isLoaded$
.pipe(first((isLoaded) => isLoaded))
.subscribe(() => {
const theme = configService.getConfig('theme', DEFAULT_THEME);
loadTheme(document, theme);
}),
deps: [ConfigService, DOCUMENT],
multi: true
};
}
bootstrapApplication(AppComponent, appConfig).catch((err) => console.log(err));

0 comments on commit 21f30d3

Please sign in to comment.