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

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

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

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

@ -21,6 +21,17 @@ class PostService {
};
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) {
var imageRef = FirebaseStorage.instance.ref('$id${postAdd.id}.jpg');
await imageRef.putFile(image);

@ -4,6 +4,8 @@ import 'package:justmusic/model/Post.dart';
import 'package:justmusic/services/PostService.dart';
import 'package:tuple/tuple.dart';
import '../main.dart';
import '../model/Music.dart';
import '../model/mapper/PostMapper.dart';
class PostViewModel {
@ -20,7 +22,8 @@ class PostViewModel {
List<Post> get bestPosts => _bestPosts;
// 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);
}
@ -35,9 +38,16 @@ class PostViewModel {
getBestPosts() async {
try {
var responseData = await _postService.getPopularPosts();
_bestPosts = responseData.map((value) => PostMapper.toModel(value)).toList();
print(_bestPosts.length);
List<String> ids = [];
var postsFutures = responseData.map((value) {
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) {
print(e);
}

Loading…
Cancel
Save