Increment done and improvement on the model done

GET_CAPSULES_LDE-EKA
root 2 years ago
parent 79d975e05a
commit a46f6325ba

@ -16,7 +16,13 @@ class PostComponent extends StatefulWidget {
final Function(int)? callback; final Function(int)? callback;
final Post post; final Post post;
final int index; final int index;
PostComponent({Key? key, required this.callback, required this.post, required this.index}) : super(key: key);
PostComponent(
{Key? key,
required this.callback,
required this.post,
required this.index})
: super(key: key);
@override @override
State<PostComponent> createState() => _PostComponentState(); State<PostComponent> createState() => _PostComponentState();
@ -27,11 +33,11 @@ class _PostComponentState extends State<PostComponent> {
DateTime today = DateTime.now(); DateTime today = DateTime.now();
Future<User?> fetchUserData() async { Future<User?> fetchUserData() async {
return await MyApp.userViewModel.getUser(widget.post.idUser); //return await MyApp.userViewModel.getUser(widget.post.idUser);
} }
Future<Music> fetchMusicData() async { Future<Music?> fetchMusicData() async {
return await MyApp.musicViewModel.getMusic(widget.post.idMusic); //return await MyApp.musicViewModel.getMusic(widget.post.idMusic);
} }
void switchChoice() { void switchChoice() {
@ -81,37 +87,58 @@ class _PostComponentState extends State<PostComponent> {
child: Padding( child: Padding(
padding: const EdgeInsets.only(left: 10), padding: const EdgeInsets.only(left: 10),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment:
CrossAxisAlignment.start,
children: [ children: [
Text( Text(
user.pseudo, user.pseudo,
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
color: Colors.white, fontWeight: FontWeight.w600), color: Colors.white,
fontWeight: FontWeight.w600),
), ),
Text( widget.post.location.item2 != null
"${widget.post.location.item1}, ${widget.post.location.item2}", ? Text(
style: GoogleFonts.plusJakartaSans( "${widget.post.location?.item1}, ${widget.post.location?.item2}",
color: Colors.white.withOpacity(0.4), style:
fontWeight: FontWeight.w300, GoogleFonts.plusJakartaSans(
color: Colors.white
.withOpacity(0.4),
fontWeight:
FontWeight.w300,
fontSize: 13),
)
: Text(
"",
style:
GoogleFonts.plusJakartaSans(
color: Colors.white
.withOpacity(0.4),
fontWeight:
FontWeight.w300,
fontSize: 13), fontSize: 13),
) )
], ],
), ),
), ),
), ),
DateTime(today.year, today.month, today.day).isAtSameMomentAs( DateTime(today.year, today.month, today.day)
DateTime(widget.post.date.year, widget.post.date.month, widget.post.date.day)) .isAtSameMomentAs(DateTime(
widget.post.date.year,
widget.post.date.month,
widget.post.date.day))
? Text( ? Text(
"Aujourd'hui, ${widget.post.date.hour}:${widget.post.date.minute}", "Aujourd'hui, ${widget.post.date.hour}:${widget.post.date.minute}",
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
color: Colors.white.withOpacity(0.4), color:
Colors.white.withOpacity(0.4),
fontWeight: FontWeight.w300, fontWeight: FontWeight.w300,
fontSize: 13), fontSize: 13),
) )
: Text( : Text(
"${widget.post.date.day}/${widget.post.date.month}/${widget.post.date.year}-${widget.post.date.hour}:${widget.post.date.minute}", "${widget.post.date.day}/${widget.post.date.month}/${widget.post.date.year}-${widget.post.date.hour}:${widget.post.date.minute}",
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
color: Colors.white.withOpacity(0.4), color:
Colors.white.withOpacity(0.4),
fontWeight: FontWeight.w300, fontWeight: FontWeight.w300,
fontSize: 13), fontSize: 13),
), ),
@ -125,7 +152,8 @@ class _PostComponentState extends State<PostComponent> {
} }
}, },
enableLongTapRepeatEvent: false, enableLongTapRepeatEvent: false,
longTapRepeatDuration: const Duration(milliseconds: 100), longTapRepeatDuration:
const Duration(milliseconds: 100),
begin: 1.0, begin: 1.0,
end: 0.99, end: 0.99,
beginDuration: const Duration(milliseconds: 70), beginDuration: const Duration(milliseconds: 70),
@ -138,10 +166,13 @@ class _PostComponentState extends State<PostComponent> {
decoration: BoxDecoration( decoration: BoxDecoration(
// add border // add border
border: const GradientBoxBorder( border: const GradientBoxBorder(
gradient: LinearGradient(colors: [ gradient: LinearGradient(
Colors.transparent, colors: [
Color(0xFF323232), Colors.transparent,
], begin: Alignment.topCenter, end: Alignment.bottomCenter), Color(0xFF323232),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
width: 2.5, width: 2.5,
), ),
// set border radius // set border radius
@ -154,7 +185,9 @@ class _PostComponentState extends State<PostComponent> {
alignment: Alignment.bottomCenter, alignment: Alignment.bottomCenter,
children: [ children: [
Image( Image(
image: NetworkImage(choice ? widget.post.selfie! : music.cover!), image: NetworkImage(choice
? widget.post.selfie!
: music.cover!),
fit: BoxFit.cover, fit: BoxFit.cover,
width: double.infinity, width: double.infinity,
), ),
@ -165,22 +198,35 @@ class _PostComponentState extends State<PostComponent> {
child: Padding( child: Padding(
padding: EdgeInsets.all(12), padding: EdgeInsets.all(12),
child: Container( child: Container(
constraints: BoxConstraints(maxWidth: 140, maxHeight: 140), constraints:
BoxConstraints(
maxWidth: 140,
maxHeight: 140),
width: 90.sp, width: 90.sp,
height: 90.sp, height: 90.sp,
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white, color: Colors.white,
// add border // add border
border: Border.all(width: 3, color: Colors.white), border: Border.all(
width: 3,
color:
Colors.white),
// set border radius // set border radius
borderRadius: BorderRadius.circular(15), borderRadius:
BorderRadius
.circular(15),
), ),
child: ClipRRect( child: ClipRRect(
borderRadius: BorderRadius.circular(13), borderRadius:
BorderRadius
.circular(13),
// implement image // implement image
child: Image( child: Image(
image: image: NetworkImage(
NetworkImage(choice ? music.cover! : widget.post.selfie!), choice
? music.cover!
: widget.post
.selfie!),
fit: BoxFit.cover, fit: BoxFit.cover,
), ),
), ),
@ -202,13 +248,18 @@ class _PostComponentState extends State<PostComponent> {
child: TextScroll( child: TextScroll(
music.artists.first.name!, music.artists.first.name!,
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
height: 1, color: Colors.white, fontWeight: FontWeight.w600, fontSize: 26.h), height: 1,
color: Colors.white,
fontWeight: FontWeight.w600,
fontSize: 26.h),
mode: TextScrollMode.endless, mode: TextScrollMode.endless,
pauseBetween: Duration(milliseconds: 500), pauseBetween: Duration(milliseconds: 500),
velocity: Velocity(pixelsPerSecond: Offset(20, 0)), velocity: Velocity(
pixelsPerSecond: Offset(20, 0)),
)), )),
Padding( Padding(
padding: EdgeInsets.only(bottom: 10.h, right: 5.w, left: 5.w), padding: EdgeInsets.only(
bottom: 10.h, right: 5.w, left: 5.w),
child: ClipOval( child: ClipOval(
child: Container( child: Container(
width: 5.h, width: 5.h,
@ -229,8 +280,10 @@ class _PostComponentState extends State<PostComponent> {
fontWeight: FontWeight.w300, fontWeight: FontWeight.w300,
fontSize: 16.h), fontSize: 16.h),
mode: TextScrollMode.endless, mode: TextScrollMode.endless,
velocity: Velocity(pixelsPerSecond: Offset(50, 20)), velocity: Velocity(
pauseBetween: Duration(milliseconds: 500), pixelsPerSecond: Offset(50, 20)),
pauseBetween:
Duration(milliseconds: 500),
), ),
)), )),
Container(width: 10), Container(width: 10),
@ -280,26 +333,45 @@ class _PostComponentState extends State<PostComponent> {
child: Padding( child: Padding(
padding: const EdgeInsets.only(left: 10), padding: const EdgeInsets.only(left: 10),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment:
CrossAxisAlignment.start,
children: [ children: [
Text( Text(
user.pseudo, user.pseudo,
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
color: Colors.white, fontWeight: FontWeight.w600), color: Colors.white,
fontWeight: FontWeight.w600),
), ),
Text( widget.post.location.item2 != null
"${widget.post.location.item1}, ${widget.post.location.item2}", ? Text(
style: GoogleFonts.plusJakartaSans( "${widget.post.location?.item1}, ${widget.post.location?.item2}",
color: Colors.white.withOpacity(0.4), style:
fontWeight: FontWeight.w300, GoogleFonts.plusJakartaSans(
color: Colors.white
.withOpacity(0.4),
fontWeight:
FontWeight.w300,
fontSize: 13),
)
: Text(
"",
style:
GoogleFonts.plusJakartaSans(
color: Colors.white
.withOpacity(0.4),
fontWeight:
FontWeight.w300,
fontSize: 13), fontSize: 13),
) )
], ],
), ),
), ),
), ),
DateTime(today.year, today.month, today.day).isAtSameMomentAs( DateTime(today.year, today.month, today.day)
DateTime(widget.post.date.year, widget.post.date.month, widget.post.date.day)) .isAtSameMomentAs(DateTime(
widget.post.date.year,
widget.post.date.month,
widget.post.date.day))
? Text( ? Text(
"Aujourd'hui, ${widget.post.date.hour}:${widget.post.date.minute}", "Aujourd'hui, ${widget.post.date.hour}:${widget.post.date.minute}",
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
@ -322,7 +394,8 @@ class _PostComponentState extends State<PostComponent> {
widget.callback!(widget.index); widget.callback!(widget.index);
}, },
enableLongTapRepeatEvent: false, enableLongTapRepeatEvent: false,
longTapRepeatDuration: const Duration(milliseconds: 100), longTapRepeatDuration:
const Duration(milliseconds: 100),
begin: 1.0, begin: 1.0,
end: 0.99, end: 0.99,
beginDuration: const Duration(milliseconds: 70), beginDuration: const Duration(milliseconds: 70),
@ -335,10 +408,13 @@ class _PostComponentState extends State<PostComponent> {
decoration: BoxDecoration( decoration: BoxDecoration(
// add border // add border
border: const GradientBoxBorder( border: const GradientBoxBorder(
gradient: LinearGradient(colors: [ gradient: LinearGradient(
Colors.transparent, colors: [
Color(0xFF323232), Colors.transparent,
], begin: Alignment.topCenter, end: Alignment.bottomCenter), Color(0xFF323232),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
width: 2.5, width: 2.5,
), ),
// set border radius // set border radius
@ -356,7 +432,8 @@ class _PostComponentState extends State<PostComponent> {
width: double.infinity, width: double.infinity,
), ),
Image( Image(
image: AssetImage("assets/images/shadow_post.png"), image: AssetImage(
"assets/images/shadow_post.png"),
fit: BoxFit.fitHeight, fit: BoxFit.fitHeight,
width: double.infinity, width: double.infinity,
), ),
@ -365,7 +442,9 @@ class _PostComponentState extends State<PostComponent> {
child: AutoSizeText( child: AutoSizeText(
'${widget.post.description}', '${widget.post.description}',
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
color: Colors.white, fontWeight: FontWeight.w400, fontSize: 15.sp), color: Colors.white,
fontWeight: FontWeight.w400,
fontSize: 15.sp),
maxFontSize: 20, maxFontSize: 20,
maxLines: 1, maxLines: 1,
), ),
@ -377,21 +456,31 @@ class _PostComponentState extends State<PostComponent> {
child: Padding( child: Padding(
padding: EdgeInsets.all(12), padding: EdgeInsets.all(12),
child: Container( child: Container(
constraints: BoxConstraints(maxWidth: 140, maxHeight: 140), constraints: BoxConstraints(
maxWidth: 140,
maxHeight: 140),
width: 90.sp, width: 90.sp,
height: 90.sp, height: 90.sp,
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white, color: Colors.white,
// add border // add border
border: Border.all(width: 3, color: Colors.white), border: Border.all(
width: 3,
color: Colors.white),
// set border radius // set border radius
borderRadius: BorderRadius.circular(15), borderRadius:
BorderRadius.circular(
15),
), ),
child: ClipRRect( child: ClipRRect(
borderRadius: BorderRadius.circular(13), borderRadius:
BorderRadius.circular(
13),
// implement image // implement image
child: Image( child: Image(
image: NetworkImage(widget.post.selfie!), image: NetworkImage(
widget
.post.selfie!),
fit: BoxFit.cover, fit: BoxFit.cover,
), ),
), ),
@ -413,13 +502,18 @@ class _PostComponentState extends State<PostComponent> {
child: TextScroll( child: TextScroll(
music.artists.first.name!, music.artists.first.name!,
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
height: 1, color: Colors.white, fontWeight: FontWeight.w600, fontSize: 26.h), height: 1,
color: Colors.white,
fontWeight: FontWeight.w600,
fontSize: 26.h),
mode: TextScrollMode.endless, mode: TextScrollMode.endless,
pauseBetween: Duration(milliseconds: 500), pauseBetween: Duration(milliseconds: 500),
velocity: Velocity(pixelsPerSecond: Offset(20, 0)), velocity: Velocity(
pixelsPerSecond: Offset(20, 0)),
)), )),
Padding( Padding(
padding: EdgeInsets.only(bottom: 10.h, right: 5.w, left: 5.w), padding: EdgeInsets.only(
bottom: 10.h, right: 5.w, left: 5.w),
child: ClipOval( child: ClipOval(
child: Container( child: Container(
width: 5.h, width: 5.h,
@ -435,9 +529,13 @@ class _PostComponentState extends State<PostComponent> {
child: TextScroll( child: TextScroll(
music.title!, music.title!,
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
height: 1, color: Colors.white, fontWeight: FontWeight.w300, fontSize: 16.h), height: 1,
color: Colors.white,
fontWeight: FontWeight.w300,
fontSize: 16.h),
mode: TextScrollMode.endless, mode: TextScrollMode.endless,
velocity: Velocity(pixelsPerSecond: Offset(50, 20)), velocity: Velocity(
pixelsPerSecond: Offset(50, 20)),
pauseBetween: Duration(milliseconds: 500), pauseBetween: Duration(milliseconds: 500),
), ),
)), )),
@ -445,7 +543,9 @@ class _PostComponentState extends State<PostComponent> {
AutoSizeText( AutoSizeText(
music.date.toString(), music.date.toString(),
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
color: Colors.white.withOpacity(0.5), fontWeight: FontWeight.w300, fontSize: 16.h), color: Colors.white.withOpacity(0.5),
fontWeight: FontWeight.w300,
fontSize: 16.h),
textAlign: TextAlign.end, textAlign: TextAlign.end,
maxFontSize: 20, maxFontSize: 20,
), ),

@ -1,23 +1,26 @@
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
import 'Music.dart';
import 'User.dart';
class Post { class Post {
final String _id; final String _id;
final String _idUser; final User _user;
String? _description; String? _description;
String _idMusic; late Music _music;
Tuple2<String,String> _location; Tuple2<String?,String?> _location;
int _nblikes; int _nblikes;
String? _selfie; String? _selfie;
DateTime _date; DateTime _date;
// Constructor // Constructor
Post(this._id, this._idUser, this._description, this._idMusic, this._location, Post(this._id, this._user, this._description, this._location,
this._nblikes, this._selfie, this._date); this._nblikes, this._selfie, this._date);
//Getters and setters //Getters and setters
String get id => _id; String get id => _id;
String get idUser => _idUser; User get user => _user;
String? get description => _description; String? get description => _description;
@ -25,15 +28,15 @@ class Post {
_description = value; _description = value;
} }
String get idMusic => _idMusic; Music get music => _music;
set idMusic(String value) { set music(Music value) {
_idMusic = value; _music = value;
} }
Tuple2<String, String> get location => _location; Tuple2<String?, String?> get location => _location;
set location(Tuple2<String, String> value) { set location(Tuple2<String?, String?> value) {
_location = value; _location = value;
} }

@ -1,19 +1,23 @@
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
import '../../main.dart';
import '../Music.dart';
import '../Post.dart'; import '../Post.dart';
import '../User.dart';
class PostMapper { class PostMapper {
static Post toModel(DocumentSnapshot<Map<String, dynamic>> snapshot) { static Future<Post> toModel (
DocumentSnapshot<Map<String, dynamic>> snapshot) async {
final data = snapshot.data(); final data = snapshot.data();
return Post( User? user = await MyApp.userViewModel.getUser(data?['user_id']);
snapshot.id, return Post(
data?["user_id"], snapshot.id,
data?["description"], user!,
data?["song_id"], data?["description"],
Tuple2(data?["place"][0], data?["place"][1]), Tuple2(data?["place"][0], data?["place"][1]),
data?["likes"], data?["likes"],
data?["selfie"], data?["selfie"],
data?["date"].toDate()); data?["date"].toDate());
} }
} }

@ -17,6 +17,9 @@ class AuthService {
"pseudo": pseudo, "pseudo": pseudo,
"phone_number": "", "phone_number": "",
"unique_id": uniqueId, "unique_id": uniqueId,
"followed": 0,
"nbCapsules": 0,
"followers": 0,
"picture": "picture":
"https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/defaultImage.png?alt=media&token=cff5ae0a-e29e-4845-91f7-817597962f6b", "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/defaultImage.png?alt=media&token=cff5ae0a-e29e-4845-91f7-817597962f6b",
"friends": [], "friends": [],

@ -21,6 +21,17 @@ class PostService {
}; };
var postAdd = await MyApp.db.collection("posts").add(post); 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});
}
});
if (image != null) { if (image != null) {
var imageRef = FirebaseStorage.instance.ref('$id${postAdd.id}.jpg'); var imageRef = FirebaseStorage.instance.ref('$id${postAdd.id}.jpg');
await imageRef.putFile(image); await imageRef.putFile(image);

@ -4,6 +4,8 @@ import 'package:justmusic/model/Post.dart';
import 'package:justmusic/services/PostService.dart'; import 'package:justmusic/services/PostService.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
import '../main.dart';
import '../model/Music.dart';
import '../model/mapper/PostMapper.dart'; import '../model/mapper/PostMapper.dart';
class PostViewModel { class PostViewModel {
@ -20,7 +22,8 @@ class PostViewModel {
List<Post> get bestPosts => _bestPosts; List<Post> get bestPosts => _bestPosts;
// Methods // Methods
addPost(String? description, String idMusic, File? image, Tuple2<String, String>? location) async { addPost(String? description, String idMusic, File? image,
Tuple2<String, String>? location) async {
await _postService.createPost(description, idMusic, image, location); await _postService.createPost(description, idMusic, image, location);
} }
@ -35,9 +38,16 @@ class PostViewModel {
getBestPosts() async { getBestPosts() async {
try { try {
var responseData = await _postService.getPopularPosts(); var responseData = await _postService.getPopularPosts();
List<String> ids = [];
_bestPosts = responseData.map((value) => PostMapper.toModel(value)).toList(); var postsFutures = responseData.map((value) {
print(_bestPosts.length); ids.add(value.data()["song_id"]);
return PostMapper.toModel(value);
}).toList();
_bestPosts = await Future.wait(postsFutures);
List<Music> musics = await MyApp.musicViewModel.getMusicsWithIds(ids);
for (int i = 0; i < _bestPosts.length; i++) {
_bestPosts[i].music = musics[i];
}
} catch (e) { } catch (e) {
print(e); print(e);
} }

Loading…
Cancel
Save