From 2379ad394723d131dab5f0a22af258e978da10c5 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 26 Aug 2024 15:17:03 +0300 Subject: [PATCH] change on auth repo impl --- .../repositories/auth_repository_impl.dart | 1 - .../remote_data_source.dart | 1 - .../remote_data_source_impl.dart | 21 +- .../data/repository/chat_repository_impl.dart | 4 - .../presentation/pages/chat_home_screen.dart | 244 ++++++------------ .../data/data_sources/remote_data_source.dart | 2 +- .../Ecommerce/lib/injection_container.dart | 1 + 7 files changed, 93 insertions(+), 181 deletions(-) diff --git a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/auth/data/repositories/auth_repository_impl.dart b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/auth/data/repositories/auth_repository_impl.dart index 59bf19979..16f9a5be7 100644 --- a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/auth/data/repositories/auth_repository_impl.dart +++ b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/auth/data/repositories/auth_repository_impl.dart @@ -34,7 +34,6 @@ class AuthRepositoryImp implements AuthRepository { if (isConnected) { try { final userModel = await authRemoteDataSource.getUser(); - final toke = authLocalDataSource.getToken(); final userEntity = UserEntity.fromModel(userModel); sl().updateUser(userEntity); diff --git a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/data_source/remote_data_source/remote_data_source.dart b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/data_source/remote_data_source/remote_data_source.dart index 8d0708bc0..48c923868 100644 --- a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/data_source/remote_data_source/remote_data_source.dart +++ b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/data_source/remote_data_source/remote_data_source.dart @@ -26,5 +26,4 @@ abstract class RemoteDataSource { Future> getChatMessages(String chatId); Stream getMessages(); - Future updateAccessToken(String token); } diff --git a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/data_source/remote_data_source/remote_data_source_impl.dart b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/data_source/remote_data_source/remote_data_source_impl.dart index 028c92b0c..9c4802680 100644 --- a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/data_source/remote_data_source/remote_data_source_impl.dart +++ b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/data_source/remote_data_source/remote_data_source_impl.dart @@ -8,29 +8,30 @@ import 'package:socket_io_client/socket_io_client.dart' as IO; import '../../../../../core/constants/constants.dart'; import '../../../../../core/error/exception.dart'; +import '../../../../product/data/data_sources/local_data_source.dart'; import '../../model/chat_model.dart'; import '../../model/message_model.dart'; import 'remote_data_source.dart'; class RemoteDataSourceImpl extends RemoteDataSource { final http.Client client; - String accessToken; + final String accessToken; + final ProductLocalDataSource productLocalDataSource; late IO.Socket socket; final StreamController _messageStreamController = StreamController.broadcast(); - RemoteDataSourceImpl({required this.client, required this.accessToken}) { + RemoteDataSourceImpl( + {required this.client, + required this.accessToken, + required this.productLocalDataSource}) { _initializeWebSocket(); } - @override - Future updateAccessToken(String token) async { - accessToken = token; - } - @override Future> getAllChats() async { + final accessToken = productLocalDataSource.getToken(); try { final response = await client.get( Uri.parse(Urls.baseChat), @@ -39,7 +40,7 @@ class RemoteDataSourceImpl extends RemoteDataSource { 'Content-Type': 'application/json', }, ); - print(response.statusCode); + print(response.statusCode); if (response.statusCode == 200) { final result = json.decode(response.body)['data']; final List answer = []; @@ -61,6 +62,7 @@ class RemoteDataSourceImpl extends RemoteDataSource { @override Future getChatById(String chatId) async { try { + final accessToken = productLocalDataSource.getToken(); final response = await http.get( Uri.parse(Urls.getChatById(chatId)), headers: { @@ -82,6 +84,7 @@ class RemoteDataSourceImpl extends RemoteDataSource { @override Future initiateChat(String recieverId) async { try { + final accessToken = productLocalDataSource.getToken(); final response = await client.post(Uri.parse(Urls.baseChat), headers: { 'Authorization': 'Bearer $accessToken', @@ -101,6 +104,7 @@ class RemoteDataSourceImpl extends RemoteDataSource { Future> getChatMessages(String chatId) async { try { + final accessToken = productLocalDataSource.getToken(); final response = await client.get( Uri.parse(Urls.getChatMessages(chatId)), headers: { @@ -144,6 +148,7 @@ class RemoteDataSourceImpl extends RemoteDataSource { } void _initializeWebSocket() { + final accessToken = productLocalDataSource.getToken(); socket = IO.io( 'https://g5-flutter-learning-path-be.onrender.com', { diff --git a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/repository/chat_repository_impl.dart b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/repository/chat_repository_impl.dart index 240fc983b..dfcc355bc 100644 --- a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/repository/chat_repository_impl.dart +++ b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/data/repository/chat_repository_impl.dart @@ -94,8 +94,4 @@ class ChatRepositoryImpl implements ChatRepository { return Left(ServerFailure(error.toString())); } } - - void updateAccessToken(String newToken) { - _remoteDataSource.updateAccessToken(newToken); - } } diff --git a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/presentation/pages/chat_home_screen.dart b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/presentation/pages/chat_home_screen.dart index 867f84661..d3fe03d10 100644 --- a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/presentation/pages/chat_home_screen.dart +++ b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/chat/presentation/pages/chat_home_screen.dart @@ -13,7 +13,6 @@ import '../bloc/chat_bloc.dart'; class ChatHomeScreen extends StatefulWidget { const ChatHomeScreen({super.key}); - @override State createState() => _ChatHomeScreenState(); } @@ -23,7 +22,7 @@ class _ChatHomeScreenState extends State { void initState() { context.read().add(GetAllChatEvent()); // context.read().add(ListOfMessageEvent(chatId: '66cc462cdab43c1a2e9803cd')); - + super.initState(); } @@ -33,183 +32,96 @@ class _ChatHomeScreenState extends State { listener: (context, state) { // log('from the home $state'); }, -<<<<<<< HEAD - child: BlocBuilder( - builder: (context, state) { - return Scaffold( - backgroundColor: ChatColors.lightBlueColor, - appBar: AppBar( - leading: IconButton( - onPressed: () { - context.read().add(SendMessage( - chatId: '66cc55f0ef08f8f02f851d12', - type: 'text', - message: 'hellow there')); - }, - icon: const Icon( - Icons.search, - color: Colors.white, - )), - backgroundColor: Colors.transparent, - ), - body: Column(children: [ - SizedBox( - height: 130, - child: ListView.builder( - padding: const EdgeInsets.all(10), - shrinkWrap: true, - itemCount: 5, - scrollDirection: Axis.horizontal, - itemBuilder: (context, index) => Padding( - padding: const EdgeInsets.all(5.0), - child: Column( - children: [ - showUser(onClicked: () { - Navigator.of(context).push(MaterialPageRoute( - builder: (context) => - const IndividiualChatScreen(), - )); - }), - const Text( - 'Marina', - style: TextStyle(color: Colors.white), - ) - ], - ), - ), - ), - ), - const SizedBox( - height: 20, - ), - Expanded( - child: Container( - padding: const EdgeInsets.only(top: 30), - decoration: const BoxDecoration( - borderRadius: BorderRadius.only( - topLeft: Radius.circular(50), - topRight: Radius.circular(50)), - color: Colors.white), - child: ListView.builder( - scrollDirection: Axis.vertical, - shrinkWrap: true, - itemCount: 100, - itemBuilder: (context, index) => ListTile( - onTap: () { - Navigator.of(context).push(MaterialPageRoute( - builder: (context) => const IndividiualChatScreen(), - )); - }, - title: const Text('Alex Lindrson'), - subtitle: const Text('How are you?'), - leading: showUser(), - trailing: Column( - children: [ - const Text('2 min ago'), - CircleAvatar( - maxRadius: 10, - backgroundColor: ChatColors.lightBlueColor, - child: const Text('3'), - ) - ], - ), - ), - ), - ), - ) - ])); - }, - ), -======= - builder: (context,state){ - if(state is ChatFailureState){ + builder: (context, state) { + if (state is ChatFailureState) { return Center(child: Text('Error: ${state.message}')); - } - else if(state is! LoadedAllChatState){ + } else if (state is! LoadedAllChatState) { return Center(child: CircularProgressIndicator()); - } - - return Scaffold( - backgroundColor: ChatColors.lightBlueColor, - appBar: AppBar( - leading: IconButton( - onPressed: () {}, - icon: const Icon( - Icons.search, - color: Colors.white, - )), - backgroundColor: Colors.transparent, - ), - body: Column(children: [ - SizedBox( - height: 130, - child: ListView.builder( - padding: const EdgeInsets.all(10), - shrinkWrap: true, - itemCount: 5, - scrollDirection: Axis.horizontal, - itemBuilder: (context, index) => Padding( - padding: const EdgeInsets.all(5.0), - child: Column( - children: [ - showUser(onClicked: () { - // Navigator.of(context).push(MaterialPageRoute( - // builder: (context) => const IndividiualChatScreen(), - // )); - }), - const Text( - 'Marina', - style: TextStyle(color: Colors.white), - ) - ], - ), - ), - ), - ), - const SizedBox( - height: 20, + + return Scaffold( + backgroundColor: ChatColors.lightBlueColor, + appBar: AppBar( + leading: IconButton( + onPressed: () { + context.read().add(SendMessage( + chatId: '66cc55f0ef08f8f02f851d12', + type: 'text', + message: 'This is new')); + }, + icon: const Icon( + Icons.search, + color: Colors.white, + )), + backgroundColor: Colors.transparent, ), - Expanded( - child: Container( - padding: const EdgeInsets.only(top: 30), - decoration: const BoxDecoration( - borderRadius: BorderRadius.only( - topLeft: Radius.circular(50), - topRight: Radius.circular(50)), - color: Colors.white), + body: Column(children: [ + SizedBox( + height: 130, child: ListView.builder( - scrollDirection: Axis.vertical, + padding: const EdgeInsets.all(10), shrinkWrap: true, - itemCount: state.allChats.length, - itemBuilder: (context, index) => ListTile( - onTap: () { - Navigator.of(context).push(MaterialPageRoute( - // builder: (context) => IndividiualChatScreen(chatId:state.allChats[index].chatId ,), - builder: (context) => const IndividiualChatScreen(), - - )); - }, - title: Text(state.allChats[index].user2.name), - subtitle: const Text('How are you?'), - leading: showUser(), - trailing: Column( + itemCount: 5, + scrollDirection: Axis.horizontal, + itemBuilder: (context, index) => Padding( + padding: const EdgeInsets.all(5.0), + child: Column( children: [ - const Text('2 min ago'), - CircleAvatar( - maxRadius: 10, - backgroundColor: ChatColors.lightBlueColor, - child: const Text('3'), + showUser(onClicked: () { + // Navigator.of(context).push(MaterialPageRoute( + // builder: (context) => const IndividiualChatScreen(), + // )); + }), + const Text( + 'Marina', + style: TextStyle(color: Colors.white), ) ], ), ), ), ), - ) - ])); - }, ->>>>>>> f81ecbd09673f5cf80adf106b574110409027a2d + const SizedBox( + height: 20, + ), + Expanded( + child: Container( + padding: const EdgeInsets.only(top: 30), + decoration: const BoxDecoration( + borderRadius: BorderRadius.only( + topLeft: Radius.circular(50), + topRight: Radius.circular(50)), + color: Colors.white), + child: ListView.builder( + scrollDirection: Axis.vertical, + shrinkWrap: true, + itemCount: state.allChats.length, + itemBuilder: (context, index) => ListTile( + onTap: () { + Navigator.of(context).push(MaterialPageRoute( + // builder: (context) => IndividiualChatScreen(chatId:state.allChats[index].chatId ,), + builder: (context) => const IndividiualChatScreen(), + )); + }, + title: Text(state.allChats[index].user2.name), + subtitle: const Text('How are you?'), + leading: showUser(), + trailing: Column( + children: [ + const Text('2 min ago'), + CircleAvatar( + maxRadius: 10, + backgroundColor: ChatColors.lightBlueColor, + child: const Text('3'), + ) + ], + ), + ), + ), + ), + ) + ])); + }, ); } } diff --git a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/product/data/data_sources/remote_data_source.dart b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/product/data/data_sources/remote_data_source.dart index fd41d4500..d0b60fad7 100644 --- a/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/product/data/data_sources/remote_data_source.dart +++ b/mobile/ASTU-mobile-group-1/Ecommerce/lib/features/product/data/data_sources/remote_data_source.dart @@ -54,7 +54,7 @@ class ProductRemoteDataSourceImpl extends ProductRemoteDataSource { await productLocalDataSource.cacheProducts(products); print(products); - return products; + return products.reversed.toList(); } else { throw ServerException(); } diff --git a/mobile/ASTU-mobile-group-1/Ecommerce/lib/injection_container.dart b/mobile/ASTU-mobile-group-1/Ecommerce/lib/injection_container.dart index 39c694528..cb2e368e9 100644 --- a/mobile/ASTU-mobile-group-1/Ecommerce/lib/injection_container.dart +++ b/mobile/ASTU-mobile-group-1/Ecommerce/lib/injection_container.dart @@ -66,6 +66,7 @@ Future init() async { sl.registerLazySingleton(() => RemoteDataSourceImpl( client: sl(), + productLocalDataSource: sl(), accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImVlQGdtYWlsLmNvbSIsInN1YiI6IjY2Y2M0NTNlZGFiNDNjMWEyZTk4MDMwYSIsImlhdCI6MTcyNDY2MzE4MywiZXhwIjoxNzI1MDk1MTgzfQ.fsrJ3pS6R_N4jzAnOZBBX6RzD7PcZoxOxnPzzYWskY0'));