diff --git a/lib/new_ui/screens/AnnouncementScreen/announcementmodel.dart b/lib/new_ui/screens/AnnouncementScreen/announcementmodel.dart new file mode 100644 index 0000000..4a78aab --- /dev/null +++ b/lib/new_ui/screens/AnnouncementScreen/announcementmodel.dart @@ -0,0 +1,42 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; + +class AnnouncementModel { + Timestamp? startDate; + Timestamp? endDate; + String? div; + String? branch; + String? batch; + String? docURL; + String? title; + + AnnouncementModel( + {this.startDate, + this.endDate, + this.div, + this.branch, + this.batch, + this.docURL, + this.title}); + + AnnouncementModel.fromJson(Map json) { + startDate = json['startDate']; + endDate = json['endDate']; + div = json['div']; + branch = json['branch']; + batch = json['batch']; + docURL = json['docURL']; + title = json['title']; + } + + Map toJson() { + final Map data = new Map(); + data['startDate'] = this.startDate; + data['endDate'] = this.endDate; + data['div'] = this.div; + data['branch'] = this.branch; + data['batch'] = this.batch; + data['docURL'] = this.docURL; + data['title'] = this.title; + return data; + } +} \ No newline at end of file diff --git a/lib/new_ui/screens/AnnouncementScreen/announcementscreen.dart b/lib/new_ui/screens/AnnouncementScreen/announcementscreen.dart new file mode 100644 index 0000000..ebcd217 --- /dev/null +++ b/lib/new_ui/screens/AnnouncementScreen/announcementscreen.dart @@ -0,0 +1,117 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/material.dart'; + +// import '../models/announcement_model/announcement_model.dart'; +import 'announcementmodel.dart'; + +class AnnouncementScreen extends StatelessWidget { + final DateTime _lastDate = DateTime.now(); + + @override + Widget build(BuildContext context) { + return Scaffold( + body: NestedScrollView( + headerSliverBuilder: (context, innerBoxIsScrolled) => [ + SliverToBoxAdapter( + // You can add a header here if needed + ), + ], + body: StreamBuilder( + 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?; + final List announcementsData = data?['content'] ?? []; + + print("in announcement screen ppppppppppppppppppppppppppppppppppppppp"); + print(data); + + final announcements = announcementsData.map((json) => AnnouncementModel.fromJson(json)).toList(); + + return ListView.builder( + itemCount: announcements.length, + itemBuilder: (context, index) { + final announcement = announcements[index]; + final listTile = AnnouncementListItem( + announcementModel: announcement, + ); + + if (_lastDate.isSameDate(_parseTimestamp(announcement.startDate))) { + return listTile; + } + + return Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + _buildDateHeader(_parseTimestamp(announcement.startDate)), + listTile, + ], + ); + }, + ); + } + }, + ), + ), + ); + } + + DateTime _parseTimestamp(dynamic timestamp) { + if (timestamp is Timestamp) { + return timestamp.toDate(); + } else if (timestamp is String) { + return DateTime.parse(timestamp); + } else { + return DateTime.now(); + } + } + + Widget _buildDateHeader(DateTime date) { + // Customize this method to build your date header + return Container( + padding: EdgeInsets.all(8.0), + color: Colors.grey, + child: Text( + "${date.toLocal()}".split(' ')[0], // Display the date in a readable format + style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold), + ), + ); + } +} + +class AnnouncementListItem extends StatelessWidget { + final AnnouncementModel announcementModel; + + AnnouncementListItem({required this.announcementModel}); + + @override + Widget build(BuildContext context) { + return ListTile( + title: Text(announcementModel.title ?? "No Title"), + subtitle: Text(announcementModel.docURL ?? "No URL"), + // Add other fields as needed + ); + } +} + +extension DateTimeExtensions on DateTime { + bool isSameDate(DateTime other) { + return this.year == other.year && this.month == other.month && this.day == other.day; + } +}