2 Commits 254972d2af ... aaaacade4d

Author SHA1 Message Date
  juan aaaacade4d cleaned, and new endpoint 2 months ago
  juan f867312af3 purged unnecessay comments out, and dead code 2 months ago
4 changed files with 77 additions and 163 deletions
  1. 18 141
      lib/api_service.dart
  2. 0 6
      lib/augment.dart
  3. 7 4
      lib/main.dart
  4. 52 12
      lib/structs.dart

+ 18 - 141
lib/api_service.dart

@@ -1,12 +1,10 @@
 import 'package:crab_ui/structs.dart';
-import 'package:english_words/english_words.dart';
 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;
+import 'dart:html' as html;
 
 //data structure
 class MailAddress {
@@ -28,77 +26,28 @@ class MailAddress {
   }
 }
 
-// //data structure
-// class SerializableMessage {
-//   final String name;
-//   final String from;
-//   final List<MailAddress> to;
-//   final List<MailAddress> cc;
-//   final String hash;
-
-//   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();
+  State<EmailPage> createState() => EmailPageState();
 }
 
-class _EmailPageState extends State<EmailPage> {
+class EmailPageState extends State<EmailPage> {
   List emails = [];
 
   void _displayEmailsFromFolder(String folder) async {
-    // Map<String, List<SerializableMessage>> messagesMap = {};
     List<GetThreadResponse> allEmails = []; //all the emails
 
     try {
       var url = Uri.http(
-          '127.0.0.1:3001', 'sorted_threads_by_date', {'folder': folder});
+          '127.0.0.1:3001', 'sorted_threads_by_date', 
+          {'folder': folder,
+           'limit': '10',
+           'offset': '0'});
       var response = await http.get(url);
-
+      print(response);
       if (response.statusCode == 200) {
         List json = jsonDecode(response.body);
         for (var item in json.take(1)) {
@@ -106,11 +55,8 @@ class _EmailPageState extends State<EmailPage> {
           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) {
-              // print(threadId);
-              // await fetchThreadMessages(threadId, allEmails);
               await fetchThreads(threadId, allEmails);
             }
-            //TODO: get exact thread with new api endpoint from chosen thread?
           }
         }
       } else {
@@ -124,39 +70,21 @@ class _EmailPageState extends State<EmailPage> {
     setState(() {
       emails = allEmails;
     });
-    // print(emails[0]);
-    // Print allEmails to debug its structure
-    // print("allEmails: ${allEmails[0].messages}");
-
-    // Convert allEmails to a list
   }
-// }
 
   Future<void> fetchThreads(
-      //complete
-      int threadId,
-      List<GetThreadResponse> allEmails) async {
+      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);
-      // print(response.body);
+
       if (response.statusCode == 200) {
         Map<String, dynamic> messagesJson = jsonDecode(response.body);
-        // print(messagesJson);
-        // List<String> messagesofThread = messagesJson['messages'];
-        // messagesJson.map((mj) => GetThreadResponse.fromJson(mj)).toList();
-        // List<GetThreadResponse> messages = messagesJson.values.map((mj) {
-        //   return GetThreadResponse.fromJson(mj as Map<String, dynamic>);
-        // }).toList();
-        // List<GetThreadResponse> thread =
-        //     messagesJson.map((mj) => GetThreadResponse.fromJson(mj)).toList();
         GetThreadResponse threadResponse =
             GetThreadResponse.fromJson(messagesJson);
 
-        allEmails.add(
-            threadResponse); //adds all the messages of the thread into all emails
-        //perhaps should change
+        allEmails.add(threadResponse);
       } else {
         throw Exception(
             'Failed to fetch thread messages for thread ID: $threadId');
@@ -166,33 +94,12 @@ class _EmailPageState extends State<EmailPage> {
     }
   }
 
-  // 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(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});
 
@@ -200,16 +107,16 @@ class _EmailPageState extends State<EmailPage> {
 
         if (response.statusCode == 200) {
           content += response.body;
+          content += "<p>new shit</p><br><br><br>";
         }
       }
     } catch (e) {
       print('_getEmailContent caught error: $e');
     }
-    print(content);
     return content;
   }
 
-  Future<List<Widget>> _getDrawerItems() async {
+  Future<List<Widget>> getDrawerItems(BuildContext context) async {
     List<String> drawerItems = [];
 
     try {
@@ -217,7 +124,7 @@ class _EmailPageState extends State<EmailPage> {
       var response = await http.get(url);
       drawerItems = List<String>.from(json.decode(response.body));
     } catch (e) {
-      print('_getDrawerItems caught error: $e');
+      print('getDrawerItems caught error: $e');
     }
 
     List<Widget> drawerWidgets = [];
@@ -248,7 +155,7 @@ class _EmailPageState extends State<EmailPage> {
       drawer: Drawer(
         child: FutureBuilder<List<Widget>>(
           future:
-              _getDrawerItems(), // call the async function to get the future
+              getDrawerItems(context), // call the async function to get the future
           builder:
               (BuildContext context, AsyncSnapshot<List<Widget>> snapshot) {
             if (snapshot.connectionState == ConnectionState.waiting) {
@@ -312,18 +219,12 @@ class EmailListScreen extends StatelessWidget {
               onTap: () async {
                 String emailContent =
                     await getEmailContent(emails[index].messages);
-                String messages = emails[index].messages.toString();
                 String fromName = emails[index].from_name.toString();
                 String fromAddress = emails[index].from_address.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();
 
                 Navigator.push(
                   context,
@@ -333,17 +234,10 @@ class EmailListScreen extends StatelessWidget {
                             from: fromAddress,
                             name: fromName,
                             to: to,
-                            // cc: cc,
-                            // hash: hash,
                             subject: subject,
                             date: date,
-                            // uid: uid,
-                            // list: list,
                             id: id,
-                            // in_reply_to: in_reply_to,
-                          )
-                      //nada hpta
-                      ),
+                          )),
                 );
               });
         },
@@ -357,39 +251,22 @@ class EmailListScreen extends StatelessWidget {
 
 class EmailView extends StatefulWidget {
   final String emailContent;
-  // final String jsonEmail;
   final String from;
   final String name;
-
   final String to;
-
-  // 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.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();
@@ -426,7 +303,7 @@ class _EmailViewState extends State<EmailView> {
     AugmentClasses.handleJump(spanId);
   }
 
-  // void _invisibility(String )
+  // TODO: void _invisibility(String )
 
   @override
   Widget build(BuildContext context) {

+ 0 - 6
lib/augment.dart

@@ -1,12 +1,6 @@
-import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
-import 'package:flutter/widgets.dart';
-import 'package:http/http.dart' as http;
-import 'dart:convert';
-import 'dart:ui_web' as ui;
 import 'dart:html' as html;
 import 'dart:js' as js;
-import 'api_service.dart';
 
 class EmailToolbar extends StatefulWidget {
   final Function(String) onJumpToSpan;

+ 7 - 4
lib/main.dart

@@ -1,18 +1,18 @@
 import 'package:crab_ui/contact.dart';
 import 'package:flutter/material.dart';
-import 'package:flutter/rendering.dart';
 import 'home_page.dart';
 import 'api_service.dart';
-import 'dart:html' as html;
+import 'login.dart';
 
 void main() {
   WidgetsFlutterBinding.ensureInitialized();
   // debugPaintSizeEnabled = true;
-  runApp(const HyM());
+  runApp( HyM());
 }
 
 class HyM extends StatelessWidget {
-  const HyM({super.key});
+  final AuthService _authService = AuthService();
+  HyM({super.key});
 
   @override
   Widget build(BuildContext context) {
@@ -20,8 +20,11 @@ class HyM extends StatelessWidget {
       debugShowCheckedModeBanner: false,
       theme: ThemeData.light(),
       title: 'HyM',
+      // home: HomeScreen(),
       home: HomeScreen(),
+
       routes: {
+        "/login": (context) => const LoginPage(),
         "/email": (context) => EmailPage(),
         "/contacts": (context) => ContactsPage(),
       },

+ 52 - 12
lib/structs.dart

@@ -1,13 +1,4 @@
-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;
 import 'api_service.dart';
-import 'dart:convert';  // For JSON decoding
-import 'package:intl/intl.dart'; 
 
 
 class GetThreadResponse {
@@ -29,13 +20,10 @@ class GetThreadResponse {
     required this.to,
   });
   factory GetThreadResponse.fromJson(Map<String, dynamic> json) {
-    var messagesList = json['messages'] as List<dynamic>;
     var toList = json['to'] as List<dynamic>;
-    // var ccList = json['cc'] as List;
 
     return GetThreadResponse (
       id: json['id'],
-      // messages: messagesList.map((message) => message.toString()).toList(),
       messages: List<String>.from(json['messages']),
       subject: json['subject'],
       date: DateTime.parse(json['date']),
@@ -45,3 +33,55 @@ class GetThreadResponse {
     );
   }
 }
+
+
+// //old data structure
+// class SerializableMessage {
+//   final String name;
+//   final String from;
+//   final List<MailAddress> to;
+//   final List<MailAddress> cc;
+//   final String hash;
+
+//   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'],
+//     );
+//   }
+// }