Skip to content

Instantly share code, notes, and snippets.

@Jarred-Sumner
Created February 24, 2021 10:38
Show Gist options
  • Save Jarred-Sumner/4ce73026383e626b69725ab8fe5f6693 to your computer and use it in GitHub Desktop.
Save Jarred-Sumner/4ce73026383e626b69725ab8fe5f6693 to your computer and use it in GitHub Desktop.
import {TSMeasureResult} from 'react-native-text-size';
import memoizee from 'memoizee';
import {NativeModules} from 'react-native';
import {LocationPermissionStatus, Location} from 'react-native-location';
import {isEmpty} from 'lodash';
import {LocationButton} from '../components/LocationButton';
import {GeocodeLocation} from './GeocodeLocation';
import {getBoundByRegion} from './getBoundByRegion';
const TextSize = NativeModules.RNTextSize;
const YeetBinder = NativeModules.YeetBinder;
const jsi = () => {
if (typeof global.YeetJSI !== 'undefined') {
return global.YeetJSI;
} else {
return undefined;
}
};
const _measureText = memoizee((fontSize, text, fontWeight, width) => {
const resp =
TextSize?.measureSync({
fontSize,
text,
fontWeight,
width,
includeFontPadding: true,
}) ?? {};
if (resp?.height) {
resp.height += fontSize;
}
return resp;
});
export const measureText = ({
fontSize,
text,
fontWeight,
width,
}): TSMeasureResult => {
return _measureText(fontSize, text, fontWeight, width) ?? {height: 0};
};
export const getMapBounds = (handle, _, region) => {
return getBoundByRegion(region);
};
export const snapSheetToPosition = (handle, size, ref) =>
ref?.updatePosition(size);
const _getItem = (key: string, type: string): any => {
if (type === 'number') {
return YeetBinder?.getDouble(key);
} else if (type === 'string') {
return YeetBinder?.getString(key);
} else if (type === 'object') {
const obj = YeetBinder?.getObject(key);
if (isEmpty(obj)) {
return null;
} else {
return obj;
}
}
};
export const getItem = (key: string, type: string): any => {
console.time('Get item');
const item = _getItem(key, type);
console.timeEnd('Get item');
return item;
};
export const removeItem = (key: string): any => YeetBinder.removeItem(key);
export const setItem = (key: string, value: any, type: string): any =>
YeetBinder.setItem(key, type, {value});
export const getLocationStatus = (): LocationPermissionStatus => {
return YeetBinder.checkLocationPermission();
};
export const geocode = (
latitude: number,
longitude: number,
): Promise<Array<GeocodeLocation>> => {
return YeetBinder.geocode({latitude, longitude});
};
export const getLastLocation = (): Location | null => {
return jsi()?.lastLocation;
};
export const hasTwitterInstalled = (): Boolean =>
YeetBinder.hasTwitterInstalled();
#include "./YeetJSI.h"
#include <jsi/jsi.h>
#include <fb/fbjni.h>
#include <jsi/JSIDynamic.h>
#include <react/jni/ReadableNativeMap.h>
#include <fb/log.h>
#include <jni.h>
#include <android/log.h>
#define APPNAME "covy"
using namespace facebook;
using namespace facebook::jni;
void JNICALL Java_com_codeblogcorp_covy_YeetBinder_install(
JNIEnv *env,
jobject thiz,
jlong runtimePtr
) {
auto &runtime = *(facebook::jsi::Runtime *)runtimePtr;
auto clazz = env->FindClass("com/codeblogcorp/covy/YeetBinder");
auto measureText = env->GetStaticMethodID(clazz, "measureText", "(DLjava/lang/String;Ljava/lang/String;D)D");
// auto getItem = env->GetStaticMethodID(clazz, "getItem", "(SS)O");
// auto setItem = env->GetStaticMethodID(clazz, "setItem", "(SSO)V");
auto module = std::make_shared<YeetJSI>(
clazz,
thiz,
measureText
// getItem,
// setItem
);
YeetJSI::install(runtime, module);
}
YeetJSI::YeetJSI(
jclass moduleClass,
jobject moduleObject,
jmethodID measureText
// jmethodID getItem,
// jmethodID setItem
):
_moduleClass(moduleClass),
_moduleObject(moduleObject),
_measureText(measureText)
// _getItem(getItem),
// _setItem(setItem)
{
}
void YeetJSI::install(
jsi::Runtime &runtime,
const std::shared_ptr<YeetJSI> module
) {
auto name = "YeetJSI";
auto object = jsi::Object::createFromHostObject(runtime, module);
runtime.global().setProperty(runtime, name, std::move(object));
}
jsi::Value YeetJSI::get(
jsi::Runtime &runtime,
const jsi::PropNameID &name
) {
auto methodName = name.utf8(runtime);
if (methodName == "measureText") {
auto &method = _measureText;
auto &moduleClass = _moduleClass;
if (_moduleObject) {
}
auto callback = [moduleClass, method](
jsi::Runtime &runtime,
const jsi::Value &thisValue,
const jsi::Value *arguments,
size_t count
) -> jsi::Value {
auto fontSize = (jdouble)arguments[0].asNumber();
printf("fontSize: %lf", fontSize);
auto text = facebook::jni::make_jstring(arguments[1].getString(runtime).utf8(runtime).c_str()).get();
// printf("fontSize: %s", text);
auto fontWeight = facebook::jni::make_jstring(arguments[2].getString(runtime).utf8(runtime).c_str()).get();
// printf("fontWeight: %s", fontWeight);
auto width =(jdouble)arguments[3].asNumber();
printf("width: %lf", width);
auto env = Environment::current();
auto val = (jdouble)env->CallStaticDoubleMethod(moduleClass, method, fontSize,
text,
fontWeight,
width);
return jsi::Value(val);
};
return jsi::Function::createFromHostFunction(runtime, name, 4, callback);
}
// else if (methodName == "getItem") {
// auto &method = _getItem;
// auto &moduleObject = _moduleObject;
// auto &moduleClass = _moduleClass;
// return jsi::Function::createFromHostFunction(runtime, name, 3, [_getItem, _moduleObject, _moduleClass](
// jsi::Runtime &runtime,
// const jsi::Value &thisValue,
// const jsi::Value *arguments,
// size_t count) -> jsi::Value {
// auto type = (jstring)arguments[1].asString(runtime);
// auto key = (jstring)arguments[0].asString(runtime);
// if (!key || ![key length]) {
// return jsi::Value::null();
// }
// const auto env = Environment::current();
// auto val = (jdouble)env->CallStaticDoubleMethod(moduleClass, method, size, width);
// });
// } else if (methodName == "setItem") {
// return jsi::Function::createFromHostFunction(runtime, name, 3, [mmkv, jsInvoker](
// jsi::Runtime &runtime,
// const jsi::Value &thisValue,
// const jsi::Value *arguments,
// size_t count) -> jsi::Value {
// NSString *type = convertJSIStringToNSString(runtime, arguments[2].asString(runtime));
// NSString *key = convertJSIStringToNSString(runtime, arguments[0].asString(runtime));
// if (!key || ![key length]) {
// return jsi::Value::null();
// }
// if ([type isEqualToString:@"string"]) {
// NSString *value = convertJSIStringToNSString(runtime, arguments[1].asString(runtime));
// if ([value length] > 0) {
// return jsi::Value([mmkv setString:value forKey:key]);
// } else {
// return jsi::Value(false);
// }
// } else if ([type isEqualToString:@"number"]) {
// double value = arguments[1].asNumber();
// return jsi::Value([mmkv setDouble:value forKey:key]);
// } else if ([type isEqualToString:@"object"]) {
// NSDictionary *value = convertJSIObjectToNSDictionary(runtime, arguments[1].asObject(runtime), jsInvoker);
// return jsi::Value([mmkv setObject:value forKey:key]);
// } else if ([type isEqualToString:@"bool"]) {
// BOOL value = arguments[2].asNumber();
// return jsi::Value([mmkv setBool:value forKey:key]);
// } else {
// return jsi::Value::null();
// }
// });
return jsi::Value::undefined();
}
#pragma once
#include <fb/fbjni.h>
#include <fb/log.h>
#include <jsi/jsi.h>
#include <jni.h>
using namespace facebook;
extern "C" {
JNIEXPORT void JNICALL
Java_com_codeblogcorp_covy_YeetBinder_install(
JNIEnv *env,
jobject thiz,
jlong runtimePtr
);
}
class YeetJSI : public jsi::HostObject {
private:
jclass _moduleClass;
jobject _moduleObject;
jmethodID _measureText;
// jmethodID _getItem;
// jmethodID _setItem;
public:
YeetJSI(
jclass moduleClass,
jobject moduleObject,
jmethodID measureText
// jmethodID getItem,
// jmethodID setItem
);
static void install(
jsi::Runtime &runtime,
const std::shared_ptr<YeetJSI> module
);
jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment