From d169d21b070855f1ec2889fb5e873c44ad51cfa3 Mon Sep 17 00:00:00 2001 From: Simar Date: Fri, 22 Dec 2023 19:24:35 +0300 Subject: [PATCH] successfully fetching annotations from backend --- .../lib/services/annotationService.dart | 23 +++++++++ app/mobile/lib/services/homePageService.dart | 48 ++++++++++++++++++- app/mobile/lib/view/homePage/homePage.dart | 2 + .../pollViewHomePage/pollViewHomePage.dart | 16 ++++--- .../lib/view/profilePage/profilePage.dart | 1 + 5 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 app/mobile/lib/services/annotationService.dart diff --git a/app/mobile/lib/services/annotationService.dart b/app/mobile/lib/services/annotationService.dart new file mode 100644 index 00000000..aedc11f6 --- /dev/null +++ b/app/mobile/lib/services/annotationService.dart @@ -0,0 +1,23 @@ +import 'package:curl_logger_dio_interceptor/curl_logger_dio_interceptor.dart'; +import 'package:dio/dio.dart'; +import 'package:get_it/get_it.dart'; + +class AnnotationService { + static final Dio _dio = Dio(); + static final GetIt getIt = GetIt.instance; + static String baseUrl = 'http://34.105.66.254:1938/'; + + // static void setup() { + // getIt.registerSingleton(_dio); + // } + + static Dio get dio => getIt(); + + static Future init() async { + // Set up your Dio instance with interceptors, base URL, etc. + dio.interceptors.add(CurlLoggerDioInterceptor(printOnSuccess: true)); + dio.options.baseUrl = baseUrl; + } + + +} diff --git a/app/mobile/lib/services/homePageService.dart b/app/mobile/lib/services/homePageService.dart index 0ab2f5b3..3cb61ab9 100644 --- a/app/mobile/lib/services/homePageService.dart +++ b/app/mobile/lib/services/homePageService.dart @@ -1,3 +1,4 @@ +import 'dart:convert'; import 'dart:ui'; import 'package:dio/dio.dart'; @@ -7,6 +8,7 @@ import 'package:mobile_app/models/comment.dart'; import 'package:mobile_app/services/apiService.dart'; import '../view/pollViewHomePage/pollViewHomePage.dart'; +import 'annotationService.dart'; class HomePageService { static Future> getPollRequests() async { @@ -18,8 +20,51 @@ class HomePageService { ); print(response.data); final List postsJson = response.data; + List pollids = []; + for (var poll in postsJson) { + pollids.add(poll['id']); + } + String pollIds = pollids.join(','); + const String getAnnotationsEndpoint = 'http://34.105.66.254:1938/annotation'; + Map>> groupedAnnotations = {}; + try { + Response annotationsResponse = await AnnotationService.dio.get( + getAnnotationsEndpoint, + queryParameters: { + 'pollIds': pollIds, + }, + ); + if (annotationsResponse.statusCode == 200) { + Map jsonMap = annotationsResponse.data; + + // Access the "annotations" key in the map + List annotations = jsonMap['annotations']; + + // Group annotations by target source + + + // Map each annotation to a Dart object and group by source + for (var annotation in annotations) { + String source = annotation['target']['source']; + if (!groupedAnnotations.containsKey(source)) { + groupedAnnotations[source] = []; + } + groupedAnnotations[source]!.add({ + 'body': annotation['body'], + 'start': annotation['target']['selector']['start'], + 'end': annotation['target']['selector']['end'], + }); + } + } + } catch (e) { + print(e); + } List posts = []; for (var post in postsJson) { + String pollId = "http://34.105.66.254:1923/${post['id']}"; + List> annotations = groupedAnnotations[pollId] ?? []; + List> indices = annotations.map>((e) => [e['start'], e['end']]).toList(); + List bodies = annotations.map((e) => e['body']['value']).toList(); final creator = post['creator']; final List tagsJson = post['tags']; final List optionsJson = post['options']; @@ -50,7 +95,8 @@ class HomePageService { approvedStatus: post['approveStatus'], didLike: post['didLike'], // You might want to format the date chosenVoteIndex: -1, //it will be post['chosenVoteIndex'] - annotationIndices: [[2,5], [7,10]], + annotationIndices: indices, + annotationTexts: bodies, )); } print(posts); diff --git a/app/mobile/lib/view/homePage/homePage.dart b/app/mobile/lib/view/homePage/homePage.dart index a1053225..f9020151 100644 --- a/app/mobile/lib/view/homePage/homePage.dart +++ b/app/mobile/lib/view/homePage/homePage.dart @@ -164,6 +164,7 @@ class _HomePageState extends State commentCount: post.commentCount, chosenVoteIndex: post.chosenVoteIndex, annotationIndices: post.annotationIndices, + annotationTexts: post.annotationTexts, ), ), ), @@ -229,6 +230,7 @@ class _HomePageState extends State commentCount: post.commentCount, chosenVoteIndex: post.chosenVoteIndex, annotationIndices: post.annotationIndices, + annotationTexts: post.annotationTexts, ), ), ), diff --git a/app/mobile/lib/view/pollViewHomePage/pollViewHomePage.dart b/app/mobile/lib/view/pollViewHomePage/pollViewHomePage.dart index 2fe4a352..3bcf9b58 100644 --- a/app/mobile/lib/view/pollViewHomePage/pollViewHomePage.dart +++ b/app/mobile/lib/view/pollViewHomePage/pollViewHomePage.dart @@ -28,6 +28,7 @@ class PollViewHomePage extends StatefulWidget { final int chosenVoteIndex; final int commentCount; final List> annotationIndices; + final List annotationTexts; const PollViewHomePage({ super.key, @@ -46,7 +47,7 @@ class PollViewHomePage extends StatefulWidget { required this.approvedStatus, required this.didLike, required this.chosenVoteIndex, - required this.commentCount, required this.annotationIndices, + required this.commentCount, required this.annotationIndices, required this.annotationTexts, }); _PollViewHomePageState createState() => _PollViewHomePageState(); } @@ -107,7 +108,7 @@ class _PollViewHomePageState extends State { // maxLines: 3, // style: const TextStyle( // fontSize: 18.0, fontWeight: FontWeight.bold)), - child: buildRichText(widget.postTitle, widget.annotationIndices) + child: buildRichText(widget.postTitle, widget.annotationIndices, widget.annotationTexts) ), TagListWidget(tags: widget.tags, tagColors: widget.tagColors), const Padding( @@ -159,16 +160,17 @@ class _PollViewHomePageState extends State { } - RichText buildRichText(String fullText, List> indices) { + RichText buildRichText(String fullText, List> indices, List annotationTexts) { List textSpans = []; int previousIndex = 0; - for (List indexPair in indices) { - int startIndex = indexPair[0]; - int endIndex = indexPair[1]; + for (int i = 0; i < indices.length; i++) { + int startIndex = indices[i][0]; + int endIndex = indices[i][1]; + String annotationText = annotationTexts[i]; // Add non-underlined text before the current underlined part textSpans.add( @@ -189,7 +191,7 @@ class _PollViewHomePageState extends State { recognizer: TapGestureRecognizer() ..onTap = () { // Handle tap on the underlined text - _showPopup(context, fullText.substring(startIndex, endIndex)); + _showPopup(context, annotationText); print('Tapped on underlined text from index $startIndex to $endIndex!'); }, ), diff --git a/app/mobile/lib/view/profilePage/profilePage.dart b/app/mobile/lib/view/profilePage/profilePage.dart index a4296288..c47b67e7 100644 --- a/app/mobile/lib/view/profilePage/profilePage.dart +++ b/app/mobile/lib/view/profilePage/profilePage.dart @@ -258,6 +258,7 @@ class _ProfilePageState extends State { chosenVoteIndex: post.chosenVoteIndex, commentCount: post.commentCount, annotationIndices: [], + annotationTexts: [], ), ), ),