-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAppVersionService.ts
More file actions
104 lines (92 loc) · 2.78 KB
/
Copy pathAppVersionService.ts
File metadata and controls
104 lines (92 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* AppVersionService - Provides app version information from React Native app config
*
* Uses react-native-device-info to get the actual app version instead of hardcoding
*/
import DeviceInfo from 'react-native-device-info';
export class AppVersionService {
private static instance: AppVersionService;
private cachedVersion: string | null = null;
private cachedBuildNumber: string | null = null;
private constructor() {}
public static getInstance(): AppVersionService {
if (!AppVersionService.instance) {
AppVersionService.instance = new AppVersionService();
}
return AppVersionService.instance;
}
/**
* Get the app version (e.g., "1.0.0")
* Caches the result for performance
*/
public async getVersion(): Promise<string> {
if (this.cachedVersion) {
return this.cachedVersion;
}
try {
this.cachedVersion = DeviceInfo.getVersion();
console.log('AppVersionService: App version:', this.cachedVersion);
return this.cachedVersion;
} catch (error) {
console.error('AppVersionService: Error getting app version:', error);
throw new Error(
`Failed to get app version: ${
error instanceof Error ? error.message : 'Unknown error'
}`,
);
}
}
/**
* Get the build number (e.g., "123")
* Caches the result for performance
*/
public async getBuildNumber(): Promise<string> {
if (this.cachedBuildNumber) {
return this.cachedBuildNumber;
}
try {
this.cachedBuildNumber = DeviceInfo.getBuildNumber();
console.log('AppVersionService: Build number:', this.cachedBuildNumber);
return this.cachedBuildNumber;
} catch (error) {
console.error('AppVersionService: Error getting build number:', error);
throw new Error(
`Failed to get build number: ${
error instanceof Error ? error.message : 'Unknown error'
}`,
);
}
}
/**
* Get the full version string (version + build number)
* Format: "1.0.0 (123)"
*/
public async getFullVersion(): Promise<string> {
try {
const version = await this.getVersion();
const buildNumber = await this.getBuildNumber();
if (buildNumber === '1') {
return version;
}
return `${version} (build: ${buildNumber})`;
} catch (error) {
console.error('AppVersionService: Error getting full version:', error);
throw error;
}
}
/**
* Reset cached versions (useful for testing)
*/
public resetCache(): void {
this.cachedVersion = null;
this.cachedBuildNumber = null;
}
/**
* Check if version is cached
*/
public isCached(): boolean {
return this.cachedVersion !== null;
}
}
// Export singleton instance for convenience
export const appVersionService = AppVersionService.getInstance();