Skip to content

Commit 0ae9274

Browse files
committed
Refactor HybridVersionCheck for improved error handling and timeout settings; update App.tsx to clarify error messages. Adjust index.ts to ensure consistent function calls for country, store URL, and latest version retrieval.
1 parent 8c30bbe commit 0ae9274

4 files changed

Lines changed: 40 additions & 28 deletions

File tree

example/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default function App() {
1919
setStoreUrl(url);
2020
setLatestVersion(latest);
2121
} catch {
22-
setError("App not found in store");
22+
setError("App not found in store"); // this is not an error, it's just a fallback
2323
}
2424
setLoading(false);
2525
};

package/android/src/main/java/com/margelo/nitro/nitroversioncheck/HybridVersionCheck.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ package com.margelo.nitro.nitroversioncheck
22

33
import com.margelo.nitro.NitroModules
44
import com.margelo.nitro.core.Promise
5+
import java.net.HttpURLConnection
56
import java.net.URL
67

7-
8-
class HybridVersionCheck: HybridVersionCheckSpec(){
8+
class HybridVersionCheck : HybridVersionCheckSpec() {
99
companion object {
10+
private const val TIMEOUT_MS = 15_000
1011
private val context = NitroModules.applicationContext
1112
private val packageInfo = context?.packageManager?.getPackageInfo(context.packageName, 0)
12-
1313
}
14+
1415
public override val version = packageInfo?.versionName ?: "unknown"
1516

1617
public override val buildNumber = if (android.os.Build.VERSION.SDK_INT >= 28) {
17-
if (packageInfo?.longVersionCode != null) packageInfo.longVersionCode.toString() else "unknown"
18+
if (packageInfo?.longVersionCode != null) packageInfo.longVersionCode.toString() else "unknown"
1819
} else {
1920
@Suppress("DEPRECATION")
20-
if (packageInfo?.versionCode != null) packageInfo.versionCode.toString() else "unknown"
21+
if (packageInfo?.versionCode != null) packageInfo.versionCode.toString() else "unknown"
2122
}
2223

24+
public override val packageName = packageInfo?.packageName ?: "unknown"
2325

2426
override fun getCountry(): String {
25-
return java.util.Locale.getDefault().country ?: "unknown"
27+
return java.util.Locale.getDefault().country ?: "unknown"
2628
}
2729

28-
public override val packageName= packageInfo?.packageName ?: "unknown"
29-
3030
override fun getStoreUrl(): Promise<String> {
3131
return Promise.async {
3232
"https://play.google.com/store/apps/details?id=$packageName"
@@ -37,8 +37,12 @@ class HybridVersionCheck: HybridVersionCheckSpec(){
3737
return Promise.async {
3838
try {
3939
val url = URL("https://play.google.com/store/apps/details?id=$packageName&hl=en")
40-
val html = url.readText()
41-
val regex = Regex("""\[\[\["(\d+\.\d+[\.\d+]*)"\]\]""")
40+
val connection = url.openConnection() as HttpURLConnection
41+
connection.connectTimeout = TIMEOUT_MS
42+
connection.readTimeout = TIMEOUT_MS
43+
connection.setRequestProperty("User-Agent", "Mozilla/5.0")
44+
val html = connection.inputStream.bufferedReader().use { it.readText() }
45+
val regex = Regex("""\]\]\],\s*"(\d+\.\d+[\d.]*\d)"""")
4246
val match = regex.find(html)
4347
match?.groupValues?.get(1)
4448
?: throw Exception("Could not parse latest version from Play Store page")
@@ -58,4 +62,4 @@ class HybridVersionCheck: HybridVersionCheckSpec(){
5862
}
5963
}
6064
}
61-
}
65+
}

package/ios/HybridVersionCheck.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
import Foundation
22
import NitroModules
33

4-
54
class HybridVersionCheck: HybridVersionCheckSpec {
5+
private static let session: URLSession = {
6+
let config = URLSessionConfiguration.default
7+
config.timeoutIntervalForRequest = 15
8+
config.timeoutIntervalForResource = 30
9+
return URLSession(configuration: config)
10+
}()
11+
612
var version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown"
713
var buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "unknown"
814
var packageName = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String ?? "unknown"
9-
15+
1016
func getCountry() throws -> String {
11-
Locale.current.regionCode ?? "unknown"
17+
if #available(iOS 16, *) {
18+
return Locale.current.region?.identifier ?? "unknown"
19+
}
20+
return Locale.current.regionCode ?? "unknown"
1221
}
1322

1423
func getStoreUrl() throws -> Promise<String> {
1524
return Promise.async {
1625
let bundleId = Bundle.main.bundleIdentifier ?? ""
1726
let url = URL(string: "https://itunes.apple.com/lookup?bundleId=\(bundleId)")!
18-
let (data, _) = try await URLSession.shared.data(from: url)
19-
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
20-
let results = json?["results"] as? [[String: Any]]
21-
guard let trackViewUrl = results?.first?["trackViewUrl"] as? String else {
27+
let (data, _) = try await HybridVersionCheck.session.data(from: url)
28+
guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
29+
let results = json["results"] as? [[String: Any]],
30+
let trackViewUrl = results.first?["trackViewUrl"] as? String else {
2231
throw NSError(domain: "VersionCheck", code: 1, userInfo: [NSLocalizedDescriptionKey: "App not found on App Store"])
2332
}
2433
return trackViewUrl
@@ -29,11 +38,10 @@ class HybridVersionCheck: HybridVersionCheckSpec {
2938
return Promise.async {
3039
let bundleId = Bundle.main.bundleIdentifier ?? ""
3140
let url = URL(string: "https://itunes.apple.com/lookup?bundleId=\(bundleId)")!
32-
let (data, _) = try await URLSession.shared.data(from: url)
33-
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
34-
let results = json?["results"] as? [[String: Any]]
35-
36-
guard let latestVersion = results?.first?["version"] as? String else {
41+
let (data, _) = try await HybridVersionCheck.session.data(from: url)
42+
guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
43+
let results = json["results"] as? [[String: Any]],
44+
let latestVersion = results.first?["version"] as? String else {
3745
throw NSError(domain: "VersionCheck", code: 2, userInfo: [NSLocalizedDescriptionKey: "App not found on App Store"])
3846
}
3947
return latestVersion

package/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const packageName = VersionCheck.packageName;
5050
* getCountry() // "US"
5151
* ```
5252
*/
53-
export const getCountry = VersionCheck.getCountry;
53+
export const getCountry = () => VersionCheck.getCountry();
5454

5555
/**
5656
* Returns the store URL for this app.
@@ -63,7 +63,7 @@ export const getCountry = VersionCheck.getCountry;
6363
* Linking.openURL(url);
6464
* ```
6565
*/
66-
export const getStoreUrl = VersionCheck.getStoreUrl;
66+
export const getStoreUrl = () => VersionCheck.getStoreUrl();
6767

6868
/**
6969
* Fetches the latest version of this app available in the store.
@@ -73,7 +73,7 @@ export const getStoreUrl = VersionCheck.getStoreUrl;
7373
* const latest = await getLatestVersion(); // "1.3.0"
7474
* ```
7575
*/
76-
export const getLatestVersion = VersionCheck.getLatestVersion;
76+
export const getLatestVersion = () => VersionCheck.getLatestVersion();
7777

7878
/**
7979
* Checks whether an app update is available.
@@ -88,6 +88,6 @@ export const getLatestVersion = VersionCheck.getLatestVersion;
8888
* }
8989
* ```
9090
*/
91-
export const needsUpdate = VersionCheck.needsUpdate;
91+
export const needsUpdate = () => VersionCheck.needsUpdate();
9292

9393
export { VersionCheck };

0 commit comments

Comments
 (0)