Explorar el Código

fixed duplicates and last page load

Veloe hace 1 semana
padre
commit
e4d84c6b11
Se han modificado 4 ficheros con 27 adiciones y 8 borrados
  1. 8 0
      lib/kemono_client.dart
  2. 9 5
      lib/models/post.dart
  3. 9 2
      lib/posts_notifier.dart
  4. 1 1
      pubspec.yaml

+ 8 - 0
lib/kemono_client.dart

@@ -52,6 +52,9 @@ class KemonoClient {
       final response = await _dio.get(
         '/api/v1/$service/user/$creatorId/posts',
         queryParameters: query.isEmpty ? {'o': start} : {'o':start, 'q': query},
+        options: Options(
+          validateStatus: (status) => true,
+        ), // Accept all status codes
       );
 
       final parsedData = switch(response.data) {
@@ -61,6 +64,11 @@ class KemonoClient {
           _ => throw FormatException('Unprocessable data')
         };
 
+      if (parsedData is Map<String,dynamic> && (parsedData).containsKey("error"))
+      {
+          return List<Post>.empty();
+      }
+
       return (parsedData as List).map((x) => Post.fromJson(x,_dio.options.baseUrl)).toList();
     } on DioException catch (e) {
       throw Exception('Posts load failed: ${e.message} ${e.stackTrace}');

+ 9 - 5
lib/models/post.dart

@@ -27,11 +27,15 @@ class Post {
     required this.baseUrl,
   });
 
-  List<Attachment> get mediaAttachments => files == null ? [
-        ...attachments.where((a) => a.isImage || a.isVideo)
-      ] : [
-        ...attachments.where((a) => a.isImage || a.isVideo),files!
-      ];
+  List<Attachment> get mediaAttachments {
+    final allAttachments = files == null ? 
+      [...attachments.where((a) => a.isImage || a.isVideo)] : 
+      [...attachments.where((a) => a.isImage || a.isVideo),files!];
+
+    final ids = allAttachments.map((e) => e.path).toSet();
+    allAttachments.retainWhere((x) => ids.remove(x.path));
+    return allAttachments;
+  }
 
   factory Post.fromJson(Map<String, dynamic> json, String baseUrl) => Post(
         added: _parseString(json['added']),

+ 9 - 2
lib/posts_notifier.dart

@@ -8,6 +8,7 @@ class PostsNotifier extends StateNotifier<AsyncValue<List<Post>>> {
   final String query;
   final KemonoClient client;
   int _currentPage = 0;
+  bool _reachedEnd = false;
 
   PostsNotifier({
     required this.creatorId,
@@ -36,8 +37,8 @@ class PostsNotifier extends StateNotifier<AsyncValue<List<Post>>> {
   }
 
   Future<void> loadNext() async {
-    if (!mounted ||state.isLoading) return;
-    
+    if (!mounted ||state.isLoading || _reachedEnd) return;
+
     final previousPosts = state.valueOrNull ?? [];
     state = AsyncValue<List<Post>>.loading().copyWithPrevious(state);
     
@@ -48,6 +49,12 @@ class PostsNotifier extends StateNotifier<AsyncValue<List<Post>>> {
         _currentPage * 50,
         query 
       );
+
+      if (newPosts.length == 0){
+        _reachedEnd = true;
+        return;
+      }
+
       state = AsyncValue.data([...previousPosts, ...newPosts]);
       _currentPage++;
     } catch (e) {

+ 1 - 1
pubspec.yaml

@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
 # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
 # In Windows, build-name is used as the major, minor, and patch parts
 # of the product and file versions while build-number is used as the build suffix.
-version: 1.0.10
+version: 1.0.11
 
 environment:
   sdk: ^3.7.2