calendar_controller.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. imageList.sort((a, b) => b.createDateTime.compareTo(a.createDateTime));
  72. break;
  73. case SortType.oldest:
  74. imageList.sort((a, b) => a.createDateTime.compareTo(b.createDateTime));
  75. break;
  76. case SortType.largeSize:
  77. // 先把目前monthlyAlbums中的每个月的大小记录到map中,然后根据这里面的大小进行排序
  78. Map<String, double> monthSizeMap = {};
  79. for (var album in monthlyAlbums) {
  80. print(
  81. "CalendarController sortAssets album: ${album.month}, totalSize: ${album.totalSize}");
  82. monthSizeMap[album.month!] = album.totalSize.value;
  83. }
  84. monthlyAlbums.sort((a, b) =>
  85. monthSizeMap[b.month!]!.compareTo(monthSizeMap[a.month!]!));
  86. imageList.clear();
  87. for (var album in monthlyAlbums) {
  88. imageList.addAll(album.images);
  89. }
  90. break;
  91. case SortType.mostQuantity:
  92. monthlyAlbums
  93. .sort((a, b) => b.images.length.compareTo(a.images.length));
  94. imageList.clear();
  95. for (var album in monthlyAlbums) {
  96. imageList.addAll(album.images);
  97. }
  98. break;
  99. }
  100. updateMonthlyAssets(); // 排序后更新分组
  101. }
  102. // 显示排序弹窗
  103. // 显示排序弹窗
  104. void clickSort() {
  105. showCupertinoModalPopup(
  106. context: Get.context!,
  107. builder: (context) {
  108. return Container(
  109. width: 360.w,
  110. decoration: ShapeDecoration(
  111. color: Color(0xFF131C27),
  112. shape: RoundedRectangleBorder(
  113. borderRadius: BorderRadius.only(
  114. topLeft: Radius.circular(44.r),
  115. topRight: Radius.circular(44.r),
  116. ),
  117. ),
  118. ),
  119. child: Column(
  120. mainAxisSize: MainAxisSize.min,
  121. children: [
  122. SizedBox(height: 10.h),
  123. Container(
  124. width: 57.w,
  125. height: 4.h,
  126. decoration: ShapeDecoration(
  127. color: Color(0xFF242D38),
  128. shape: RoundedRectangleBorder(
  129. borderRadius: BorderRadius.circular(2)),
  130. ),
  131. ),
  132. SizedBox(height: 19.h),
  133. Text(
  134. 'Sort By',
  135. textAlign: TextAlign.center,
  136. style: TextStyle(
  137. color: Colors.white,
  138. fontSize: 16.sp,
  139. fontWeight: FontWeight.w700,
  140. ),
  141. ),
  142. SizedBox(height: 14.h),
  143. // Latest
  144. sortOption(SortType.latest, 'Latest'),
  145. SizedBox(height: 14.h),
  146. // Oldest
  147. sortOption(SortType.oldest, 'Oldest'),
  148. SizedBox(height: 14.h),
  149. // Large Size
  150. sortOption(SortType.largeSize, 'Largest Storage Usage'),
  151. SizedBox(height: 14.h),
  152. // Most Quantity
  153. sortOption(SortType.mostQuantity, 'Most Quantity'),
  154. SizedBox(height: 55.h),
  155. ],
  156. ),
  157. );
  158. },
  159. );
  160. }
  161. Widget sortOption(SortType type, String title) {
  162. bool isSelected = currentSortType.value == type; // 判断是否是选中的排序类型
  163. return GestureDetector(
  164. onTap: () {
  165. Navigator.pop(Get.context!);
  166. sortAssets(type);
  167. },
  168. child: Container(
  169. width: 300.w,
  170. height: 50.h,
  171. alignment: Alignment.center,
  172. decoration: ShapeDecoration(
  173. color: isSelected ? Color(0x1C0279FB) : Color(0xFF242D38),
  174. shape: RoundedRectangleBorder(
  175. borderRadius: BorderRadius.circular(14.r),
  176. ),
  177. ),
  178. child: Text(
  179. title,
  180. style: TextStyle(
  181. color:
  182. isSelected ? Colors.white : Colors.white.withValues(alpha: 0.8),
  183. fontSize: isSelected ? 16.sp : 14.sp,
  184. fontWeight: isSelected ? FontWeight.w700 : FontWeight.w500,
  185. ),
  186. ),
  187. ),
  188. );
  189. }
  190. }
  191. // 排序类型
  192. enum SortType {
  193. latest, // 最新
  194. oldest, // 最旧
  195. largeSize, // 按文件组大小排序
  196. mostQuantity // 按每个月照片数量排序
  197. }