File size: 6,503 Bytes
9a92a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import React, { useState, useEffect, useRef } from 'react';
import { View, Image, StyleSheet, Animated, Easing, Text } from 'react-native';

const LiveTrainingPin = ({ images = [], size = 150, autoPlay = true }) => {
  const [currentIndex, setCurrentIndex] = useState(0);
  const [showPopup, setShowPopup] = useState(true);
  const fadeAnim = useRef(new Animated.Value(0)).current;
  const scaleAnim = useRef(new Animated.Value(0)).current;
  const pulseAnim = useRef(new Animated.Value(1)).current;
  const imageFadeAnim = useRef(new Animated.Value(1)).current;

  // Derived sizes
  const pointerHeight = Math.max(18, Math.round(size * 0.28));
  const triangleBase = Math.max(12, Math.round(pointerHeight * 0.7));
  const ringSize = Math.round(size * 1.2);

  // Default fallback images if none provided
  const defaultImages = [
    'https://via.placeholder.com/300/10B981/FFFFFF?text=Training',
    'https://via.placeholder.com/300/3B82F6/FFFFFF?text=Live',
  ];

  const displayImages = images.length > 0 ? images : defaultImages;

  // Pin drop animation on mount
  useEffect(() => {
    Animated.parallel([
      Animated.timing(scaleAnim, {
        toValue: 1,
        duration: 600,
        easing: Easing.bounce,
        useNativeDriver: true,
      }),
      Animated.timing(fadeAnim, {
        toValue: 1,
        duration: 400,
        useNativeDriver: true,
      }),
    ]).start();

    // Start pulsing animation
    startPulse();
  }, []);

  // Pulsing animation for live indicator
  const startPulse = () => {
    Animated.loop(
      Animated.sequence([
        Animated.timing(pulseAnim, {
          toValue: 1.3,
          duration: 1000,
          easing: Easing.inOut(Easing.ease),
          useNativeDriver: true,
        }),
        Animated.timing(pulseAnim, {
          toValue: 1,
          duration: 1000,
          easing: Easing.inOut(Easing.ease),
          useNativeDriver: true,
        }),
      ])
    ).start();
  };

  // Image rotation logic
  useEffect(() => {
    if (!autoPlay || displayImages.length === 0) return;

    const interval = setInterval(() => {
      // Fade out current image
      Animated.timing(imageFadeAnim, {
        toValue: 0,
        duration: 200,
        useNativeDriver: true,
      }).start(() => {
        // Change image
        setCurrentIndex((prevIndex) => {
          const nextIndex = (prevIndex + 1) % displayImages.length;
          
          // If we've shown all images, hide popup for 1 second
          if (nextIndex === 0 && displayImages.length > 1) {
            setShowPopup(false);
            setTimeout(() => {
              setShowPopup(true);
            }, 1000);
          }
          
          return nextIndex;
        });
        
        // Fade in new image
        Animated.timing(imageFadeAnim, {
          toValue: 1,
          duration: 200,
          useNativeDriver: true,
        }).start();
      });
    }, 3000); // Change image every 3 seconds

    return () => clearInterval(interval);
  }, [autoPlay, displayImages.length]);

  // Fade animation when showing/hiding popup
  useEffect(() => {
    Animated.timing(fadeAnim, {
      toValue: showPopup ? 1 : 0,
      duration: 300,
      useNativeDriver: true,
    }).start();
  }, [showPopup]);

  console.log('🎨 LiveTrainingPin rendering:', {
    imagesCount: images.length,
    currentIndex,
    showPopup,
    displayImages: displayImages.length
  });

  return (
    <Animated.View
      style={[
        styles.container,
        {
          width: size,
          height: size + pointerHeight,
          opacity: fadeAnim,
          transform: [{ scale: scaleAnim }],
        },
      ]}
    >
      {/* Pulsing outer ring centered behind the image */}
      <View style={styles.ringOverlay} pointerEvents="none">
        <Animated.View
          style={[
            styles.pulseRing,
            {
              width: ringSize,
              height: ringSize,
              borderRadius: ringSize / 2,
              transform: [{ scale: pulseAnim }],
            },
          ]}
        />
      </View>

      {/* Image popup */}
      {showPopup && (
        <Animated.View
          style={[
            styles.imagePopup,
            { opacity: fadeAnim, width: size, aspectRatio: 1 },
          ]}
        >
          <Animated.Image
            source={{ uri: displayImages[currentIndex] }}
            style={[styles.image, { opacity: imageFadeAnim }]}
            resizeMode="contain"
          />
          <View style={styles.liveIndicator}>
            <View style={styles.liveDot} />
            <Text style={styles.liveText}>LIVE</Text>
          </View>
        </Animated.View>
      )}

      {/* Pin pointer */}
      <View
        style={[
          styles.pinPointer,
          {
            borderLeftWidth: triangleBase,
            borderRightWidth: triangleBase,
            borderTopWidth: pointerHeight,
          },
        ]}
      />
    </Animated.View>
  );
};

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
    justifyContent: 'flex-start',
    overflow: 'visible',
  },
  pulseRing: {
    position: 'absolute',
    backgroundColor: 'rgba(16, 185, 129, 0.2)',
    borderWidth: 4,
    borderColor: 'rgba(16, 185, 129, 0.6)',
    zIndex: 0,
  },
  ringOverlay: {
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    justifyContent: 'center',
    alignItems: 'center',
  },
  imagePopup: {
    borderRadius: 15,
    backgroundColor: '#FFFFFF',
    padding: 8,
    borderWidth: 4,
    borderColor: '#10B981',
    overflow: 'hidden',
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 6 },
    shadowOpacity: 0.4,
    shadowRadius: 8,
    elevation: 12,
    zIndex: 1,
  },
  image: {
    width: '100%',
    height: '100%',
    backgroundColor: '#F7FAFC',
  },
  liveIndicator: {
    position: 'absolute',
    top: 8,
    right: 8,
    backgroundColor: '#EF4444',
    borderRadius: 12,
    paddingHorizontal: 8,
    paddingVertical: 4,
    flexDirection: 'row',
    alignItems: 'center',
  },
  liveDot: {
    width: 8,
    height: 8,
    borderRadius: 4,
    backgroundColor: '#FFFFFF',
  },
  liveText: {
    color: '#FFFFFF',
    fontSize: 10,
    fontWeight: 'bold',
    marginLeft: 4,
  },
  pinPointer: {
    width: 0,
    height: 0,
    borderLeftColor: 'transparent',
    borderRightColor: 'transparent',
    borderTopColor: '#10B981',
    alignSelf: 'center',
    marginTop: 2,
  },
});

export default LiveTrainingPin;