calendar_controller.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import 'package:clean/module/calendar/calendar_state.dart';
  2. import 'package:clean/module/calendar/preview/calendar_preview_view.dart';
  3. import 'package:clean/module/people_photo/photo_group.dart';
  4. import 'package:flutter/Material.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter_screenutil/flutter_screenutil.dart';
  7. import 'package:intl/intl.dart';
  8. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  9. import '../../base/base_controller.dart';
  10. import 'package:get/get.dart';
  11. import '../image_picker/image_picker_util.dart';
  12. import 'calendar_month_view.dart';
  13. class CalendarController extends BaseController {
  14. final RxList<PhotoGroup> monthlyAlbums = CalendarState.monthlyAlbums;
  15. final RxList<AssetEntity> imageList = <AssetEntity>[].obs;
  16. final Rx<SortType> currentSortType = SortType.latest.obs; // 当前排序类型
  17. @override
  18. void onInit() {
  19. super.onInit();
  20. loadAssets();
  21. }
  22. // 加载并分组图片
  23. Future<void> loadAssets() async {
  24. final List<AssetEntity> result = await ImagePickerUtil.loadAssets();
  25. result.sort((a, b) => b.createDateTime.compareTo(a.createDateTime));
  26. imageList.value = result;
  27. updateMonthlyAssets();
  28. }
  29. // 更新按月份分组的照片
  30. void updateMonthlyAssets() {
  31. Map<String, List<AssetEntity>> groupedAssets = {};
  32. // 按月份分组照片
  33. for (var asset in imageList) {
  34. final monthKey = DateFormat('yyyy-MM').format(asset.createDateTime);
  35. groupedAssets.putIfAbsent(monthKey, () => []).add(asset);
  36. }
  37. monthlyAlbums.clear();
  38. groupedAssets.forEach((month, assets) {
  39. assets
  40. .sort((a, b) => b.createDateTime.compareTo(a.createDateTime)); // 时间排序
  41. monthlyAlbums
  42. .add(PhotoGroup(month: month, images: assets, isSelected: false));
  43. });
  44. // 计算
  45. for (var album in monthlyAlbums) {
  46. album.initTotalSize();
  47. }
  48. // 打印结果
  49. for (var album in monthlyAlbums) {
  50. debugPrint(
  51. 'CalendarController month: ${album.month}, images: ${album.images.length} ,totalSize: ${album.totalSize}');
  52. }
  53. }
  54. // 点击月份卡片,跳转到对应月份的照片详情页
  55. void clickMonthCard(PhotoGroup photoGroup) {
  56. print("clickMonthCard photoGroup: ${photoGroup.totalSize}");
  57. CalendarMonthPage.start(photoGroup: photoGroup);
  58. }
  59. // 点击图片,查看该图片详情
  60. void clickImage(PhotoGroup photoGroup, int imageIndex) {
  61. print("CalendarController clickImage");
  62. CalendarPreviewPage.start(
  63. photoGroup: photoGroup,
  64. currentImageId: photoGroup.images[imageIndex].id);
  65. }
  66. // 排序照片列表
  67. void sortAssets(SortType sortType) {
  68. currentSortType.value = sortType; // 更新当前排序类型
  69. switch (sortType) {
  70. case SortType.latest:
  71. // 按月份倒序排列
  72. monthlyAlbums.sort((a, b) => b.month!.compareTo(a.month!));
  73. break;
  74. case SortType.oldest:
  75. // 按月份正序排列
  76. monthlyAlbums.sort((a, b) => a.month!.compareTo(b.month!));
  77. break;
  78. case SortType.largeSize:
  79. for (var album in monthlyAlbums) {
  80. print(
  81. "CalendarController album: ${album.month}, images: ${album.images.length} ,totalSize: ${album.totalSize}");
  82. album.initTotalSize();
  83. }
  84. monthlyAlbums
  85. .sort((a, b) => b.totalSize.value.compareTo(a.totalSize.value));
  86. break;
  87. case SortType.mostQuantity:
  88. // 按照片数量排序
  89. monthlyAlbums
  90. .sort((a, b) => b.images.length.compareTo(a.images.length));
  91. break;
  92. }
  93. }
  94. // 显示排序弹窗
  95. void clickSort() {
  96. showCupertinoModalPopup(
  97. context: Get.context!,
  98. builder: (context) {
  99. return Container(
  100. width: 360.w,
  101. decoration: ShapeDecoration(
  102. color: Color(0xFF131C27),
  103. shape: RoundedRectangleBorder(
  104. borderRadius: BorderRadius.only(
  105. topLeft: Radius.circular(44.r),
  106. topRight: Radius.circular(44.r),
  107. ),
  108. ),
  109. ),
  110. child: Column(
  111. mainAxisSize: MainAxisSize.min,
  112. children: [
  113. SizedBox(height: 10.h),
  114. Container(
  115. width: 57.w,
  116. height: 4.h,
  117. decoration: ShapeDecoration(
  118. color: Color(0xFF242D38),
  119. shape: RoundedRectangleBorder(
  120. borderRadius: BorderRadius.circular(2)),
  121. ),
  122. ),
  123. SizedBox(height: 19.h),
  124. Text(
  125. 'Sort By',
  126. textAlign: TextAlign.center,
  127. style: TextStyle(
  128. color: Colors.white,
  129. fontSize: 16.sp,
  130. fontWeight: FontWeight.w700,
  131. ),
  132. ),
  133. SizedBox(height: 14.h),
  134. // Latest
  135. sortOption(SortType.latest, 'Latest'),
  136. SizedBox(height: 14.h),
  137. // Oldest
  138. sortOption(SortType.oldest, 'Oldest'),
  139. SizedBox(height: 14.h),
  140. // Large Size
  141. sortOption(SortType.largeSize, 'Largest Storage Usage'),
  142. SizedBox(height: 14.h),
  143. // Most Quantity
  144. sortOption(SortType.mostQuantity, 'Most Quantity'),
  145. SizedBox(height: 55.h),
  146. ],
  147. ),
  148. );
  149. },
  150. );
  151. }
  152. Widget sortOption(SortType type, String title) {
  153. bool isSelected = currentSortType.value == type; // 判断是否是选中的排序类型
  154. return GestureDetector(
  155. onTap: () {
  156. Navigator.pop(Get.context!);
  157. sortAssets(type);
  158. },
  159. child: Container(
  160. width: 300.w,
  161. height: 50.h,
  162. alignment: Alignment.center,
  163. decoration: ShapeDecoration(
  164. color: isSelected ? Color(0x1C0279FB) : Color(0xFF242D38),
  165. shape: RoundedRectangleBorder(
  166. borderRadius: BorderRadius.circular(14.r),
  167. ),
  168. ),
  169. child: Text(
  170. title,
  171. style: TextStyle(
  172. color:
  173. isSelected ? Colors.white : Colors.white.withValues(alpha: 0.8),
  174. fontSize: isSelected ? 16.sp : 14.sp,
  175. fontWeight: isSelected ? FontWeight.w700 : FontWeight.w500,
  176. ),
  177. ),
  178. ),
  179. );
  180. }
  181. }
  182. // 排序类型
  183. enum SortType {
  184. latest, // 最新
  185. oldest, // 最旧
  186. largeSize, // 按文件组大小排序
  187. mostQuantity // 按每个月照片数量排序
  188. }