diff --git a/lib/email.dart b/lib/email.dart index 296f61b..244194e 100644 --- a/lib/email.dart +++ b/lib/email.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:markdown/markdown.dart' as md; import 'api_service.dart'; import 'structs.dart'; import 'emailView.dart'; @@ -8,24 +9,28 @@ class EmailListScreen extends StatefulWidget { final Future> Function(List, String) getEmailContent; final String folder; final GlobalKey<_EmailListScreenState> key; + final Function(List)? onSelectionChanged; - EmailListScreen( - {required this.key, - required this.emails, - required this.getEmailContent, - required this.folder}) - : super(key: key); + EmailListScreen({ + required this.key, + required this.emails, + required this.getEmailContent, + required this.folder, + this.onSelectionChanged, + }) : super(key: key); @override _EmailListScreenState createState() => _EmailListScreenState(); } -class _EmailListScreenState extends State { +class _EmailListScreenState extends State + with TickerProviderStateMixin { late List selectStates; // for checkboxes if its selected or not late List selectedEmails = []; // holds the emails that are selected i.e. the emails that got the checkbox on final Set _hoveredRows = {}; //the row that is being hovered over atm bool bulkSelectMenu = false; + final GlobalKey _emailPageKey = GlobalKey(); @override void initState() { @@ -42,21 +47,57 @@ class _EmailListScreenState extends State { } bool selectAllChecks(bool selectionType) { + //perhaps it should return a list of the selected setState(() { + selectedEmails = []; if (selectionType) { bulkSelectMenu = true; - } - for (int email = 0; email < selectStates.length; email++) { - selectStates[email] = selectionType; + for (int email = 0; email < selectStates.length; email++) { + selectStates[email] = selectionType; + selectedEmails.add(widget.emails[email]); + } + } else { + for (int email = 0; email < selectStates.length; email++) { + selectStates[email] = selectionType; + } + selectedEmails = []; } }); + widget.onSelectionChanged?.call(selectedEmails); printTheSelected(); return false; } + bool markAsRead(bool read) { + print("markasread $read"); + setState(() { + if (read) { + //read + for (int email = 0; email < selectedEmails.length; email++) { + selectedEmails[email].seen = read; + ApiService() + .markAsSeen(selectedEmails[email].id); //the remote or .json + } + } else { + //unread + for (int email = 0; email < selectedEmails.length; email++) { + selectedEmails[email].seen = read; + ApiService() + .markAsUnseen(selectedEmails[email].id); //the remote or .json + print(selectedEmails[email].subject); + } + } + }); + return false; + } + + List listOfSelectedThreads() { + return selectedEmails; + } + void printTheSelected() { for (int i = 0; i < selectedEmails.length; i++) { - print(selectedEmails); + print(selectedEmails[i].subject); } } @@ -83,11 +124,19 @@ class _EmailListScreenState extends State { setState(() { //works great selectStates[index] = value ?? false; - if (value!) { - selectedEmails.add(widget.emails[index]); - } else { - selectedEmails.remove(widget.emails[index]); - } + + setState(() { + if (value!) { + selectedEmails.add(widget.emails[index]); + //here i must update the other side + _emailPageKey.currentState?.getListOfSelected(); + } else { + selectedEmails.remove(widget.emails[index]); + _emailPageKey.currentState?.getListOfSelected(); + } + widget.onSelectionChanged?.call(selectedEmails); + print(selectedEmails); + }); }); }, ), @@ -97,7 +146,6 @@ class _EmailListScreenState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [Text(email.subject)], ), - // tileColor: () , trailing: _hoveredRows.contains(index) ? Row( mainAxisSize: MainAxisSize.min, @@ -161,10 +209,12 @@ class _EmailListScreenState extends State { // ignore: must_be_immutable class EmailPage extends StatefulWidget { - EmailPage({Key? key}) : super(key: key); String selectedFolder = "INBOX"; //starter int offset = 0; int page = 1; + final Function(List)? onSelectionChanged; + + EmailPage({Key? key, this.onSelectionChanged}) : super(key: key); @override EmailPageState createState() => EmailPageState(); @@ -239,9 +289,19 @@ class EmailPageState extends State { bool selectAllEmails(bool selectionType) { emailListKey.currentState?.selectAllChecks(selectionType); - return false; + return selectionType; } + bool markSelectedAsRead(bool selectionType) { + emailListKey.currentState?.markAsRead(selectionType); + return selectionType; + } + + List getListOfSelected() { + return emailListKey.currentState!.listOfSelectedThreads() ?? []; + } + // return [GetThreadResponse(id: 1, messages: [], subject: "subject", date: DateTime(2025), from_name: "from_name", from_address: "from_address", to: [], seen: false)]; + @override Widget build(BuildContext context) { return Scaffold( @@ -251,6 +311,7 @@ class EmailPageState extends State { // getEmailContent: apiService.fetchEmailContent, getEmailContent: apiService.fetchMarkdownContent, folder: widget.selectedFolder, //try to grab from it directly + onSelectionChanged: widget.onSelectionChanged, )); } }