Skip to content

Commit

Permalink
[#31] Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nkhanh44 committed Aug 24, 2023
1 parent e081b79 commit 6c2ea17
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
65 changes: 34 additions & 31 deletions lib/screens/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
}

Future<void> _initData() async {
ref.read(homeViewModelProvider.notifier).loadSurveys();
ref.read(homeViewModelProvider.notifier).loadSurveys(isRefreshing: false);
}

@override
Expand Down Expand Up @@ -68,39 +68,42 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
);
}
return Scaffold(
backgroundColor: Colors.black,
body: RefreshIndicator(
color: Colors.white,
backgroundColor: Colors.black,
onRefresh:() => ref.read(homeViewModelProvider.notifier).loadSurveys(isRefreshing: true),
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: Stack(
children: [
if (surveys.isNotEmpty) ...[
HomePagesWidget(
surveys: surveys,
currentPage: _currentPage,
),
const HomeHeaderWidget(),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(bottom: 220),
child: HomePageIndicatorWidget(
surveysLength: surveys.length,
currentPage: _currentPage,
color: Colors.white,
backgroundColor: Colors.black,
onRefresh: () => ref
.read(homeViewModelProvider.notifier)
.loadSurveys(isRefreshing: true),
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: Stack(
children: [
if (surveys.isNotEmpty) ...[
HomePagesWidget(
surveys: surveys,
currentPage: _currentPage,
),
const HomeHeaderWidget(),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(bottom: 220),
child: HomePageIndicatorWidget(
surveysLength: surveys.length,
currentPage: _currentPage,
),
),
)
],
if (surveys.isEmpty || isLoading) _buildShimmerLoading(),
],
),
)
],
if (surveys.isEmpty || isLoading) _buildShimmerLoading(),
],
),
),
),
),
),
));
));
}

Widget _buildShimmerLoading() {
Expand Down
6 changes: 4 additions & 2 deletions lib/screens/home/home_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ class HomeViewModel extends StateNotifier<HomeState> {
final _error = StreamController<String>();
Stream<String> get error => _error.stream;

void loadSurveys() async {
_loadSurveysFromCache();
Future<void> loadSurveys({required bool isRefreshing}) async {
if (!isRefreshing) {
_loadSurveysFromCache();
}
_loadSurveysFromRemote();
}

Expand Down
15 changes: 13 additions & 2 deletions test/screens/home/home_view_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void main() {
.thenAnswer((_) => Future.value(Success(surveys)));
final surveysStream = homeViewModel.surveys;
final stateStream = homeViewModel.stream;
homeViewModel.loadSurveys();
homeViewModel.loadSurveys(isRefreshing: false);
expect(surveysStream, emitsInOrder([surveys]));
expect(
stateStream,
Expand All @@ -61,6 +61,17 @@ void main() {
},
);

test(
'When refreshing surveys successfully and emits a list of surveys with state LoadSurveysSuccess',
() {
when(mockGetSurveysUseCase.call(any)).thenAnswer(
(_) async => Success(surveys),
);
final surveysStream = homeViewModel.surveys;
homeViewModel.loadSurveys(isRefreshing: true);
expect(surveysStream, emitsInOrder([surveys]));
});

test(
'loads surveys with error and emits error state',
() async {
Expand All @@ -71,7 +82,7 @@ void main() {
(_) async => Failed(exception),
);
final errorStream = homeViewModel.error;
homeViewModel.loadSurveys();
homeViewModel.loadSurveys(isRefreshing: false);
expect(
errorStream,
emitsInOrder(
Expand Down

0 comments on commit 6c2ea17

Please sign in to comment.