|
- import 'package:farm_tpf/custom_model/NotificationDTO.dart';
- import 'package:farm_tpf/data/repository/repository.dart';
- import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
- import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
- import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_detail.dart';
- import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_information.dart';
- import 'package:farm_tpf/utils/pref.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_bloc/flutter_bloc.dart';
- import 'package:farm_tpf/utils/formatter.dart';
-
- import 'bloc/noti_bloc.dart';
-
- class NotificationScreen extends StatefulWidget {
- @override
- _NotificationScreenState createState() => _NotificationScreenState();
- }
-
- class _NotificationScreenState extends State<NotificationScreen> {
- @override
- Widget build(BuildContext context) {
- return BlocProvider(
- create: (context) =>
- NotiBloc(repository: Repository())..add(DataFetched()),
- child: HoldInfinityWidget(),
- );
- }
- }
-
- class HoldInfinityWidget extends StatelessWidget {
- final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- key: _scaffoldKey,
- appBar: AppBar(
- centerTitle: true,
- title: Text("Thông báo"),
- actions: <Widget>[
- IconButton(
- icon: Icon(Icons.done_all),
- onPressed: () {
- BlocProvider.of<NotiBloc>(context)
- .add(MarkAllNotificationUpdate(status: "1"));
- })
- ],
- ),
- body: InfinityView());
- }
- }
-
- class InfinityView extends StatefulWidget {
- @override
- _InfinityViewState createState() => _InfinityViewState();
- }
-
- class _InfinityViewState extends State<InfinityView> {
- final _scrollController = ScrollController();
- final _scrollThreshold = 250.0;
- NotiBloc _notiBloc;
- List<NotificationDTO> currentItems = new List<NotificationDTO>();
- int latestId = 0;
- int currentPage = 0;
- bool currentHasReachedMax = true;
- bool isUpdatingFromApi = true;
-
- var pref = LocalPref();
- // final SocketService socketService = injector.get<SocketService>();
- var token;
- Future<Null> getSharedPrefs() async {
- _scrollController.addListener(() {
- final maxScroll = _scrollController.position.maxScrollExtent;
- final currentScroll = _scrollController.position.pixels;
- if (maxScroll - currentScroll < _scrollThreshold) {
- _notiBloc.add(DataFetched());
- }
- });
- token = await pref.getString(DATA_CONST.TOKEN_KEY);
- // socketService.initial(token);
- _notiBloc = BlocProvider.of<NotiBloc>(context);
- }
-
- @override
- void initState() {
- getSharedPrefs();
- super.initState();
- }
-
- @override
- Widget build(BuildContext context) {
- return BlocConsumer<NotiBloc, NotiState>(
- listener: (context, state) {
- //Handle Socket
- /*
- if (state is NotiLoadding) {
- isUpdatingFromApi = true;
- }
- if (state is NotiSuccess) {
- currentItems = List<NotificationDTO>.from(state.items);
- latestId = int.parse(currentItems[0].id) > latestId
- ? int.parse(currentItems[0].id)
- : latestId;
- socketService.createSocketNotificationConnection(latestId, (data) {
- print("receive data");
- if (isUpdatingFromApi == false) {
- print("Update from socket");
- var notis =
- NotificationObjectDTO.fromJson(data, NotificationDTO());
- _notiBloc.add(ReceiveDataFromSocket(
- currentItems: currentItems,
- page: currentPage,
- hasReachedMax: currentHasReachedMax,
- updatedItemObject: notis));
- } else {
- //dont need update from socket
- print("dont need update from socket");
- }
- }, (error) {});
- isUpdatingFromApi = false;
- }
- */
- },
- builder: (context, state) {
- if (state is NotiFailure) {
- return Center(child: Text(state.errorString));
- }
- if (state is NotiSuccess) {
- if (state.items.isEmpty) {
- return Center(child: Text("Dữ liệu rỗng"));
- }
- currentItems = List<NotificationDTO>.from(state.items);
- currentPage = state.page;
- currentHasReachedMax = state.hasReachedMax;
- return Column(
- children: <Widget>[
- Container(
- child:
- countNotification(unread: state.unread, read: state.read),
- ),
- Expanded(
- child: RefreshIndicator(
- child: ListView.builder(
- itemBuilder: (BuildContext context, int index) {
- return index >= state.items.length
- ? BottomLoader()
- : ItemInfinityWidget(
- unread: state.unread,
- read: state.read,
- currentItems: currentItems,
- item: state.items[index],
- currentPage: state.page,
- currentReachedMax: state.hasReachedMax,
- );
- },
- itemCount: state.hasReachedMax
- ? state.items.length
- : state.items.length + 1,
- controller: _scrollController,
- ),
- onRefresh: () async {
- _notiBloc.add(OnRefresh());
- }))
- ],
- );
- }
- return Center(
- child: LoadingListPage(),
- );
- },
- );
- }
-
- Widget countNotification({num unread, num read}) {
- return Container(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- mainAxisSize: MainAxisSize.max,
- children: <Widget>[
- Expanded(
- child: OutlineButton(
- child: RichText(
- text: TextSpan(
- text: "Chưa đọc ",
- style: TextStyle(
- color: Colors.black, fontWeight: FontWeight.bold),
- children: <TextSpan>[
- TextSpan(
- text: "($unread)",
- style: TextStyle(color: Colors.blue)),
- ])),
- onPressed: () {})),
- Expanded(
- child: OutlineButton(
- child: RichText(
- text: TextSpan(
- text: "Đã đọc ",
- style: TextStyle(
- color: Colors.black, fontWeight: FontWeight.bold),
- children: <TextSpan>[
- TextSpan(
- text: "($read)",
- style: TextStyle(color: Colors.grey)),
- ])),
- onPressed: () {}))
- ],
- ),
- );
- }
-
- @override
- void dispose() {
- _scrollController.dispose();
- // socketService.disconnect();
- super.dispose();
- }
- }
-
- class ItemInfinityWidget extends StatelessWidget {
- final int unread;
- final int read;
- final NotificationDTO item;
- final List<NotificationDTO> currentItems;
- final int currentPage;
- final bool currentReachedMax;
-
- const ItemInfinityWidget(
- {Key key,
- @required this.unread,
- @required this.read,
- @required this.currentItems,
- @required this.item,
- @required this.currentPage,
- @required this.currentReachedMax})
- : super(key: key);
-
- @override
- Widget build(BuildContext context) {
- return GestureDetector(
- child: Card(
- margin: EdgeInsets.all(4.0),
- child: ListTile(
- title: Text(item.subject),
- subtitle: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Text(item.message),
- Text(
- item.sendDate.format_DDMMYY_HHmm(),
- style: TextStyle(
- color: item.isRead == 1 ? Colors.grey : Colors.blue),
- ),
- ],
- ),
- leading: Icon(
- Icons.notifications_active,
- color: item.isRead == 1 ? Colors.grey : Colors.blue,
- ))),
- onTap: () {
- if (item.contents == "ENV_UPDATE") {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (BuildContext context) =>
- PlotDetailScreen(cropId: item.tbCropId))).then((value) {
- if (item.isRead == 0) {
- _updateReadNotification(
- context: context,
- unread: unread,
- read: read,
- item: item,
- currentItems: currentItems,
- currentPage: currentPage,
- currentReachedMax: currentReachedMax);
- }
- });
- } else if (item.contents == "PIC_UPDATE") {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (BuildContext context) => PlotInformationScreen(
- cropId: item.tbCropId,
- ))).then((value) {
- if (item.isRead == 0) {
- _updateReadNotification(
- context: context,
- unread: unread,
- read: read,
- item: item,
- currentItems: currentItems,
- currentPage: currentPage,
- currentReachedMax: currentReachedMax);
- }
- });
- } else {}
- });
- }
-
- _updateReadNotification(
- {BuildContext context,
- int unread,
- int read,
- NotificationDTO item,
- List<NotificationDTO> currentItems,
- int currentPage,
- bool currentReachedMax}) {
- List<NotificationDTO> updatedItems = new List<NotificationDTO>();
- currentItems.forEach((e) {
- if (e.id == item.id) {
- e.isRead = 1;
- } else {}
- updatedItems.add(NotificationDTO.clone(e));
- });
-
- BlocProvider.of<NotiBloc>(context).add(OnUpdate<NotificationDTO>(
- unread: unread - 1,
- read: read + 1,
- currentItemId: item.id,
- currentItems: updatedItems,
- currentPage: currentPage,
- hasReachedMax: currentReachedMax));
- }
- }
|