From 8ec55f95e1a354968171de9434e4eefdd6e23461 Mon Sep 17 00:00:00 2001 From: Shubhamshirva Date: Wed, 14 Aug 2024 13:49:57 +0530 Subject: [PATCH] some fixes for community details page and network api services to check if user token is invalid --- .../controller/data/network/network_api.dart | 73 ++++++++++++--- lib/sidemenu/Community/Group/view/Group.dart | 3 +- .../Community/Members/CommunityMembers.dart | 1 + .../Community/MyCommunity/AddGroups.dart | 22 ++++- .../MyCommunity/View/CommunityDetails.dart | 88 +++++++++++-------- 5 files changed, 134 insertions(+), 53 deletions(-) diff --git a/lib/Common/controller/data/network/network_api.dart b/lib/Common/controller/data/network/network_api.dart index 853694a..663e624 100644 --- a/lib/Common/controller/data/network/network_api.dart +++ b/lib/Common/controller/data/network/network_api.dart @@ -6,6 +6,8 @@ import 'package:flutter/foundation.dart'; import 'package:get/get.dart' hide Response; import 'package:regroup/Global.dart'; import 'package:regroup/Common/base_manager.dart'; +import 'package:regroup/Utils/dialogs.dart'; +import 'package:regroup/resources/routes/route_name.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; @@ -16,8 +18,8 @@ class NetworkApiServices { Dio dio = Dio(); final controllerEntryPoint = Get.put(EntryPointController()); - String basicAuth = 'Basic ${base64.encode( - utf8.encode('RegroupUserName:71%@L%es^bUX94`J9XT*@bh,._WWM{\$%^^&&'))}'; + String basicAuth = + 'Basic ${base64.encode(utf8.encode('RegroupUserName:71%@L%es^bUX94`J9XT*@bh,._WWM{\$%^^&&'))}'; Future getApi(String url, {bool optionalpar = false}) async { if (kDebugMode) { @@ -45,12 +47,48 @@ class NetworkApiServices { : Options( headers: { 'authorization': basicAuth, - 'access-token': token + 'access-token': + token }, ), ); - } on Exception catch (e) { - log(e.toString()); + } + on Exception catch (e) { + // log(e.toString()); + if (e is DioException) { + log(e.response.toString()); + if (e.response == null) { + return ResponseData( + 'Oops something Went Wrong, Please try again!', + ResponseStatus.FAILED, + ); + } + if (e.response!.statusCode == 401) { + if (e.response!.data['message'] == 'Invalid token') { + // prefs.remove('access-token'); + // await prefs.clear(); + // Get.offNamed(RouteName.loginScreen); + utils.showToast('Please login again'); + } else { + return ResponseData( + 'Oops something Went Wrong, Please try again!', + ResponseStatus.FAILED, + ); + } + + } + if (e.response!.statusCode == 403) { + if (e.response!.data['message'] is List) { + return ResponseData( + e.response!.data['message'][0]!, ResponseStatus.FAILED, + data: e.response!.data); + } else { + return ResponseData( + e.response!.data['message'], ResponseStatus.FAILED, + data: e.response!.data); + } + } + } return ResponseData( 'Oops something Went Wrong', ResponseStatus.FAILED); } @@ -174,7 +212,9 @@ class NetworkApiServices { }, ) : Options( - headers: {'authorization': basicAuth, 'access-token': token}, + headers: {'authorization': basicAuth, 'access-token': + token + }, ), ); log(response.toString()); @@ -188,13 +228,22 @@ class NetworkApiServices { ); } if (e.response!.statusCode == 401) { - prefs.remove('token'); - prefs.remove('refreshToken'); + if (e.response!.data['message'] == 'Invalid token') { + prefs.remove('access-token'); + await prefs.clear(); + Get.offNamed(RouteName.loginScreen); + utils.showToast('Please login again'); + } else { + return ResponseData( + 'Oops something Went Wrong, Please try again!', + ResponseStatus.FAILED, + ); + } // Get.toNamed(RouteName.login); - return ResponseData( - 'Oops something Went Wrong, Please try again!', - ResponseStatus.FAILED, - ); + // return ResponseData( + // 'Oops something Went Wrong, Please try again!', + // ResponseStatus.FAILED, + // ); } if (e.response!.statusCode == 403) { if (e.response!.data['message'] is List) { diff --git a/lib/sidemenu/Community/Group/view/Group.dart b/lib/sidemenu/Community/Group/view/Group.dart index 8dba1a7..8971cf5 100644 --- a/lib/sidemenu/Community/Group/view/Group.dart +++ b/lib/sidemenu/Community/Group/view/Group.dart @@ -19,7 +19,6 @@ class Group extends StatefulWidget { } class _GroupState extends State { - int id = Get.arguments["id"]; bool iscommunity = Get.arguments["iscommunity"]; @@ -40,7 +39,7 @@ class _GroupState extends State { resizeToAvoidBottomInset: false, body: iscommunity == true ? FutureBuilder( - future: Getcommunity().getCommunitygroups(1), + future: Getcommunity().getCommunitygroups(id), builder: (ctx, snapshot) { if (snapshot.data == null) { return const Column( diff --git a/lib/sidemenu/Community/Members/CommunityMembers.dart b/lib/sidemenu/Community/Members/CommunityMembers.dart index d553566..55b3e61 100644 --- a/lib/sidemenu/Community/Members/CommunityMembers.dart +++ b/lib/sidemenu/Community/Members/CommunityMembers.dart @@ -29,6 +29,7 @@ class _CommunityMembersState extends State { @override void initState() { + print(communityid); var updata = communityid; var updata2 = ""; Getcommunity().getCommunityMemberssearch(updata, updata2, diff --git a/lib/sidemenu/Community/MyCommunity/AddGroups.dart b/lib/sidemenu/Community/MyCommunity/AddGroups.dart index 6d7dcba..babe215 100644 --- a/lib/sidemenu/Community/MyCommunity/AddGroups.dart +++ b/lib/sidemenu/Community/MyCommunity/AddGroups.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:developer'; import 'dart:io'; +import 'package:cached_network_image/cached_network_image.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; @@ -333,9 +334,26 @@ class _AddGroupState extends State { backgroundImage: AssetImage('assets/images/png/img2.png'), radius: 20.r, ) - : CircleAvatar( - backgroundImage: NetworkImage(imagePath), + : + // CircleAvatar( + // backgroundImage: NetworkImage(imagePath), + // radius: 20.r, + // ), + CircleAvatar( + backgroundImage: CachedNetworkImageProvider(imagePath), radius: 20.r, + child: CachedNetworkImage( + cacheKey: index.toString(), + maxHeightDiskCache: 100, + maxWidthDiskCache: 100, + imageUrl: imagePath, + placeholder: (context, url) => Container(), + errorWidget: (context, url, error) => Icon(Icons.error), + imageBuilder: (context, imageProvider) => CircleAvatar( + backgroundImage: imageProvider, + radius: 25.r, + ), + ), ), sizedBoxWidth(10.w), Expanded( diff --git a/lib/sidemenu/Community/MyCommunity/View/CommunityDetails.dart b/lib/sidemenu/Community/MyCommunity/View/CommunityDetails.dart index 94f998b..4cfcb1e 100644 --- a/lib/sidemenu/Community/MyCommunity/View/CommunityDetails.dart +++ b/lib/sidemenu/Community/MyCommunity/View/CommunityDetails.dart @@ -1,4 +1,6 @@ +import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:flutter_reaction_button/flutter_reaction_button.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; @@ -237,23 +239,23 @@ class _CommunityDetailsState extends State { ], ), sizedBoxHeight(30.h), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - text13w400_FCFCFC("Show Details"), - sizedBoxWidth(4.w), - InkWell( - onTap: () { - additionalContent = !additionalContent!; - setState(() {}); - }, - child: Image.asset( + InkWell( + onTap: () { + additionalContent = !additionalContent!; + setState(() {}); + }, + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + text13w400_FCFCFC("Show Details"), + sizedBoxWidth(4.w), + Image.asset( "assets/images/png/Vector 8.png", height: 8.h, width: 20.w, - ), - ) - ], + ) + ], + ), ), if (additionalContent == true) Column( @@ -314,28 +316,36 @@ class _CommunityDetailsState extends State { customWidget: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - Row( - children: [ - sizedBoxWidth(16.w), - stackContainers( - number: "+2", - containerImages: [ - "assets/images/png/cimg3.png", - "assets/images/png/cimg2.png", - "assets/images/png/cimg3.png", - "assets/images/png/cimg2.png", - ], - ), - sizedBoxWidth(90.w), - text16w400_white('7 members'), - Spacer(), - Icon( - Icons.arrow_forward, - size: 20.sp, - color: Colors.white, - ), - sizedBoxWidth(16.w), - ], + GestureDetector( + onTap: () { + Get.toNamed(RouteName.communitymembers, + arguments: { + 'communityid': CommunityId, + }); + }, + child: Row( + children: [ + sizedBoxWidth(16.w), + stackContainers( + number: "+2", + containerImages: [ + "assets/images/png/cimg3.png", + "assets/images/png/cimg2.png", + "assets/images/png/cimg3.png", + "assets/images/png/cimg2.png", + ], + ), + sizedBoxWidth(90.w), + text16w400_white('7 members'), + Spacer(), + Icon( + Icons.arrow_forward, + size: 20.sp, + color: Colors.white, + ), + sizedBoxWidth(16.w), + ], + ), ), ], ), @@ -343,7 +353,11 @@ class _CommunityDetailsState extends State { sizedBoxHeight(20.h), GestureDetector( onTap: () { - Get.toNamed(RouteName.addgroup); + // Get.toNamed(RouteName.addgroup); + Get.toNamed(RouteName.group, arguments: { + 'id': CommunityId, + 'iscommunity': true, + }); }, child: commonGlassUI( width: double.infinity,