Skip to content

Commit

Permalink
fix: android fixes and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Cavallando committed Oct 31, 2023
1 parent 20a1b4d commit 95a3077
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 51 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ dependencies {
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'com.google.android.material:material:1.4.0'
implementation "com.commandbar.android:commandbar:1.0.0"
implementation "com.commandbar.android:commandbar:1.0.5"
}
17 changes: 12 additions & 5 deletions android/src/main/java/com/commandbarmodule/CommandBarModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.commandbarmodule


import com.facebook.react.bridge.*
import com.commandbar.android.CommandBar

import com.commandbar.android.CommandBar
import com.commandbar.android.CommandBarOptions

class CommandBarModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
Expand All @@ -13,15 +13,22 @@ class CommandBarModule(reactContext: ReactApplicationContext) :
}

@ReactMethod
fun openHelpHub(orgId: String, promise: Promise) {
fun openHelpHub(options: ReadableMap, onFallbackActionCallback: Callback? = null) {
val activity = currentActivity
if (activity == null) {
promise.reject("ACTIVITY_NULL", "Current activity not available")
return
}

val commandBarOptions = CommandBarOptions(dictionary = options.toHashMap())

activity.runOnUiThread {
CommandBar.openHelpHub(activity, orgId)
if (onFallbackActionCallback != null) {
CommandBar.openHelpHub(activity, commandBarOptions) {
onFallbackActionCallback.invoke(it)
}
} else {
CommandBar.openHelpHub(activity, commandBarOptions)
}
}
}
}
32 changes: 26 additions & 6 deletions android/src/main/java/com/commandbarmodule/HelpHubViewManager.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
package com.commandbarmodule

import com.commandbar.android.CommandBarOptions
import com.commandbar.android.HelpHubWebView
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
import com.facebook.react.modules.core.DeviceEventManagerModule
import com.facebook.react.uimanager.ReactStylesDiffMap
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.commandbar.android.HelpHubWebView
import com.facebook.react.uimanager.ViewManager
import com.facebook.react.uimanager.annotations.ReactProp

