@aguacerowx/react-native 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ // packages/com/aguacerowx/reactnative/WeatherFrameProcessorModule.java
2
+
1
3
  package com.aguacerowx.reactnative;
2
4
 
3
5
  import androidx.annotation.NonNull;
@@ -10,6 +12,7 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule;
10
12
  import com.facebook.react.bridge.ReactMethod;
11
13
  import com.facebook.react.bridge.ReadableMap;
12
14
  import com.facebook.react.bridge.WritableMap;
15
+ import com.facebook.react.modules.network.OkHttpClientProvider;
13
16
 
14
17
  import org.json.JSONObject;
15
18
 
@@ -17,12 +20,16 @@ import java.io.ByteArrayOutputStream;
17
20
  import java.io.File;
18
21
  import java.io.FileOutputStream;
19
22
  import java.io.IOException;
20
- import java.net.HttpURLConnection;
21
- import java.net.URL;
23
+
22
24
  import java.nio.charset.StandardCharsets;
23
25
  import java.util.concurrent.ExecutorService;
24
26
  import java.util.concurrent.Executors;
25
27
 
28
+ import okhttp3.OkHttpClient;
29
+ import okhttp3.Request;
30
+ import okhttp3.Response;
31
+ import okhttp3.ResponseBody;
32
+
26
33
 
27
34
  public class WeatherFrameProcessorModule extends ReactContextBaseJavaModule {
28
35
 
@@ -59,74 +66,73 @@ public class WeatherFrameProcessorModule extends ReactContextBaseJavaModule {
59
66
  FileOutputStream fos = null;
60
67
  try {
61
68
  if (taskToken != this.currentRunToken) {
62
- // Task was cancelled, do nothing.
63
- return;
69
+ return; // Task was cancelled
64
70
  }
65
-
66
- // 1. Network Request (This part is perfect, no changes needed)
71
+
72
+ // --- START OF REPLACEMENT ---
73
+
74
+ // 1. Get the shared OkHttpClient instance from React Native
75
+ OkHttpClient client = OkHttpClientProvider.getOkHttpClient();
76
+
77
+ // 2. Get request details from options
67
78
  String urlString = options.getString("url");
68
79
  String apiKey = options.getString("apiKey");
69
80
  String bundleId = options.getString("bundleId");
70
- URL requestUrl = new URL(urlString);
71
- HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
72
- conn.setRequestMethod("GET");
73
- conn.setRequestProperty("x-api-key", apiKey);
81
+
82
+ // 3. Build the OkHttp Request
83
+ Request.Builder requestBuilder = new Request.Builder()
84
+ .url(urlString)
85
+ .header("x-api-key", apiKey);
86
+
74
87
  if (bundleId != null) {
75
- conn.setRequestProperty("x-app-identifier", bundleId);
76
- }
77
-
78
- if (conn.getResponseCode() != 200) {
79
- throw new IOException("HTTP Error: " + conn.getResponseCode());
88
+ requestBuilder.header("x-app-identifier", bundleId);
80
89
  }
81
90
 
82
- ByteArrayOutputStream result = new ByteArrayOutputStream();
83
- byte[] buffer = new byte[8192];
84
- int length;
85
- while ((length = conn.getInputStream().read(buffer)) != -1) {
86
- result.write(buffer, 0, length);
91
+ Request request = requestBuilder.build();
92
+
93
+ String responseString;
94
+ // 4. Execute the request and handle the response
95
+ try (Response response = client.newCall(request).execute()) {
96
+ if (!response.isSuccessful()) {
97
+ throw new IOException("HTTP Error: " + response.code());
98
+ }
99
+
100
+ ResponseBody body = response.body();
101
+ if (body == null) {
102
+ throw new IOException("Response body was null");
103
+ }
104
+ responseString = body.string();
87
105
  }
88
- String responseString = result.toString(StandardCharsets.UTF_8.name());
89
- JSONObject jsonResponse = new JSONObject(responseString);
90
- conn.disconnect();
91
106
 
107
+ // --- END OF REPLACEMENT ---
108
+
109
+ // The rest of your logic remains the same
110
+ JSONObject jsonResponse = new JSONObject(responseString);
92
111
  String b64CompressedData = jsonResponse.getString("data");
93
112
  JSONObject encoding = jsonResponse.getJSONObject("encoding");
94
-
95
- // --- START OF EDITS ---
96
113
 
97
- // 2. Decode Base64 into the RAW COMPRESSED bytes.
98
- // DO NOT decompress it here.
99
114
  byte[] compressedData = Base64.decode(b64CompressedData, Base64.DEFAULT);
100
115
 
101
- // 3. Write the SMALL, COMPRESSED data to a file.
102
116
  File cacheDir = reactContext.getCacheDir();
103
- // Use a predictable filename to avoid creating duplicate files for the same frame
104
117
  String fileName = "frame_" + urlString.hashCode() + ".zst";
105
118
  File dataFile = new File(cacheDir, fileName);
106
119
 
107
120
  fos = new FileOutputStream(dataFile);
108
- fos.write(compressedData); // Write the compressed bytes
121
+ fos.write(compressedData);
109
122
  fos.flush();
110
123
  fos.close();
111
- fos = null; // Set to null after successful close
124
+ fos = null;
112
125
 
113
- // 4. Prepare Success Response with the file path and metadata
114
126
  WritableMap responseMap = Arguments.createMap();
115
127
  responseMap.putString("filePath", dataFile.getAbsolutePath());
116
128
  responseMap.putDouble("scale", encoding.getDouble("scale"));
117
129
  responseMap.putDouble("offset", encoding.getDouble("offset"));
118
130
  responseMap.putDouble("missing", encoding.getDouble("missing_quantized"));
119
-
120
- // We still pass this for the very first frame to load instantly without a flicker.
121
- // The preloader logic on the JS side will ignore this and just use the filePath.
122
131
  responseMap.putString("dataAsBase64", b64CompressedData);
123
132
 
124
133
  callback.invoke(null, responseMap);
125
134
 
126
- // --- END OF EDITS ---
127
-
128
135
  } catch (Exception e) {
129
- // Don't invoke callback if task was cancelled, to avoid spamming logs
130
136
  if (taskToken == this.currentRunToken) {
131
137
  android.util.Log.e("AguaceroWX", "Error processing frame", e);
132
138
  callback.invoke(e.getMessage(), null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aguacerowx/react-native",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Native weather rendering for React Native",
5
5
  "main": "index.js",
6
6
  "react-native": "index.js",