Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

fix: Removed network call to backend server when transitioning from "Member Profile" to "Send Request" screen. #138

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/remote/repositories/user_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class UserRepository {

/// Returns home statistics for the current user
Future<HomeStats> getHomeStats() async {
final body = await ApiManager.callSafely(() => ApiManager.instance.userService.getHomeStats());
final body = await ApiManager.callSafely(
() => ApiManager.instance.userService.getHomeStats());
return HomeStats.fromJson(body);
}

Expand All @@ -37,21 +38,22 @@ class UserRepository {

/// Returns current user profile
Future<User> getCurrentUser() async {
final body =
await ApiManager.callSafely(() => ApiManager.instance.userService.getCurrentUser());
final body = await ApiManager.callSafely(
() => ApiManager.instance.userService.getCurrentUser());
return User.fromJson(body);
}

/// Returns user profile with the specified id
Future<User> getUser(int userId) async {
final body = await ApiManager.callSafely(() => ApiManager.instance.userService.getUser(userId));
final body = await ApiManager.callSafely(
() => ApiManager.instance.userService.getUser(userId));
return User.fromJson(body);
}

/// Updates current user's profile
Future<CustomResponse> updateUser(User user) async {
final body =
await ApiManager.callSafely(() => ApiManager.instance.userService.updateUser(user));
final body = await ApiManager.callSafely(
() => ApiManager.instance.userService.updateUser(user));
return CustomResponse.fromJson(body);
}

Expand Down
45 changes: 33 additions & 12 deletions lib/screens/home/pages/members/bloc/members_page_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import './bloc.dart';
class MembersPageBloc extends Bloc<MembersPageEvent, MembersPageState> {
final UserRepository userRepository;
int pageNumber = 1;
MembersPageBloc({@required this.userRepository}) : assert(userRepository != null);
MembersPageBloc({@required this.userRepository})
: assert(userRepository != null);
@override
MembersPageState get initialState => MembersPageInitial();

Expand All @@ -25,47 +26,67 @@ class MembersPageBloc extends Bloc<MembersPageEvent, MembersPageState> {
}
}

Stream<MembersPageState> _mapEventToMembersShowed(MembersPageEvent event) async* {
Stream<MembersPageState> _mapEventToMembersShowed(
MembersPageEvent event) async* {
final currentState = state;

if (event is MembersPageShowed && !_hasReachedMax(currentState)) {
try {
if (currentState is MembersPageInitial) {
yield MembersPageLoading();
final List<User> users = await userRepository.getVerifiedUsers(pageNumber);
yield MembersPageSuccess(users: users, hasReachedMax: false);
final List<User> users =
await userRepository.getVerifiedUsers(pageNumber);
final User currentUser = await userRepository.getCurrentUser();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This network call will be made when the state is MembersPageInitial. This is a necessary call.

yield MembersPageSuccess(
users: users,
hasReachedMax: false,
currentUser: currentUser,
);
}
if (currentState is MembersPageSuccess) {
final users =
await userRepository.getVerifiedUsers((currentState.users.length ~/ 10) + 1);
final users = await userRepository
.getVerifiedUsers((currentState.users.length ~/ 10) + 1);
yield users.isEmpty
? currentState.copyWith(hasReachedMax: true)
: MembersPageSuccess(
users: currentState.users + users,
hasReachedMax: false,
currentUser: currentState.currentUser,
Copy link
Author

@yashhalgaonkar yashhalgaonkar Aug 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here while initializing MembersPageSuccess, we need not make the network call again. So removed it.

);
}
} on Failure catch (failure) {
Logger.root.severe("MembersPageBloc: Failure catched: $failure.message");
Logger.root
.severe("MembersPageBloc: Failure catched: $failure.message");
yield MembersPageFailure(failure.message);
}
}
}

Stream<MembersPageState> _mapEventToMembersRefresh(MembersPageEvent event) async* {
Stream<MembersPageState> _mapEventToMembersRefresh(
MembersPageEvent event) async* {
final currentState = state;

if (event is MembersPageRefresh) {
try {
yield MembersPageLoading();
final List<User> users = await userRepository.getVerifiedUsers(pageNumber);
yield MembersPageSuccess(users: users, hasReachedMax: false);
final List<User> users =
await userRepository.getVerifiedUsers(pageNumber);
final User currentUser = (currentState is MembersPageSuccess)
? currentState.currentUser
: await userRepository.getCurrentUser();
yield MembersPageSuccess(
users: users,
hasReachedMax: false,
currentUser: currentUser,
);
} on Failure catch (failure) {
Logger.root.severe("MembersPageBloc: Failure catched: $failure.message");
Logger.root
.severe("MembersPageBloc: Failure catched: $failure.message");
yield state;
}
}
}
}

bool _hasReachedMax(MembersPageState state) => state is MembersPageSuccess && state.hasReachedMax;
bool _hasReachedMax(MembersPageState state) =>
state is MembersPageSuccess && state.hasReachedMax;
2 changes: 2 additions & 0 deletions lib/screens/home/pages/members/bloc/members_page_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ class MembersPageLoading extends MembersPageState {}

class MembersPageSuccess extends MembersPageState {
final List<User> users;
final User currentUser;
final bool hasReachedMax;

MembersPageSuccess({
this.users,
this.hasReachedMax,
this.currentUser,
});

// We implemented copyWith so that we can copy an instance of MembersPageSuccess
Expand Down
15 changes: 11 additions & 4 deletions lib/screens/home/pages/members/members_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class _MembersPageState extends State<MembersPage> {

@override
Widget build(BuildContext context) {
return BlocConsumer<MembersPageBloc, MembersPageState>(listener: (context, state) {
return BlocConsumer<MembersPageBloc, MembersPageState>(
listener: (context, state) {
if (state is MembersPageShowed) {
_refreshCompleter?.complete();
_refreshCompleter = Completer();
Expand Down Expand Up @@ -75,11 +76,14 @@ class _MembersPageState extends State<MembersPage> {
return _reachedEnd
? BottomLoader()
: InkWell(
onTap: () => _openMemberProfileScreen(context, user),
onTap: () =>
_openMemberProfileScreen(context, user),
child: MemberListTile(user: user),
);
},
itemCount: state.hasReachedMax ? (state.users.length) : (state.users.length + 1),
itemCount: state.hasReachedMax
? (state.users.length)
: (state.users.length + 1),
controller: _scrollController,
),
);
Expand All @@ -96,7 +100,10 @@ class _MembersPageState extends State<MembersPage> {
void _openMemberProfileScreen(BuildContext context, User user) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => MemberProfileScreen(user: user),
builder: (context) => MemberProfileScreen(
user: user,
membersPageBloc: _membersPageBloc,
),
),
);
}
Expand Down
16 changes: 11 additions & 5 deletions lib/screens/member_profile/member_profile.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import 'package:flutter/material.dart';
import 'package:mentorship_client/remote/models/user.dart';
import 'package:mentorship_client/remote/repositories/user_repository.dart';
import 'package:mentorship_client/screens/home/pages/members/bloc/bloc.dart';
import 'package:mentorship_client/screens/member_profile/user_data_list.dart';
import 'package:mentorship_client/screens/send_request/send_request_screen.dart';
import 'package:mentorship_client/widgets/loading_indicator.dart';

class MemberProfileScreen extends StatelessWidget {
final User user;
final MembersPageBloc membersPageBloc;

const MemberProfileScreen({Key key, @required this.user})
: assert(user != null),
const MemberProfileScreen({
Key key,
@required this.user,
@required this.membersPageBloc,
}) : assert(user != null),
super(key: key);

@override
Expand Down Expand Up @@ -55,7 +59,9 @@ class MemberProfileScreen extends StatelessWidget {
),
onPressed: () async {
showProgressIndicator(context);
var currentUser = await UserRepository.instance.getCurrentUser();
final User currentUser =
(membersPageBloc.state as MembersPageSuccess)
.currentUser;
Navigator.of(context).pop();
Navigator.of(context).push(
PageRouteBuilder(
Expand All @@ -69,7 +75,7 @@ class MemberProfileScreen extends StatelessWidget {
),
);
}),
)
),
],
),
),
Expand Down