Browse Source

got back the functionality of switching between folders

juan 1 month ago
parent
commit
a34ca6512e
4 changed files with 40 additions and 160 deletions
  1. 25 18
      lib/email.dart
  2. 3 2
      lib/folder_drawer.dart
  3. 9 138
      lib/home_page.dart
  4. 3 2
      lib/main.dart

+ 25 - 18
lib/email.dart

@@ -1,8 +1,6 @@
 import 'package:flutter/material.dart';
 import 'api_service.dart';
 import 'structs.dart';
-import 'folder_drawer.dart';
-
 
 class EmailListScreen extends StatelessWidget {
   final List<GetThreadResponse> emails;
@@ -13,15 +11,13 @@ class EmailListScreen extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     return Scaffold(
-      appBar: AppBar(
-        title: Text('Emails'),
-      ),
       body: ListView.separated(
         itemCount: emails.length,
         itemBuilder: (context, index) {
           final email = emails[index];
           return ListTile(
-            title: Text(email.from_name, style: TextStyle(fontWeight: FontWeight.bold)),
+            title: Text(email.from_name,
+                style: TextStyle(fontWeight: FontWeight.bold)),
             subtitle: Column(
               crossAxisAlignment: CrossAxisAlignment.start,
               children: [Text(email.subject)],
@@ -52,32 +48,43 @@ class EmailListScreen extends StatelessWidget {
   }
 }
 
-
 class EmailPage extends StatefulWidget {
+  EmailPage({Key? key}) : super(key: key);
+  String selectedFolder = "INBOX";
+  
   @override
-  _EmailPageState createState() => _EmailPageState();
+  EmailPageState createState() => EmailPageState();
 }
 
-class _EmailPageState extends State<EmailPage> {
+class EmailPageState extends State<EmailPage> {
   final ApiService apiService = ApiService();
   List<GetThreadResponse> emails = [];
 
-  // Function to handle folder selection
-  void _onFolderSelected(String folder) async {
-    List<GetThreadResponse> fetchedEmails = await apiService.fetchEmailsFromFolder(folder, 0);
+  void updateSelectedFolder(String folder) {
     setState(() {
-      emails = fetchedEmails;
+      widget.selectedFolder = folder;
     });
+    print(folder);
+    _fetchEmails();
+  }
+
+  void _fetchEmails() async {
+    // print(selectedFolder)
+    try {
+      List<GetThreadResponse> fetchedEmails =
+          await apiService.fetchEmailsFromFolder(widget.selectedFolder, 0);
+      setState(() {
+        emails = fetchedEmails; // Update the list of emails
+      });
+    } catch (e) {
+      print('Error fetching emails: $e');
+    }
   }
 
   @override
   Widget build(BuildContext context) {
+    _fetchEmails();
     return Scaffold(
-      appBar: AppBar(title: Text('Email Page')),
-      drawer: FolderDrawer(
-        apiService: apiService,
-        onFolderTap: _onFolderSelected,
-      ),
       body: EmailListScreen(
         emails: emails,
         getEmailContent: apiService.fetchEmailContent,

+ 3 - 2
lib/folder_drawer.dart

@@ -45,7 +45,8 @@ class _FolderDrawerState extends State<FolderDrawer> {
               title: Text(folder),
               onTap: () {
                 widget.onFolderTap(folder);
-                Navigator.pop(context); // Close the drawer after selecting a folder
+                Navigator.pop(
+                    context); // Close the drawer after selecting a folder
               },
             );
           }).toList(),
@@ -53,7 +54,7 @@ class _FolderDrawerState extends State<FolderDrawer> {
             leading: Icon(Icons.refresh),
             title: Text('Refresh Folders'),
             onTap: () {
-              _fetchFolders();  // Refresh folders when this tile is tapped
+              _fetchFolders(); // Refresh folders when this tile is tapped
               Navigator.pop(context); // Close the drawer after refresh
             },
           ),

+ 9 - 138
lib/home_page.dart

@@ -1,59 +1,37 @@
-// import 'package:crab_ui/email.dart';
 import 'package:crab_ui/folder_drawer.dart';
-
+import 'package:flutter/widgets.dart';
 import 'api_service.dart';
 import 'package:flutter/material.dart';
 import 'email.dart';
-import 'dart:html' as html;
-import 'dart:ui_web' as ui;
 
 class HomeScreen extends StatefulWidget {
   @override
   _HomeScreenState createState() => _HomeScreenState();
 }
 
-//TODO: ADD ALL THE DIFF FOLDERS TO THE SIDEBAR
+
 class _HomeScreenState extends State<HomeScreen> {
-  // final _emailPageState = EmailPageState();
   final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
+  final GlobalKey<EmailPageState> _emailPageKey = GlobalKey<EmailPageState>();
   ApiService apiService = ApiService();
+  bool _isSidebarOpen = true;
 
   @override
   void initState() {
     super.initState();
   }
 
-  bool _isSidebarOpen = true;
-  // void holder() async {
-  //   List<Widget> drawerItems = await EmailPageState.getDrawerItems(context);
-  // }
-
-  // Method to handle what happens when a folder is tapped
-  void _onFolderSelected(String folder) async {
-    // Fetch emails from the selected folder using the ApiService
-    // List fetchedEmails = await apiService.fetchEmailsFromFolder(folder);
-    // setState(() {
-    //   emails = fetchedEmails; // Update the email list
-    // });
-    print(folder);
-  }
 
   @override
   Widget build(BuildContext context) {
-    final List<String> emails = [
-      'Welcome to Flutter!',
-      'You have new updates',
-      'Your order is shipped',
-      'Important: Security update',
-      'Meeting tomorrow',
-    ];
     return Scaffold(
       key: _scaffoldKey,
       drawer: FolderDrawer(
-        apiService: apiService, 
-        onFolderTap: _onFolderSelected,
+        apiService: apiService,
+        onFolderTap: (folder) {
+          _emailPageKey.currentState?.updateSelectedFolder(folder);
+        },
       ),
-
       body: Stack(
         children: [
           Row(
@@ -78,119 +56,13 @@ class _HomeScreenState extends State<HomeScreen> {
                           // Navigate to Settings
                         },
                       ),
-                      // EmailPage(),
-                      // GestureDetector(
-
-                      //     // onTapDown: (TapDownDetails details) {
-                      //     //   _showPopupMenu(context, details.globalPosition);
-                      //     // },
-                      //   // child: Expanded(
-                      //   child: Scaffold(
-                      //     appBar: AppBar(
-                      //       // leading: Icon(Icons.email),
-                      //       // flexibleSpace: Padding(
-                      //       //   padding: const EdgeInsets.only(
-                      //       //       top: 40), // Adjust padding as needed
-
-                      //         // child: Wrap(
-                      //         //   spacing: 10.0,
-                      //         //   alignment: WrapAlignment.center,
-                      //         //   // children: [
-                      //         //   //   ListTile(leading: Icon(Icons.mail),),
-                      //         //   //   ListTile(leading: Icon(Icons.mail),),
-                      //         //   //   ListTile(leading: Icon(Icons.mail),),
-                      //         //   // ],
-                      //         // ),
-                      //       // ),
-                      //       // ],
-                      //     ),
-                      //     body: ListView.separated(
-                      //       itemBuilder: (context, index) {
-                      //         return ListTile(
-                      //           leading: Icon(Icons.mail),
-                      //           title: Text(emails[index]),
-                      //         );
-                      //       },
-                      //       separatorBuilder: (context, index) => Divider(),
-                      //       itemCount: 5,
-                      //     ),
-                      //   )
                       ListTile(
                         leading: Icon(Icons.email),
                         onTap: () {
                           _scaffoldKey.currentState?.openDrawer();
                         },
                       ),
-                      
-                      // leading: SizedBox(
-                      //   width: 20,
-                      //   child: GestureDetector(
-                      //       onTapDown: (TapDownDetails details) {
-                      //         _showPopupMenu(context, details.globalPosition);
-                      //       },
-                      //       child: SizedBox(
-                      //         width: 40,
-                      //         child: Icon(Icons.email)
-                      // ),
-                      // ),
-                      // ),
-                      // ),
-
-                      // trailing: SizedBox(
-                      //   width: 20,
-                      //   child: PopupMenuButton<String>(
-                      //     onSelected: (value) {
-                      //       print(value);
-                      //       print('ajdsnv');
-                      //     },
-                      //     itemBuilder: (BuildContext context) {
-                      //       return <PopupMenuEntry<String>>[
-                      //         PopupMenuItem<String>(
-                      //           value: 'Open',
-                      //           child: Text('Open'),
-                      //         )
-                      //       ];
-                      //     },
-                      //   ),
-                      // ),
-                      // ),
-
-                      // onTap: () {
-                      //   Navigator.push(
-                      //     context,
-                      //     MaterialPageRoute(
-                      //         builder: (context) => EmailPage()),
-                      //   );
-                      // },
-
-                      // ),
-
-                      // Drawer(
-                      //     child: FutureBuilder<List<Widget>>()
-                      //   future: _emailPageState.getDrawerItems(context), // 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
-                      //       );
-                      //     }
-                      //   },
-                      // )
-                      // ),
                       Spacer(),
-                      
                       Padding(
                         padding: const EdgeInsets.all(8.0),
                         child: Align(
@@ -251,12 +123,11 @@ class _HomeScreenState extends State<HomeScreen> {
                           Container(
                             child: Text('hiiiiiii'),
                           ),
-                          
                         ],
                       ),
                     ),
                     Expanded(
-                      child: EmailPage(selectedFolder: "INBOX",),
+                      child: EmailPage(key: _emailPageKey),
                     )
                   ],
                 ),

+ 3 - 2
lib/main.dart

@@ -1,8 +1,9 @@
 import 'package:crab_ui/contact.dart';
 import 'package:flutter/material.dart';
 import 'home_page.dart';
-import 'api_service.dart';
+// import 'api_service.dart';
 import 'login.dart';
+import 'email.dart';
 
 void main() {
   WidgetsFlutterBinding.ensureInitialized();
@@ -25,7 +26,7 @@ class HyM extends StatelessWidget {
 
       routes: {
         "/login": (context) => const LoginPage(),
-        "/email": (context) => EmailPage(),
+        // "/email": (context) => EmailPage(),
         "/contacts": (context) => ContactsPage(),
       },
     );