Currently there are two files EditSchedule and rectangle. I want the text to update appropriate when i drag the rectangle down. As in when the bottom edge of the rectangle goes down the end time increases. If the bottom edge goes up the end time decreases. Is this a correct implementation
import React, { useState } from "react";
import { View, StyleSheet, Text } from "react-native";
import moment from "moment";
import Timetable from "react-native-calendar-timetable";
import Rectangle from "../common/rectanglev2";
const CALENDAR_WIDTH = 807;
const CALENDAR_HEIGHT = 670;
// Calculate time change in minutes
const heightDiff = newHeight - initialHeight;
const minutesChange = Math.round(heightDiff);
// Call the onUpdateEndTime function with the new end time
runOnJS(onUpdateEndTime)(dayIndex, minutesChange);
})
Based on your description, you want users to adjust the event's end time by dragging the bottom edge of the rectangle up or down. Currently, your implementation uses a pinch gesture to resize the rectangle and updates the end time accordingly. However, this doesn't match the interaction pattern you're aiming for.
Issues with the Current Implementation:
Gesture Handling:
You're using a pinch gesture (Gesture.Pinch()) to adjust the height of the rectangle. Pinch gestures are typically associated with two-finger zooming, not dragging.
The pan gesture (Gesture.Pan()) is only logging the translation but not affecting the rectangle's height or the event's end time.
User Experience:
Users might find it unintuitive to use a pinch gesture to adjust the end time. They typically expect to drag the bottom edge to change the duration of an event.
Recommendations:
Use Pan Gesture for Resizing:
Replace the pinch gesture with a pan gesture specifically for resizing the rectangle from the bottom edge.
Detect when the user is dragging the bottom edge and adjust the height accordingly.
Update Event End Time Based on Pan Gesture:
Calculate the change in height during the pan gesture.
Convert the height change to a time change (e.g., pixels to minutes).
Update the event's end time in your state.
Adjust the Animated Styles:
Use the translateY value to adjust the height of the rectangle dynamically as the user drags.
Ensure that the rectangle doesn't shrink beyond a minimum size or extend beyond the timetable's bounds.
Revised Implementation:
Here's how you can modify your Rectangle component to use the pan gesture for resizing:
jsx
import React from 'react';
import Animated, { useAnimatedStyle, useSharedValue, runOnJS } from 'react-native-reanimated';
import { GestureDetector, Gesture } from 'react-native-gesture-handler';
import { View, Text } from 'react-native';
const Rectangle = ({ style, item, dayIndex, onUpdateEndTime, event }) => {
const initialHeight = style.height;
const animatedHeight = useSharedValue(initialHeight);
const panGesture = Gesture.Pan()
.onUpdate((event) => {
// We only want vertical dragging
const newHeight = animatedHeight.value + event.changeY;
// Set limits to prevent negative height
if (newHeight >= 0) {
animatedHeight.value = newHeight;
const heightDiff = newHeight - initialHeight;
const minutesChange = Math.round(heightDiff / PIXELS_PER_MINUTE);
runOnJS(onUpdateEndTime)(dayIndex, minutesChange);
}
})
.onEnd(() => {
// Reset the gesture handler
});
const animatedStyle = useAnimatedStyle(() => {
return {
height: animatedHeight.value,
};
});
// ... rest of your component code
};
Notes:
You'll need to define PIXELS_PER_MINUTE based on your timetable's scaling to accurately convert pixel changes to minutes.
Ensure that animatedHeight.value doesn't become negative or exceed the timetable's maximum height.
Remove the pinch gesture unless you have another use for it.
Update the onUpdateEndTime Function:
Ensure your onUpdateEndTime function in EditSchedule properly handles the minutes change:
jsx
const handleUpdateEndTime = (dayIndex, minutesChange) => {
setEvents(prevEvents => {
const currDay = prevEvents[dayIndex];
const { hour, minute } = currDay.end;
const date = new Date(2023, 0, 1, hour, minute);
date.setMinutes(date.getMinutes() + minutesChange);
// Prevent the end time from being before the start time
const { hour: startHour, minute: startMinute } = currDay.start;
const startDate = new Date(2023, 0, 1, startHour, startMinute);
if (date < startDate) {
date.setTime(startDate.getTime()); // Set end time equal to start time
}
return {
...prevEvents,
[dayIndex]: {
...currDay,
end: {
hour: date.getHours(),
minute: date.getMinutes()
}
}
};
});
};
Conclusion:
Your current implementation doesn't fully align with the interaction you described. By adjusting the Rectangle component to use the pan gesture for resizing via dragging the bottom edge, you'll provide a more intuitive experience that matches your requirements.
Feel free to ask if you need further assistance with the implementation!