@umituz/react-native-mascot 1.0.4 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -0
- package/package.json +2 -1
- package/src/application/services/AnimationStateManager.ts +69 -0
- package/src/application/services/AppearanceManagement.ts +40 -0
- package/src/application/services/MascotService.ts +42 -33
- package/src/application/services/PersonalityManagement.ts +39 -0
- package/src/application/services/StateHistory.ts +55 -0
- package/src/application/services/StateMachine.ts +154 -0
- package/src/application/services/StateTransitions.ts +73 -0
- package/src/application.ts +40 -0
- package/src/assets/index.ts +14 -19
- package/src/core.ts +62 -0
- package/src/domain/entities/Mascot.ts +186 -127
- package/src/domain/types/AnimationStateTypes.ts +148 -0
- package/src/domain/types/MascotTypes.ts +9 -0
- package/src/domain/value-objects/AnimationState.ts +126 -0
- package/src/index.ts +9 -99
- package/src/infrastructure/controllers/AnimationController.ts +26 -122
- package/src/infrastructure/controllers/AnimationPlayer.ts +104 -0
- package/src/infrastructure/controllers/AnimationTimer.ts +62 -0
- package/src/infrastructure/controllers/EventManager.ts +108 -0
- package/src/infrastructure/di/Container.ts +73 -10
- package/src/infrastructure/managers/AssetManager.ts +134 -63
- package/src/infrastructure/managers/MascotBuilder.ts +89 -0
- package/src/infrastructure/managers/MascotFactory.ts +24 -176
- package/src/infrastructure/managers/MascotTemplates.ts +151 -0
- package/src/infrastructure/utils/LRUCache.ts +218 -0
- package/src/infrastructure.ts +24 -0
- package/src/presentation/components/LottieMascot.tsx +85 -0
- package/src/presentation/components/MascotView.tsx +42 -233
- package/src/presentation/components/SVGMascot.tsx +61 -0
- package/src/presentation/contexts/MascotContext.tsx +2 -3
- package/src/presentation/hooks/useMascot.ts +118 -39
- package/src/presentation/hooks/useMascotAnimation.ts +9 -15
- package/src/presentation/hooks/useMascotState.ts +213 -0
- package/src/presentation.ts +37 -0
- package/src/types.d.ts +4 -0
- package/src/application/index.ts +0 -8
- package/src/domain/value-objects/index.ts +0 -9
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useMascotState Hook
|
|
3
|
+
* State-based mascot hook with auto-transitions (OPTIMIZED)
|
|
4
|
+
* Inspired by AIStylistMascot implementation
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
8
|
+
import type { MascotAnimationState, MascotSize } from '../../domain/types/AnimationStateTypes';
|
|
9
|
+
import { DEFAULT_SIZE_CONFIG } from '../../domain/types/AnimationStateTypes';
|
|
10
|
+
import { AnimationStateManager } from '../../application/services/AnimationStateManager';
|
|
11
|
+
import { AnimationState } from '../../domain/value-objects/AnimationState';
|
|
12
|
+
|
|
13
|
+
export interface UseMascotStateOptions {
|
|
14
|
+
initialState?: MascotAnimationState;
|
|
15
|
+
size?: MascotSize;
|
|
16
|
+
enableAutoTransition?: boolean;
|
|
17
|
+
onStateChange?: (from: MascotAnimationState, to: MascotAnimationState) => void;
|
|
18
|
+
onAnimationComplete?: (state: MascotAnimationState) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface UseMascotStateReturn {
|
|
22
|
+
// State
|
|
23
|
+
state: MascotAnimationState;
|
|
24
|
+
size: number;
|
|
25
|
+
isLooping: boolean;
|
|
26
|
+
duration: number;
|
|
27
|
+
speed: number;
|
|
28
|
+
|
|
29
|
+
// Actions
|
|
30
|
+
setState: (state: MascotAnimationState) => void;
|
|
31
|
+
triggerSuccess: () => void;
|
|
32
|
+
triggerError: () => void;
|
|
33
|
+
startLoading: () => void;
|
|
34
|
+
stopLoading: (success?: boolean) => void;
|
|
35
|
+
reset: () => void;
|
|
36
|
+
|
|
37
|
+
// State queries
|
|
38
|
+
isIdle: () => boolean;
|
|
39
|
+
isLoading: () => boolean;
|
|
40
|
+
isSuccess: () => boolean;
|
|
41
|
+
isError: () => boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get size in pixels from size variant (memoized)
|
|
46
|
+
*/
|
|
47
|
+
function getSizePixels(size: MascotSize): number {
|
|
48
|
+
if (typeof size === 'number') {
|
|
49
|
+
return size;
|
|
50
|
+
}
|
|
51
|
+
return DEFAULT_SIZE_CONFIG[size];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function useMascotState(options: UseMascotStateOptions = {}): UseMascotStateReturn {
|
|
55
|
+
const {
|
|
56
|
+
initialState = 'idle',
|
|
57
|
+
size: sizeVariant = 'medium',
|
|
58
|
+
enableAutoTransition = true,
|
|
59
|
+
onStateChange,
|
|
60
|
+
onAnimationComplete,
|
|
61
|
+
} = options;
|
|
62
|
+
|
|
63
|
+
const [state, setState] = useState<MascotAnimationState>(initialState);
|
|
64
|
+
|
|
65
|
+
// Memoize size to avoid recalculation
|
|
66
|
+
const size = useMemo(() => getSizePixels(sizeVariant), [sizeVariant]);
|
|
67
|
+
|
|
68
|
+
// Memoize animation state to avoid unnecessary recreations
|
|
69
|
+
const animationState = useMemo(
|
|
70
|
+
() => AnimationState.create(state),
|
|
71
|
+
[state]
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
// Stable refs for callbacks to prevent unnecessary re-renders
|
|
75
|
+
const onStateChangeRef = useRef(onStateChange);
|
|
76
|
+
const onAnimationCompleteRef = useRef(onAnimationComplete);
|
|
77
|
+
|
|
78
|
+
// Update refs without causing re-renders
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
onStateChangeRef.current = onStateChange;
|
|
81
|
+
onAnimationCompleteRef.current = onAnimationComplete;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const stateManagerRef = useRef<AnimationStateManager | null>(null);
|
|
85
|
+
const cleanupTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
86
|
+
|
|
87
|
+
// Initialize state manager (only once)
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
const manager = new AnimationStateManager(initialState, {
|
|
90
|
+
enableAutoTransition,
|
|
91
|
+
onStateChange: (from, to) => {
|
|
92
|
+
setState(to);
|
|
93
|
+
onStateChangeRef.current?.(from, to);
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
stateManagerRef.current = manager;
|
|
98
|
+
|
|
99
|
+
return () => {
|
|
100
|
+
// Clear any pending cleanup timeout
|
|
101
|
+
const cleanupTimeout = cleanupTimeoutRef.current;
|
|
102
|
+
if (cleanupTimeout) {
|
|
103
|
+
clearTimeout(cleanupTimeout);
|
|
104
|
+
cleanupTimeoutRef.current = null;
|
|
105
|
+
}
|
|
106
|
+
// Destroy state manager and clear all timers
|
|
107
|
+
manager.destroy();
|
|
108
|
+
};
|
|
109
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
// Optimized animation completion handler with proper cleanup
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
if (!animationState.shouldLoop()) {
|
|
115
|
+
const duration = animationState.getDuration();
|
|
116
|
+
|
|
117
|
+
const timeout = setTimeout(() => {
|
|
118
|
+
onAnimationCompleteRef.current?.(state);
|
|
119
|
+
}, duration);
|
|
120
|
+
|
|
121
|
+
return () => {
|
|
122
|
+
clearTimeout(timeout);
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return undefined;
|
|
126
|
+
}, [state, animationState]);
|
|
127
|
+
|
|
128
|
+
// Memoized callbacks to prevent unnecessary re-renders
|
|
129
|
+
const setStateCallback = useCallback((newState: MascotAnimationState) => {
|
|
130
|
+
const manager = stateManagerRef.current;
|
|
131
|
+
if (!manager) {
|
|
132
|
+
setState(newState);
|
|
133
|
+
onStateChangeRef.current?.(state, newState);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
manager.transitionTo(newState);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.warn('Invalid state transition:', error);
|
|
141
|
+
setState(newState);
|
|
142
|
+
onStateChangeRef.current?.(state, newState);
|
|
143
|
+
}
|
|
144
|
+
}, [state]);
|
|
145
|
+
|
|
146
|
+
const triggerSuccess = useCallback(() => {
|
|
147
|
+
const manager = stateManagerRef.current;
|
|
148
|
+
if (manager) {
|
|
149
|
+
manager.triggerSuccess();
|
|
150
|
+
} else {
|
|
151
|
+
setStateCallback('success');
|
|
152
|
+
}
|
|
153
|
+
}, [setStateCallback]);
|
|
154
|
+
|
|
155
|
+
const triggerError = useCallback(() => {
|
|
156
|
+
const manager = stateManagerRef.current;
|
|
157
|
+
if (manager) {
|
|
158
|
+
manager.triggerError();
|
|
159
|
+
} else {
|
|
160
|
+
setStateCallback('error');
|
|
161
|
+
}
|
|
162
|
+
}, [setStateCallback]);
|
|
163
|
+
|
|
164
|
+
const startLoading = useCallback(() => {
|
|
165
|
+
const manager = stateManagerRef.current;
|
|
166
|
+
if (manager) {
|
|
167
|
+
manager.startLoading();
|
|
168
|
+
} else {
|
|
169
|
+
setStateCallback('loading');
|
|
170
|
+
}
|
|
171
|
+
}, [setStateCallback]);
|
|
172
|
+
|
|
173
|
+
const stopLoading = useCallback((success = true) => {
|
|
174
|
+
const manager = stateManagerRef.current;
|
|
175
|
+
if (manager) {
|
|
176
|
+
manager.stopLoading(success);
|
|
177
|
+
} else {
|
|
178
|
+
setStateCallback(success ? 'success' : 'error');
|
|
179
|
+
}
|
|
180
|
+
}, [setStateCallback]);
|
|
181
|
+
|
|
182
|
+
const reset = useCallback(() => {
|
|
183
|
+
const manager = stateManagerRef.current;
|
|
184
|
+
if (manager) {
|
|
185
|
+
manager.reset();
|
|
186
|
+
} else {
|
|
187
|
+
setStateCallback('idle');
|
|
188
|
+
}
|
|
189
|
+
}, [setStateCallback]);
|
|
190
|
+
|
|
191
|
+
const isIdle = useCallback(() => state === 'idle', [state]);
|
|
192
|
+
const isLoading = useCallback(() => state === 'loading', [state]);
|
|
193
|
+
const isSuccess = useCallback(() => state === 'success', [state]);
|
|
194
|
+
const isError = useCallback(() => state === 'error', [state]);
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
state,
|
|
198
|
+
size,
|
|
199
|
+
isLooping: animationState.shouldLoop(),
|
|
200
|
+
duration: animationState.getDuration(),
|
|
201
|
+
speed: animationState.getSpeed(),
|
|
202
|
+
setState: setStateCallback,
|
|
203
|
+
triggerSuccess,
|
|
204
|
+
triggerError,
|
|
205
|
+
startLoading,
|
|
206
|
+
stopLoading,
|
|
207
|
+
reset,
|
|
208
|
+
isIdle,
|
|
209
|
+
isLoading,
|
|
210
|
+
isSuccess,
|
|
211
|
+
isError,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Presentation Layer Exports (60 lines)
|
|
3
|
+
* Components, hooks, and contexts
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Presentation - Components
|
|
7
|
+
export { MascotView } from './presentation/components/MascotView';
|
|
8
|
+
export type { MascotViewProps } from './presentation/components/MascotView';
|
|
9
|
+
|
|
10
|
+
export { LottieMascot } from './presentation/components/LottieMascot';
|
|
11
|
+
export type { LottieMascotProps } from './presentation/components/LottieMascot';
|
|
12
|
+
|
|
13
|
+
export { SVGMascot } from './presentation/components/SVGMascot';
|
|
14
|
+
export type { SVGMascotProps } from './presentation/components/SVGMascot';
|
|
15
|
+
|
|
16
|
+
// Presentation - Hooks
|
|
17
|
+
export { useMascot } from './presentation/hooks/useMascot';
|
|
18
|
+
export type {
|
|
19
|
+
UseMascotOptions,
|
|
20
|
+
UseMascotReturn,
|
|
21
|
+
} from './presentation/hooks/useMascot';
|
|
22
|
+
|
|
23
|
+
export { useMascotAnimation } from './presentation/hooks/useMascotAnimation';
|
|
24
|
+
export type {
|
|
25
|
+
UseMascotAnimationOptions,
|
|
26
|
+
UseMascotAnimationReturn,
|
|
27
|
+
} from './presentation/hooks/useMascotAnimation';
|
|
28
|
+
|
|
29
|
+
export { useMascotState } from './presentation/hooks/useMascotState';
|
|
30
|
+
export type {
|
|
31
|
+
UseMascotStateOptions,
|
|
32
|
+
UseMascotStateReturn,
|
|
33
|
+
} from './presentation/hooks/useMascotState';
|
|
34
|
+
|
|
35
|
+
// Presentation - Contexts
|
|
36
|
+
export { MascotProvider, useMascotContext } from './presentation/contexts/MascotContext';
|
|
37
|
+
export type { MascotProviderProps, MascotContextValue } from './presentation/contexts/MascotContext';
|
package/src/types.d.ts
ADDED
package/src/application/index.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Value Objects
|
|
3
|
-
* Encapsulate domain logic and validation
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export { Mood } from './Mood';
|
|
7
|
-
export { EnergyLevel } from './EnergyLevel';
|
|
8
|
-
export { FriendlinessLevel } from './FriendlinessLevel';
|
|
9
|
-
export { PlayfulnessLevel } from './PlayfulnessLevel';
|