123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- import 'dart:convert';
- import 'dart:ui_web' as ui;
- import 'dart:html' as html;
- import 'augment.dart';
- import 'dart:js' as js;
- class MailAddress {
- final String? name;
- final String address;
- MailAddress({this.name, required this.address});
- factory MailAddress.fromJson(Map<String, dynamic> json) {
- return MailAddress(
- name: json['name'],
- address: json['address'],
- );
- }
- @override
- String toString() {
- // TODO: implement toString
- return '${name} <${address}>';
- }
- }
- class SerializableMessage {
- final String name;
- final String from;
- final List<MailAddress> to;
- final List<MailAddress> cc;
- final String hash;
- // final String path;
- final String subject;
- final String date;
- final int uid;
- final String list;
- final String id;
- final String in_reply_to;
- SerializableMessage({
- required this.name,
- required this.from,
- required this.to,
- required this.cc,
- required this.hash,
- required this.subject,
- required this.date,
- required this.uid,
- required this.list,
- required this.id,
- required this.in_reply_to,
- });
- factory SerializableMessage.fromJson(Map<String, dynamic> json) {
- var toList = json['to'] as List;
- var ccList = json['cc'] as List;
- return SerializableMessage(
- name: json['name'],
- from: json['from'],
- // to: json['name', 'address']
- to: toList.map((i) => MailAddress.fromJson(i)).toList(),
- cc: ccList.map((i) => MailAddress.fromJson(i)).toList(),
- // path: json['path'],
- hash: json['hash'],
- subject: json['subject'],
- date: json['date'],
- uid: json['uid'],
- list: json['list'],
- id: json['id'],
- in_reply_to: json['in_reply_to'],
- );
- }
- }
- class EmailPage extends StatefulWidget {
- const EmailPage({super.key});
- final String title = 'Emails';
- @override
- State<EmailPage> createState() => _EmailPageState();
- }
- class _EmailPageState extends State<EmailPage> {
- List emails = [];
- void _displayEmailsFromFolder(String folder) async {
- // Map<String, List<SerializableMessage>> messagesMap = {};
- List<SerializableMessage> allEmails = [];
- try {
- var url = Uri.http(
- '127.0.0.1:3001', 'sorted_threads_by_date', {'folder': folder});
- var response = await http.get(url);
- // print(response.body);
- // Map<String, dynamic> json = jsonDecode(response.body); original
- // json.forEach((key, value) {
- // List<SerializableMessage> messages = (value as List)
- // .map((item) => SerializableMessage.fromJson(item))
- // .toList();
- // messagesMap[key] = messages;
- // });
- // new shit
- if (response.statusCode == 200) {
- List<dynamic> json = jsonDecode(response.body);
- for (var item in json) {
- 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 fetchThreadMessages(threadId, allEmails);
- }
- }
- }
- } else {
- throw Exception('Failed to load threads');
- }
- } catch (e) {
- print('_displayEmailsFromFolder caught error: $e');
- }
- setState(() {
- emails.clear();
- // emails = messagesMap.values.toList().expand((list) => list).toList();
- emails.addAll(allEmails);
- ;
- });
- }
- Future<void> fetchThreadMessages(
- int threadId, List<SerializableMessage> allEmails) async {
- try {
- var url = Uri.http(
- '127.0.0.1:3001', 'get_thread_messages', {'id': threadId.toString()});
- var response = await http.get(url);
- if (response.statusCode == 200) {
- List<dynamic> messagesJson = jsonDecode(response.body);
- List<SerializableMessage> messages =
- messagesJson.map((mj) => SerializableMessage.fromJson(mj)).toList();
- allEmails.addAll(messages);
- } else {
- throw Exception(
- 'Failed to fetch thread messages for thread ID: $threadId');
- }
- } catch (e) {
- print('Error fetching thread messages: $e');
- }
- }
- Future<String> _getEmailContent(String id) async {
- String content = r"""
- """;
- try {
- 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;
- }
- } catch (e) {
- print('_getEmailContent caught error: $e');
- }
- return content;
- }
- Future<List<Widget>> _getDrawerItems() async {
- List<String> drawerItems = [];
- try {
- var url = Uri.http('127.0.0.1:3001', 'folders');
- var response = await http.get(url);
- drawerItems = List<String>.from(json.decode(response.body));
- } catch (e) {
- print('_getDrawerItems caught error: $e');
- }
- List<Widget> drawerWidgets = [];
- for (String item in drawerItems) {
- drawerWidgets.add(
- ListTile(
- leading: Icon(Icons.mail),
- title: Text(item),
- onTap: () {
- _displayEmailsFromFolder(item);
- Navigator.pop(context);
- },
- ),
- );
- }
- return drawerWidgets;
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- backgroundColor: Theme.of(context).colorScheme.inversePrimary,
- title: Text(widget.title),
- ),
- drawer: Drawer(
- child: FutureBuilder<List<Widget>>(
- future:
- _getDrawerItems(), // call the async function to get the future
- builder:
- (BuildContext context, AsyncSnapshot<List<Widget>> snapshot) {
- if (snapshot.connectionState == ConnectionState.waiting) {
- // While data is loading, show a progress indicator
- return Center(child: CircularProgressIndicator());
- } else if (snapshot.hasError) {
- // If something went wrong, show an error message
- return Center(child: Text('Error: ${snapshot.error}'));
- } else {
- // When data is fetched successfully, display the items
- return ListView(
- padding: EdgeInsets.zero,
- children:
- snapshot.data!, // Unwrap the data once confirmed it's there
- );
- }
- },
- ),
- ),
- body: EmailListScreen(
- emails: emails,
- getEmailContent: _getEmailContent,
- // getJsonEmail: _getThreadMessagesJson
- ),
- );
- }
- }
- class EmailListScreen extends StatelessWidget {
- final List emails;
- final Future<String> Function(String) getEmailContent;
- EmailListScreen({
- required this.emails,
- required this.getEmailContent,
- });
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Emails'),
- ),
- body: ListView.separated(
- itemCount: emails.length,
- itemBuilder: (context, index) {
- return ListTile(
- title: Text(emails[index].from,
- style: TextStyle(fontWeight: FontWeight.bold)),
- subtitle: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(emails[index].subject),
- ],
- ),
- trailing: Text(emails[index].date.toString()),
- onTap: () async {
- String emailContent = await getEmailContent(emails[index].id);
- String from = emails[index].from.toString();
- String name = emails[index].name.toString();
- String to = emails[index].to.toString();
- String cc = emails[index].cc.toString();
- String hash = emails[index].hash.toString();
- String subject = emails[index].subject.toString();
- String date = emails[index].date.toString();
- String uid = emails[index].uid.toString();
- String list = emails[index].list.toString();
- String id = emails[index].id.toString();
- String in_reply_to = emails[index].in_reply_to.toString();
- // String jsonbuilt =
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => EmailView(
- emailContent: emailContent,
- // jsonEmail: jsonContent,
- from: from, name: name, to: to, cc: cc, hash: hash,
- subject: subject, date: date,
- uid: uid, list: list, id: id,
- in_reply_to: in_reply_to,
- )),
- );
- });
- },
- separatorBuilder: (context, index) {
- return Divider();
- },
- ),
- );
- }
- }
- class EmailView extends StatefulWidget {
- final String emailContent;
- // final String jsonEmail;
- final String from;
- final String name;
- final String to;
- final String cc;
- final String hash;
- final String subject;
- final String date;
- final String uid;
- final String list;
- final String id;
- final String in_reply_to;
- const EmailView(
- {Key? key,
- required this.emailContent,
- // required this.jsonEmail,
- required this.from,
- required this.name,
- required this.to,
- required this.cc,
- required this.hash,
- required this.subject,
- required this.date,
- required this.uid,
- required this.list,
- required this.id,
- required this.in_reply_to})
- : super(key: key);
- @override
- _EmailViewState createState() => _EmailViewState();
- }
- class _EmailViewState extends State<EmailView> {
- late Key iframeKey;
- late String currentContent;
- late String viewTypeId;
- @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');
- });
- }
- @override
- Widget build(BuildContext context) {
- // print(currentContent);
- return Scaffold(
- appBar: AppBar(
- title: Text(widget.name),
- ),
- body: Column(
- children: [
- EmailToolbar(
- onButtonPressed: () {
- _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,
- ),
- ),
- ],
- ));
- }
- }
|