# M Features - Core

`import { ... } from '@metromobilite/m-features/core'`

## Geolocation

Provide a simple wrapper to the nativ API.

Also provide a guard to prevent page loading if the user geolocation is required.

## Helpers

Helpers are services.

| Name           | description                                                                                      |
| -------------- | ------------------------------------------------------------------------------------------------ |
| `ArrayHelper`  | Provides utility methods (sort, groupBy) to work on arrays.                                      |
| `DateHelper`   | Provides utility methods (isAfter/Before, addDays, etc...) to work with native date objects.     |
| `IconsHelper`  | Responsible for loading custom icons.                                                            |
| `StringHelper` | Provides utility methods to clean strings.                                                       |
| `ObjectHelper` | Provides utility methods (get a property from a string path) to work with object.                |
| `StyleHelper`  | Allow TS code to access to CSS custom properties.                                                |
| `TypesHelper`  | Provides utility methods (displayLinesNear, idField, etc...) to work with some functional types. |


## Interceptors

The core module define 2 interceptors to work with the domain/api urls.

First of all, the domain. Each service provided by the m-features package uses the `@domain` placeholder,
they don't know anything about the actual domain being used.

The domain can be seen as the environment. Sometimes, mainly in the local environment, the domain alone is not enought.
So this module provides a second interceptor to handle the basic part of the API.

Exemple

```typescript
http.get('@domain/@api/<AN>/<API>/<ENDPOINT>').subscribe(/*...*/);
```

## Interval

This package provide an interval builder service, useful to create interval (using rxjs) that take into account visibilitychange  API.

```typescript
// Start a 60s interval.
const interval = intervalBuilderService.build(60000);
interval.pause();
interval.resume();
interval.onTick$.subscribe(_ => /*...*/ );
// Make sure to call the destroy method to complete the interval correctly.
interval.destroy();
```

Exemple of interval that handle http request.
```typescript
// We creates a 2s interval to call a http requests.
const interval = this.intervalBuilder.build(2000);
interval
	// Note the 'on' method usage instead of 'onTick$'.
	// The 'on' method allow us to listen to the end of each http request.
	// Subscribers are automatically unsubscribed when the interval.destroy method is called.
	.on( of(Math.random()).pipe(delay(1000)) )
	// The subscribe callback is called on each end of http requests.
	.subscribe(val => {
	});
// Destroy the interval 10s after.
setTimeout(() => {
	interval.destroy();
}, 10000);
```

## Clusters

Provide a service/model and resolver to work with the `/routers/default/index/routes/<LINE_ID>/clusters` API endpoint.

## Lines

Provide a service/model/resolver to work with the `/routers/default/index/routes` API endpoint.

## Visibility

The visibility service allow developer to take into account app background/foreground events.
Because the event that the app is coming back to the foreground doesn't work all the time, a mechanism is set up and hidden by this service.

## Module Configuration

| token                       | default   | description                                                                               |
| --------------------------- | --------- | ----------------------------------------------------------------------------------------- |
| AVAILABLE_LINE_TYPES        | All types | A list of line types used in the application.                                             |
| VISIBILITY_BACKGROUND_TIMER | 900       | The background timer use by the visibility service to ensure the visibility change event. |
| ICONS_MAPPING               |           | An object that maps icon types with asset url.                                            |
| GEOLOCATION_CONFIG          |           | The geolocation configuration (See native options).                                       |

## Front configuration

You can get the data types and map filters configuration from the ws.
You can use resolvers (`DataTypesResolver` and `MapFiltersResolver` from this module),
or use the `this.configService.getData(FRONT_CONFIG.*)` to do that.

### How to

In your ngModule:

```typescript
{
	providers: [
		// The app need SEM only.
		{ provide: AVAILABLE_LINE_TYPES, useValue: ['SEM'] },

		// Set the background timer to 500ms instead of 900.
		{ provide: VISIBILITY_BACKGROUND_TIMER, useValue: 500 },

		// Override default icons mapping.
		{ provide: ICONS_MAPPING, useValue: myCustomIconsMapping },

	]
}
```

## User settings

### Theme

If you use the UserSettingsService service. the initial theme (if no localstorage theme key found) is set from the body classes.

```html
<body class="dark-theme">
	<!-- ... -->
</body>
```

## Pipes

| Name          | Description                                     |
| ------------- | ----------------------------------------------- |
| commune       | Get the "commune" label from the POI data.      |
| distance      | Format a distance number.                       |
| duration      | Format a duration number.                       |
| extractCity   | Get the city property from a string.            |
| extractLineId | Get the line id from a string.                  |
| id            | Get the POI id from the properties object.      |
| libelle       | Get the POI libelle from the properties object. |
| lineColor     | Get the line color using the line id.           |
| line          | Get the line object from the line id.           |
| mode          | Get the line mode property using the line id.   |
| time          | Get the time value from an object.              |
| timeUnit      | Get the time unit from an object.               |
| timeSchedule  | Get the time schedule from an object.           |



## Maps

Allow user to get the maps (TAG and more network).

## Status bar

A basic service to handle the Android status bar color and udpate it dynamically.
