android/ios-adaption feature, markdown, and augment #6
					 1 changed files with 38 additions and 76 deletions
				
			
		| 
						 | 
					@ -1,16 +1,13 @@
 | 
				
			||||||
// this file should handle most of the API calls
 | 
					// 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:async';
 | 
				
			||||||
import 'dart:typed_data';
 | 
					import 'dart:typed_data';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import 'structs.dart';
 | 
					import 'structs.dart';
 | 
				
			||||||
import 'package:http/http.dart' as http;
 | 
					import 'package:http/http.dart' as http;
 | 
				
			||||||
import 'dart:convert';
 | 
					import 'dart:convert';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
class ApiService {
 | 
					class ApiService {
 | 
				
			||||||
  static String ip = "";
 | 
					  static String ip = "";
 | 
				
			||||||
  static String port = "";
 | 
					  static String port = "";
 | 
				
			||||||
| 
						 | 
					@ -141,7 +138,6 @@ class ApiService {
 | 
				
			||||||
    } catch (e) {
 | 
					    } catch (e) {
 | 
				
			||||||
      print('_getEmailContent caught error: $e');
 | 
					      print('_getEmailContent caught error: $e');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    // return content;
 | 
					 | 
				
			||||||
    return HTMLofThread;
 | 
					    return HTMLofThread;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -344,75 +340,41 @@ class ApiService {
 | 
				
			||||||
    return AttachmentResponse(name: "error", data: Uint8List(0));
 | 
					    return AttachmentResponse(name: "error", data: Uint8List(0));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  //TODO: MOVE THIS INTO WEB
 | 
					  Future<List<String>> fetchMarkdownContent(
 | 
				
			||||||
//   Future<List<Map<String, dynamic>>> getMarkerPosition() async {
 | 
					      List<String> IDsString, String emailFolder) async {
 | 
				
			||||||
//     //this is so we can put a widget right below each email, but the way how the email content is generated
 | 
					    List<String> MDofThread = [];
 | 
				
			||||||
//     //leads to problems as for a) the html is added one right after the other in one iframe, b)
 | 
					    threadAttachments = [];
 | 
				
			||||||
//     // if it was multiple iframes then the scrolling to jump would not work as expected
 | 
					    int counter = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//     print("marker called");
 | 
					    try {
 | 
				
			||||||
//     // JavaScript code embedded as a string
 | 
					      //attaches email after email from a thread
 | 
				
			||||||
//     String jsCode = '''
 | 
					      for (var id in IDsString) {
 | 
				
			||||||
// (async function waitForIframeAndMarkers() {
 | 
					        var url = Uri.http('$ip:$port', 'email_md', {'id': id});
 | 
				
			||||||
//   try {
 | 
					        print(url);
 | 
				
			||||||
//     return await new Promise((resolve) => {
 | 
					        var response = await http.get(url);
 | 
				
			||||||
//       const interval = setInterval(() => {
 | 
					        currThread.add(id);
 | 
				
			||||||
//         console.log("⏳ Checking for iframe...");
 | 
					        if (response.statusCode == 200) {
 | 
				
			||||||
//         var iframe = document.getElementsByTagName('iframe')[0];
 | 
					          counter += 1;
 | 
				
			||||||
//         if (iframe && iframe.contentDocument) {
 | 
					          Map<String, dynamic> json = jsonDecode(response.body);
 | 
				
			||||||
//           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 {
 | 
					          MDofThread.add(json['md'] ?? '');
 | 
				
			||||||
//       // Execute the JavaScript code using eval
 | 
					          try {
 | 
				
			||||||
//       // final result = await js.context.callMethod('eval', [jsCode]);
 | 
					            List<AttachmentInfo> attachments =
 | 
				
			||||||
 | 
					                await getAttachmentsInfo(emailFolder, id);
 | 
				
			||||||
//       if (result != null && result is String) {
 | 
					            for (var attachment in attachments) {
 | 
				
			||||||
//         print("Result received: $result");
 | 
					              //TODO: for each attachment creaate at the bottom a widget for each individual one
 | 
				
			||||||
 | 
					              threadAttachments
 | 
				
			||||||
//         // Parse the JSON string returned by JavaScript into a Dart list of maps
 | 
					                  .add(await getAttachment(emailFolder, id, attachment.name));
 | 
				
			||||||
//         final List<dynamic> parsedResult = jsonDecode(result);
 | 
					            }
 | 
				
			||||||
//         var positions = List<Map<String, dynamic>>.from(parsedResult);
 | 
					          } catch (innerError) {
 | 
				
			||||||
//         print("positions put on");
 | 
					            print('_getAttachment info caught error $innerError');
 | 
				
			||||||
//         print(positions);
 | 
					          }
 | 
				
			||||||
//         return positions;
 | 
					        }
 | 
				
			||||||
//       } else {
 | 
					      }
 | 
				
			||||||
//         print("result is null or not a string");
 | 
					    } catch (e) {
 | 
				
			||||||
//       }
 | 
					      print('_getMDContent caught error: $e');
 | 
				
			||||||
//     } catch (e, stackTrace) {
 | 
					    }
 | 
				
			||||||
//       print("Error executing JavaScript: $e");
 | 
					    
 | 
				
			||||||
//       print(stackTrace);
 | 
					    return MDofThread;
 | 
				
			||||||
//     }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
//     return [];
 | 
					 | 
				
			||||||
//   }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue