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



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


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

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

yield NotiLoadding(); yield NotiLoadding();
final response = final response =
await repository.getNotifications(page: 0, size: pageSize); await repository.getNotifications(page: 0, size: pageSize);
List<NotificationDTO> items = new List<NotificationDTO>();
response.notificationDTO
.forEach((e) => items.add(NotificationDTO.clone(e)));
yield NotiSuccess( yield NotiSuccess(
unread: response.numberUnreadTotal, unread: response.numberUnreadTotal,
read: response.numberReadTotal, read: response.numberReadTotal,
items: response.notificationDTO,
items: items,
page: 0, page: 0,
hasReachedMax: hasReachedMax:
response.notificationDTO.length < pageSize ? true : false); response.notificationDTO.length < pageSize ? true : false);
} catch (e) { } catch (e) {
yield NotiFailure(errorString: AppException.handleError(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) { if (event is OnUpdate) {

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



class OnRefresh extends NotiEvent {} class OnRefresh extends NotiEvent {}


class OnRefreshFromNotification extends NotiEvent {}

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

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

import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_detail.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/presentation/screens/plot_detail/sc_plot_information.dart';
import 'package:farm_tpf/utils/pref.dart'; import 'package:farm_tpf/utils/pref.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:farm_tpf/utils/formatter.dart'; import 'package:farm_tpf/utils/formatter.dart';
} }


class _NotificationScreenState extends State<NotificationScreen> { 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 @override
Widget build(BuildContext context) { 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 { class HoldInfinityWidget extends StatelessWidget {
final NotiBloc notiBloc;
HoldInfinityWidget({@required this.notiBloc});
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
title: Text("Thông báo"), title: Text("Thông báo"),
actions: <Widget>[ actions: <Widget>[
BlocBuilder<NotiBloc, NotiState>( BlocBuilder<NotiBloc, NotiState>(
cubit: notiBloc,
builder: (context, state) { builder: (context, state) {
if (state is NotiSuccess) { if (state is NotiSuccess) {
if (state.items.length > 0) { if (state.items.length > 0) {
) )
], ],
), ),
body: InfinityView());
body: BlocBuilder<NotiBloc, NotiState>(
cubit: notiBloc,
builder: (context, state) {
return InfinityView(
notiBloc: notiBloc,
);
},
));
} }
} }


class InfinityView extends StatefulWidget { class InfinityView extends StatefulWidget {
final NotiBloc notiBloc;
InfinityView({@required this.notiBloc});
@override @override
_InfinityViewState createState() => _InfinityViewState(); _InfinityViewState createState() => _InfinityViewState();
} }
class _InfinityViewState extends State<InfinityView> { class _InfinityViewState extends State<InfinityView> {
final _scrollController = ScrollController(); final _scrollController = ScrollController();
final _scrollThreshold = 250.0; final _scrollThreshold = 250.0;
NotiBloc _notiBloc;
List<NotificationDTO> currentItems = new List<NotificationDTO>(); List<NotificationDTO> currentItems = new List<NotificationDTO>();
int latestId = 0; int latestId = 0;
int currentPage = 0; int currentPage = 0;
final maxScroll = _scrollController.position.maxScrollExtent; final maxScroll = _scrollController.position.maxScrollExtent;
final currentScroll = _scrollController.position.pixels; final currentScroll = _scrollController.position.pixels;
if (maxScroll - currentScroll < _scrollThreshold) { if (maxScroll - currentScroll < _scrollThreshold) {
_notiBloc.add(DataFetched());
widget.notiBloc.add(DataFetched());
} }
}); });
token = await pref.getString(DATA_CONST.TOKEN_KEY); token = await pref.getString(DATA_CONST.TOKEN_KEY);
// socketService.initial(token);
_notiBloc = BlocProvider.of<NotiBloc>(context);
} }


@override @override
void initState() { void initState() {
getSharedPrefs(); getSharedPrefs();
widget.notiBloc.add(DataFetched());
super.initState(); super.initState();
} }


@override @override
Widget build(BuildContext context) { 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) { builder: (context, state) {
if (state is NotiFailure) { if (state is NotiFailure) {
return Center(child: Text(state.errorString)); return Center(child: Text(state.errorString));
}
if (state is NotiSuccess) {
} else if (state is NotiSuccess) {
if (state.items.isEmpty) { if (state.items.isEmpty) {
return Center(child: Text("Dữ liệu rỗng")); return Center(child: Text("Dữ liệu rỗng"));
} }
controller: _scrollController, controller: _scrollController,
), ),
onRefresh: () async { 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

description: A new Flutter project. description: A new Flutter project.


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


environment: environment:
sdk: ">=2.7.0 <3.0.0" sdk: ">=2.7.0 <3.0.0"

Loading…
Cancel
Save