Skip to content

Commit ec5336c

Browse files
authored
Completed navigate tap notification (#285)
* Navigate when tap noti * Complete navigate to rescue post
1 parent 78d6bd7 commit ec5336c

File tree

6 files changed

+68
-13
lines changed

6 files changed

+68
-13
lines changed

lib/notification/bloc/notification_bloc.dart

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class NotificationBloc extends TBloc<NotificationEvent, NotificationState> {
55
final List<PetNotification> notifications = [];
66
final int limit = 15;
77
@protected
8+
bool isLocked = false;
9+
@protected
810
Timer timer;
911

1012
@protected
@@ -25,7 +27,10 @@ class NotificationBloc extends TBloc<NotificationEvent, NotificationState> {
2527
_reloadNotification(event);
2628
break;
2729
case RetrieveNotificationEvent:
28-
_retrieveNotification(event);
30+
if (!isLocked) {
31+
isLocked = true;
32+
_retrieveNotification(event);
33+
}
2934
break;
3035
case ReadNotification:
3136
stopListener();
@@ -67,7 +72,8 @@ class NotificationBloc extends TBloc<NotificationEvent, NotificationState> {
6772
service
6873
.getNotifications(offset: event.offset, limit: event.limit)
6974
.then(updateNotifications)
70-
.catchError((ex) => Log.error(ex));
75+
.catchError((ex) => Log.error(ex))
76+
.whenComplete(() => isLocked = false);
7177
}
7278

7379
@override
@@ -84,7 +90,7 @@ class NotificationBloc extends TBloc<NotificationEvent, NotificationState> {
8490
void startListener() {
8591
getNotification();
8692
if (timer?.isActive == true) timer.cancel();
87-
timer = Timer.periodic(const Duration(seconds: 3), (_) => getNotification());
93+
timer = Timer.periodic(const Duration(seconds: 5), (_) => getNotification());
8894
}
8995

9096
void clear() {

lib/notification/notification.dart

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:flutter_template/login/widget/widget.dart';
1212
import 'package:flutter_template/notification/bloc/bloc.dart';
1313
import 'package:flutter_template/notification/widget/widget.dart';
1414
import 'package:flutter_template/post/post.dart';
15+
import 'package:flutter_template/rescue_post/screen/screen.dart';
1516
import 'package:petisland_core/domain/domain.dart';
1617
import 'package:petisland_core/petisland_core.dart';
1718
import 'package:pull_to_refresh/pull_to_refresh.dart';

lib/notification/notification_screen.dart

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ class _NotificationScreenState extends TState<NotificationScreen> {
2222
appBar: AppBar(
2323
title: Text(
2424
'Notification',
25-
style: TTextStyles.bold(
26-
fontSize: 18,
27-
color: TColors.white
28-
),
25+
style: TTextStyles.bold(fontSize: 18, color: TColors.white),
2926
textAlign: TextAlign.start,
3027
),
3128
elevation: 1,
@@ -111,14 +108,18 @@ class _NotificationScreenState extends TState<NotificationScreen> {
111108

112109
void _onTap(PetNotification notification) async {
113110
// if (!notification.isRead)
111+
Log.info('PetNotification:: ${notification.type.id} ${notification.type.name}');
114112
bloc.readNotification(notification.id);
115113
setState(() {
116114
notification.isRead = true;
117115
});
118116
bloc.stopListener();
119117
final PopResult data = await navigateToScreen<PopResult>(
120118
context: context,
121-
screen: PostLoadingScreen(id: notification.type.id),
119+
screen: PostLoadingScreen(
120+
id: notification.type.id,
121+
type: notification.type.name,
122+
),
122123
screenName: PostLoadingScreen.name,
123124
);
124125
if (data == PopResult.Failure) {

lib/notification/post_loading_screen.dart

+30-5
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ part of petisland.notification;
33
class PostLoadingScreen extends StatefulWidget {
44
static const String name = '/PostLoadingScreen';
55
final String id;
6+
final String type;
67

7-
const PostLoadingScreen({Key key, @required this.id}) : super(key: key);
8+
const PostLoadingScreen({Key key, @required this.id, @required this.type})
9+
: super(key: key);
810

911
@override
1012
_PostLoadingScreenState createState() => _PostLoadingScreenState();
1113
}
1214

1315
class _PostLoadingScreenState extends State<PostLoadingScreen> {
14-
final PostService service = DI.get(PostService);
16+
PostService get postService => DI.get(PostService);
17+
RescueService get rescueService => DI.get(RescueService);
1518

1619
@override
1720
void initState() {
1821
super.initState();
19-
20-
service.getPost(widget.id).then(_handleResult).catchError(_handleError);
22+
loadData();
2123
}
2224

2325
@override
@@ -30,7 +32,7 @@ class _PostLoadingScreenState extends State<PostLoadingScreen> {
3032
Navigator.pop<PopResult>(context, PopResult.Failure);
3133
}
3234

33-
FutureOr _handleResult(Post value) {
35+
FutureOr _navigatePost(Post value) {
3436
Navigator.pushReplacement(
3537
context,
3638
TPageRoute(
@@ -42,4 +44,27 @@ class _PostLoadingScreenState extends State<PostLoadingScreen> {
4244
),
4345
);
4446
}
47+
48+
void loadData() {
49+
switch (widget.type.toLowerCase()) {
50+
case 'rescue':
51+
rescueService.getRescue(widget.id).then(_navigateRescue).catchError(_handleError);
52+
break;
53+
54+
case 'post':
55+
postService.getPost(widget.id).then(_navigatePost).catchError(_handleError);
56+
break;
57+
default:
58+
}
59+
}
60+
61+
FutureOr _navigateRescue(Rescue value) {
62+
Navigator.pushReplacement(
63+
context,
64+
TPageRoute(
65+
builder: (_) => RescueDetailScreen(rescue: value),
66+
settings: RouteSettings(name: RescueDetailScreen.name),
67+
),
68+
);
69+
}
4570
}

petisland_core/lib/repository/rescue_repository.dart

+15
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ class MockRescueRepository extends RescueRepository {
178178
// TODO: implement addComment
179179
throw UnimplementedError();
180180
}
181+
182+
@override
183+
Future<Rescue> getRescue(String rescueId) {
184+
// TODO: implement getRescue
185+
throw UnimplementedError();
186+
}
181187
}
182188

183189
abstract class RescueRepository {
@@ -200,6 +206,8 @@ abstract class RescueRepository {
200206
Future<bool> deleteComment(String rescueId, String commentId);
201207

202208
Future<bool> addComment(String rescueId, String message);
209+
210+
Future<Rescue> getRescue(String rescueId);
203211
// Future<List<Hero
204212
}
205213

@@ -303,4 +311,11 @@ class RescueRepositoryImpl extends RescueRepository {
303311
final body = {'message': message};
304312
return client.post('/rescue-service/$rescueId/comment', body).then((value) => true);
305313
}
314+
315+
@override
316+
Future<Rescue> getRescue(String rescueId) {
317+
return client
318+
.get('/rescue-service/rescue-posts/$rescueId')
319+
.then((json) => Rescue.fromJson(json));
320+
}
306321
}

petisland_core/lib/service/rescue_service.dart

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ abstract class RescueService {
2020
Future<bool> unJoin(String id);
2121

2222
Future<bool> addComment(String rescueId, String message);
23+
24+
Future<Rescue> getRescue(String rescueId);
2325
}
2426

2527
class RescueServiceImpl extends RescueService {
@@ -77,4 +79,9 @@ class RescueServiceImpl extends RescueService {
7779
Future<bool> addComment(String rescueId, String message) {
7880
return repository.addComment(rescueId, message);
7981
}
82+
83+
@override
84+
Future<Rescue> getRescue(String rescueId) {
85+
return repository.getRescue(rescueId);
86+
}
8087
}

0 commit comments

Comments
 (0)