Skip to content

Commit

Permalink
Completed navigate tap notification (#285)
Browse files Browse the repository at this point in the history
* Navigate when tap noti

* Complete navigate to rescue post
  • Loading branch information
tvc12 authored Jul 4, 2020
1 parent 78d6bd7 commit ec5336c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
12 changes: 9 additions & 3 deletions lib/notification/bloc/notification_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class NotificationBloc extends TBloc<NotificationEvent, NotificationState> {
final List<PetNotification> notifications = [];
final int limit = 15;
@protected
bool isLocked = false;
@protected
Timer timer;

@protected
Expand All @@ -25,7 +27,10 @@ class NotificationBloc extends TBloc<NotificationEvent, NotificationState> {
_reloadNotification(event);
break;
case RetrieveNotificationEvent:
_retrieveNotification(event);
if (!isLocked) {
isLocked = true;
_retrieveNotification(event);
}
break;
case ReadNotification:
stopListener();
Expand Down Expand Up @@ -67,7 +72,8 @@ class NotificationBloc extends TBloc<NotificationEvent, NotificationState> {
service
.getNotifications(offset: event.offset, limit: event.limit)
.then(updateNotifications)
.catchError((ex) => Log.error(ex));
.catchError((ex) => Log.error(ex))
.whenComplete(() => isLocked = false);
}

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

void clear() {
Expand Down
1 change: 1 addition & 0 deletions lib/notification/notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:flutter_template/login/widget/widget.dart';
import 'package:flutter_template/notification/bloc/bloc.dart';
import 'package:flutter_template/notification/widget/widget.dart';
import 'package:flutter_template/post/post.dart';
import 'package:flutter_template/rescue_post/screen/screen.dart';
import 'package:petisland_core/domain/domain.dart';
import 'package:petisland_core/petisland_core.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
Expand Down
11 changes: 6 additions & 5 deletions lib/notification/notification_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ class _NotificationScreenState extends TState<NotificationScreen> {
appBar: AppBar(
title: Text(
'Notification',
style: TTextStyles.bold(
fontSize: 18,
color: TColors.white
),
style: TTextStyles.bold(fontSize: 18, color: TColors.white),
textAlign: TextAlign.start,
),
elevation: 1,
Expand Down Expand Up @@ -111,14 +108,18 @@ class _NotificationScreenState extends TState<NotificationScreen> {

void _onTap(PetNotification notification) async {
// if (!notification.isRead)
Log.info('PetNotification:: ${notification.type.id} ${notification.type.name}');
bloc.readNotification(notification.id);
setState(() {
notification.isRead = true;
});
bloc.stopListener();
final PopResult data = await navigateToScreen<PopResult>(
context: context,
screen: PostLoadingScreen(id: notification.type.id),
screen: PostLoadingScreen(
id: notification.type.id,
type: notification.type.name,
),
screenName: PostLoadingScreen.name,
);
if (data == PopResult.Failure) {
Expand Down
35 changes: 30 additions & 5 deletions lib/notification/post_loading_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ part of petisland.notification;
class PostLoadingScreen extends StatefulWidget {
static const String name = '/PostLoadingScreen';
final String id;
final String type;

const PostLoadingScreen({Key key, @required this.id}) : super(key: key);
const PostLoadingScreen({Key key, @required this.id, @required this.type})
: super(key: key);

@override
_PostLoadingScreenState createState() => _PostLoadingScreenState();
}

class _PostLoadingScreenState extends State<PostLoadingScreen> {
final PostService service = DI.get(PostService);
PostService get postService => DI.get(PostService);
RescueService get rescueService => DI.get(RescueService);

@override
void initState() {
super.initState();

service.getPost(widget.id).then(_handleResult).catchError(_handleError);
loadData();
}

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

FutureOr _handleResult(Post value) {
FutureOr _navigatePost(Post value) {
Navigator.pushReplacement(
context,
TPageRoute(
Expand All @@ -42,4 +44,27 @@ class _PostLoadingScreenState extends State<PostLoadingScreen> {
),
);
}

void loadData() {
switch (widget.type.toLowerCase()) {
case 'rescue':
rescueService.getRescue(widget.id).then(_navigateRescue).catchError(_handleError);
break;

case 'post':
postService.getPost(widget.id).then(_navigatePost).catchError(_handleError);
break;
default:
}
}

FutureOr _navigateRescue(Rescue value) {
Navigator.pushReplacement(
context,
TPageRoute(
builder: (_) => RescueDetailScreen(rescue: value),
settings: RouteSettings(name: RescueDetailScreen.name),
),
);
}
}
15 changes: 15 additions & 0 deletions petisland_core/lib/repository/rescue_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ class MockRescueRepository extends RescueRepository {
// TODO: implement addComment
throw UnimplementedError();
}

@override
Future<Rescue> getRescue(String rescueId) {
// TODO: implement getRescue
throw UnimplementedError();
}
}

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

Future<bool> addComment(String rescueId, String message);

Future<Rescue> getRescue(String rescueId);
// Future<List<Hero
}

Expand Down Expand Up @@ -303,4 +311,11 @@ class RescueRepositoryImpl extends RescueRepository {
final body = {'message': message};
return client.post('/rescue-service/$rescueId/comment', body).then((value) => true);
}

@override
Future<Rescue> getRescue(String rescueId) {
return client
.get('/rescue-service/rescue-posts/$rescueId')
.then((json) => Rescue.fromJson(json));
}
}
7 changes: 7 additions & 0 deletions petisland_core/lib/service/rescue_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ abstract class RescueService {
Future<bool> unJoin(String id);

Future<bool> addComment(String rescueId, String message);

Future<Rescue> getRescue(String rescueId);
}

class RescueServiceImpl extends RescueService {
Expand Down Expand Up @@ -77,4 +79,9 @@ class RescueServiceImpl extends RescueService {
Future<bool> addComment(String rescueId, String message) {
return repository.addComment(rescueId, message);
}

@override
Future<Rescue> getRescue(String rescueId) {
return repository.getRescue(rescueId);
}
}

0 comments on commit ec5336c

Please sign in to comment.