You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.0 KiB

3 years ago
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;
3 years ago
const Inbox({Key? key, this.user}) : super(key: key);
3 years ago
@override
_InboxState createState() => _InboxState(user: this.user);
}
class _InboxState extends State<Inbox> {
final TesoUser? user;
TesoUser? currentUser = new TesoUser();
3 years ago
TextEditingController controller = new TextEditingController();
Uint8List? bytes;
List<QueryDocumentSnapshot>? listMessage = new List.from([]);
String? id;
late SharedPreferences prefs;
Timer? timer;
3 years ago
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<UserProvider>(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<QuerySnapshot> snapshot) {
3 years ago
if (snapshot.data == null) {
return Container(
child: Center(
child: CupertinoActivityIndicator(
animating: true,
radius: 15,
),
),
);
} else {
listMessage = snapshot.data!.docs;
3 years ago
return ListView.builder(
padding: EdgeInsets.all(5.0),
itemCount: listMessage!.length,
3 years ago
itemBuilder: (context, index) {
int timeInMillis =
int.parse(snapshot.data!.docs[index]['timestamp']);
3 years ago
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'];
3 years ago
username.firstname =
snapshot.data!.docs[index]['firstname'];
3 years ago
username.lastname =
snapshot.data!.docs[index]['surname'];
3 years ago
username.thumbnail_dp =
snapshot.data!.docs[index]['thumbnail'];
3 years ago
username.username =
snapshot.data!.docs[index]['username'];
3 years ago
InboxMessage message = new InboxMessage();
message.bio = "";
message.userID = snapshot.data!.docs[index]['peerID'];
3 years ago
message.firstname =
snapshot.data!.docs[index]['firstname'];
message.surname = snapshot.data!.docs[index]['surname'];
3 years ago
message.thumbnail =
snapshot.data!.docs[index]['thumbnail'];
message.message = snapshot.data!.docs[index]['content'];
3 years ago
message.timestamp = DateTime.parse(formattedDate);
message.messageID = listMessage![index].reference.toString();
3 years ago
bool status;
if (snapshot.data!.docs[index]['senderID'] == id) {
3 years ago
status = true;
} else if (snapshot.data!.docs[index]['senderID'] !=
3 years ago
id &&
!listMessage![index]['read']) {
3 years ago
status = false;
} else {
status = true;
}
return InkWell(
onTap: () => Navigator.push(
context,
PageTransition(
child: ChatScreen(
user: username,
),
type: PageTransitionType.leftToRightWithFade),
),
child: buildInboxTile(context, message, status),
);
},
);
}
}),
);
}
}