Skip to content

Commit

Permalink
refactor: 남은 시간 계산 함수 분리
Browse files Browse the repository at this point in the history
* util 파일로 분리하고 여러 곳에서 이용 가능하도록 리팩토링
  • Loading branch information
jcw1031 committed Sep 17, 2024
1 parent b90548c commit 0d3b753
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
20 changes: 1 addition & 19 deletions app/bus/timetable/components/BusArrivalTimeListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import FontText from '@/common/text/FontText';
import colors, { campusColors } from '@/constants/colors';
import { BusArrivalProps } from '@/types/busTypes';
import { hexToRgba } from '@/utils/color';
import { calculateTimeRemaining } from '@/utils/date';
import { useEffect, useRef } from 'react';
import { Animated, StyleSheet, View } from 'react-native';
import AnimatedValue = Animated.AnimatedValue;
Expand All @@ -19,25 +20,6 @@ const BusArrivalTimeListItem = ({ arrivalBus, isNext }: BusArrivalTimeListItemPr
const { campus } = useCampus();
const animatedBackgroundOpacity = useRef<AnimatedValue>(new Animated.Value(0)).current;

const calculateTimeRemaining = (arrivalTime: string): string => {
const now = new Date();
const [hours, minutes] = arrivalTime.split(':').map(Number);
const arrival = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes);
if (arrival < now) {
return '';
}
const diffInMinutes = Math.round((arrival.getTime() - now.getTime()) / 60000);
if (diffInMinutes < 1) {
return '도착';
} else if (diffInMinutes < 60) {
return `${diffInMinutes}분 후`;
} else {
const hours = Math.floor(diffInMinutes / 60);
const minutes = diffInMinutes % 60;
return `${hours}시간 ${minutes}분 후`;
}
};

useEffect(() => {
if (isNext) {
const animation = Animated.sequence([
Expand Down
4 changes: 3 additions & 1 deletion app/bus/timetable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import PageLayout from '@/common/components/PageLayout';
import { useTheme } from '@/common/contexts/ThemeContext';
import colors from '@/constants/colors';
import { CityBusRouteProps } from '@/types/busTypes';
import { calculateTimeRemaining } from '@/utils/date';
import {
BottomSheetBackdrop,
BottomSheetBackdropProps,
Expand Down Expand Up @@ -37,7 +38,7 @@ const CityBusTimetablePage = () => {
), []);

const busRouteListItem = useCallback((item: CityBusRouteProps) => {
const time = item.nextArrivalTime && item.nextArrivalTime.slice(0, -3);
const time = item.nextArrivalTime && calculateTimeRemaining(item.nextArrivalTime);
return (
<BusRouteListItem
origin={item.origin}
Expand All @@ -60,6 +61,7 @@ const CityBusTimetablePage = () => {
showsVerticalScrollIndicator={false}
refreshControl={
<RefreshControl
tintColor={colors[theme].gray100}
refreshing={isLoading}
onRefresh={mutate}
/>
Expand Down
19 changes: 19 additions & 0 deletions utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,22 @@ export const determineTimeStatus = (timeRange: string): OperatingType => {
return OperatingStatus.OPERATE as OperatingType;
}
};

export const calculateTimeRemaining = (arrivalTime: string): string => {
const now = new Date();
const [hours, minutes] = arrivalTime.split(':').map(Number);
const arrival = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes);
if (arrival < now) {
return '';
}
const diffInMinutes = Math.round((arrival.getTime() - now.getTime()) / 60000);
if (diffInMinutes < 1) {
return '곧 도착';
} else if (diffInMinutes < 60) {
return `${diffInMinutes}분 후`;
} else {
const hours = Math.floor(diffInMinutes / 60);
const minutes = diffInMinutes % 60;
return `${hours}시간 ${minutes}분 후`;
}
};

0 comments on commit 0d3b753

Please sign in to comment.