Skip to content

Commit

Permalink
refactor: replace loaded with completed
Browse files Browse the repository at this point in the history
  • Loading branch information
duguyihou committed Oct 5, 2024
1 parent 5d662ff commit edfdd02
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class TurboImageViewManager : SimpleViewManager<TurboImageView>() {
val reactContext = view.context as ReactContext
UIManagerHelper.getEventDispatcher(reactContext, view.id)?.let {
val payload = Arguments.createMap().apply {
putDouble("loaded", bytesRead.toDouble())
putDouble("completed", bytesRead.toDouble())
putDouble("total", contentLength.toDouble())
}
val surfaceId = UIManagerHelper.getSurfaceId(reactContext)
Expand Down
8 changes: 3 additions & 5 deletions example/src/screens/events/FailureScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type NativeSyntheticEvent,
} from 'react-native';
import React, { useState } from 'react';
import type { Failure, TaskState } from 'react-native-turbo-image';
import type { Failure, Start } from 'react-native-turbo-image';
import TurboImage from 'react-native-turbo-image';

type Information = {
Expand All @@ -19,13 +19,11 @@ const FailureResultScreen = () => {
setInformation(nativeEvent);
};

const handleStart = ({ nativeEvent }: NativeSyntheticEvent<TaskState>) => {
const handleStart = ({ nativeEvent }: NativeSyntheticEvent<Start>) => {
setStart(nativeEvent.state === 'running');
};

const handleCompletion = ({
nativeEvent,
}: NativeSyntheticEvent<TaskState>) => {
const handleCompletion = ({ nativeEvent }: NativeSyntheticEvent<Start>) => {
setCompletion(nativeEvent.state === 'completed');
};

Expand Down
13 changes: 9 additions & 4 deletions example/src/screens/events/SuccessScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import {
type NativeSyntheticEvent,
} from 'react-native';
import React, { useState } from 'react';
import type { Progress, Success, TaskState } from 'react-native-turbo-image';
import type {
Progress,
Success,
Start,
Completion,
} from 'react-native-turbo-image';
import TurboImage from 'react-native-turbo-image';

type Information = {
Expand All @@ -23,19 +28,19 @@ const SuccessScreen = () => {
setInformation(nativeEvent);
};

const handleStart = ({ nativeEvent }: NativeSyntheticEvent<TaskState>) => {
const handleStart = ({ nativeEvent }: NativeSyntheticEvent<Start>) => {
setStart(nativeEvent.state === 'running');
};

const handleCompletion = ({
nativeEvent,
}: NativeSyntheticEvent<TaskState>) => {
}: NativeSyntheticEvent<Completion>) => {
setCompletion(nativeEvent.state === 'completed');
};

const handleProgress = ({ nativeEvent }: NativeSyntheticEvent<Progress>) => {
const percentage = `${(
(100 * nativeEvent.loaded) /
(100 * nativeEvent.completed) /
nativeEvent.total
).toFixed(2)}%`;
setProgress((prev) => [...prev, percentage]);
Expand Down
5 changes: 3 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ export type {
Source,
CachePolicy,
Format,
Start,
Progress,
Success,
Failure,
Progress,
TaskState,
Completion,
ResizeMode,
Indicator,
IndicatorStyle,
Expand Down
12 changes: 8 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ export type Placeholder = {
memoryCacheKey: string;
};

export type TaskState = {
type TaskState = {
state: State;
};

export type Start = TaskState;

export type Completion = TaskState;

export type Progress = {
loaded: number;
completed: number;
total: number;
};

Expand Down Expand Up @@ -68,11 +72,11 @@ export interface TurboImageProps extends AccessibilityProps, ViewProps {
enableLiveTextInteraction?: boolean;
allowHardware?: boolean;
format?: Format;
onStart?: (result: NativeSyntheticEvent<TaskState>) => void;
onStart?: (result: NativeSyntheticEvent<Start>) => void;
onSuccess?: (result: NativeSyntheticEvent<Success>) => void;
onProgress?: (result: NativeSyntheticEvent<Progress>) => void;
onFailure?: (result: NativeSyntheticEvent<Failure>) => void;
onCompletion?: (result: NativeSyntheticEvent<TaskState>) => void;
onCompletion?: (result: NativeSyntheticEvent<Completion>) => void;
}

export type TurboImageApi = {
Expand Down

0 comments on commit edfdd02

Please sign in to comment.