Browse Source

Fetch get list notification when receive onMessage

master
daivph 5 years ago
parent
commit
1503e3533d
5 changed files with 76 additions and 53 deletions
  1. +3
    -3
      lib/data/api/dio_provider.dart
  2. +21
    -1
      lib/presentation/screens/notification/bloc/noti_bloc.dart
  3. +2
    -0
      lib/presentation/screens/notification/bloc/noti_event.dart
  4. +49
    -48
      lib/presentation/screens/notification/sc_notification.dart
  5. +1
    -1
      pubspec.yaml

+ 3
- 3
lib/data/api/dio_provider.dart View File

@@ -36,14 +36,14 @@ class HttpLogInterceptor extends InterceptorsWrapper {

@override
Future onResponse(Response response) {
log("onResponse: $response");
// log("onResponse: $response");
return super.onResponse(response);
}

@override
Future onError(DioError err) {
log("onError: $err\n"
"Response: ${err.response}");
// log("onError: $err\n"
// "Response: ${err.response}");
return super.onError(err);
}
}

+ 21
- 1
lib/presentation/screens/notification/bloc/noti_bloc.dart View File

@@ -29,10 +29,13 @@ class NotiBloc extends Bloc<NotiEvent, NotiState> {
yield NotiLoadding();
final response =
await repository.getNotifications(page: 0, size: pageSize);
List<NotificationDTO> items = new List<NotificationDTO>();
response.notificationDTO
.forEach((e) => items.add(NotificationDTO.clone(e)));
yield NotiSuccess(
unread: response.numberUnreadTotal,
read: response.numberReadTotal,
items: response.notificationDTO,
items: items,
page: 0,
hasReachedMax:
response.notificationDTO.length < pageSize ? true : false);
@@ -73,6 +76,23 @@ class NotiBloc extends Bloc<NotiEvent, NotiState> {
} catch (e) {
yield NotiFailure(errorString: AppException.handleError(e));
}
} else if (event is OnRefreshFromNotification) {
try {
final response =
await repository.getNotifications(page: 0, size: pageSize);
List<NotificationDTO> items = new List<NotificationDTO>();
response.notificationDTO
.forEach((e) => items.add(NotificationDTO.clone(e)));
yield NotiSuccess(
unread: response.numberUnreadTotal,
read: response.numberReadTotal,
items: items,
page: 0,
hasReachedMax:
response.notificationDTO.length < pageSize ? true : false);
} catch (e) {
yield NotiFailure(errorString: AppException.handleError(e));
}
}

if (event is OnUpdate) {

+ 2
- 0
lib/presentation/screens/notification/bloc/noti_event.dart View File

@@ -11,6 +11,8 @@ class DataFetched extends NotiEvent {}

class OnRefresh extends NotiEvent {}

class OnRefreshFromNotification extends NotiEvent {}

class OnUpdate<T> extends NotiEvent {
final int unread;
final int read;

+ 49
- 48
lib/presentation/screens/notification/sc_notification.dart View File

@@ -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();
},
);
}

+ 1
- 1
pubspec.yaml View File

@@ -2,7 +2,7 @@ name: farm_tpf
description: A new Flutter project.

publish_to: 'none'
version: 0.5.0+1
version: 0.6.0+1

environment:
sdk: ">=2.7.0 <3.0.0"

Loading…
Cancel
Save