Skip to content

Commit

Permalink
bus history by bus number implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian0KIM committed Dec 2, 2024
1 parent e471ddf commit 42c97d8
Showing 1 changed file with 122 additions and 6 deletions.
128 changes: 122 additions & 6 deletions front/lib/bus_history_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,64 @@ class BusHistoryPage extends StatefulWidget {
class _BusHistoryPageState extends State<BusHistoryPage> {
bool isLoading = false;
Map<int, List<String>> groupedData = {};
final Map<int, ScrollController> _scrollControllers = {};

@override
void initState() {
super.initState();
fetchBusHistory();
}
@override
void dispose() {
// ScrollController 해제
for (var controller in _scrollControllers.values) {
controller.dispose();
}
super.dispose();
}
void _scrollToCurrentTime(int daysAgo, List<String> times) {
if (times.isEmpty) return;

final now = DateTime.now();
final currentTimeString = "${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}";

// 현재 시간과 가장 가까운 인덱스 찾기
int closestIndex = 0;
int minDifference = double.maxFinite.toInt();

for (int i = 0; i < times.length; i++) {
final difference = _getTimeDifference(currentTimeString, times[i]);
if (difference.abs() < minDifference) {
minDifference = difference.abs();
closestIndex = i;
}
}

// ScrollController가 없으면 생성
if (!_scrollControllers.containsKey(daysAgo)) {
_scrollControllers[daysAgo] = ScrollController();
}

// 스크롤 위치 조정
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_scrollControllers[daysAgo]!.hasClients) {
_scrollControllers[daysAgo]!.animateTo(
closestIndex * 56.0, // ListTile의 대략적인 높이
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
}
});
}
int _getTimeDifference(String time1, String time2) {
final t1Parts = time1.split(':');
final t2Parts = time2.split(':');

final t1Minutes = int.parse(t1Parts[0]) * 60 + int.parse(t1Parts[1]);
final t2Minutes = int.parse(t2Parts[0]) * 60 + int.parse(t2Parts[1]);

return t2Minutes - t1Minutes;
}
Future<void> fetchBusHistory() async {
setState(() {
isLoading = true;
Expand Down Expand Up @@ -64,6 +115,9 @@ class _BusHistoryPageState extends State<BusHistoryPage> {

setState(() {
groupedData = tempGroupedData;
groupedData.forEach((daysAgo, times) {
_scrollToCurrentTime(daysAgo, times);
});
});
}
}
Expand Down Expand Up @@ -95,11 +149,23 @@ class _BusHistoryPageState extends State<BusHistoryPage> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"${widget.stationName}(${widget.busNumber})",
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
RichText(
text: TextSpan(
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black, // 기본 텍스트 색상
),
children: [
TextSpan(text: widget.stationName),
TextSpan(
text: " ${widget.busNumber}",
style: TextStyle(
color: getBusColor(widget.busNumber),
),
),
const TextSpan(text: "번 버스"),
],
),
),
const Text(
Expand All @@ -109,6 +175,13 @@ class _BusHistoryPageState extends State<BusHistoryPage> {
fontSize: 14,
),
),
Text(
_getDateMessage(), // 요일별 안내 메시지 추가
style: const TextStyle(
color: Colors.grey,
fontSize: 12,
),
),
],
),
),
Expand Down Expand Up @@ -139,12 +212,13 @@ class _BusHistoryPageState extends State<BusHistoryPage> {
const Divider(height: 1),
Expanded(
child: ListView.separated(
controller: _scrollControllers[entry.key],
padding: EdgeInsets.zero,
itemCount: entry.value.length,
separatorBuilder: (context, index) => const Divider(height: 1),
itemBuilder: (context, index) {
return ListTile(
leading: const Icon(Icons.check),
leading: Icon(Icons.directions_bus, color: getBusColor(widget.busNumber)),
title: Text(
entry.value[index],
style: const TextStyle(
Expand All @@ -167,4 +241,46 @@ class _BusHistoryPageState extends State<BusHistoryPage> {
),
);
}
}


Color getBusColor(String routeNumber) {
switch (routeNumber) {
case "9":
return const Color(0xff33CC99); // 지선버스
case "1112":
return const Color(0xffE60012); // 지선버스
case "5100":
return const Color(0xffE60012); // 광역버스
case "7000":
return const Color(0xffE60012); // 광역버스
case "M5107":
return const Color(0xff006896); // M버스
case "1560A":
return const Color(0xffE60012); // 지선버스
case "1560B":
return const Color(0xffE60012); // 지선버스
default:
return Colors.black;
}
}
String _getDateMessage() {
final today = DateTime.now();
final weekday = today.weekday; // 1 = 월요일, 7 = 일요일

switch (weekday) {
case 1: // 월요일
return 'ⓘ 월요일: 3일전, 7일전 도착 기록을 제공합니다.';
case 2: // 화요일
return 'ⓘ 화요일: 1일전, 7일전 도착 기록을 제공합니다.';
case 3: // 수요일
case 4: // 목요일
case 5: // 금요일
return 'ⓘ 수요일, 목요일, 금요일: 1일전, 2일전, 7일전 도착 기록을 제공합니다.';
case 6: // 토요일
case 7: // 일요일
return 'ⓘ 토요일, 일요일: 7일전 도착 기록을 제공합니다.';
default:
return 'ⓘ 7일전 도착 기록을 제공합니다.';
}
}

0 comments on commit 42c97d8

Please sign in to comment.