Skip to content

Commit

Permalink
fixed tsc error and setup a simple dev environment
Browse files Browse the repository at this point in the history
  • Loading branch information
susonthapa committed Oct 1, 2022
1 parent 8595c34 commit 793f878
Show file tree
Hide file tree
Showing 19 changed files with 2,718 additions and 2,870 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ android {
abortOnError false
}
compileOptions {
targetCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
}
Expand Down
315 changes: 169 additions & 146 deletions android/src/main/java/com/shopify/rnandroidauto/AndroidAutoModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import androidx.activity.OnBackPressedCallback;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand All @@ -19,162 +17,187 @@
import com.google.android.libraries.car.app.CarToast;
import com.google.android.libraries.car.app.ScreenManager;
import com.google.android.libraries.car.app.model.Template;

import java.util.WeakHashMap;

@ReactModule(name = AndroidAutoModule.MODULE_NAME)
public class AndroidAutoModule extends ReactContextBaseJavaModule {
static final String MODULE_NAME = "CarModule";

private static ReactApplicationContext mReactContext;
private CarContext mCarContext;
private CarScreen mCurrentCarScreen;
private ScreenManager mScreenManager;

private WeakHashMap<String, CarScreen> carScreens;
private WeakHashMap<CarScreen, ReactCarRenderContext> reactCarRenderContextMap;

public String getName() {
return MODULE_NAME;
}

AndroidAutoModule(ReactApplicationContext context) {
super(context);
carScreens = new WeakHashMap();
reactCarRenderContextMap = new WeakHashMap();

mReactContext = context;
}

@ReactMethod
public void invalidate(String name) {
CarScreen screen = getScreen(name);
if (screen == mScreenManager.getTop()) {
screen.invalidate();
}
}

@ReactMethod
public void setTemplate(String name, ReadableMap renderMap, Callback callback) {
ReactCarRenderContext reactCarRenderContext = new ReactCarRenderContext();
CarScreen screen = getScreen(name);
if (screen == null) {
screen = mCurrentCarScreen;
}

reactCarRenderContext.setEventCallback(callback).setScreenMarker(screen.getMarker());

Template template = parseTemplate(renderMap, reactCarRenderContext);
reactCarRenderContextMap.remove(screen);
reactCarRenderContextMap.put(screen, reactCarRenderContext);

screen.setTemplate(template);
}

@ReactMethod
public void pushScreen(String name, ReadableMap renderMap, Callback callback) {
ReactCarRenderContext reactCarRenderContext = new ReactCarRenderContext();
reactCarRenderContext.setEventCallback(callback).setScreenMarker(name);

Template template = parseTemplate(renderMap, reactCarRenderContext);

CarScreen screen = new CarScreen(mCarContext, mReactContext);
reactCarRenderContextMap.remove(screen);
reactCarRenderContextMap.put(screen, reactCarRenderContext);

screen.setMarker(name);
screen.setTemplate(template);
carScreens.put(name, screen);
mCurrentCarScreen = screen;
mScreenManager.push(screen);
}

@ReactMethod
public void popScreen() {
static final String MODULE_NAME = "CarModule";

private static ReactApplicationContext mReactContext;
private CarContext mCarContext;
private CarScreen mCurrentCarScreen;
private ScreenManager mScreenManager;

private WeakHashMap<String, CarScreen> carScreens;
private WeakHashMap<CarScreen, ReactCarRenderContext> reactCarRenderContextMap;

public String getName() {
return MODULE_NAME;
}

AndroidAutoModule(ReactApplicationContext context) {
super(context);
carScreens = new WeakHashMap();
reactCarRenderContextMap = new WeakHashMap();

mReactContext = context;
}

@ReactMethod
public void invalidate(String name) {
CarScreen screen = getScreen(name);
if (screen == mScreenManager.getTop()) {
screen.invalidate();
}
}

@ReactMethod
public void setTemplate(
String name,
ReadableMap renderMap,
Callback callback
) {
ReactCarRenderContext reactCarRenderContext = new ReactCarRenderContext();
CarScreen screen = getScreen(name);
if (screen == null) {
screen = mCurrentCarScreen;
Log.d("ReactAUTO", "Screen " + name + " not found!");
}

reactCarRenderContext
.setEventCallback(callback)
.setScreenMarker(screen.getMarker());

Template template = parseTemplate(renderMap, reactCarRenderContext);
reactCarRenderContextMap.remove(screen);
reactCarRenderContextMap.put(screen, reactCarRenderContext);

screen.setTemplate(template);
}

@ReactMethod
public void pushScreen(
String name,
ReadableMap renderMap,
Callback callback
) {
ReactCarRenderContext reactCarRenderContext = new ReactCarRenderContext();
reactCarRenderContext.setEventCallback(callback).setScreenMarker(name);

Template template = parseTemplate(renderMap, reactCarRenderContext);

CarScreen screen = new CarScreen(mCarContext, mReactContext);
reactCarRenderContextMap.remove(screen);
reactCarRenderContextMap.put(screen, reactCarRenderContext);

screen.setMarker(name);
screen.setTemplate(template);
carScreens.put(name, screen);
mCurrentCarScreen = screen;
mScreenManager.push(screen);
}

@ReactMethod
public void popScreen() {
mScreenManager.pop();
removeScreen(mCurrentCarScreen);
mCurrentCarScreen = (CarScreen) mScreenManager.getTop();
}

@ReactMethod
public void mapNavigate(String address) {
mCarContext.startCarApp(
new Intent(CarContext.ACTION_NAVIGATE, Uri.parse("geo:0,0?q=" + address))
);
}

@ReactMethod
public void toast(String text, int duration) {
CarToast.makeText(mCarContext, text, duration).show();
}

@ReactMethod
public void reload() {
DevSettingsModule devSettingsModule = mReactContext.getNativeModule(
DevSettingsModule.class
);
if (devSettingsModule != null) {
devSettingsModule.reload();
}
}

@ReactMethod
public void finishCarApp() {
mCarContext.finishCarApp();
}

@ReactMethod
public void setEventCallback(String name, Callback callback) {
CarScreen screen = (CarScreen) getScreen(name);

Log.d("ReactAUTO", "Set callback 1 for " + name);
if (screen == null) {
return;
}

Log.d("ReactAUTO", "Set callback 2 for " + name);
ReactCarRenderContext reactCarRenderContext = reactCarRenderContextMap.get(
screen
);

if (reactCarRenderContext == null) {
return;
}

Log.d("ReactAUTO", "Set callback 3 for " + name);
reactCarRenderContext.setEventCallback(callback);
}

public void setCarContext(CarContext carContext, CarScreen currentCarScreen) {
mCarContext = carContext;
mCurrentCarScreen = currentCarScreen;
mScreenManager = currentCarScreen.getScreenManager();
carScreens.put("root", mCurrentCarScreen);

OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
Log.d("ReactAUTO", "Back button pressed");
mScreenManager.pop();
removeScreen(mCurrentCarScreen);
mCurrentCarScreen = (CarScreen) mScreenManager.getTop();
}

@ReactMethod
public void mapNavigate(String address) {
mCarContext.startCarApp(new Intent(CarContext.ACTION_NAVIGATE, Uri.parse("geo:0,0?q=" + address)));
}

@ReactMethod
public void toast(String text, int duration) {
CarToast.makeText(mCarContext, text, duration).show();
}

@ReactMethod
public void reload() {
DevSettingsModule devSettingsModule = mReactContext.getNativeModule(DevSettingsModule.class);
if (devSettingsModule != null) {
devSettingsModule.reload();
}
}

@ReactMethod
public void finishCarApp() {
mCarContext.finishCarApp();
}

@ReactMethod
public void setEventCallback(String name, Callback callback) {
CarScreen screen = (CarScreen) getScreen(name);

if (screen == null) {
return;
}
sendEvent("android_auto:back_button", new WritableNativeMap());
}
};

ReactCarRenderContext reactCarRenderContext = reactCarRenderContextMap.get(screen);
carContext.getOnBackPressedDispatcher().addCallback(callback);

if (reactCarRenderContext == null) {
return;
}
sendEvent("android_auto:ready", new WritableNativeMap());
}

