Skip to content

Commit 6c2ea17

Browse files
committed
[#31] Refactor
1 parent e081b79 commit 6c2ea17

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-35
lines changed

lib/screens/home/home_screen.dart

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
3434
}
3535

3636
Future<void> _initData() async {
37-
ref.read(homeViewModelProvider.notifier).loadSurveys();
37+
ref.read(homeViewModelProvider.notifier).loadSurveys(isRefreshing: false);
3838
}
3939

4040
@override
@@ -68,39 +68,42 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
6868
);
6969
}
7070
return Scaffold(
71+
backgroundColor: Colors.black,
7172
body: RefreshIndicator(
72-
color: Colors.white,
73-
backgroundColor: Colors.black,
74-
onRefresh:() => ref.read(homeViewModelProvider.notifier).loadSurveys(isRefreshing: true),
75-
child: SingleChildScrollView(
76-
physics: const AlwaysScrollableScrollPhysics(),
77-
child: SizedBox(
78-
height: MediaQuery.of(context).size.height,
79-
child: Stack(
80-
children: [
81-
if (surveys.isNotEmpty) ...[
82-
HomePagesWidget(
83-
surveys: surveys,
84-
currentPage: _currentPage,
85-
),
86-
const HomeHeaderWidget(),
87-
Align(
88-
alignment: Alignment.bottomCenter,
89-
child: Padding(
90-
padding: const EdgeInsets.only(bottom: 220),
91-
child: HomePageIndicatorWidget(
92-
surveysLength: surveys.length,
93-
currentPage: _currentPage,
73+
color: Colors.white,
74+
backgroundColor: Colors.black,
75+
onRefresh: () => ref
76+
.read(homeViewModelProvider.notifier)
77+
.loadSurveys(isRefreshing: true),
78+
child: SingleChildScrollView(
79+
physics: const AlwaysScrollableScrollPhysics(),
80+
child: SizedBox(
81+
height: MediaQuery.of(context).size.height,
82+
child: Stack(
83+
children: [
84+
if (surveys.isNotEmpty) ...[
85+
HomePagesWidget(
86+
surveys: surveys,
87+
currentPage: _currentPage,
88+
),
89+
const HomeHeaderWidget(),
90+
Align(
91+
alignment: Alignment.bottomCenter,
92+
child: Padding(
93+
padding: const EdgeInsets.only(bottom: 220),
94+
child: HomePageIndicatorWidget(
95+
surveysLength: surveys.length,
96+
currentPage: _currentPage,
9497
),
95-
),
96-
)
97-
],
98-
if (surveys.isEmpty || isLoading) _buildShimmerLoading(),
99-
],
98+
),
99+
)
100+
],
101+
if (surveys.isEmpty || isLoading) _buildShimmerLoading(),
102+
],
103+
),
104+
),
100105
),
101-
),
102-
),
103-
));
106+
));
104107
}
105108

106109
Widget _buildShimmerLoading() {

lib/screens/home/home_view_model.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ class HomeViewModel extends StateNotifier<HomeState> {
3939
final _error = StreamController<String>();
4040
Stream<String> get error => _error.stream;
4141

42-
void loadSurveys() async {
43-
_loadSurveysFromCache();
42+
Future<void> loadSurveys({required bool isRefreshing}) async {
43+
if (!isRefreshing) {
44+
_loadSurveysFromCache();
45+
}
4446
_loadSurveysFromRemote();
4547
}
4648

test/screens/home/home_view_model_test.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void main() {
5050
.thenAnswer((_) => Future.value(Success(surveys)));
5151
final surveysStream = homeViewModel.surveys;
5252
final stateStream = homeViewModel.stream;
53-
homeViewModel.loadSurveys();
53+
homeViewModel.loadSurveys(isRefreshing: false);
5454
expect(surveysStream, emitsInOrder([surveys]));
5555
expect(
5656
stateStream,
@@ -61,6 +61,17 @@ void main() {
6161
},
6262
);
6363

64+
test(
65+
'When refreshing surveys successfully and emits a list of surveys with state LoadSurveysSuccess',
66+
() {
67+
when(mockGetSurveysUseCase.call(any)).thenAnswer(
68+
(_) async => Success(surveys),
69+
);
70+
final surveysStream = homeViewModel.surveys;
71+
homeViewModel.loadSurveys(isRefreshing: true);
72+
expect(surveysStream, emitsInOrder([surveys]));
73+
});
74+
6475
test(
6576
'loads surveys with error and emits error state',
6677
() async {
@@ -71,7 +82,7 @@ void main() {
7182
(_) async => Failed(exception),
7283
);
7384
final errorStream = homeViewModel.error;
74-
homeViewModel.loadSurveys();
85+
homeViewModel.loadSurveys(isRefreshing: false);
7586
expect(
7687
errorStream,
7788
emitsInOrder(

0 commit comments

Comments
 (0)