Skip to content

Commit

Permalink
🆕 Add: Import Code to src/temp
Browse files Browse the repository at this point in the history
Related:
* `TODO:2023-03-28-18-52-17` - Pre-migration to `react-native-ios-utilites`.
* `TODO:2023-03-29-04-50-50` - Copy over `react-native-ios-utilities` js/ts source to `src/temp`.

empty
  • Loading branch information
dominicstop committed Mar 28, 2023
1 parent 5bff09c commit 472edff
Show file tree
Hide file tree
Showing 22 changed files with 481 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/temp/constants/RNIBaseErrorCodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { RNIBaseErrorCode } from "../types/RNIError";


export const RNIBaseErrorCodes: {
[key in RNIBaseErrorCode]: key;
} = {
runtimeError : 'runtimeError' ,
libraryError : 'libraryError' ,
reactError : 'reactError' ,
unknownError : 'unknownError' ,
invalidArgument: 'invalidArgument',
outOfBounds : 'outOfBounds' ,
invalidReactTag: 'invalidReactTag',
nilValue : 'nilValue' ,
};
20 changes: 20 additions & 0 deletions src/temp/functions/ErrorUtilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { NativeError } from "../types/NativeError";
import { TypeUtils } from "./TypeUtils";


export class ErrorUtilities {
static isNativeError(error: unknown): error is NativeError {
if(error == null) return false;
if(typeof error !== 'object') return false;

if(!TypeUtils.hasKey('code' , error)) return false;
if(!TypeUtils.hasKey('domain' , error)) return false;
if(!TypeUtils.hasKey('message', error)) return false;

if(typeof error.code !== 'string') return false;
if(typeof error.domain !== 'string') return false;
if(typeof error.message !== 'string') return false;

return true;
};
};
52 changes: 52 additions & 0 deletions src/temp/functions/Helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { findNodeHandle } from 'react-native';

/** wrapper func for setState that returns a promise */
// eslint-disable-next-line consistent-this
export function setStateAsync<T>(
that: React.Component,
newState: T | ((prevState: T) => T)
) {
return new Promise<void>((resolve) => {
that.setState(newState, () => {
resolve();
});
});
};

/** wrapper for timeout that returns a promise */
export function timeout(ms: Number) {
return new Promise<void>((resolve) => {
const timeoutID = setTimeout(() => {
clearTimeout(timeoutID);
resolve();
}, ms);
});
};

/** Wraps a promise that will reject if not not resolved in <ms> milliseconds */
export function promiseWithTimeout<T>(ms: Number, promise: Promise<T>) {
// Create a promise that rejects in <ms> milliseconds
const timeoutPromise = new Promise<T>((_, reject) => {
const timeoutID = setTimeout(() => {
clearTimeout(timeoutID);
reject(`Promise timed out in ${ms} ms.`)
}, ms);
});

// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeoutPromise]);
};

export function pad(num: number | string, places = 2) {
return String(num).padStart(places, '0');
};

export function getNativeNodeHandle(nativeRef: React.Component) {
const nodeHandle = findNodeHandle(nativeRef);

if (nodeHandle === null) {
throw new Error('Unable to get the node handle for the native ref.');
};

return nodeHandle;
};
11 changes: 11 additions & 0 deletions src/temp/functions/TypeUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


export class TypeUtils {
static hasKey<K extends string, T extends object>(
key: K, object: T
): object is T & Record<K, unknown> {
return key in object;
};
};


18 changes: 18 additions & 0 deletions src/temp/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export * from './functions/Helpers';
export * from './functions/ErrorUtilities';
export * from './functions/TypeUtils';

export * from './types/RNIError';
export * from './types/NativeError';
export * from './types/MiscTypes';
export * from './types/UtilityTypes';
export * from './types/RNICleanupMode';
export * from './types/RNIJSComponentWillUnmountNotifiable';
export * from './types/ImageItemConfig';

export * from './constants/RNIBaseErrorCodes';

export * from './native_components/RNIWrapperView';
export * from './native_modules/RNIWrapperViewModule';

