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 1 commit
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
44 changes: 32 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,66 @@ 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);
final User currentUser = await userRepository.getCurrentUser();
Copy link
Contributor

Choose a reason for hiding this comment

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

@yashhalgaonkar I'm talking about this line?

Copy link
Contributor

Choose a reason for hiding this comment

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

this is the event added when the user scrolls to the end of the page to load more users

Copy link
Author

Choose a reason for hiding this comment

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

Okay. I am looking into this.

Copy link
Author

Choose a reason for hiding this comment

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

Should I use sqflite to store the current user info in local storage? This will make it easy to get current user info in all other parts of app as well, solving the problem once and for all.

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, give it a try

yield users.isEmpty
? currentState.copyWith(hasReachedMax: true)
: MembersPageSuccess(
users: currentState.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 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 = 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