grade 쿼리파라미터를 생략하면 전체 학년 통합 랭킹을 주도록 백엔드를 바꾸고, 랭킹 화면에 "N학년"/"전체랭킹" 필 필터를 추가해 시간대 필터(오늘/이번주/전체)와 별도로 고를 수 있게 했다. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
56 lines
1.5 KiB
Dart
56 lines
1.5 KiB
Dart
// 🏆 학년별 공부시간 랭킹 화면의 기능(서버 통신/상태) 담당 컨트롤러.
|
|
import 'dart:convert';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import '../config.dart';
|
|
|
|
class StudyRankingController extends ChangeNotifier {
|
|
final int grade;
|
|
|
|
StudyRankingController({required this.grade});
|
|
|
|
bool _isLoading = true;
|
|
String _period = 'today'; // today / week / total
|
|
String _scope = 'grade'; // grade / all
|
|
List<dynamic> _ranking = [];
|
|
|
|
bool get isLoading => _isLoading;
|
|
String get period => _period;
|
|
String get scope => _scope;
|
|
List<dynamic> get ranking => _ranking;
|
|
|
|
Future<void> init() => fetchRanking();
|
|
|
|
Future<void> setPeriod(String period) async {
|
|
if (_period == period) return;
|
|
_period = period;
|
|
await fetchRanking();
|
|
}
|
|
|
|
Future<void> setScope(String scope) async {
|
|
if (_scope == scope) return;
|
|
_scope = scope;
|
|
await fetchRanking();
|
|
}
|
|
|
|
Future<void> fetchRanking() async {
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
try {
|
|
final gradeParam = _scope == 'grade' ? '&grade=$grade' : '';
|
|
final response = await http.get(
|
|
Uri.parse('$baseUrl/api/study/ranking?period=$_period$gradeParam'),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
|
_ranking = data['ranking'] ?? [];
|
|
}
|
|
} catch (_) {
|
|
// 조용히 무시 - 마지막으로 받아온 랭킹을 유지한다.
|
|
} finally {
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
}
|