From 10846dcbc793439e18936b79f88721521a8e2fbf Mon Sep 17 00:00:00 2001 From: Lucas Delanier Date: Wed, 2 Aug 2023 16:42:27 +0200 Subject: [PATCH] change text artist and title --- .../components/setting_part_component.dart | 23 +++--- Sources/justMUSIC/lib/main.dart | 72 ++++++++++++++++++- .../justMUSIC/lib/screens/profile_screen.dart | 2 +- .../lib/view_model/UserViewModel.dart | 8 +-- 4 files changed, 87 insertions(+), 18 deletions(-) diff --git a/Sources/justMUSIC/lib/components/setting_part_component.dart b/Sources/justMUSIC/lib/components/setting_part_component.dart index 4541391..21e47f1 100644 --- a/Sources/justMUSIC/lib/components/setting_part_component.dart +++ b/Sources/justMUSIC/lib/components/setting_part_component.dart @@ -1,3 +1,4 @@ +import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:justmusic/values/icons.dart'; @@ -8,29 +9,32 @@ class SettingPartComponent extends StatelessWidget { final JustMusicIcon icon; final String label; final bool important; - const SettingPartComponent( - {Key? key, - required this.icon, - required this.label, - this.important = false}) + const SettingPartComponent({Key? key, required this.icon, required this.label, this.important = false}) : super(key: key); @override Widget build(BuildContext context) { + Future logout() async { + print("cc"); + await FirebaseAuth.instance.signOut(); + Navigator.pushNamed(context, '/welcome'); + } + return Material( color: important ? warningBttnColor : settingColor, borderOnForeground: false, child: InkWell( onTap: () { - print('InkWell was tapped!'); + if (icon == JustMusicIcon.cross) { + logout(); + } }, splashColor: Colors.transparent, highlightColor: Colors.white.withOpacity(0.08), child: Container( width: double.infinity, child: Padding( - padding: const EdgeInsets.fromLTRB( - defaultPadding, 19, defaultPadding, 19), + padding: const EdgeInsets.fromLTRB(defaultPadding, 19, defaultPadding, 19), child: Row( children: [ Image( @@ -45,8 +49,7 @@ class SettingPartComponent extends StatelessWidget { child: Text( label, overflow: TextOverflow.ellipsis, - style: GoogleFonts.plusJakartaSans( - color: Colors.white, fontWeight: FontWeight.w700), + style: GoogleFonts.plusJakartaSans(color: Colors.white, fontWeight: FontWeight.w700), ), ), Spacer(), diff --git a/Sources/justMUSIC/lib/main.dart b/Sources/justMUSIC/lib/main.dart index b2416fd..7baa4cb 100644 --- a/Sources/justMUSIC/lib/main.dart +++ b/Sources/justMUSIC/lib/main.dart @@ -1,6 +1,9 @@ +import 'dart:async'; import 'package:audioplayers/audioplayers.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_core/firebase_core.dart'; +import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @@ -13,10 +16,11 @@ import 'package:justmusic/screens/post_screen.dart'; import 'package:justmusic/screens/profile_screen.dart'; import 'package:justmusic/screens/registration_screen.dart'; import 'package:justmusic/screens/welcome_screen.dart'; +import 'package:justmusic/values/constants.dart'; import 'package:justmusic/view_model/MusicViewModel.dart'; import 'package:justmusic/view_model/PostViewModel.dart'; import 'package:justmusic/view_model/UserViewModel.dart'; - +import 'package:justmusic/model/User.dart' as userJustMusic; import 'firebase_options.dart'; Future main() async { @@ -27,8 +31,9 @@ Future main() async { runApp(const MyApp()); } -class MyApp extends StatelessWidget { +class MyApp extends StatefulWidget { static FirebaseFirestore db = FirebaseFirestore.instance; + static UserViewModel userViewModel = UserViewModel(); static MusicViewModel musicViewModel = MusicViewModel(); static PostViewModel postViewModel = PostViewModel(); @@ -36,6 +41,40 @@ class MyApp extends StatelessWidget { const MyApp({super.key}); + @override + State createState() => _MyAppState(); +} + +class _MyAppState extends State { + late StreamSubscription user; + Stream userCurrent = Stream.empty(); + + @override + void initState() { + super.initState(); + checkSignIn(); + } + + Future checkSignIn() async { + user = FirebaseAuth.instance.authStateChanges().listen((user) async { + if (user == null) { + print('User is currently signed out!'); + return null; + } else { + MyApp.userViewModel.userCurrent = (await (MyApp.userViewModel.getUser(user.uid)))!; + userCurrent = Stream.value(MyApp.userViewModel.userCurrent); + print('User is signed in!'); + } + }); + return null; + } + + @override + void dispose() { + user.cancel(); + super.dispose(); + } + // This widget is the root of your application. @override Widget build(BuildContext context) { @@ -69,7 +108,34 @@ class MyApp extends StatelessWidget { // is not restarted. primarySwatch: Colors.blue, ), - home: WellcomeScreen()); + home: FirebaseAuth.instance.currentUser != null + ? StreamBuilder( + stream: userCurrent, + initialData: null, + builder: (context, snapshot) { + if (snapshot.hasData) { + print("hasdata"); + + return AnimatedSwitcher( + duration: Duration(milliseconds: 1000), + transitionBuilder: (child, animation) { + return FadeTransition(opacity: animation, child: child); + }, + child: FeedScreen(), + ); + } else { + return Scaffold( + backgroundColor: bgColor, + body: Center( + child: Image( + image: AssetImage("assets/images/logo.png"), + width: 130, + ), + ), + ); + } + }) + : WellcomeScreen()); }, designSize: Size(390, 844), ); diff --git a/Sources/justMUSIC/lib/screens/profile_screen.dart b/Sources/justMUSIC/lib/screens/profile_screen.dart index 3fd339c..5d13b0c 100644 --- a/Sources/justMUSIC/lib/screens/profile_screen.dart +++ b/Sources/justMUSIC/lib/screens/profile_screen.dart @@ -80,7 +80,7 @@ class _ProfileScreenState extends State { ClipRRect( borderRadius: BorderRadius.circular(8), child: Column( - children: const [ + children: [ SettingPartComponent( icon: JustMusicIcon.profile, label: 'Compte', diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index 1636212..1817835 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -8,7 +8,7 @@ import '../main.dart'; class UserViewModel { late User _userCurrent; - final AuthService _authService = AuthService(); + final AuthService authService = AuthService(); final UserService _userService = UserService(); User get userCurrent => _userCurrent; @@ -27,7 +27,7 @@ class UserViewModel { login(String pseudo, String password) async { try { - await _authService.login(pseudo, password); + await authService.login(pseudo, password); final user = await MyApp.db.collection("users").doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid).get(); User data = UserMapper.toModel(user); _userCurrent = data; @@ -57,7 +57,7 @@ class UserViewModel { } try { - await _authService.register(pseudo.toLowerCase(), email, password); + await authService.register(pseudo.toLowerCase(), email, password); final user = await MyApp.db.collection("users").doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid).get(); User data = UserMapper.toModel(user); _userCurrent = data; @@ -88,7 +88,7 @@ class UserViewModel { } logout() { - _authService.signOut(); + authService.signOut(); } bool isFriend(String id) {