feat:增加加载中显示
This commit is contained in:
204
lib/provider/blog.dart
Normal file
204
lib/provider/blog.dart
Normal file
@@ -0,0 +1,204 @@
|
||||
import 'package:blog_app/models/common.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:blog_app/apis/blog.dart';
|
||||
import 'package:blog_app/models/blog.dart';
|
||||
|
||||
class BlogProvider with ChangeNotifier {
|
||||
List<Blog> blogList = [];
|
||||
List<BlogCategory> categoryList = [];
|
||||
Blog currentBlog = Blog(
|
||||
id: 0,
|
||||
title: '',
|
||||
topValue: 0,
|
||||
isGreat: false,
|
||||
category: '',
|
||||
summary: '',
|
||||
content: '',
|
||||
wordCount: 0,
|
||||
readDuration: 0,
|
||||
visitCount: 0,
|
||||
createTime: '',
|
||||
updateTime: '',
|
||||
);
|
||||
int currentPage = 1;
|
||||
final int pageSize = 5;
|
||||
bool hasMore = true;
|
||||
bool isLoading = false;
|
||||
int currentYear = DateTime.now().year;
|
||||
String? error;
|
||||
|
||||
late List<ChartData> approvedStats = [];
|
||||
late List<ChartData> visitStats = [];
|
||||
late List<ChartData> visitRankStats = [];
|
||||
late List<ChartData> categoryStats = [];
|
||||
late List<ChartData> readRankStats = [];
|
||||
late BlogStats overviewStats = BlogStats(
|
||||
blogCount: 0,
|
||||
categoryCount: 0,
|
||||
greatCount: 0,
|
||||
visitCount: 0,
|
||||
wordCount: 0,
|
||||
);
|
||||
|
||||
Future<void> queryBlogByPage({bool isRefresh = true}) async {
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
notifyListeners();
|
||||
|
||||
// 如果是刷新,重置页码
|
||||
if (isRefresh) {
|
||||
currentPage = 1;
|
||||
}
|
||||
|
||||
final result = await queryBlogByPageApi(currentPage, pageSize);
|
||||
|
||||
// 更新数据
|
||||
if (isRefresh) {
|
||||
blogList = result.records;
|
||||
} else {
|
||||
blogList.addAll(result.records);
|
||||
}
|
||||
|
||||
// 判断是否还有更多数据
|
||||
hasMore = result.current < result.pages;
|
||||
if (hasMore) {
|
||||
currentPage++;
|
||||
}
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshBlogCategory() async {
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
categoryList = [];
|
||||
notifyListeners();
|
||||
|
||||
final result = await queryBlogCategoryApi();
|
||||
categoryList = result;
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshBlogStats() async {
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
notifyListeners();
|
||||
|
||||
final approvedResult = await queryBlogApprovedStatsApi(currentYear);
|
||||
final visitResult = await queryBlogVisitStatsApi(currentYear);
|
||||
final visitRankResult = await queryBlogVisitRankStatsApi();
|
||||
final categoryResult = await queryBlogCategoryStatsApi();
|
||||
final readRankResult = await queryBlogReadRankStatsApi();
|
||||
final overviewResult = await queryBlogOverviewStatsApi();
|
||||
|
||||
approvedStats = approvedResult;
|
||||
visitStats = visitResult;
|
||||
visitRankStats = visitRankResult;
|
||||
categoryStats = categoryResult;
|
||||
readRankStats = readRankResult;
|
||||
overviewStats = overviewResult;
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshBlogByCategory(String category) async {
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
blogList = [];
|
||||
notifyListeners();
|
||||
|
||||
final result = await queryBlogByConditionApi(category, null, null);
|
||||
blogList = result;
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshBlogByYear() async {
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
blogList = [];
|
||||
notifyListeners();
|
||||
|
||||
final result = await queryBlogByConditionApi(null, null, currentYear);
|
||||
blogList = result;
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshBlogList() async {
|
||||
await queryBlogByPage(isRefresh: true);
|
||||
}
|
||||
|
||||
Future<void> queryBlogById(int blogId) async {
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
notifyListeners();
|
||||
|
||||
final result = await queryBlogByIdApi(blogId);
|
||||
currentBlog = result;
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
// 加载更多
|
||||
Future<void> loadMoreBlogList() async {
|
||||
if (hasMore && !isLoading) {
|
||||
await queryBlogByPage(isRefresh: false);
|
||||
}
|
||||
}
|
||||
|
||||
// 清除错误
|
||||
void clearError() {
|
||||
error = null;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user