From e26146ead2e1da6066c3558fad03c458db8b3001 Mon Sep 17 00:00:00 2001 From: juan Date: Wed, 18 Jun 2025 16:32:49 -0400 Subject: [PATCH] clean and fetchMarkdown content added --- lib/api_service.dart | 114 +++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 76 deletions(-) diff --git a/lib/api_service.dart b/lib/api_service.dart index 4769377..ecd5a83 100644 --- a/lib/api_service.dart +++ b/lib/api_service.dart @@ -1,16 +1,13 @@ // this file should handle most of the API calls -// it also builds some widgets, but it will be modulated later +// it also builds some widgets, but it will be modulated later // chat it did import 'dart:async'; import 'dart:typed_data'; - - import 'structs.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; - class ApiService { static String ip = ""; static String port = ""; @@ -141,7 +138,6 @@ class ApiService { } catch (e) { print('_getEmailContent caught error: $e'); } - // return content; return HTMLofThread; } @@ -344,75 +340,41 @@ class ApiService { return AttachmentResponse(name: "error", data: Uint8List(0)); } - //TODO: MOVE THIS INTO WEB -// Future>> getMarkerPosition() async { -// //this is so we can put a widget right below each email, but the way how the email content is generated -// //leads to problems as for a) the html is added one right after the other in one iframe, b) -// // if it was multiple iframes then the scrolling to jump would not work as expected + Future> fetchMarkdownContent( + List IDsString, String emailFolder) async { + List MDofThread = []; + threadAttachments = []; + int counter = 0; -// print("marker called"); -// // JavaScript code embedded as a string -// String jsCode = ''' -// (async function waitForIframeAndMarkers() { -// try { -// return await new Promise((resolve) => { -// const interval = setInterval(() => { -// console.log("⏳ Checking for iframe..."); -// var iframe = document.getElementsByTagName('iframe')[0]; -// if (iframe && iframe.contentDocument) { -// console.log("✅ Iframe found!"); -// var iframeDoc = iframe.contentDocument || iframe.contentWindow.document; -// var markers = iframeDoc.querySelectorAll('[id^="JuanBedarramarker"]'); -// if (markers.length > 0) { -// console.log(`✅ Found markers in the iframe.`); -// var positions = []; -// markers.forEach((marker) => { -// var rect = marker.getBoundingClientRect(); -// positions.push({ -// id: marker.id, -// x: rect.left + window.scrollX, -// y: rect.top + window.scrollY, -// }); -// }); -// console.log("📌 Marker positions:", positions); -// clearInterval(interval); -// resolve(JSON.stringify(positions)); // Ensure proper JSON string -// } else { -// console.log("❌ No markers found yet."); -// } -// } else { -// console.log("❌ Iframe not found or not loaded yet."); -// } -// }, 200); -// }); -// } catch (error) { -// console.error("JS Error:", error); -// throw error; // Propagate error to Dart -// } -// })(); -// '''; - -// try { -// // Execute the JavaScript code using eval -// // final result = await js.context.callMethod('eval', [jsCode]); - -// if (result != null && result is String) { -// print("Result received: $result"); - -// // Parse the JSON string returned by JavaScript into a Dart list of maps -// final List parsedResult = jsonDecode(result); -// var positions = List>.from(parsedResult); -// print("positions put on"); -// print(positions); -// return positions; -// } else { -// print("result is null or not a string"); -// } -// } catch (e, stackTrace) { -// print("Error executing JavaScript: $e"); -// print(stackTrace); -// } - -// return []; -// } -} \ No newline at end of file + try { + //attaches email after email from a thread + for (var id in IDsString) { + var url = Uri.http('$ip:$port', 'email_md', {'id': id}); + print(url); + var response = await http.get(url); + currThread.add(id); + if (response.statusCode == 200) { + counter += 1; + Map json = jsonDecode(response.body); + + MDofThread.add(json['md'] ?? ''); + try { + List attachments = + await getAttachmentsInfo(emailFolder, id); + for (var attachment in attachments) { + //TODO: for each attachment creaate at the bottom a widget for each individual one + threadAttachments + .add(await getAttachment(emailFolder, id, attachment.name)); + } + } catch (innerError) { + print('_getAttachment info caught error $innerError'); + } + } + } + } catch (e) { + print('_getMDContent caught error: $e'); + } + + return MDofThread; + } +}