Skip to content

Commit 497554d

Browse files
authored
Merge pull request #67 from nimblehq/feature/16-refresh-survey-list
2 parents 5ac9973 + 6c2ea17 commit 497554d

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

lib/screens/home/home_screen.dart

Lines changed: 36 additions & 23 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,29 +68,42 @@ class _HomeScreenState extends ConsumerState<HomeScreen> {
6868
);
6969
}
7070
return Scaffold(
71-
body: Stack(
72-
children: [
73-
if (surveys.isNotEmpty) ...[
74-
HomePagesWidget(
75-
surveys: surveys,
76-
currentPage: _currentPage,
77-
),
78-
const HomeHeaderWidget(),
79-
Align(
80-
alignment: Alignment.bottomCenter,
81-
child: Padding(
82-
padding: const EdgeInsets.only(bottom: 220),
83-
child: HomePageIndicatorWidget(
84-
surveysLength: surveys.length,
85-
currentPage: _currentPage,
86-
),
71+
backgroundColor: Colors.black,
72+
body: RefreshIndicator(
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,
97+
),
98+
),
99+
)
100+
],
101+
if (surveys.isEmpty || isLoading) _buildShimmerLoading(),
102+
],
87103
),
88-
)
89-
],
90-
if (surveys.isEmpty || isLoading) _buildShimmerLoading(),
91-
],
92-
),
93-
);
104+
),
105+
),
106+
));
94107
}
95108

96109
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)