익명 커뮤니티 게시판(백판) 기능 추가
디시인사이드식 고정 카테고리 게시판 + 에타 스타일 익명(글 안에서만 유효한 익명 번호) 댓글. 신고는 자동 삭제 없이 운영자(교사/관리자) 검토 후 처리하도록 별도 신고 검토 화면을 대시보드에 추가. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
// 🗣️ 커뮤니티 게시판 - 카테고리별 게시글 목록 화면의 기능(서버 통신/상태) 담당 컨트롤러.
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import '../config.dart';
|
||||
|
||||
class BoardListController extends ChangeNotifier {
|
||||
final String category;
|
||||
final String studentId;
|
||||
|
||||
BoardListController({required this.category, required this.studentId});
|
||||
|
||||
bool _isLoading = true;
|
||||
bool _isLoadingMore = false;
|
||||
bool _hasMore = true;
|
||||
int _page = 1;
|
||||
List<dynamic> _posts = [];
|
||||
bool _disposed = false;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
bool get isLoadingMore => _isLoadingMore;
|
||||
bool get hasMore => _hasMore;
|
||||
List<dynamic> get posts => _posts;
|
||||
|
||||
static const int _pageSize = 20;
|
||||
|
||||
void _safeNotify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> init() => refresh();
|
||||
|
||||
Future<void> refresh() async {
|
||||
_isLoading = true;
|
||||
_page = 1;
|
||||
_hasMore = true;
|
||||
_safeNotify();
|
||||
await _fetchPage(replace: true);
|
||||
_isLoading = false;
|
||||
_safeNotify();
|
||||
}
|
||||
|
||||
Future<void> loadMore() async {
|
||||
if (_isLoadingMore || !_hasMore || _isLoading) return;
|
||||
_isLoadingMore = true;
|
||||
_safeNotify();
|
||||
_page += 1;
|
||||
await _fetchPage(replace: false);
|
||||
_isLoadingMore = false;
|
||||
_safeNotify();
|
||||
}
|
||||
|
||||
Future<void> _fetchPage({required bool replace}) async {
|
||||
try {
|
||||
final response = await http.get(
|
||||
Uri.parse(
|
||||
'$baseUrl/api/board/posts?category=$category&studentId=$studentId&page=$_page',
|
||||
),
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
final fetched = (data['posts'] as List<dynamic>?) ?? [];
|
||||
_posts = replace ? fetched : [..._posts, ...fetched];
|
||||
_hasMore = fetched.length >= _pageSize;
|
||||
}
|
||||
} catch (_) {
|
||||
// 조용히 무시 - 마지막으로 받아온 목록을 유지한다.
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user