reactCarRenderContext.setEventCallback(callback);
}

public void setCarContext(CarContext carContext, CarScreen currentCarScreen) {
mCarContext = carContext;
mCurrentCarScreen = currentCarScreen;
mScreenManager = currentCarScreen.getScreenManager();
carScreens.put("root", mCurrentCarScreen);

OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
sendEvent("android_auto:back_button", new WritableNativeMap());
}
};
private Template parseTemplate(
ReadableMap renderMap,
ReactCarRenderContext reactCarRenderContext
) {
TemplateParser templateParser = new TemplateParser(reactCarRenderContext);
return templateParser.parseTemplate(renderMap);
}

carContext.getOnBackPressedDispatcher().addCallback(callback);
private CarScreen getScreen(String name) {
return carScreens.get(name);
}

sendEvent("android_auto:ready", new WritableNativeMap());
}
private void removeScreen(CarScreen screen) {
WritableNativeMap params = new WritableNativeMap();
params.putString("screen", screen.getMarker());

private Template parseTemplate(ReadableMap renderMap, ReactCarRenderContext reactCarRenderContext) {
TemplateParser templateParser = new TemplateParser(reactCarRenderContext);
return templateParser.parseTemplate(renderMap);
}
sendEvent("android_auto:remove_screen", params);

private CarScreen getScreen(String name) {
return carScreens.get(name);
}
carScreens.values().remove(screen);
}

private void removeScreen(CarScreen screen) {
WritableNativeMap params = new WritableNativeMap();
params.putString("screen", screen.getMarker());

sendEvent("android_auto:remove_screen", params);

carScreens.values().remove(screen);
}

private void sendEvent(String eventName, Object params) {
mReactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
private void sendEvent(String eventName, Object params) {
mReactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public Template getTemplate() {

return PaneTemplate.builder(
Pane.builder().setIsLoading(true).build()
).setTitle("Shopify Local Delivery").build();
).setTitle("Loading...").build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.modules.appregistry.AppRegistry;
import com.facebook.react.modules.core.Timing;
import com.facebook.react.modules.core.TimingModule;
import com.google.android.libraries.car.app.CarAppService;
import com.google.android.libraries.car.app.Screen;
import com.shopify.rnandroidauto.AndroidAutoModule;
Expand Down Expand Up @@ -120,7 +120,7 @@ private void invokeStartTask(ReactContext reactContext) {
}

catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
Timing timingModule = reactContext.getNativeModule(Timing.class);
TimingModule timingModule = reactContext.getNativeModule(TimingModule.class);

AndroidAutoModule carModule = mReactInstanceManager
.getCurrentReactContext()
Expand Down
Loading

0 comments on commit 793f878

Please sign in to comment.