Skip to content

Commit 5b349fc

Browse files
authored
Merge pull request #4693 from crazyserver/MOBILE-4948
Mobile 4948 translatable pipes
2 parents 58f7d40 + 8d85397 commit 5b349fc

File tree

7 files changed

+234
-35
lines changed

7 files changed

+234
-35
lines changed

src/core/pipes/bytes-to-size.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,63 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { Pipe, PipeTransform } from '@angular/core';
15+
import { Pipe, PipeTransform, OnDestroy } from '@angular/core';
16+
import { Translate } from '@singletons';
1617

1718
import { CoreLogger } from '@static/logger';
1819
import { CoreText } from '@static/text';
20+
import { Subscription } from 'rxjs';
1921

2022
/**
2123
* Pipe to turn a number in bytes to a human readable size (e.g. 5,25 MB).
2224
*/
2325
@Pipe({
2426
name: 'coreBytesToSize',
27+
pure: false,
2528
})
26-
export class CoreBytesToSizePipe implements PipeTransform {
29+
export class CoreBytesToSizePipe implements PipeTransform, OnDestroy {
2730

2831
protected logger: CoreLogger;
32+
protected cachedResult?: string;
33+
protected subscription: Subscription;
34+
35+
protected lastValue?: number | string;
2936

3037
constructor() {
3138
this.logger = CoreLogger.getInstance('CoreBytesToSizePipe');
39+
40+
this.subscription = Translate.onLangChange.subscribe(() => {
41+
this.cachedResult = undefined;
42+
});
3243
}
3344

3445
/**
35-
* Takes a number and turns it to a human readable size.
46+
* Pipes a number into a human readable size.
3647
*
3748
* @param value The bytes to convert.
3849
* @returns Readable bytes.
3950
*/
4051
transform(value: number | string): string {
41-
if (typeof value == 'string') {
52+
if (this.lastValue !== value) {
53+
this.lastValue = value;
54+
this.cachedResult = undefined;
55+
}
56+
57+
if (this.cachedResult === undefined) {
58+
this.cachedResult = this.formatBytes(value);
59+
}
60+
61+
return this.cachedResult;
62+
}
63+
64+
/**
65+
* Takes a number and turns it to a human readable size.
66+
*
67+
* @param value The bytes to convert.
68+
* @returns Readable bytes.
69+
*/
70+
protected formatBytes(value: number | string): string {
71+
if (typeof value === 'string') {
4272
// Convert the value to a number.
4373
const numberValue = parseInt(value, 10);
4474
if (isNaN(numberValue)) {
@@ -52,4 +82,11 @@ export class CoreBytesToSizePipe implements PipeTransform {
5282
return CoreText.bytesToSize(value);
5383
}
5484

85+
/**
86+
* @inheritdoc
87+
*/
88+
ngOnDestroy(): void {
89+
this.subscription.unsubscribe();
90+
}
91+
5592
}

src/core/pipes/date-day-or-time.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { Pipe, PipeTransform } from '@angular/core';
15+
import { Pipe, PipeTransform, OnDestroy } from '@angular/core';
1616
import { dayjs } from '@/core/utils/dayjs';
1717

1818
import { CoreTime } from '@static/time';
1919
import { Translate } from '@singletons';
2020
import { CoreLogger } from '@static/logger';
21+
import { Subscription } from 'rxjs';
2122

2223
/**
2324
* Filter to display a date using the day, or the time.
@@ -34,22 +35,50 @@ import { CoreLogger } from '@static/logger';
3435
*/
3536
@Pipe({
3637
name: 'coreDateDayOrTime',
38+
pure: false,
3739
})
38-
export class CoreDateDayOrTimePipe implements PipeTransform {
40+
export class CoreDateDayOrTimePipe implements PipeTransform, OnDestroy {
3941

4042
protected logger: CoreLogger;
43+
protected cachedResult?: string;
44+
protected subscription: Subscription;
45+
46+
protected lastTimestamp?: number | string;
4147

4248
constructor() {
4349
this.logger = CoreLogger.getInstance('CoreDateDayOrTimePipe');
50+
51+
this.subscription = Translate.onLangChange.subscribe(() => {
52+
this.cachedResult = undefined;
53+
});
4454
}
4555

4656
/**
47-
* Format a timestamp.
57+
* Pipes a timestamp into a formatted time or date.
4858
*
4959
* @param timestamp The UNIX timestamp (without milliseconds).
5060
* @returns Formatted time.
5161
*/
5262
transform(timestamp: string | number): string {
63+
if (this.lastTimestamp !== timestamp) {
64+
this.lastTimestamp = timestamp;
65+
this.cachedResult = undefined;
66+
}
67+
68+
if (this.cachedResult === undefined) {
69+
this.cachedResult = this.formatTimestamp(timestamp);
70+
}
71+
72+
return this.cachedResult;
73+
}
74+
75+
/**
76+
* Format a timestamp.
77+
*
78+
* @param timestamp The UNIX timestamp (without milliseconds).
79+
* @returns Formatted time.
80+
*/
81+
protected formatTimestamp(timestamp: string | number): string {
5382
if (typeof timestamp === 'string') {
5483
// Convert the value to a number.
5584
const numberTimestamp = parseInt(timestamp, 10);
@@ -69,4 +98,11 @@ export class CoreDateDayOrTimePipe implements PipeTransform {
6998
});
7099
}
71100

101+
/**
102+
* @inheritdoc
103+
*/
104+
ngOnDestroy(): void {
105+
this.subscription.unsubscribe();
106+
}
107+
72108
}

src/core/pipes/duration.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,85 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { Pipe, PipeTransform } from '@angular/core';
15+
import { Pipe, PipeTransform, OnDestroy } from '@angular/core';
16+
import { Translate } from '@singletons';
1617
import { CoreLogger } from '@static/logger';
1718
import { CoreTime } from '@static/time';
19+
import { Subscription } from 'rxjs';
1820

1921
/**
2022
* Filter to turn a number of seconds to a duration. E.g. 60 -> 1 minute.
2123
*/
2224
@Pipe({
2325
name: 'coreDuration',
26+
pure: false,
2427
})
25-
export class CoreDurationPipe implements PipeTransform {
28+
export class CoreDurationPipe implements PipeTransform, OnDestroy {
2629

2730
protected logger: CoreLogger;
31+
protected cachedResult?: string;
32+
protected subscription: Subscription;
33+
34+
protected lastSeconds?: number | string;
35+
protected lastPrecision?: number;
2836

2937
constructor() {
30-
this.logger = CoreLogger.getInstance('CoreBytesToSizePipe');
38+
this.logger = CoreLogger.getInstance('CoreDurationPipe');
39+
40+
this.subscription = Translate.onLangChange.subscribe(() => {
41+
this.cachedResult = undefined;
42+
});
3143
}
3244

3345
/**
3446
* Turn a number of seconds to a duration. E.g. 60 -> 1 minute.
3547
*
3648
* @param seconds The number of seconds.
49+
* @param precision Number of elements to have in precision.
3750
* @returns Formatted duration.
3851
*/
39-
transform(seconds: string | number): string {
40-
if (typeof seconds == 'string') {
52+
transform(seconds: string | number, precision = 2): string {
53+
if (this.lastSeconds !== seconds || this.lastPrecision !== precision) {
54+
this.lastSeconds = seconds;
55+
this.lastPrecision = precision;
56+
this.cachedResult = undefined;
57+
}
58+
59+
if (this.cachedResult === undefined) {
60+
this.cachedResult = this.formatTime(seconds, precision);
61+
}
62+
63+
return this.cachedResult;
64+
}
65+
66+
/**
67+
* Format duration given the number of seconds.
68+
*
69+
* @param seconds Number of seconds.
70+
* @param precision Number of elements to have in precision.
71+
* @returns Formatted time.
72+
*/
73+
protected formatTime(seconds: string | number, precision: number): string {
74+
if (typeof seconds === 'string') {
4175
// Convert the value to a number.
4276
const numberSeconds = parseInt(seconds, 10);
4377
if (isNaN(numberSeconds)) {
4478
this.logger.error('Invalid value received', seconds);
4579

4680
return seconds;
4781
}
82+
4883
seconds = numberSeconds;
4984
}
5085

51-
return CoreTime.formatTime(seconds);
86+
return CoreTime.formatTime(seconds, precision);
87+
}
88+
89+
/**
90+
* @inheritdoc
91+
*/
92+
ngOnDestroy(): void {
93+
this.subscription.unsubscribe();
5294
}
5395

5496
}

src/core/pipes/format-date.ts

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,71 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { Pipe, PipeTransform } from '@angular/core';
15+
import { OnDestroy, Pipe, PipeTransform } from '@angular/core';
1616
import { CoreTime } from '@static/time';
1717
import { CoreLogger } from '@static/logger';
18+
import { Translate } from '@singletons';
19+
import { Subscription } from 'rxjs';
1820

1921
/**
2022
* Filter to format a date.
2123
*/
2224
@Pipe({
2325
name: 'coreFormatDate',
26+
pure: false,
2427
})
25-
export class CoreFormatDatePipe implements PipeTransform {
28+
export class CoreFormatDatePipe implements PipeTransform, OnDestroy {
2629

2730
protected logger: CoreLogger;
31+
protected cachedResult?: string;
32+
protected subscription: Subscription;
33+
34+
protected lastFormat?: string;
35+
protected lastTimestamp?: string | number;
36+
protected lastConvert?: boolean;
2837

2938
constructor() {
3039
this.logger = CoreLogger.getInstance('CoreFormatDatePipe');
40+
41+
this.subscription = Translate.onLangChange.subscribe(() => {
42+
this.cachedResult = undefined;
43+
});
3144
}
3245

3346
/**
34-
* Format a date.
47+
* Pipes a timestamp into a formatted date.
3548
*
3649
* @param timestamp Timestamp to format (in milliseconds). If not defined, use current time.
3750
* @param format Format to use. It should be a string code to handle i18n (e.g. core.strftimetime).
3851
* Defaults to strftimedaydatetime.
3952
* @param convert If true, convert the format from PHP to DayJS. Set it to false for DayJS formats.
4053
* @returns Formatted date.
4154
*/
42-
transform(timestamp: string | number, format?: string, convert?: boolean): string {
55+
transform(timestamp: string | number, format = 'strftimedaydatetime', convert?: boolean): string {
56+
if (this.lastTimestamp !== timestamp || this.lastFormat !== format || this.lastConvert !== convert) {
57+
this.lastTimestamp = timestamp;
58+
this.lastFormat = format;
59+
this.lastConvert = convert;
60+
this.cachedResult = undefined;
61+
}
62+
63+
if (this.cachedResult === undefined) {
64+
this.cachedResult = this.formatDate(timestamp, format, convert);
65+
}
66+
67+
return this.cachedResult;
68+
}
69+
70+
/**
71+
* Format a date.
72+
*
73+
* @param timestamp Timestamp to format (in milliseconds). If not defined, use current time.
74+
* @param format Format to use. It should be a string code to handle i18n (e.g. core.strftimetime).
75+
* @param convert If true, convert the format from PHP to DayJS. Set it to false for DayJS formats.
76+
* @returns Formatted date.
77+
*/
78+
protected formatDate(timestamp: string | number, format: string, convert?: boolean): string {
4379
timestamp = timestamp || Date.now();
44-
format = format || 'strftimedaydatetime';
4580

4681
if (typeof timestamp === 'string') {
4782
// Convert the value to a number.
@@ -55,16 +90,23 @@ export class CoreFormatDatePipe implements PipeTransform {
5590
}
5691

5792
// Add "core." if needed.
58-
if (format.indexOf('strf') === 0 || format.indexOf('df') === 0) {
93+
if (format.startsWith('strf') || format.startsWith('df')) {
5994
format = `core.${format}`;
6095
}
6196

6297
if (convert === undefined) {
6398
// Initialize convert param. Set it to false if it's a core.df format, set it to true otherwise.
64-
convert = format.indexOf('core.df') != 0;
99+
convert = !format.startsWith('core.df');
65100
}
66101

67102
return CoreTime.userDate(timestamp, format, convert);
68103
}
69104

105+
/**
106+
* @inheritdoc
107+
*/
108+
ngOnDestroy(): void {
109+
this.subscription.unsubscribe();
110+
}
111+
70112
}

src/core/pipes/seconds-to-hms.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class CoreSecondsToHMSPipe implements PipeTransform {
3838
* Convert a number of seconds to Hours:Minutes:Seconds.
3939
*
4040
* @param seconds Number of seconds.
41+
* @param showHours Whether to always show hours (even if 0).
4142
* @returns Formatted seconds.
4243
*/
4344
transform(seconds: string | number, showHours = true): string {

0 commit comments

Comments
 (0)