123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- // 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<List<GetThreadResponse>> 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<GetThreadResponse> 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<int> threadIDs = List<int>.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<void> fetchThreads(
- //populates allEmails, which is the List that contains all the emails in a thread
- int threadId,
- List<GetThreadResponse> 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<String, dynamic> 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<List<SerializableMessage>> 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<dynamic> messagesJson = json.decode(response.body);
- List<SerializableMessage> messages =
- messagesJson.map((mj) => SerializableMessage.fromJson(mj)).toList();
- return messages;
- }
- } catch (e) {
- print("caught $e");
- }
- return [];
- }
- Future<String> fetchEmailContent(List<String> 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 += "<hr>";
- }
- }
- } catch (e) {
- print('_getEmailContent caught error: $e');
- }
- return content;
- }
- // void _addMailBox async(BuildContext context){
- // //add email folder
- // showDialog(context: context, builder: builder)
- // }
- Future<List<String>> fetchFolders() async {
- try {
- var url = Uri.http('127.0.0.1:3001', 'folders');
- var response = await http.get(url);
- return List<String>.from(json.decode(response.body));
- } catch (e) {
- print('fetchFolders caught error: $e');
- return [];
- }
- }
- Future<void> createFolder(String folderName) async {
- var url = Uri.http('127.0.0.1:3001', 'create_folder');
- Map<String, String> 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<void> deleteFolder(String folderName) async {
- var url = Uri.http('127.0.0.1:3001', 'delete_folder');
- Map<String, String> 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<EmailView> {
- 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"""
- // <h1>Welcome to My Website</h1>
- // <p>This is a simple HTML page.</p>
- // <h2>What is HTML?</h2>
- // <p>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).</p>
- // <h3>Here's a simple list:</h3>
- // <ul>
- // <li>HTML elements are the building blocks of HTML pages</li>
- // <li>HTML uses tags like <code><tag></code> to organize and format content</li>
- // <li>CSS is used with HTML to style pages</li>
- // </ul>
- // <p>Copyright © 2023</p>
- // """);
- // 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,
- ),
- ),
- ],
- ));
- }
- }
|