// this file should handle most of the API calls // it also builds some widgets, but it will be modulated later import 'package:crab_ui/structs.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import 'dart:ui_web' as ui; import 'augment.dart'; import 'dart:html' as html; class ApiService { Future> fetchEmailsFromFolder( String folder, int pagenitaion) async { try { var url = Uri.http('127.0.0.1:3001', 'sorted_threads_by_date', { 'folder': folder, 'limit': '50', 'offset': pagenitaion.toString(), }); var response = await http.get(url); // print(response); List allEmails = []; if (response.statusCode == 200) { List json = jsonDecode(response.body); for (var item in json) { //each item in the json is a date if (item.length > 1 && item[0] is String && item[1] is List) { List threadIDs = List.from(item[1]); for (var threadId in threadIDs) { await fetchThreads(threadId, allEmails); } } } return allEmails; } else { throw Exception('Failed to load threads'); } } catch (e) { print('_displayEmailsFromFolder caught error: $e'); return []; } } Future fetchThreads( //populates allEmails, which is the List that contains all the emails in a thread int threadId, List allEmails) async { try { var url = Uri.http('127.0.0.1:3001', 'get_thread', {'id': threadId.toString()}); var response = await http.get(url); if (response.statusCode == 200) { Map messagesJson = jsonDecode(response.body); GetThreadResponse threadResponse = GetThreadResponse.fromJson(messagesJson); allEmails.add(threadResponse); } else { throw Exception( 'Failed to fetch thread messages for thread ID: $threadId'); } } catch (e) { print('Error fetching thread messages: $e'); } } Future> sonicSearch( String list, int limit, int offset, String query) async { try { var url = Uri.http('127.0.0.1:3001', 'search', { 'list': list, 'limit': limit.toString(), 'offset': offset.toString(), 'query': query }); var response = await http.get(url); if (response.statusCode == 200) { List messagesJson = json.decode(response.body); List messages = messagesJson.map((mj) => SerializableMessage.fromJson(mj)).toList(); return messages; } } catch (e) { print("caught $e"); } return []; } Future fetchEmailContent(List IDs) async { String content = r""" """; try { //attaches email after email from a thread for (var id in IDs) { var url = Uri.http('127.0.0.1:3001', 'email', {'id': id}); var response = await http.get(url); if (response.statusCode == 200) { content += response.body; content += "
"; } } } catch (e) { print('_getEmailContent caught error: $e'); } return content; } // void _addMailBox async(BuildContext context){ // //add email folder // showDialog(context: context, builder: builder) // } Future> fetchFolders() async { try { var url = Uri.http('127.0.0.1:3001', 'folders'); var response = await http.get(url); return List.from(json.decode(response.body)); } catch (e) { print('fetchFolders caught error: $e'); return []; } } Future createFolder(String folderName) async { var url = Uri.http('127.0.0.1:3001', 'create_folder'); Map requestBody = {'name': folderName}; try { var response = await http.post( url, headers: { 'Content-Type': 'application/json', }, body: jsonEncode(requestBody), ); if (response.statusCode == 200) { print('response body: ${response.body}'); } else { print('Error: ${response.statusCode}, response body: ${response.body}'); } } catch (e) { print('error making post req: $e'); } } Future deleteFolder(String folderName) async { var url = Uri.http('127.0.0.1:3001', 'delete_folder'); Map requestBody = {'name': folderName}; try { var response = await http.post( url, headers: { 'Content-Type': 'application/json', }, body: jsonEncode(requestBody), ); if (response.statusCode == 200) { print('response body: ${response.body}'); } else { print('Error: ${response.statusCode}, response body: ${response.body}'); } } catch (e) { print('error making post req: $e'); } } } class EmailView extends StatefulWidget { final String emailContent; final String from; final String name; final String to; final String subject; final String date; final String id; const EmailView({ Key? key, required this.emailContent, required this.from, required this.name, required this.to, required this.subject, required this.date, required this.id, }) : super(key: key); @override _EmailViewState createState() => _EmailViewState(); } class _EmailViewState extends State { late Key iframeKey; late String currentContent; late String viewTypeId; // TextEditingController _jumpController = TextEditingController(); @override void initState() { super.initState(); String currentContent = widget.emailContent; viewTypeId = "iframe-${DateTime.now().millisecondsSinceEpoch}"; _registerViewFactory(currentContent); } void _registerViewFactory(String currentContent) { setState(() { viewTypeId = 'iframe-${DateTime.now().millisecondsSinceEpoch}'; ui.platformViewRegistry.registerViewFactory( viewTypeId, (int viewId) => html.IFrameElement() ..width = '100%' ..height = '100%' ..srcdoc = currentContent ..style.border = 'none'); }); } void _scrollToNumber(String spanId) { AugmentClasses.handleJump(spanId); } // TODO: void _invisibility(String ) @override Widget build(BuildContext context) { // print(currentContent); return Scaffold( appBar: AppBar( title: Text(widget.name), ), body: Column( children: [ EmailToolbar( onJumpToSpan: _scrollToNumber, onButtonPressed: () => {}, // AugmentClasses.handleJump(viewTypeId, '1'); // print("button got pressed?"); // _registerViewFactory(r""" //

Welcome to My Website

//

This is a simple HTML page.

//

What is HTML?

//

HTML (HyperText Markup Language) is the most basic building block of the Web. It defines the meaning and structure of web content. Other technologies besides HTML are generally used to describe a web page's appearance/presentation (CSS) or functionality/behavior (JavaScript).

//

Here's a simple list:

//
    //
  • HTML elements are the building blocks of HTML pages
  • //
  • HTML uses tags like <tag> to organize and format content
  • //
  • CSS is used with HTML to style pages
  • //
//

Copyright © 2023

// """); // print("change"); // widget.emailContent = r" // " // }, ), Row( // title of email children: [ Text( widget.subject, style: TextStyle(fontSize: 30), ), ], ), Row( children: [ Text( 'from ${widget.name}', style: TextStyle(fontSize: 18), ), Text( '<${widget.from}>', style: TextStyle(fontSize: 18), ), Spacer(), Text( '${widget.date}', textAlign: TextAlign.right, ) ], ), // TODO: make a case where if one of these is the user's email it just says me :))))) Row( children: [ Text( 'to ${widget.to.toString()}', style: TextStyle(fontSize: 15), ) ], ), Expanded( child: HtmlElementView( key: UniqueKey(), viewType: viewTypeId, ), ), ], )); } }