export * from './native_modules/RNIUtilitiesModule';
71 changes: 71 additions & 0 deletions src/temp/native_components/RNIWrapperView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react';
import { requireNativeComponent, ViewProps, ViewStyle } from 'react-native';

import { RNIWrapperViewModule } from '../native_modules/RNIWrapperViewModule';

import * as Helpers from '../functions/Helpers';


export type RNIWrapperViewProps = ViewProps & {
style?: ViewStyle;
nativeID?: string;
children?: React.ReactElement;

shouldNotifyComponentWillUnmount?: boolean;
shouldAutoCleanupOnJSUnmount?: boolean;
shouldAutoCleanupOnWindowNil?: boolean;
shouldAutoSetSizeOnLayout?: boolean;

isDummyView?: boolean;
shouldAutoDetachSubviews?: boolean;

shouldCreateTouchHandlerForParentView?: boolean;
shouldCreateTouchHandlerForSubviews?: boolean;
};


const COMPONENT_NAME = 'RNIWrapperView';

const NativeComponent =
requireNativeComponent<RNIWrapperViewProps>(COMPONENT_NAME);


export class RNIWrapperView extends React.PureComponent<RNIWrapperViewProps> {
nativeRef!: React.Component<RNIWrapperViewProps>;

constructor(props: RNIWrapperViewProps){
super(props);
};

private _handleNativeRef = (ref: React.Component<RNIWrapperViewProps>) => {
this.nativeRef = ref;
};

componentWillUnmount(){
const shouldNotifyComponentWillUnmount =
this.props.shouldNotifyComponentWillUnmount ?? false;

if(shouldNotifyComponentWillUnmount){
this.notifyComponentWillUnmount(false);
};
};

notifyComponentWillUnmount = (isManuallyTriggered: boolean = true) => {
RNIWrapperViewModule.notifyComponentWillUnmount(
Helpers.getNativeNodeHandle(this.nativeRef),
{ isManuallyTriggered }
);
};

render(){
const props = this.props;

return React.createElement(NativeComponent, {
...props,
ref: this._handleNativeRef,

shouldNotifyComponentWillUnmount:
props.shouldNotifyComponentWillUnmount ?? false,
});
};
};
15 changes: 15 additions & 0 deletions src/temp/native_modules/RNIUtilitiesModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NativeModules } from 'react-native';


interface RNIUtilitiesModule {
initialize(params: object): void;
};

const MODULE_NAME = "RNIUtilitiesModule";

export const RNIUtilitiesModule: RNIUtilitiesModule =
NativeModules[MODULE_NAME];

// Calling this will initialize `RNIUtilities.sharedBridge`.
// Note: This module must be imported for this function to be invoked.
RNIUtilitiesModule.initialize({});
16 changes: 16 additions & 0 deletions src/temp/native_modules/RNIWrapperViewModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NativeModules } from 'react-native';


interface RNIWrapperViewModule {
notifyComponentWillUnmount(
node: number,
params: {
isManuallyTriggered: boolean;
}
): void;
};

const MODULE_NAME = "RNIWrapperViewModule";

export const RNIWrapperViewModule: RNIWrapperViewModule =
NativeModules[MODULE_NAME];
25 changes: 25 additions & 0 deletions src/temp/types/ImageItemConfig/ImageGradientConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { PointPreset } from "../MiscTypes";
import type { Point } from "../MiscTypes";
import type { ImageRectConfig } from "./ImageRectConfig";


export type ImageGradientConfig = Partial<Pick<ImageRectConfig,
| 'width'
| 'height'
| 'borderRadius'
>> & {
/* An array defining the color of each gradient stop. */
colors: Array<string>;

/* Defines the location of each gradient stop. */
locations?: Array<number>;

/* The start point of the gradient when drawn in the layer’s coordinate space. */
startPoint?: Point | PointPreset;

/* The end point of the gradient when drawn in the layer’s coordinate space. */
endPoint?: Point | PointPreset;

/* Style of gradient drawn by the layer. Defaults to axial. */
type?: 'axial' | 'conic' | 'radial'
};
53 changes: 53 additions & 0 deletions src/temp/types/ImageItemConfig/ImageItemConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { ImageOptions } from './ImageOptions';
import type { ImageLoadingConfig, ImageRemoteUrlConfig, ImageRemoteURLLoadingConfig } from './ImageLoadingConfig';
import type { ImageSystemConfig } from './ImageSystemConfig';
import type { ImageResolvedAssetSource } from './ImageMiscTypes';
import type { ImageRectConfig } from './ImageRectConfig';
import type { ImageGradientConfig } from './ImageGradientConfig';

/**
* A configuration object for images.
*
* This configuration object is used to either programmatically create images,
* (e.g. gradients, rects, SFSymbols icons, etc)., or refer to existing image assets
* in the project (e.g. js image assets, XCAssets).
*/
export type ImageItemConfig = {
type: 'IMAGE_ASSET';
/** The corresponding key of asset item in the asset catalog */
imageValue: string;
imageOptions?: ImageOptions;

} | {
type: 'IMAGE_SYSTEM';
/** The key/name of the SF Symbols system icon */
imageValue: ImageSystemConfig;
imageOptions?: ImageOptions;

} | {
type: 'IMAGE_REQUIRE';
/** Object returned by `Image.resolveAssetSource()` */
imageValue: ImageResolvedAssetSource;
imageOptions?: ImageOptions;
imageLoadingConfig?: ImageLoadingConfig;

} | {
type: 'IMAGE_EMPTY';

} | {
type: 'IMAGE_RECT';
imageValue: ImageRectConfig;

} | {
type: 'IMAGE_GRADIENT';
imageValue: ImageGradientConfig;
imageOptions?: ImageOptions;

} | {
type: 'IMAGE_REMOTE_URL';
imageValue: ImageRemoteUrlConfig;
imageLoadingConfig?: ImageRemoteURLLoadingConfig;
imageOptions?: ImageOptions;
};

export type ImageItemConfigType = ImageItemConfig['type'];
23 changes: 23 additions & 0 deletions src/temp/types/ImageItemConfig/ImageLoadingConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { ImageItemConfig } from "./ImageItemConfig";


export type ImageLoadingConfig = {
shouldCache?: boolean;
shouldLazyLoad?: boolean;
};

export type ImageRemoteUrlConfig = {
url: string;
fallbackImage?: ImageItemConfig;
};

export type ImageRemoteURLFallbackBehavior =
| 'afterFinalAttempt'
| 'whileNotLoaded'
| 'onLoadError';

export type ImageRemoteURLLoadingConfig = ImageLoadingConfig & {
maxRetryAttempts?: number;
shouldImmediatelyRetryLoading?: boolean;
fallbackBehavior?: ImageRemoteURLFallbackBehavior
};
17 changes: 17 additions & 0 deletions src/temp/types/ImageItemConfig/ImageMiscTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

/** Object returned by `Image.resolveAssetSource()` */
export type ImageResolvedAssetSource = {
height: number;
width: number;
scale: number;
uri: string;
};

export enum ImageTypes {
IMAGE_ASSET = 'IMAGE_ASSET' ,
IMAGE_SYSTEM = 'IMAGE_SYSTEM' ,
IMAGE_REQUIRE = 'IMAGE_REQUIRE' ,
IMAGE_EMPTY = 'IMAGE_EMPTY' ,
IMAGE_RECT = 'IMAGE_RECT' ,
IMAGE_GRADIENT = 'IMAGE_GRADIENT',
};
16 changes: 16 additions & 0 deletions src/temp/types/ImageItemConfig/ImageOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { DynamicColor } from "../MiscTypes";


/** Maps to `UIImage.RenderingMode`*/
export type ImageRenderingModes =
'automatic' | 'alwaysOriginal' | 'alwaysTemplate';

/** `UIImage`-related */
export type UIImageConfig = {
tint?: string | DynamicColor;
renderingMode?: ImageRenderingModes;
};

export type ImageOptions = UIImageConfig & {
cornerRadius?: number
};
7 changes: 7 additions & 0 deletions src/temp/types/ImageItemConfig/ImageRectConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export type ImageRectConfig = {
width: number;
height: number;
fillColor: string;
borderRadius?: number;
};
Loading

0 comments on commit 472edff

Please sign in to comment.