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.
 
 

145 lines
5.1 KiB

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<Inbox> {
final TesoUser user;
TesoUser currentUser = new TesoUser();
TextEditingController controller = new TextEditingController();
Uint8List bytes;
List<QueryDocumentSnapshot> listMessage = new List.from([]);
String id;
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<UserProvider>(context, listen: false).currentUser;
return Scaffold(
body: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('inbox')
.doc(id)
.collection("lastMessage")
.orderBy("timestamp", descending: false)
.snapshots(),
builder: (context, 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].data()['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].data()['peerID'];
username.firstname =
snapshot.data.docs[index].data()['firstname'];
username.lastname =
snapshot.data.docs[index].data()['surname'];
username.thumbnail_dp =
snapshot.data.docs[index].data()['thumbnail'];
username.username =
snapshot.data.docs[index].data()['username'];
InboxMessage message = new InboxMessage();
message.bio = "";
message.userID = snapshot.data.docs[index].data()['peerID'];
message.firstname =
snapshot.data.docs[index].data()['firstname'];
message.surname = snapshot.data.docs[index].data()['surname'];
message.thumbnail =
snapshot.data.docs[index].data()['thumbnail'];
message.message = snapshot.data.docs[index].data()['content'];
message.timestamp = DateTime.parse(formattedDate);
message.messageID = listMessage[index].reference.toString();
bool status;
if (snapshot.data.docs[index].data()['senderID'] == id) {
status = true;
} else if (snapshot.data.docs[index].data()['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),
);
},
);
}
}),
);
}
}