Skip to content

Commit

Permalink
Merge pull request #658 from bounswe/612-connect-the-annotations-with…
Browse files Browse the repository at this point in the history
…-the-backend

successfully fetching annotations from backend
  • Loading branch information
simarahmtkhy authored Dec 22, 2023
2 parents b3ef85f + d169d21 commit b44740c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
23 changes: 23 additions & 0 deletions app/mobile/lib/services/annotationService.dart
Original file line number Diff line number Diff line change
@@ -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>(_dio);
// }

static Dio get dio => getIt<Dio>();

static Future<void> init() async {
// Set up your Dio instance with interceptors, base URL, etc.
dio.interceptors.add(CurlLoggerDioInterceptor(printOnSuccess: true));
dio.options.baseUrl = baseUrl;
}


}
48 changes: 47 additions & 1 deletion app/mobile/lib/services/homePageService.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:ui';

import 'package:dio/dio.dart';
Expand All @@ -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<List<PollViewHomePage>> getPollRequests() async {
Expand All @@ -18,8 +20,51 @@ class HomePageService {
);
print(response.data);
final List<dynamic> postsJson = response.data;
List<String> 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<String, List<Map<String, dynamic>>> groupedAnnotations = {};
try {
Response annotationsResponse = await AnnotationService.dio.get(
getAnnotationsEndpoint,
queryParameters: {
'pollIds': pollIds,
},
);
if (annotationsResponse.statusCode == 200) {
Map<String, dynamic> jsonMap = annotationsResponse.data;

// Access the "annotations" key in the map
List<dynamic> 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<PollViewHomePage> posts = [];
for (var post in postsJson) {
String pollId = "http://34.105.66.254:1923/${post['id']}";
List<Map<String, dynamic>> annotations = groupedAnnotations[pollId] ?? [];
List<List<int>> indices = annotations.map<List<int>>((e) => [e['start'], e['end']]).toList();
List<String> bodies = annotations.map<String>((e) => e['body']['value']).toList();
final creator = post['creator'];
final List<dynamic> tagsJson = post['tags'];
final List<dynamic> optionsJson = post['options'];
Expand Down Expand Up @@ -54,7 +99,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);
Expand Down
2 changes: 2 additions & 0 deletions app/mobile/lib/view/homePage/homePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class _HomePageState extends State<HomePage>
commentCount: post.commentCount,
chosenVoteIndex: post.chosenVoteIndex,
annotationIndices: post.annotationIndices,
annotationTexts: post.annotationTexts,
),
),
),
Expand Down Expand Up @@ -229,6 +230,7 @@ class _HomePageState extends State<HomePage>
commentCount: post.commentCount,
chosenVoteIndex: post.chosenVoteIndex,
annotationIndices: post.annotationIndices,
annotationTexts: post.annotationTexts,
),
),
),
Expand Down
16 changes: 9 additions & 7 deletions app/mobile/lib/view/pollViewHomePage/pollViewHomePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PollViewHomePage extends StatefulWidget {
final int chosenVoteIndex;
final int commentCount;
final List<List<int>> annotationIndices;
final List<String> annotationTexts;

const PollViewHomePage({
super.key,
Expand All @@ -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();
}
Expand Down Expand Up @@ -107,7 +108,7 @@ class _PollViewHomePageState extends State<PollViewHomePage> {
// 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(
Expand Down Expand Up @@ -159,16 +160,17 @@ class _PollViewHomePageState extends State<PollViewHomePage> {
}


RichText buildRichText(String fullText, List<List<int>> indices) {
RichText buildRichText(String fullText, List<List<int>> indices, List<String> annotationTexts) {


List<TextSpan> textSpans = [];

int previousIndex = 0;

for (List<int> 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(
Expand All @@ -189,7 +191,7 @@ class _PollViewHomePageState extends State<PollViewHomePage> {
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!');
},
),
Expand Down
1 change: 1 addition & 0 deletions app/mobile/lib/view/profilePage/profilePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class _ProfilePageState extends State<ProfilePage> {
chosenVoteIndex: post.chosenVoteIndex,
commentCount: post.commentCount,
annotationIndices: [],
annotationTexts: [],
),
),
),
Expand Down

0 comments on commit b44740c

Please sign in to comment.