import 'dart:io'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:tuple/tuple.dart'; import 'package:firebase_storage/firebase_storage.dart'; import '../main.dart'; class PostService { createPost(String? description, String idMusic, File? image, Tuple2? location) async { var id = MyApp.userViewModel.userCurrent.id; final post = { "user_id": id, "description": description, "date": DateTime.now(), "place": [location?.item1, location?.item2], "song_id": idMusic, "likes": 0 }; var postAdd = await MyApp.db.collection("posts").add(post); var userRef = MyApp.db.collection("users").doc(id); await MyApp.db.runTransaction((transaction) async { var userSnapshot = await transaction.get(userRef); if (userSnapshot.exists) { int currentNbCapsules = userSnapshot.data()?['nbCapsules'] ?? 0; transaction.update(userRef, {'nbCapsules': currentNbCapsules + 1}); MyApp.userViewModel.userCurrent.capsules++; } }); if (image != null) { var imageRef = FirebaseStorage.instance.ref('$id${postAdd.id}.jpg'); await imageRef.putFile(image); var imageUrl = await imageRef.getDownloadURL(); postAdd.update({"selfie": imageUrl}); } } deletePost() {} Future>>> getPopularPosts({int limit = 10, int offset = 0}) async { DateTime twentyFourHoursAgo = DateTime.now().subtract(Duration(hours: 24)); Timestamp twentyFourHoursAgoTimestamp = Timestamp.fromDate(twentyFourHoursAgo); QuerySnapshot> response = await FirebaseFirestore.instance .collection("posts") .where("date", isGreaterThan: twentyFourHoursAgoTimestamp) .limit(limit) .get(); var filteredPosts = response.docs.where((doc) { String user = doc["user_id"]; return user != MyApp.userViewModel.userCurrent.id; }).toList(); return filteredPosts; } Timestamp _getTwentyFourHoursAgoTimestamp() { DateTime twentyFourHoursAgo = DateTime.now().subtract(Duration(hours: 24)); return Timestamp.fromDate(twentyFourHoursAgo); } Future>>> getPostsFriends({int limit = 10, int offset = 0}) async { var timestamp = _getTwentyFourHoursAgoTimestamp(); var response = await FirebaseFirestore.instance .collection("posts") .where("user_id", whereIn: MyApp.userViewModel.userCurrent.followed) .where("date", isGreaterThan: timestamp) .orderBy("date") .limit(limit) .get(); return response.docs; } Future getAvailable(String idUser) async { DateTime today = DateTime.now(); QuerySnapshot> response = await FirebaseFirestore.instance.collection("posts").where("user_id", isEqualTo: idUser).get(); bool isTodayAvailable = response.docs.any((doc) { DateTime date = doc["date"].toDate(); // Assuming the field name is "date" return date.day == today.day && date.month == today.month && date.year == today.year; }); return !isTodayAvailable; } Future> recapSevenDays(String id) async { List recapList = []; DateTime sevenDaysAgo = DateTime.now().subtract(Duration(days: 6)); QuerySnapshot> response = await FirebaseFirestore.instance.collection("posts").where("user_id", isEqualTo: id).get(); List?> postList = response.docs.map((DocumentSnapshot> doc) => doc.data()).toList(); for (int i = 0; i < 7; i++) { DateTime date = sevenDaysAgo.add(Duration(days: i)); bool postExists = postList.any((post) => post?["date"] != null && post?["date"].toDate().year == date.year && post?["date"].toDate().month == date.month && post?["date"].toDate().day == date.day); recapList.add(postExists); } return recapList; } }