|
|
|
@@ -5,6 +5,7 @@ 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:firebase_messaging/firebase_messaging.dart'; |
|
|
|
import 'package:flutter/material.dart'; |
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart'; |
|
|
|
import 'package:farm_tpf/utils/formatter.dart'; |
|
|
|
@@ -17,17 +18,38 @@ class NotificationScreen extends StatefulWidget { |
|
|
|
} |
|
|
|
|
|
|
|
class _NotificationScreenState extends State<NotificationScreen> { |
|
|
|
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); |
|
|
|
var notiBloc = NotiBloc(repository: Repository()); |
|
|
|
|
|
|
|
@override |
|
|
|
void initState() { |
|
|
|
super.initState(); |
|
|
|
_firebaseMessaging.configure( |
|
|
|
onMessage: (Map<String, dynamic> message) async { |
|
|
|
print("onMessage: $message"); |
|
|
|
try { |
|
|
|
notiBloc.add(OnRefreshFromNotification()); |
|
|
|
} catch (e) { |
|
|
|
print(e); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@override |
|
|
|
Widget build(BuildContext context) { |
|
|
|
return BlocProvider( |
|
|
|
create: (context) => |
|
|
|
NotiBloc(repository: Repository())..add(DataFetched()), |
|
|
|
child: HoldInfinityWidget(), |
|
|
|
); |
|
|
|
return BlocBuilder( |
|
|
|
cubit: notiBloc, |
|
|
|
builder: (context, state) { |
|
|
|
return HoldInfinityWidget( |
|
|
|
notiBloc: notiBloc, |
|
|
|
); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
class HoldInfinityWidget extends StatelessWidget { |
|
|
|
final NotiBloc notiBloc; |
|
|
|
HoldInfinityWidget({@required this.notiBloc}); |
|
|
|
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); |
|
|
|
@override |
|
|
|
Widget build(BuildContext context) { |
|
|
|
@@ -38,6 +60,7 @@ class HoldInfinityWidget extends StatelessWidget { |
|
|
|
title: Text("Thông báo"), |
|
|
|
actions: <Widget>[ |
|
|
|
BlocBuilder<NotiBloc, NotiState>( |
|
|
|
cubit: notiBloc, |
|
|
|
builder: (context, state) { |
|
|
|
if (state is NotiSuccess) { |
|
|
|
if (state.items.length > 0) { |
|
|
|
@@ -57,11 +80,20 @@ class HoldInfinityWidget extends StatelessWidget { |
|
|
|
) |
|
|
|
], |
|
|
|
), |
|
|
|
body: InfinityView()); |
|
|
|
body: BlocBuilder<NotiBloc, NotiState>( |
|
|
|
cubit: notiBloc, |
|
|
|
builder: (context, state) { |
|
|
|
return InfinityView( |
|
|
|
notiBloc: notiBloc, |
|
|
|
); |
|
|
|
}, |
|
|
|
)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
class InfinityView extends StatefulWidget { |
|
|
|
final NotiBloc notiBloc; |
|
|
|
InfinityView({@required this.notiBloc}); |
|
|
|
@override |
|
|
|
_InfinityViewState createState() => _InfinityViewState(); |
|
|
|
} |
|
|
|
@@ -69,7 +101,6 @@ class InfinityView extends StatefulWidget { |
|
|
|
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; |
|
|
|
@@ -84,59 +115,27 @@ class _InfinityViewState extends State<InfinityView> { |
|
|
|
final maxScroll = _scrollController.position.maxScrollExtent; |
|
|
|
final currentScroll = _scrollController.position.pixels; |
|
|
|
if (maxScroll - currentScroll < _scrollThreshold) { |
|
|
|
_notiBloc.add(DataFetched()); |
|
|
|
widget.notiBloc.add(DataFetched()); |
|
|
|
} |
|
|
|
}); |
|
|
|
token = await pref.getString(DATA_CONST.TOKEN_KEY); |
|
|
|
// socketService.initial(token); |
|
|
|
_notiBloc = BlocProvider.of<NotiBloc>(context); |
|
|
|
} |
|
|
|
|
|
|
|
@override |
|
|
|
void initState() { |
|
|
|
getSharedPrefs(); |
|
|
|
widget.notiBloc.add(DataFetched()); |
|
|
|
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; |
|
|
|
} |
|
|
|
*/ |
|
|
|
}, |
|
|
|
return BlocBuilder<NotiBloc, NotiState>( |
|
|
|
cubit: widget.notiBloc, |
|
|
|
builder: (context, state) { |
|
|
|
if (state is NotiFailure) { |
|
|
|
return Center(child: Text(state.errorString)); |
|
|
|
} |
|
|
|
if (state is NotiSuccess) { |
|
|
|
} else if (state is NotiSuccess) { |
|
|
|
if (state.items.isEmpty) { |
|
|
|
return Center(child: Text("Dữ liệu rỗng")); |
|
|
|
} |
|
|
|
@@ -170,14 +169,16 @@ class _InfinityViewState extends State<InfinityView> { |
|
|
|
controller: _scrollController, |
|
|
|
), |
|
|
|
onRefresh: () async { |
|
|
|
_notiBloc.add(OnRefresh()); |
|
|
|
widget.notiBloc.add(OnRefresh()); |
|
|
|
})) |
|
|
|
], |
|
|
|
); |
|
|
|
} else if (state is NotiLoadding) { |
|
|
|
return Center( |
|
|
|
child: LoadingListPage(), |
|
|
|
); |
|
|
|
} |
|
|
|
return Center( |
|
|
|
child: LoadingListPage(), |
|
|
|
); |
|
|
|
return Container(); |
|
|
|
}, |
|
|
|
); |
|
|
|
} |