class HelpHubViewManager(reactContext: ReactApplicationContext) : SimpleViewManager<HelpHubWebView>() {

class HelpHubViewManager(var reactContext: ReactApplicationContext) : SimpleViewManager<HelpHubWebView>() {
private lateinit var commandBarOptions: CommandBarOptions

override fun getName(): String {
return "HelpHubView"
}

@ReactProp(name = "orgId")
fun setOrgId(view: HelpHubWebView, orgId: String) {
view.setOrgId(orgId)
@ReactProp(name = "options")
fun setOptions(view: HelpHubWebView, options: ReadableMap) {
this.commandBarOptions = CommandBarOptions(dictionary = options.toHashMap())
view.setOptions(this.commandBarOptions)
view.setFallbackActionCallback { this.handleFallbackAction(it) }
}

fun handleFallbackAction(action: Map<String, Any>) {
val payload: WritableMap = Arguments.makeNativeMap(action)

this.reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit("onFallbackAction", payload)
}

override fun createViewInstance(reactContext: ThemedReactContext): HelpHubWebView {
return HelpHubWebView(reactContext, null)
return HelpHubWebView(reactContext)
}
}
12 changes: 6 additions & 6 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- boost (1.76.0)
- CocoaAsyncSocket (7.6.5)
- CommandBarIOS (1.0.1)
- CommandBarIOS (1.0.5)
- DoubleConversion (1.1.6)
- FBLazyVector (0.72.6)
- FBReactNativeSpec (0.72.6):
Expand Down Expand Up @@ -376,8 +376,8 @@ PODS:
- React-jsinspector (0.72.6)
- React-logger (0.72.6):
- glog
- react-native-commandbar (1.0.0):
- CommandBarIOS (~> 1.0.1)
- react-native-commandbar (1.0.4):
- CommandBarIOS (~> 1.0.5)
- RCT-Folly (= 2021.07.22.00)
- React-Core
- React-NativeModulesApple (0.72.6):
Expand Down Expand Up @@ -665,7 +665,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
boost: 57d2868c099736d80fcd648bf211b4431e51a558
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
CommandBarIOS: 67ea81e5f6b61ad2407ebc519d55be3bfe12fdfb
CommandBarIOS: fa6fe132824f41fa8ade49143ed9de6b1d7802e0
DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54
FBLazyVector: 748c0ef74f2bf4b36cfcccf37916806940a64c32
FBReactNativeSpec: 966f29e4e697de53a3b366355e8f57375c856ad9
Expand Down Expand Up @@ -697,7 +697,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: 3bf18ff7cb03cd8dfdce08fbbc0d15058c1d71ae
React-jsinspector: 194e32c6aab382d88713ad3dd0025c5f5c4ee072
React-logger: cebf22b6cf43434e471dc561e5911b40ac01d289
react-native-commandbar: 5f8c0cfe780f3923d11e2abcace6948f30b3e854
react-native-commandbar: d28e45df4dfdabb7f994057e4ddf1a093964971c
React-NativeModulesApple: 02e35e9a51e10c6422f04f5e4076a7c02243fff2
React-perflogger: e3596db7e753f51766bceadc061936ef1472edc3
React-RCTActionSheet: 17ab132c748b4471012abbcdcf5befe860660485
Expand All @@ -721,4 +721,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 06225a65a3da2ed681342fd9abd8d07b9d6df5ba

COCOAPODS: 1.14.0
COCOAPODS: 1.14.2
24 changes: 17 additions & 7 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
import * as React from 'react';
import { StyleSheet, View } from 'react-native';
import { HelpHubView } from 'react-native-commandbar';

import { Button, StyleSheet, View } from 'react-native';
import { CommandBar } from 'react-native-commandbar';
const commandbarOptions = { orgId: '641ade4d', launchCode: 'preview_4686' };

export default function App() {
const handleOpenHelpHub = async () => {
await CommandBar.openHelpHub('your_org_id');
const handleFallbackAction = (action: any) => {
console.log('onFallbackAction', action);
};

// const handleOpenHelpHub = () => {
// CommandBar.openHelpHub(commandbarOptions, handleFallbackAction);
// };

return (
<View style={styles.container}>
<Button title="Open HelpHub" onPress={handleOpenHelpHub} />
{/* <Button title="Open HelpHub" onPress={handleOpenHelpHub} /> */}
<HelpHubView
options={commandbarOptions}
onFallbackAction={handleFallbackAction}
style={styles.helphub}
/>
</View>
);
}

const styles = StyleSheet.create({
helphub: { width: '100%', height: '100%' },
container: {
flex: 1,
// height: '100%',
// width: '100%',
alignItems: 'center',
justifyContent: 'center',
},
Expand Down
20 changes: 12 additions & 8 deletions ios/HelpHubViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import CommandBarIOS

@objc(HelpHubViewManager)
class HelpHubViewManager: RCTViewManager {
@objc var orgId: NSString = ""
@objc var onOpenSupportChat: RCTDirectEventBlock?

func sendUpdate() {
// TODO: Hook up callback
// onOpenSupportChat!(["count": count])
}
@objc var options: NSDictionary?
@objc var onFallbackAction: RCTDirectEventBlock?

override func view() -> UIView! {
return HelpHubWebView(orgId: self.orgId as String, frame: CGRect.zero)
let options = CommandBarOptions(dictionary: self.options as! [String : Any])
let helpHubWebView = HelpHubWebView(frame: CGRect.zero, options: options)
helpHubWebView.delegate = self
return helpHubWebView
}

override static func requiresMainQueueSetup() -> Bool {
return true
}
}

extension HelpHubViewManager: HelpHubWebViewDelegate {
func didReceiveFallbackAction(_ action: [String : Any]) {
self.onFallbackAction?(action)
}
}
4 changes: 2 additions & 2 deletions ios/RNCommandBar.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ + (BOOL)requiresMainQueueSetup

@interface RCT_EXTERN_MODULE(HelpHubViewManager, RCTViewManager)

RCT_EXPORT_VIEW_PROPERTY(orgId, NSString*)
RCT_EXPORT_VIEW_PROPERTY(onOpenSupportChat, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(options, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(onFallbackAction, RCTDirectEventBlock)

@end
7 changes: 5 additions & 2 deletions ios/RNCommandBar.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Foundation
import WebKit
import CommandBarIOS

@objc(RNCommandBar)
class RNCommandBar : NSObject {
@objc
func openHelpHub(_ orgId: NSString, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
CommandBar.openHelpHub(orgId: orgId as String, resolve: nil, reject: nil)
func openHelpHub(_ options: NSDictionary, onFallBackAction fallbackAction: RCTResponseSenderBlock? = nil) -> Void {
let options = CommandBarOptions(dictionary: options as! [String : Any])
let commandbar = CommandBar(options: options)
commandbar.openHelpHub(resolve: nil, reject: nil)
resolve(true)
}
}
1 change: 1 addition & 0 deletions ios/RNCommandbar-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTViewManager.h>
#import <React/RCTEventEmitter.h>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-commandbar",
"version": "1.0.0",
"version": "1.0.4",
"description": "Copilot & HelpHub in React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
2 changes: 1 addition & 1 deletion react-native-commandbar.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|

s.source_files = "ios/**/*.{h,m,mm,swift}"

s.dependency 'CommandBarIOS', '~> 1.0.1'
s.dependency 'CommandBarIOS', '~> 1.0.5'

# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
Expand Down
22 changes: 10 additions & 12 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ViewStyle } from 'react-native';
import { NativeModules, Platform, requireNativeComponent } from 'react-native';
import { NativeModules, Platform } from 'react-native';
import { HelpHubView } from './HelpHubView';
import type { CommandBarOptions } from './CommandBar';

// Define type for the LINKING_ERROR constant
const LINKING_ERROR: string =
Expand All @@ -8,11 +9,6 @@ const LINKING_ERROR: string =
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo Go\n';

type RNCommandBar = {
openHelpHub(orgId: string): Promise<void>;
HelpHubView: React.ComponentClass<HelpHubViewProps>;
};

const RNCommandBar = NativeModules.RNCommandBar
? NativeModules.RNCommandBar
: new Proxy(
Expand All @@ -24,13 +20,15 @@ const RNCommandBar = NativeModules.RNCommandBar
}
);

export type HelpHubViewProps = {
orgId: string;
style?: ViewStyle;
type RNCommandBar = {
openHelpHub(
options: CommandBarOptions,
onFallbackAction?: (action: any) => void
): void;
HelpHubView: typeof HelpHubView;
};

export const HelpHubView: React.ComponentClass<HelpHubViewProps> =
requireNativeComponent('HelpHubView');
export * from './HelpHubView';

export const CommandBar: RNCommandBar = {
...RNCommandBar,
Expand Down

0 comments on commit 95a3077

Please sign in to comment.