Skip to content

Commit ae9c217

Browse files
authored
feat(added): add new architecture support (#209)
* feat(added): add new architecture support
1 parent 8352dd0 commit ae9c217

36 files changed

+1262
-269
lines changed

android/build.gradle

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ android {
5757
defaultConfig {
5858
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
5959
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
60-
60+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
6161
}
6262

6363
buildTypes {
@@ -74,6 +74,19 @@ android {
7474
sourceCompatibility JavaVersion.VERSION_1_8
7575
targetCompatibility JavaVersion.VERSION_1_8
7676
}
77+
78+
sourceSets {
79+
main {
80+
if (isNewArchitectureEnabled()) {
81+
java.srcDirs += [
82+
"generated/java",
83+
"generated/jni"
84+
]
85+
} else {
86+
java.srcDirs += ['src/oldarch']
87+
}
88+
}
89+
}
7790
}
7891

7992
repositories {
@@ -91,3 +104,10 @@ implementation 'com.google.android.gms:play-services-location:21.0.1'
91104
implementation "com.facebook.react:react-native:+"
92105
}
93106

107+
if (isNewArchitectureEnabled()) {
108+
react {
109+
jsRootDir = file("../src/")
110+
libraryName = "RNEspPosPrinterSpec"
111+
codegenJavaPackageName = "com.escposprinter"
112+
}
113+
}

android/src/main/java/com/reactnativeescposprinter/EposStringHelper.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
public class EposStringHelper {
2121
private static int ERR_INIT = -1;
22+
private static int CONSTANT_NOT_SUPPORTED_FOR_PLATFORM = -404;
2223

2324

2425

@@ -66,8 +67,14 @@ public static Map<String, Object> getDiscoveryConstants() {
6667
constants.put("TYPE_OTHER_PERIPHERAL", Discovery.TYPE_OTHER_PERIPHERAL);
6768
constants.put("FILTER_NAME", Discovery.FILTER_NAME);
6869
constants.put("FILTER_NONE", Discovery.FILTER_NONE);
69-
constants.put("TRUE", Discovery.TRUE);
70-
constants.put("FALSE", Discovery.FALSE);
70+
constants.put("PRINTER_TRUE", Discovery.TRUE);
71+
constants.put("PRINTER_FALSE", Discovery.FALSE);
72+
// stub constants for cross-platform compatibility (iOS only)
73+
constants.put("PORTTYPE_BLUETOOTH_LE", CONSTANT_NOT_SUPPORTED_FOR_PLATFORM);
74+
constants.put("BT_ERR_PARAM", CONSTANT_NOT_SUPPORTED_FOR_PLATFORM);
75+
constants.put("BT_ERR_UNSUPPORTED", CONSTANT_NOT_SUPPORTED_FOR_PLATFORM);
76+
constants.put("BT_ERR_CANCEL", CONSTANT_NOT_SUPPORTED_FOR_PLATFORM);
77+
constants.put("BT_ERR_ILLEGAL_DEVICE", CONSTANT_NOT_SUPPORTED_FOR_PLATFORM);
7178
// return values
7279
constants.put("ERR_PARAM", Epos2Exception.ERR_PARAM);
7380
constants.put("ERR_ILLEGAL", Epos2Exception.ERR_ILLEGAL);
@@ -140,6 +147,7 @@ public static Map<String, Object> getPrinterConstants() {
140147
constants.put("CODE_ERR_PROCESSING", Epos2CallbackCode.CODE_ERR_PROCESSING);
141148
constants.put("CODE_ERR_ILLEGAL", Epos2CallbackCode.CODE_ERR_ILLEGAL);
142149
constants.put("CODE_ERR_DEVICE_BUSY", Epos2CallbackCode.CODE_ERR_DEVICE_BUSY);
150+
constants.put("CODE_ERR_PARAM", Epos2CallbackCode.CODE_ERR_PARAM);
143151

144152
// get printer settings
145153

@@ -186,8 +194,8 @@ public static Map<String, Object> getPrinterConstants() {
186194

187195
// printer status
188196

189-
constants.put("TRUE", Printer.TRUE);
190-
constants.put("FALSE", Printer.FALSE);
197+
constants.put("PRINTER_TRUE", Printer.TRUE);
198+
constants.put("PRINTER_FALSE", Printer.FALSE);
191199
constants.put("UNKNOWN", Printer.UNKNOWN);
192200
constants.put("PAPER_OK", Printer.PAPER_OK);
193201
constants.put("PAPER_NEAR_END", Printer.PAPER_NEAR_END);

android/src/main/java/com/reactnativeescposprinter/EscPosPrinterDiscoveryModule.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@
4040
import com.google.android.gms.common.ConnectionResult;
4141

4242
import com.escposprinter.EposStringHelper;
43+
import com.escposprinter.NativeEscPosPrinterDiscoverySpec;
4344

4445
import java.util.ArrayList;
4546
import java.util.List;
4647

4748

4849
@ReactModule(name = EscPosPrinterDiscoveryModule.NAME)
49-
public class EscPosPrinterDiscoveryModule extends ReactContextBaseJavaModule implements ActivityEventListener {
50+
public class EscPosPrinterDiscoveryModule extends NativeEscPosPrinterDiscoverySpec implements ActivityEventListener {
5051

5152
private Context mContext;
5253

@@ -63,8 +64,7 @@ public EscPosPrinterDiscoveryModule(ReactApplicationContext reactContext) {
6364
reactContext.addActivityEventListener(this);
6465
}
6566

66-
@Override
67-
public Map<String, Object> getConstants() {
67+
protected Map<String, Object> getTypedExportedConstants() {
6868
return EposStringHelper.getDiscoveryConstants();
6969
}
7070

@@ -78,11 +78,19 @@ public String getName() {
7878
@Override
7979
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
8080
if (data != null && resultCode == Activity.RESULT_OK) {
81-
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
82-
.emit("enableLocationSettingSuccess", "Success");
81+
if(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
82+
emitEnableLocationSettingSuccess();
83+
} else {
84+
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
85+
.emit("enableLocationSettingSuccess", "Success");
86+
}
8387
} else {
88+
if(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
89+
emitEnableLocationSettingFailure();
90+
} else {
8491
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
8592
.emit("enableLocationSettingFailure", "Failure");
93+
}
8694
}
8795
}
8896

@@ -153,11 +161,15 @@ public void onFailure(@NonNull Exception e) {
153161
}
154162

155163
private void sendEvent(ReactApplicationContext reactContext, String eventName, @Nullable WritableArray params) {
156-
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
164+
if(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
165+
emitOnDiscovery(params);
166+
} else {
167+
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
168+
}
157169
}
158170

159171
@ReactMethod
160-
private void startDiscovery(final ReadableMap paramsMap, Promise promise) {
172+
public void startDiscovery(final ReadableMap paramsMap, Promise promise) {
161173
mDeviceList.clear();
162174
FilterOption mFilterOption = getFilterOptionsFromParams(paramsMap);
163175

@@ -173,7 +185,7 @@ private void startDiscovery(final ReadableMap paramsMap, Promise promise) {
173185
}
174186

175187
@ReactMethod
176-
private void stopDiscovery(Promise promise) {
188+
public void stopDiscovery(Promise promise) {
177189
try {
178190
Discovery.stop();
179191
promise.resolve(null);
@@ -241,4 +253,8 @@ public synchronized void run() {
241253
});
242254
}
243255
};
256+
257+
public void pairBluetoothDevice(String macAddress, Promise promise) {
258+
259+
}
244260
}

0 commit comments

Comments
 (0)