api实现
This commit is contained in:
61
lib/src/controllers/feed/hot_controller.dart
Normal file
61
lib/src/controllers/feed/hot_controller.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
// 2026-04-09 | HotController | 热门排行控制器 | 管理今日/本月/累计排行
|
||||
import 'package:get/get.dart';
|
||||
import 'package:mom_kitchen/src/controllers/base/base_controller.dart';
|
||||
import 'package:mom_kitchen/src/models/recipe/recipe_model.dart';
|
||||
import 'package:mom_kitchen/src/repositories/hot_repository.dart' as repo;
|
||||
|
||||
enum HotPeriod { today, month, total }
|
||||
|
||||
class HotController extends BaseController {
|
||||
final repo.HotRepository _hotRepository = repo.HotRepository();
|
||||
|
||||
final Rx<HotPeriod> currentPeriod = Rx<HotPeriod>(HotPeriod.today);
|
||||
final Rx<List<RecipeModel>> hotList = Rx<List<RecipeModel>>([]);
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
loadHot();
|
||||
}
|
||||
|
||||
void switchPeriod(HotPeriod period) {
|
||||
if (currentPeriod.value == period) return;
|
||||
currentPeriod.value = period;
|
||||
loadHot();
|
||||
}
|
||||
|
||||
Future<void> loadHot() async {
|
||||
await runWithLoading(() async {
|
||||
try {
|
||||
List<RecipeModel> result;
|
||||
switch (currentPeriod.value) {
|
||||
case HotPeriod.today:
|
||||
result = await _hotRepository.fetchToday();
|
||||
break;
|
||||
case HotPeriod.month:
|
||||
result = await _hotRepository.fetchMonth();
|
||||
break;
|
||||
case HotPeriod.total:
|
||||
result = await _hotRepository.fetchTotal();
|
||||
break;
|
||||
}
|
||||
hotList.value = result;
|
||||
} catch (e) {
|
||||
errorMessage.value = e.toString();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
String get periodName {
|
||||
switch (currentPeriod.value) {
|
||||
case HotPeriod.today:
|
||||
return '今日';
|
||||
case HotPeriod.month:
|
||||
return '本月';
|
||||
case HotPeriod.total:
|
||||
return '累计';
|
||||
}
|
||||
}
|
||||
|
||||
static List<String> get periodNames => ['今日', '本月', '累计'];
|
||||
}
|
||||
Reference in New Issue
Block a user