Skip to content

Commit

Permalink
Merge pull request #252 from 12fahed/main
Browse files Browse the repository at this point in the history
docURL in Announcement nullable, fixed logic of announcement
  • Loading branch information
12fahed authored Aug 3, 2024
2 parents d90a14d + 83408ca commit 6df701b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AnnouncementModel {
div = json['div'];
branch = json['branch'];
batch = json['batch'];
docURL = json['docURL'];
docURL = json['docURL'] as String?;
title = json['title'];
gradYear = json["gradYear"];
}
Expand Down
193 changes: 97 additions & 96 deletions lib/new_ui/screens/AnnouncementScreen/announcementscreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class _AnnouncementScreenState extends ConsumerState<AnnouncementScreen> {
void initState() {
super.initState();
final UserModel? data = ref.read(userModelProvider);
setState(() {
studentModel = data?.studentModel;
});
studentModel = data?.studentModel;
}

@override
Expand All @@ -38,109 +36,112 @@ class _AnnouncementScreenState extends ConsumerState<AnnouncementScreen> {
titleTextStyle: TextStyle(fontSize: 20),
title: Text("Announcements"),
),
body: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) => [
SliverToBoxAdapter(
// You can add a header here if needed
),
],
body: StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance.collection('ImportantNotice').doc('Content').snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(
child: Text(
'Error: ${snapshot.error}',
style: TextStyle(color: Colors.white),
),
);
} else if (!snapshot.hasData || !snapshot.data!.exists) {
return Center(
child: Text(
'No data available',
style: TextStyle(color: Colors.white),
),
);
} else {
final data = snapshot.data!.data() as Map<String, dynamic>?;
final List<dynamic> announcementsData = data?['content'] ?? [];

final announcements = announcementsData.map((json) => AnnouncementModel.fromJson(json)).toList();
return ListView.builder(
itemCount: announcements.length,
itemBuilder: (context, index) {
final announcement = announcements[(announcements.length-1) - index];
final listTile = AnnouncementListItem(
announcementModel: announcement,
);


//Condition if the Date has ended or deadline has crossed
if(!announcement.endDate!.toDate().isAfter(DateTime.now(),)){
return SizedBox();
}
body: StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance.collection('ImportantNotice').doc('Content').snapshots(),
builder: (context, snapshot) {
// Loading Announcements
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}

// Has Error in fetching
if (snapshot.hasError) {
return Center(
child: Text(
'Error: ${snapshot.error}',
style: TextStyle(color: Colors.white),
),
);
}

// No Announcement in collection
if (!snapshot.hasData || !snapshot.data!.exists) {
return Center(
child: Text(
'No Announcement Available',
style: TextStyle(color: Colors.white),
),
);
}

final data = snapshot.data!.data() as Map<String, dynamic>?;
final List<dynamic> announcementsData = data?['content'] ?? [];

// Filter announcements based on conditions
final announcements = announcementsData
.map((json) => AnnouncementModel.fromJson(json))
.where((announcement) {
// Condition if the Date has ended or deadline has crossed
if (!announcement.endDate!.toDate().isAfter(DateTime.now())) {
return false;
}

if(studentModel==null && announcement.batch!.contains("All")){
return listTile;
}
// Condition when Student Didn't Logged in
if (studentModel == null && announcement.gradYear!.contains("All")) {
return true;
}

//Condition for All Grad Year
if(announcement.gradYear!.contains("All")) {
return listTile;
// Condition when Student Logged in
if (studentModel != null) {
// For All Gradyear
if (announcement.gradYear!.contains("All")) {
return true;
}
// Specific Gradyear
if (announcement.gradYear == studentModel!.gradyear) {
// all Branch
if (announcement.branch!.contains("All")) {
return true;
}
// specific branch
if (announcement.branch == studentModel!.branch) {
// All Division
if (announcement.div!.contains("All")) {
return true;
}

//Condition for Student Matches Grad Year
if(announcement.gradYear == studentModel!.gradyear){

//Condition to match all Batches of that Grad Year
if(announcement.branch!.contains("All")){
return listTile;
// specific div
if (announcement.div == studentModel!.div) {
// all batch
if (announcement.batch!.contains("All")) {
return true;
}

//Student Belongs to the Same Branch
if(announcement.branch == studentModel!.branch){

//All Divisions of that branch
if(announcement.div!.contains("All")){
return listTile;
}

//Student is in the Same Division
if(announcement.div == studentModel!.div){

//All Batches of Same Division
if(announcement.batch!.contains("All")){
return listTile;
}

if(announcement.batch == studentModel!.batch){
return listTile;
}

}
// specific batch
if (announcement.batch == studentModel!.batch) {
return true;
}
}
}
}
}

return SizedBox();

/*return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildDateHeader(_parseTimestamp(announcement.startDate)),
listTile,
],
);*/
},
return false;
}).toList();

// Check if there are any announcements to display
if (announcements.isEmpty) {
return Center(
child: Text(
'No Announcement Available',
style: TextStyle(color: Colors.white),
),
);
}

return ListView.builder(
itemCount: announcements.length,
itemBuilder: (context, index) {
final announcement = announcements[(announcements.length - 1) - index];
return AnnouncementListItem(
announcementModel: announcement,
);
}
},
),
},
);
},
),
);
}


DateTime _parseTimestamp(dynamic timestamp) {
if (timestamp is Timestamp) {
return timestamp.toDate();
Expand Down Expand Up @@ -172,7 +173,7 @@ class AnnouncementListItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
if(announcementModel.docURL == null || announcementModel.startDate==null || announcementModel.endDate==null){
if(announcementModel.startDate==null || announcementModel.endDate==null){
return const SizedBox();
}
DateTime startDate = announcementModel.startDate!.toDate();
Expand Down Expand Up @@ -206,7 +207,7 @@ class AnnouncementListItem extends StatelessWidget {
),
)
),
if(announcementModel.docURL !=null || announcementModel.docURL !="")
if(announcementModel.docURL !=null && announcementModel.docURL !="")
InkWell(splashFactory: NoSplash.splashFactory,onTap: ()=>launchUrl(Uri.parse(announcementModel.docURL.toString(),),)
,child:Icon(Icons.link,color: Colors.blue,),
),
Expand Down

0 comments on commit 6df701b

Please sign in to comment.