import 'dart:async'; import 'dart:typed_data'; import 'package:intl/intl.dart'; import 'package:teso/Classes/TesoUser.dart'; import 'package:teso/Classes/inbox.dart'; import 'package:teso/Pages/PageWidgets/Inbox/inboxTile.dart'; import 'package:teso/Pages/Sub_Pages/Notifications/ChatScreen.dart'; import 'package:teso/providers/pageAnimations.dart'; import 'package:flutter/material.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:teso/providers/user_provider.dart'; import 'package:provider/provider.dart'; import 'package:flutter/cupertino.dart'; class Inbox extends StatefulWidget { final TesoUser? user; const Inbox({Key? key, this.user}) : super(key: key); @override _InboxState createState() => _InboxState(user: this.user); } class _InboxState extends State { final TesoUser? user; TesoUser? currentUser = new TesoUser(); TextEditingController controller = new TextEditingController(); Uint8List? bytes; List? listMessage = new List.from([]); String? id; late SharedPreferences prefs; Timer? timer; int counter = 0; _InboxState({this.user}); @override void initState() { readLocal(); super.initState(); timer = Timer.periodic(Duration(seconds: 30), (Timer t) => addValue()); } void addValue() { setState(() { counter++; }); } @override void dispose() { controller.dispose(); timer?.cancel(); super.dispose(); } readLocal() async { prefs = await SharedPreferences.getInstance(); id = prefs.getString('id') ?? ''; setState(() {}); } @override Widget build(BuildContext context) { currentUser = Provider.of(context, listen: false).currentUser; return Scaffold( body: StreamBuilder( stream: FirebaseFirestore.instance .collection('inbox') .doc(id) .collection("lastMessage") .orderBy("timestamp", descending: false) .snapshots(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.data == null) { return Container( child: Center( child: CupertinoActivityIndicator( animating: true, radius: 15, ), ), ); } else { listMessage = snapshot.data!.docs; return ListView.builder( padding: EdgeInsets.all(5.0), itemCount: listMessage!.length, itemBuilder: (context, index) { int timeInMillis = int.parse(snapshot.data!.docs[index]['timestamp']); var date = DateTime.fromMillisecondsSinceEpoch(timeInMillis); var formattedDate = DateFormat("yyyy-MM-dd HH:mm:ss").format(date); TesoUser username = new TesoUser(); username.userGUID = snapshot.data!.docs[index]['peerID']; username.firstname = snapshot.data!.docs[index]['firstname']; username.lastname = snapshot.data!.docs[index]['surname']; username.thumbnail_dp = snapshot.data!.docs[index]['thumbnail']; username.username = snapshot.data!.docs[index]['username']; InboxMessage message = new InboxMessage(); message.bio = ""; message.userID = snapshot.data!.docs[index]['peerID']; message.firstname = snapshot.data!.docs[index]['firstname']; message.surname = snapshot.data!.docs[index]['surname']; message.thumbnail = snapshot.data!.docs[index]['thumbnail']; message.message = snapshot.data!.docs[index]['content']; message.timestamp = DateTime.parse(formattedDate); message.messageID = listMessage![index].reference.toString(); bool status; if (snapshot.data!.docs[index]['senderID'] == id) { status = true; } else if (snapshot.data!.docs[index]['senderID'] != id && !listMessage![index]['read']) { status = false; } else { status = true; } return InkWell( onTap: () => Navigator.push( context, PageTransition( child: ChatScreen( user: username, ), type: PageTransitionType.leftToRightWithFade), ), child: buildInboxTile(context, message, status), ); }, ); } }), ); } }