Просмотр исходного кода

[new]增加本地音频分页加载功能

zk 1 год назад
Родитель
Сommit
c0ab460f9f

+ 32 - 4
lib/module/audiopicker/controller.dart

@@ -5,12 +5,15 @@ import 'package:electronic_assistant/resource/string.gen.dart';
 import 'package:electronic_assistant/utils/audio_picker_utils.dart';
 import 'package:electronic_assistant/utils/file_upload_check_helper.dart';
 import 'package:electronic_assistant/utils/toast_util.dart';
+import 'package:flutter/cupertino.dart';
 import 'package:get/get.dart';
 import 'package:photo_manager/photo_manager.dart';
+import 'package:pull_to_refresh/pull_to_refresh.dart';
 import 'package:uuid/uuid.dart';
 
 import '../../data/bean/talks.dart';
 import '../../data/repositories/talk_repository.dart';
+import '../../utils/error_handler.dart';
 import '../talk/view.dart';
 
 class AudioPickerController extends BaseController {
@@ -20,6 +23,11 @@ class AudioPickerController extends BaseController {
   final _currentEntity = Rxn<AssetEntity?>();
 
   AssetEntity? get currentEntity => _currentEntity.value;
+  final refreshController = RefreshController(initialRefresh: false);
+
+  int limit = 20;
+
+  int totalCount = 0;
 
   @override
   void onReady() async {
@@ -32,15 +40,31 @@ class AudioPickerController extends BaseController {
       }
     }
     currentPath = await initPathEntity();
-    requestList();
+    totalCount = await currentPath?.assetCountAsync ?? 0;
+    requestList(0, limit, isClearAll: true);
   }
 
-  void requestList() {
+  void requestList(int offset, int limit, {bool? isClearAll = false}) {
     if (currentPath == null) {
       return;
     }
-    AudioPickerUtils.getAssetList(currentPath!, 0).then((value) {
-      audioList.addAll(value);
+    AudioPickerUtils.getAssetListRange(currentPath!, offset, limit)
+        .then((list) {
+      if (isClearAll == true) {
+        audioList.clear();
+      }
+      audioList.addAll(list);
+      if (audioList.length >= totalCount) {
+        debugPrint("getAssetListRange-没有更多数据了");
+        refreshController.loadNoData();
+      } else {
+        refreshController.loadComplete();
+      }
+      refreshController.refreshCompleted();
+    }).catchError((error) {
+      debugPrint("requestTalkData-catchError-$error");
+      refreshController.loadFailed();
+      refreshController.refreshFailed();
     });
   }
 
@@ -81,4 +105,8 @@ class AudioPickerController extends BaseController {
       ToastUtil.showToast(e.toString());
     }
   }
+
+  void onLoadMoreData() {
+    requestList(audioList.length, limit);
+  }
 }

+ 10 - 3
lib/module/audiopicker/view.dart

@@ -11,6 +11,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
 import 'package:get/get.dart';
 import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
 import 'package:photo_manager/photo_manager.dart';
+import 'package:pull_to_refresh/pull_to_refresh.dart';
 
 import '../../resource/colors.gen.dart';
 import '../../utils/common_style.dart';
@@ -39,9 +40,15 @@ class AudioPickerPage extends BasePage<AudioPickerController> {
         children: [
           _buildTitleView(),
           Expanded(child: Obx(() {
-            return ListView.builder(
-              itemBuilder: _buildItem,
-              itemCount: controller.audioList.length,
+            return SmartRefresher(
+              enablePullUp: true,
+              enablePullDown: false,
+              onLoading: controller.onLoadMoreData,
+              controller: controller.refreshController,
+              child: ListView.builder(
+                itemBuilder: _buildItem,
+                itemCount: controller.audioList.length,
+              ),
             );
           })),
           _buildImportBtnView()

+ 11 - 2
lib/utils/audio_picker_utils.dart

@@ -40,8 +40,8 @@ class AudioPickerUtils {
   }
 
   //获取本地音频资源列表
-  static Future<List<AssetEntity>> getAssetList(AssetPathEntity path, int page,
-      {int size = 300}) async {
+  static Future<List<AssetEntity>> getAssetListPaged(
+      AssetPathEntity path, int page, int size) async {
     final List<AssetEntity> entities = await path.getAssetListPaged(
       page: page,
       size: size,
@@ -49,6 +49,15 @@ class AudioPickerUtils {
     return entities;
   }
 
+  static Future<List<AssetEntity>> getAssetListRange(
+      AssetPathEntity path, int offset, int limit) async {
+    final List<AssetEntity> entities = await path.getAssetListRange(
+      start: offset,
+      end: offset + limit,
+    );
+    return entities;
+  }
+
   //根据id获取AssetEntity
   static Future<AssetEntity?> getAssetEntity(String? id) async {
     if (id == null) {