feat:增加加载中组件
This commit is contained in:
@@ -1,20 +1,134 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:food_hub_app/apis/recipe.dart';
|
||||
import 'package:food_hub_app/apis/stats.dart';
|
||||
import 'package:food_hub_app/models/recipe.dart';
|
||||
import 'package:food_hub_app/models/stats.dart';
|
||||
import 'package:food_hub_app/utils/date_util.dart';
|
||||
import 'package:food_hub_app/utils/index.dart';
|
||||
|
||||
class FoodProvider with ChangeNotifier {
|
||||
late FoodRecord _recordFormItem;
|
||||
late FoodRecord recordFormItem;
|
||||
|
||||
late bool isEditing;
|
||||
bool isLoading = false;
|
||||
String? error;
|
||||
|
||||
FoodRecord get recordFormItem => _recordFormItem;
|
||||
late List<RecipeSummary> recipeSummaryList = [];
|
||||
|
||||
DateTime selectedDay = DateTime.now();
|
||||
DateTime focusedDay = DateTime.now();
|
||||
|
||||
late List<FoodRecord> recordList = [];
|
||||
late List<FoodRecord> selectRecordList = [];
|
||||
|
||||
late SummaryStats summaryStats = SummaryStats(
|
||||
recipeCount: 0,
|
||||
categoryCount: 0,
|
||||
workCount: 0,
|
||||
);
|
||||
late double averageRecordCount = 0;
|
||||
late List<ChartData> recordStats = [];
|
||||
late List<ChartData> categoryStats = [];
|
||||
late List<ChartData> rankStats = [];
|
||||
|
||||
void resetRecordForm() {
|
||||
_recordFormItem = FoodRecord.getEmpty();
|
||||
recordFormItem = FoodRecord.getEmpty();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void initRecordForm(FoodRecord record) {
|
||||
_recordFormItem = record;
|
||||
recordFormItem = record;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> refreshRecipeList() async {
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
notifyListeners();
|
||||
|
||||
final result = await queryRecipeApi(RecipeQuery(category: ""));
|
||||
recipeSummaryList = result;
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshRecordList(String? startDate, String? endDate) async {
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
notifyListeners();
|
||||
|
||||
startDate ??= getFirstDayOfMonth(focusedDay);
|
||||
endDate ??= getLastDayOfMonth(focusedDay);
|
||||
|
||||
final result = await queryRecordApi(startDate, endDate);
|
||||
|
||||
recordList = result;
|
||||
|
||||
if (recordList.isNotEmpty) {
|
||||
selectedDay = DateTime.parse(recordList.last.date);
|
||||
} else {
|
||||
selectedDay = focusedDay;
|
||||
}
|
||||
|
||||
refreshSelectRecord();
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void refreshSelectRecord() {
|
||||
selectRecordList =
|
||||
recordList
|
||||
.where((record) => record.date == formatDateTime(selectedDay))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> refreshBlogStats() async {
|
||||
if (isLoading) return;
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
notifyListeners();
|
||||
|
||||
final statsResult = await queryStatsApi();
|
||||
final recordStatsResult = await queryRecordStatsApi();
|
||||
final categoryStatsResult = await queryCategoryStatsApi();
|
||||
final rankStatsResult = await queryRankStatsApi();
|
||||
|
||||
summaryStats = statsResult;
|
||||
recordStats = recordStatsResult;
|
||||
categoryStats = categoryStatsResult;
|
||||
rankStats = rankStatsResult;
|
||||
|
||||
if (recordStats.isNotEmpty) {
|
||||
double sumValue = recordStats.fold(
|
||||
0.0,
|
||||
(sum, item) => sum + item.value,
|
||||
);
|
||||
averageRecordCount = sumValue / recordStats.length;
|
||||
}
|
||||
} catch (e) {
|
||||
error = '加载数据失败: $e';
|
||||
debugPrint('加载数据失败: $e');
|
||||
} finally {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user