Files
Regroup/lib/Main_Screens/Community_HomePage/Community.dart
2024-08-22 19:43:00 +05:30

920 lines
34 KiB
Dart

// ignore_for_file: unused_local_variable, non_constant_identifier_names, avoid_print, file_names
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:regroup/Common/CommonBottomNavigationBar.dart';
import 'package:regroup/Common/CommonGlassmorphism.dart';
import 'package:regroup/Common/CommonTabBar.dart';
import 'package:regroup/Common/ConvertServerDateToUserDate.dart';
import 'package:regroup/Common/controller/NormalPostCard.dart';
import 'package:regroup/Common/controller/MainScreen.dart';
import 'package:regroup/Main_Screens/Community/Model/CommonDatumObjModel.dart';
import 'package:regroup/Main_Screens/Community/Model/FeedPostModel.dart'
as feedobj;
// import 'package:regroup/Main_Screens/Community/Model/PopularPostModel.dart';
// import 'package:regroup/Main_Screens/Community/Model/LatestPostModel.dart' as latest;
import 'package:regroup/Main_Screens/Community/Model/fetchicons.dart';
import 'package:regroup/Main_Screens/Community/ViewModel/FeedPost.dart';
import 'package:regroup/Main_Screens/Community/ViewModel/LatestPost.dart';
import 'package:regroup/Main_Screens/Community/ViewModel/PopularPost.dart';
import 'package:regroup/Main_Screens/Community/ViewModel/getmethod.dart';
import 'package:regroup/Main_Screens/Community_HomePage/PostDetailScreen/View/ReactionView.dart';
import 'package:regroup/Utils/Common/ShimmerCommon.dart';
// import 'package:regroup/Feed%20Module/sidemenu/sidemenu.dart';
import 'package:regroup/Utils/Common/sized_box.dart';
import 'package:regroup/Utils/texts.dart';
import 'package:regroup/resources/routes/route_name.dart';
import 'package:regroup/sidemenu/sidemenu.dart';
import 'view_model/CountersHelper.dart';
class CommunityScreen extends StatefulWidget {
const CommunityScreen({super.key});
@override
State<CommunityScreen> createState() => _CommunityScreenState();
}
class _CommunityScreenState extends State<CommunityScreen> {
final GlobalKey<ScaffoldState> _scaffoldKey1 = GlobalKey<ScaffoldState>();
final CountersHelper countersHelper = Get.put(CountersHelper());
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
key: _scaffoldKey1,
backgroundColor: const Color(0xFF222935),
drawer: SizedBox(width: 300.w, child: const SideMenu()),
extendBody: true,
appBar: AppBar(
scrolledUnderElevation: 0.0,
backgroundColor: const Color(0xff222935),
elevation: 0,
automaticallyImplyLeading: false,
titleSpacing: 0,
leading: InkWell(
onTap: () {
_scaffoldKey1.currentState?.openDrawer();
},
child: Center(
child: Image.asset(
'assets/images/png/menu.png',
height: 15.h,
width: 20.w,
),
),
),
title: Image.asset(
'assets/images/png/Group.png',
width: 26.w,
height: 25.h,
),
actions: [
GestureDetector(
onTap: () {
Get.toNamed(RouteName.explorescreen);
},
child: Image.asset(
'assets/images/png/ion_search-outline.png',
height: 25.h,
width: 25.w,
),
),
sizedBoxWidth(10.w),
GestureDetector(
onTap: () {
Get.toNamed(RouteName.notificaationpage);
},
child: Image.asset(
'assets/images/png/Frame 9.png',
height: 25.h,
width: 25.w,
),
),
sizedBoxWidth(16.w),
],
),
body: Stack(clipBehavior: Clip.none, children: [
Container(
decoration: const BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage(
'assets/images/png/HomeScreenBackground.png',
))),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Column(children: [
Expanded(
child: DefaultTabController(
length: 3,
child: Column(
children: [
const CommonTabBar(tabs: [
Tab(
text: 'Feed',
),
Tab(
text: 'Popular',
),
Tab(
text: 'Latest',
),
]),
const Expanded(
child: TabBarView(
children: [
FeedTab(),
PopularTab(),
LatestTab(),
],
),
),
sizedBoxHeight(90.h)
],
),
),
),
]))
]),
floatingActionButton: Container(
height: 40.h,
width: 164.w,
decoration: BoxDecoration(
// shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(30.r),
boxShadow: const [
BoxShadow(
color: Color(0x40000000),
offset: Offset(0, 6),
blurRadius: 8,
),
],
),
child: FloatingActionButton(
onPressed: () {
Get.toNamed(RouteName.postscreen);
},
backgroundColor: const Color(0xFFD90B2E),
autofocus: true,
// shape: const OvalBorder(),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.r),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// SvgPicture.asset('assets/images/svg/add 1.svg'),
Image.asset('assets/images/png/add 1.png'),
sizedBoxWidth(4.w),
text16400white('Create posts')
],
)
// Image.asset(
// "assets/images/png/iconamoon_edit-thin.png",
// height: 30.h,
// width: 30.w,
// ),
),
),
bottomNavigationBar: bottomnavigationbar(mainController),
);
}
}
class FeedTab extends StatefulWidget {
const FeedTab({super.key});
@override
State<FeedTab> createState() => _FeedTabState();
}
class _FeedTabState extends State<FeedTab> {
late Future feedfuture;
List<ReactionData> _reactions = [];
final Map<int, ReactionData?> _selectedReactions = {};
final CountersHelper countersHelper = Get.find<CountersHelper>();
int? likeIconIdnew;
List<feedobj.PinnedPost> combinedList = [];
@override
void initState() {
feedfuture = FeedpostApi().getFeedPostApi();
super.initState();
}
_fetchIcons() {
_reactions = fetchlikeIconsModel!.data ?? [];
for (var post in commonobjmodel!.data) {
//Check if post as a like icon
if (post.likeIcon?.likeIcon?.id != null) {
likeIconIdnew = post.likeIcon!.likeIconsXid;
final selectedReaction = _reactions.firstWhere(
(r) => r.id == likeIconIdnew,
orElse: () => _reactions.first,
);
_selectedReactions[post.id!] =
selectedReaction; // Set selected reaction for this post
} else {
_selectedReactions[post.id!] = null; // No reaction selected
print('No reaction selected for post $postId');
}
}
}
Future<void> setValues() async {
if (mounted) {
combinedList.clear();
combinedListGlobal.clear();
combinedList.addAll(feedpostobj!.data!.pinnedCommunityPost);
combinedList.addAll(feedpostobj!.data!.pinnedUserPost);
combinedList.addAll(feedpostobj!.data!.pinnedTagsPost);
combinedListGlobal.addAll(combinedList);
_setModel();
countersHelper.setListsPopular();
await _fetchIcons();
}
}
_sortTags(index) {
var tags = commonobjmodel!.data[index].attachTags
.where((tag) => tag.manageTag?.name?.isNotEmpty ?? false)
.map((tag) => tag.manageTag!)
.toList();
return tags;
}
_setModel() {
commonobjmodel = null;
commonobjmodel = CommonDatumObjModel(
status: feedpostobj!.status,
statusCode: feedpostobj!.statusCode,
message: feedpostobj!.message,
data: combinedList
.map((e) => CommonDatumObjModelData(
id: e.id,
likecount: e.likecount,
isISaved: e.isISaved,
tagsXid: e.tagsXid,
isILiked: e.isILiked,
totalComment: e.totalComment,
totalSave: e.totalSave,
iamPrincipalXid: e.iamPrincipalXid,
postIn: e.postIn,
caption: e.caption,
image: e.image,
manageTagsXids: e.manageTagsXids,
postAs: e.postAs,
ctaTitle: e.ctaTitle,
ctaLink: e.ctaLink,
createdAt: e.createdAt,
likeIcon: LikeIcon1(
likeIcon: LikeIconClass1(
id: e.likeIcon?.likeIcon?.id,
image: e.likeIcon?.likeIcon?.image),
likeIconsXid: e.likeIcon?.likeIconsXid,
),
totalViewCount: e.totalViewCount,
totalReactionCount: e.totalReactionCount,
totalCommentCount: e.totalCommentCount,
totalImpressionCount: e.totalImpressionCount,
totalPopularScore: e.totalPopularScore,
totalHoursAgo: e.totalHoursAgo,
iamPrincipal: IamPrincipal(
id: e.iamPrincipal!.id,
isUserPinned: e.iamPrincipal!.isUserPinned,
principalTypeXid: e.iamPrincipal!.principalTypeXid,
userName: e.iamPrincipal!.userName,
fullName: e.iamPrincipal!.fullName,
profilePhoto: e.iamPrincipal!.profilePhoto),
community: Community(
id: e.community!.id,
communityProfilePhoto: e.community!.communityProfilePhoto,
communityName: e.community!.communityName),
attachTags: e.attachTags
.map((e) => AttachTag1(
managePostXid: e.managePostXid,
manageTagXid: e.manageTagXid,
manageTag: ManageTagPopular(
id: e.manageTag?.id,
isPinned: e.manageTag?.isPinned,
name: e.manageTag?.name)))
.toList()))
.toList());
}
@override
Widget build(BuildContext context) {
return Obx(
() => countersHelper.updateFeedsPage.value
? FutureBuilder(
future: FeedpostApi().getFeedPostApi(),
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: ShimmerCommon());
}
if (snapshot.hasError) {
return Center(
child: Text(
'${snapshot.error} occurred',
style: TextStyle(fontSize: 18.spMin),
),
);
}
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
log("TRUE");
setValues();
return Stack(
clipBehavior: Clip.none,
children: [
Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
"assets/images/png/Ellipse 1496.png"),
fit: BoxFit.fill),
),
),
combinedList.isEmpty
? Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"No posts are currently pinned",
style: TextStyle(
color: Colors.white,
fontSize: 16.sp,
fontWeight: FontWeight.w600),
)
],
),
)
: Column(
children: [
sizedBoxHeight(20.h),
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: commonobjmodel!.data.length,
itemBuilder: (context, index) {
var timeAgo = ConvertServerDateToUserDate()
.convertServerDateToReadableFormate(
commonobjmodel!
.data[index].createdAt
.toString());
return Column(
children: [
NormalCardTile(
tags: _sortTags(index),
createAt: timeAgo ?? '1 hour',
commonObj:
commonobjmodel!.data[index],
forWhichTab: 'feed',
reactions: _reactions,
selectedReactions:
_selectedReactions,
currentIndex: index,
),
sizedBoxHeight(20.h)
],
);
},
),
),
],
)
],
);
}
return Container();
},
)
: FutureBuilder(
future: FeedpostApi().getFeedPostApi(),
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: ShimmerCommon());
}
if (snapshot.hasError) {
return Center(
child: Text(
'${snapshot.error} occurred',
style: TextStyle(fontSize: 18.spMin),
),
);
}
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
log("FALSE");
setValues();
return Stack(
clipBehavior: Clip.none,
children: [
Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
"assets/images/png/Ellipse 1496.png"),
fit: BoxFit.fill),
),
),
combinedList.isEmpty
? Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"No posts are currently pinned",
style: TextStyle(
color: Colors.white,
fontSize: 16.sp,
fontWeight: FontWeight.w600),
)
],
),
)
: Column(
children: [
sizedBoxHeight(20.h),
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: commonobjmodel!.data.length,
itemBuilder: (context, index) {
var timeAgo = ConvertServerDateToUserDate()
.convertServerDateToReadableFormate(
commonobjmodel!
.data[index].createdAt
.toString());
return Column(
children: [
NormalCardTile(
tags: _sortTags(index),
createAt: timeAgo ?? '1 hour',
commonObj:
commonobjmodel!.data[index],
forWhichTab: 'feed',
reactions: _reactions,
selectedReactions:
_selectedReactions,
currentIndex: index,
),
sizedBoxHeight(20.h)
],
);
},
),
),
],
)
],
);
}
return Container();
},
),
);
}
}
class PopularTab extends StatefulWidget {
const PopularTab({super.key});
@override
State<PopularTab> createState() => _PopularTabState();
}
class _PopularTabState extends State<PopularTab> {
late Future popularfuture;
List<ReactionData> _reactions = [];
final Map<int, ReactionData?> _selectedReactions = {};
final CountersHelper countersHelper = Get.find<CountersHelper>();
int? likeIconIdnew;
@override
void initState() {
popularfuture = PopularpostApi().getPopularPostApi();
super.initState();
}
Future<void> _fetchIcons() async {
_reactions = fetchlikeIconsModel!.data ?? [];
for (var post in commonobjmodel!.data) {
//Check if post as a like icon
if (post.likeIcon?.likeIcon?.id != null) {
likeIconIdnew = post.likeIcon!.likeIconsXid;
final selectedReaction = _reactions.firstWhere(
(r) => r.id == likeIconIdnew,
orElse: () => _reactions.first,
);
_selectedReactions[post.id!] =
selectedReaction; // Set selected reaction for this post
} else {
_selectedReactions[post.id!] = null; // No reaction selected
print('No reaction selected for post $postId');
}
}
}
bool _isDataInitialized = true;
Future<void> setValues() async {
if (_isDataInitialized) {
_setModel();
countersHelper.setListsPopular();
await _fetchIcons();
_isDataInitialized = false;
}
}
_sortTags(index) {
var tags = commonobjmodel!.data[index].attachTags
.where((tag) => tag.manageTag?.name?.isNotEmpty ?? false)
.map((tag) => tag.manageTag!)
.toList();
return tags;
}
_setModel() {
commonobjmodel = null;
commonobjmodel = CommonDatumObjModel(
status: popularpostobj!.status,
statusCode: popularpostobj!.statusCode,
message: popularpostobj!.message,
data: popularpostobj!.data
.map((e) => CommonDatumObjModelData(
id: e.id,
likecount: e.likecount,
isISaved: e.isISaved,
tagsXid: e.tagsXid,
isILiked: e.isILiked,
totalComment: e.totalComment,
totalSave: e.totalSave,
iamPrincipalXid: e.iamPrincipalXid,
postIn: e.postIn,
caption: e.caption,
image: e.image,
manageTagsXids: e.manageTagsXids,
postAs: e.postAs,
ctaTitle: e.ctaTitle,
ctaLink: e.ctaLink,
createdAt: e.createdAt,
likeIcon: LikeIcon1(
likeIcon: LikeIconClass1(
id: e.likeIcon?.likeIcon?.id,
image: e.likeIcon?.likeIcon?.image),
likeIconsXid: e.likeIcon?.likeIconsXid,
),
totalViewCount: e.totalViewCount,
totalReactionCount: e.totalReactionCount,
totalCommentCount: e.totalCommentCount,
totalImpressionCount: e.totalImpressionCount,
totalPopularScore: e.totalPopularScore,
totalHoursAgo: e.totalHoursAgo,
iamPrincipal: IamPrincipal(
id: e.iamPrincipal!.id,
isUserPinned: e.iamPrincipal!.isUserPinned,
principalTypeXid: e.iamPrincipal!.principalTypeXid,
userName: e.iamPrincipal!.userName,
fullName: e.iamPrincipal!.fullName,
profilePhoto: e.iamPrincipal!.profilePhoto),
community: Community(
id: e.community!.id,
communityProfilePhoto: e.community!.communityProfilePhoto,
communityName: e.community!.communityName),
attachTags: e.attachTags
.map((e) => AttachTag1(
managePostXid: e.managePostXid,
manageTagXid: e.manageTagXid,
manageTag: ManageTagPopular(
id: e.manageTag?.id,
isPinned: e.manageTag?.isPinned,
name: e.manageTag?.name)))
.toList()))
.toList());
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: popularfuture,
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: ShimmerCommon());
}
if (snapshot.hasError) {
return Center(
child: Text(
'${snapshot.error} occurred',
style: TextStyle(fontSize: 18.spMin),
),
);
}
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
setValues();
return Stack(clipBehavior: Clip.none, children: [
Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/png/Ellipse 1496.png"),
fit: BoxFit.fill),
),
),
popularpostobj!.data.isEmpty
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Column(
children: [
Text(
"No Posts Available",
style: TextStyle(
color: Colors.white,
fontSize: 16.sp,
fontWeight: FontWeight.w600),
)
],
),
),
])
: Column(
children: [
sizedBoxHeight(20.h),
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: commonobjmodel!.data.length,
itemBuilder: (context, index) {
var timeAgo = ConvertServerDateToUserDate()
.convertServerDateToReadableFormate(
popularpostobj!.data[index].createdAt
.toString());
return Column(
children: [
NormalCardTile(
tags: _sortTags(index),
createAt: timeAgo ?? '1 hour',
commonObj: commonobjmodel!.data[index],
forWhichTab: 'popular',
reactions: _reactions,
selectedReactions: _selectedReactions,
currentIndex: index,
),
sizedBoxHeight(20.h)
],
);
},
),
),
],
)
]);
}
return Container();
});
}
}
class LatestTab extends StatefulWidget {
const LatestTab({super.key});
@override
State<LatestTab> createState() => _LatestTabState();
}
class _LatestTabState extends State<LatestTab> {
late Future latestfuture;
@override
void initState() {
latestfuture = LatespostApi().getLatestPostApi();
super.initState();
}
List<ReactionData> _reactions = [];
final Map<int, ReactionData?> _selectedReactions = {};
final CountersHelper countersHelper = Get.find<CountersHelper>();
int? likeIconIdnew;
_fetchIcons() {
_reactions = fetchlikeIconsModel!.data ?? [];
for (var post in commonobjmodel!.data) {
//Check if post as a like icon
if (post.likeIcon?.likeIcon?.id != null) {
likeIconIdnew = post.likeIcon!.likeIconsXid;
final selectedReaction = _reactions.firstWhere(
(r) => r.id == likeIconIdnew,
orElse: () => _reactions.first,
);
_selectedReactions[post.id!] =
selectedReaction; // Set selected reaction for this post
} else {
_selectedReactions[post.id!] = null; // No reaction selected
print('No reaction selected for post $postId');
}
}
}
bool _isDataInitialized = true;
Future<void> setValues() async {
if (_isDataInitialized) {
_setModel();
countersHelper.setListsPopular();
_fetchIcons();
_isDataInitialized = false;
}
}
_sortTags(index) {
var tags = commonobjmodel!.data[index].attachTags
.where((tag) => tag.manageTag?.name?.isNotEmpty ?? false)
.map((tag) => tag.manageTag!)
.toList();
return tags;
}
_setModel() {
commonobjmodel = null;
commonobjmodel = CommonDatumObjModel(
status: latestpostobj!.status,
statusCode: latestpostobj!.statusCode,
message: latestpostobj!.message,
data: latestpostobj!.data
.map((e) => CommonDatumObjModelData(
id: e.id,
likecount: e.likecount,
isISaved: e.isISaved,
tagsXid: e.tagsXid,
isILiked: e.isILiked,
totalComment: e.totalComment,
totalSave: e.totalSave,
iamPrincipalXid: e.iamPrincipalXid,
postIn: e.postIn,
caption: e.caption,
image: e.image,
manageTagsXids: e.manageTagsXids,
postAs: e.postAs,
ctaTitle: e.ctaTitle,
ctaLink: e.ctaLink,
createdAt: e.createdAt,
likeIcon: LikeIcon1(
likeIcon: LikeIconClass1(
id: e.likeIcon?.likeIcon?.id,
image: e.likeIcon?.likeIcon?.image),
likeIconsXid: e.likeIcon?.likeIconsXid,
),
totalViewCount: e.totalViewCount,
totalReactionCount: e.totalReactionCount,
totalCommentCount: e.totalCommentCount,
totalImpressionCount: e.totalImpressionCount,
totalPopularScore: e.totalPopularScore,
totalHoursAgo: e.totalHoursAgo,
iamPrincipal: IamPrincipal(
id: e.iamPrincipal!.id,
isUserPinned: e.iamPrincipal!.isUserPinned,
principalTypeXid: e.iamPrincipal!.principalTypeXid,
userName: e.iamPrincipal!.userName,
fullName: e.iamPrincipal!.fullName,
profilePhoto: e.iamPrincipal!.profilePhoto),
community: Community(
id: e.community!.id,
communityProfilePhoto: e.community!.communityProfilePhoto,
communityName: e.community!.communityName),
attachTags: e.attachTags
.map((e) => AttachTag1(
managePostXid: e.managePostXid,
manageTagXid: e.manageTagXid,
manageTag: ManageTagPopular(
id: e.manageTag?.id,
isPinned: e.manageTag?.isPinned,
name: e.manageTag?.name)))
.toList()))
.toList());
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: latestfuture,
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: ShimmerCommon());
}
if (snapshot.hasError) {
return Center(
child: Text(
'${snapshot.error} occurred',
style: TextStyle(fontSize: 18.spMin),
),
);
}
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
setValues();
return Stack(
clipBehavior: Clip.none,
children: [
Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/png/Ellipse 1496.png"),
fit: BoxFit.fill),
),
),
latestpostobj!.data.isEmpty
? Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Column(
children: [
Text(
"No Post Available",
style: TextStyle(
color: Colors.white,
fontSize: 16.sp,
fontWeight: FontWeight.w600),
)
],
),
),
],
),
)
: Column(
children: [
sizedBoxHeight(20.h),
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: commonobjmodel!.data.length,
itemBuilder: (context, index) {
var timeAgo = ConvertServerDateToUserDate()
.convertServerDateToReadableFormate(
commonobjmodel!.data[index].createdAt
.toString());
return Column(
children: [
NormalCardTile(
tags: _sortTags(index),
createAt: timeAgo ?? '1 hour',
commonObj: commonobjmodel!.data[index],
forWhichTab: 'latest',
reactions: _reactions,
selectedReactions: _selectedReactions,
currentIndex: index,
),
sizedBoxHeight(20.h)
],
);
},
),
),
],
),
],
);
}
return Container();
},
);
}
}