diff --git a/Sources/justMUSIC/lib/main.dart b/Sources/justMUSIC/lib/main.dart index fd68156..935bbd1 100644 --- a/Sources/justMUSIC/lib/main.dart +++ b/Sources/justMUSIC/lib/main.dart @@ -12,6 +12,7 @@ import 'package:justmusic/screens/profile_screen.dart'; import 'package:justmusic/screens/registration_screen.dart'; import 'package:justmusic/screens/welcome_screen.dart'; import 'package:justmusic/view_model/MusicViewModel.dart'; +import 'package:justmusic/view_model/PostViewModel.dart'; import 'package:justmusic/view_model/UserViewModel.dart'; import 'firebase_options.dart'; @@ -28,6 +29,7 @@ class MyApp extends StatelessWidget { static FirebaseFirestore db = FirebaseFirestore.instance; static UserViewModel userViewModel = UserViewModel(); static MusicViewModel musicViewModel = MusicViewModel(); + static PostViewModel postViewModel = PostViewModel(); static AudioPlayer audioPlayer = AudioPlayer(); const MyApp({super.key}); diff --git a/Sources/justMUSIC/lib/model/mapper/UserMapper.dart b/Sources/justMUSIC/lib/model/mapper/UserMapper.dart index 8ef5bbc..036a838 100644 --- a/Sources/justMUSIC/lib/model/mapper/UserMapper.dart +++ b/Sources/justMUSIC/lib/model/mapper/UserMapper.dart @@ -6,7 +6,7 @@ class UserMapper { SnapshotOptions? options) { final data = snapshot.data(); return User( - data?["uid"] ?? "", + snapshot.id, data?["unique_id"] ?? "", data?["country"] ?? "", data?["mail"] ?? "", diff --git a/Sources/justMUSIC/lib/screens/post_screen.dart b/Sources/justMUSIC/lib/screens/post_screen.dart index 1c68a80..f87b384 100644 --- a/Sources/justMUSIC/lib/screens/post_screen.dart +++ b/Sources/justMUSIC/lib/screens/post_screen.dart @@ -7,7 +7,9 @@ import 'package:justmusic/screens/search_song_screen.dart'; import 'package:tuple/tuple.dart'; import '../components/editable_post_component.dart'; import '../components/post_button_component.dart'; +import '../main.dart'; import '../model/Music.dart'; +import '../model/Post.dart'; import '../values/constants.dart'; class PostScreen extends StatefulWidget { @@ -91,10 +93,9 @@ class _PostScreenState extends State ); } - displayinfo() { - print("cc"); - print( - "${selectedCity},${selectedMusic?.title},${selectedImage?.path},${description}"); + handleSubmit() async { + await MyApp.postViewModel.addPost( + description, (selectedMusic?.id)!, selectedImage, selectedCity); } @override @@ -150,7 +151,7 @@ class _PostScreenState extends State ), PostButtonComponent( empty: selectedMusic == null, - callback: displayinfo, + callback: handleSubmit, ), SizedBox( height: 40.h, diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index 499533d..c07059b 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -1,14 +1,32 @@ +import 'dart:io'; + +import 'package:tuple/tuple.dart'; +import 'package:firebase_storage/firebase_storage.dart'; + import '../main.dart'; -import '../model/Post.dart'; -import '../model/mapper/PostMapper.dart'; class PostService { - createPost(Post post) { - MyApp.db - .collection("posts") - .add(PostMapper.toFirebase(post)) - .then((value) => print("Post Added")) - .catchError((error) => print("Failed to add post: $error")); + 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], + "selfie": null, + "song_id": idMusic, + "likes": 0 + }; + + var postAdd = await MyApp.db.collection("posts").add(post); + + if (image != null) { + var imageUrl = FirebaseStorage.instance.ref(id + postAdd.id); + await imageUrl.putFile(image); + postAdd.update({"selfie": imageUrl}); + } } deletePost() {} diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart index 0ee2ec2..b61d96a 100644 --- a/Sources/justMUSIC/lib/view_model/PostViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/PostViewModel.dart @@ -1,5 +1,8 @@ +import 'dart:io'; + import 'package:justmusic/model/Post.dart'; import 'package:justmusic/services/PostService.dart'; +import 'package:tuple/tuple.dart'; class PostViewModel { List _postsFriends = []; @@ -15,8 +18,9 @@ class PostViewModel { List get bestPosts => _bestPosts; // Methods - addPost(Post post) async { - await _postService.createPost(post); + addPost(String? description, String idMusic, File? image, + Tuple2? location) async { + await _postService.createPost(description, idMusic, image, location); } List getPostsFriends() { diff --git a/Sources/justMUSIC/pubspec.lock b/Sources/justMUSIC/pubspec.lock index 7903bf2..ecf84cd 100644 --- a/Sources/justMUSIC/pubspec.lock +++ b/Sources/justMUSIC/pubspec.lock @@ -289,6 +289,30 @@ packages: url: "https://pub.dev" source: hosted version: "2.6.0" + firebase_storage: + dependency: "direct main" + description: + name: firebase_storage + sha256: "4b747005aee0c611242cdd553f58795f51e1567d2dfd4f75692fac3f67c8c336" + url: "https://pub.dev" + source: hosted + version: "11.2.5" + firebase_storage_platform_interface: + dependency: transitive + description: + name: firebase_storage_platform_interface + sha256: c77c7b6b7d283280993c81ea8ac95552b2ae521a7bb46a95181c1482e62d1633 + url: "https://pub.dev" + source: hosted + version: "4.4.4" + firebase_storage_web: + dependency: transitive + description: + name: firebase_storage_web + sha256: "6906245579f1af225e43df0395c9d9631cb3135cbfa3521a839196d3383bb89a" + url: "https://pub.dev" + source: hosted + version: "3.6.5" flutter: dependency: "direct main" description: flutter diff --git a/Sources/justMUSIC/pubspec.yaml b/Sources/justMUSIC/pubspec.yaml index 808fac1..230f3be 100644 --- a/Sources/justMUSIC/pubspec.yaml +++ b/Sources/justMUSIC/pubspec.yaml @@ -63,6 +63,7 @@ dependencies: animated_appear: ^0.0.4 geolocator: ^9.0.2 tuple: ^2.0.2 + firebase_storage: ^11.2.5 dev_dependencies: flutter_test: