Skip to content

Commit 63a825a

Browse files
authored
🔀 #87 from boostcampwm-2022/feat/swipeRefresh
Shimmer관련 이슈 수정, 새로고침 기능 구현
2 parents fd2a23a + a967d9e commit 63a825a

File tree

4 files changed

+72
-34
lines changed

4 files changed

+72
-34
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ buildscript {
3535
hiltWorkerVersion = "1.0.0"
3636
hiltCompilerVersion = "1.0.0"
3737
crashlyticsVersion = "2.9.2"
38+
swipeRefreshLayoutVersion = "1.1.0"
3839
}
3940
dependencies {
4041
classpath "com.google.gms:google-services:$googleServiceVersion"

presentation/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,8 @@ dependencies {
117117
// HiltWorker
118118
implementation "androidx.hilt:hilt-work:$hiltWorkerVersion"
119119
kapt "androidx.hilt:hilt-compiler:$hiltCompilerVersion"
120+
121+
// SwipeRefresh
122+
implementation "androidx.swiperefreshlayout:swiperefreshlayout:$swipeRefreshLayoutVersion"
123+
120124
}

presentation/src/main/java/com/whyranoid/presentation/community/CommunityItemFragment.kt

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,22 @@ internal class CommunityItemFragment :
118118
viewLifecycleOwner.repeatWhenUiStarted {
119119
postAdapter.loadStateFlow.collectLatest { loadStates ->
120120
binding.shimmerCommunity.isVisible = loadStates.refresh is LoadState.Loading
121+
binding.rvCommunity.isVisible = (loadStates.refresh is LoadState.Loading).not()
121122
}
122123
}
123124

124125
binding.rvCommunity.adapter = postAdapter
125126

127+
binding.swipeRefreshLayout.apply {
128+
setOnRefreshListener {
129+
postAdapter.refresh()
130+
binding.rvCommunity.scrollToPosition(POSITION_TOP)
131+
binding.swipeRefreshLayout.isRefreshing = false
132+
}
133+
134+
setColorSchemeColors(context.getColor(R.color.mogakrun_on_primary))
135+
}
136+
126137
viewLifecycleOwner.repeatWhenUiStarted {
127138
viewModel.myGroupList.collectLatest { myGroupList ->
128139
postAdapter.setMyGroupList(myGroupList)
@@ -138,6 +149,7 @@ internal class CommunityItemFragment :
138149
}
139150

140151
private fun setMyGroupAdapter() {
152+
binding.swipeRefreshLayout.isEnabled = false
141153
val myGroupAdapter = MyGroupAdapter { groupInfo ->
142154
viewModel.onGroupItemClicked(groupInfo)
143155
}
@@ -169,21 +181,33 @@ internal class CommunityItemFragment :
169181
viewLifecycleOwner.repeatWhenUiStarted {
170182
postAdapter.loadStateFlow.collectLatest { loadStates ->
171183
binding.shimmerCommunity.isVisible = loadStates.refresh is LoadState.Loading
184+
binding.rvCommunity.isVisible = (loadStates.refresh is LoadState.Loading).not()
172185
}
173186
}
174187

175188
binding.rvCommunity.adapter = postAdapter
176189

190+
binding.swipeRefreshLayout.apply {
191+
setOnRefreshListener {
192+
postAdapter.refresh()
193+
binding.rvCommunity.scrollToPosition(POSITION_TOP)
194+
binding.swipeRefreshLayout.isRefreshing = false
195+
}
196+
197+
setColorSchemeColors(context.getColor(R.color.mogakrun_on_primary))
198+
}
199+
177200
viewLifecycleOwner.repeatWhenUiStarted {
178201
viewModel.myGroupList.collect { myGroupList ->
179202
postAdapter.setMyGroupList(myGroupList)
180203
}
181204
}
182205

183206
viewLifecycleOwner.repeatWhenUiStarted {
184-
viewModel.getMyPagingPostsUseCase(viewLifecycleOwner.lifecycleScope).collectLatest { myPostList ->
185-
postAdapter.submitData(myPostList)
186-
}
207+
viewModel.getMyPagingPostsUseCase(viewLifecycleOwner.lifecycleScope)
208+
.collectLatest { myPostList ->
209+
postAdapter.submitData(myPostList)
210+
}
187211
}
188212
}
189213
}
@@ -197,6 +221,7 @@ internal class CommunityItemFragment :
197221

198222
companion object {
199223
private const val COMMUNITY_CATEGORY_KEY = "communityCategoryKey"
224+
private const val POSITION_TOP = 0
200225

201226
fun newInstance(communityCategory: CommunityCategory): CommunityItemFragment {
202227
val fragment = CommunityItemFragment()

presentation/src/main/res/layout/fragment_community_item.xml

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,51 @@
77

88
</data>
99

10-
<androidx.constraintlayout.widget.ConstraintLayout
10+
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
11+
android:id="@+id/swipe_refresh_layout"
1112
android:layout_width="match_parent"
1213
android:layout_height="match_parent"
13-
tools:context=".community.CommunityItemFragment">
14-
15-
<androidx.recyclerview.widget.RecyclerView
16-
android:id="@+id/rv_community"
17-
android:layout_width="0dp"
18-
android:layout_height="0dp"
19-
app:layout_constraintStart_toStartOf="parent"
20-
app:layout_constraintEnd_toEndOf="parent"
21-
app:layout_constraintTop_toTopOf="parent"
22-
app:layout_constraintBottom_toBottomOf="parent"
23-
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
24-
25-
<com.facebook.shimmer.ShimmerFrameLayout
26-
android:id="@+id/shimmer_community"
27-
android:layout_width="match_parent"
28-
android:layout_height="match_parent">
14+
app:swipeRefreshLayoutProgressSpinnerBackgroundColor="@color/mogakrun_background">
2915

30-
<LinearLayout
16+
<androidx.constraintlayout.widget.ConstraintLayout
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent"
19+
tools:context=".community.CommunityItemFragment">
20+
21+
<androidx.recyclerview.widget.RecyclerView
22+
android:id="@+id/rv_community"
23+
android:layout_width="0dp"
24+
android:layout_height="0dp"
25+
app:layout_constraintStart_toStartOf="parent"
26+
app:layout_constraintEnd_toEndOf="parent"
27+
app:layout_constraintTop_toTopOf="parent"
28+
app:layout_constraintBottom_toBottomOf="parent"
29+
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
30+
31+
<com.facebook.shimmer.ShimmerFrameLayout
32+
android:id="@+id/shimmer_community"
3133
android:layout_width="match_parent"
32-
android:layout_height="match_parent"
33-
android:orientation="vertical">
34+
android:layout_height="match_parent">
35+
36+
<LinearLayout
37+
android:layout_width="match_parent"
38+
android:layout_height="match_parent"
39+
android:orientation="vertical">
40+
41+
<include layout="@layout/my_group_item_loading"/>
42+
<include layout="@layout/my_group_item_loading"/>
43+
<include layout="@layout/my_group_item_loading"/>
44+
<include layout="@layout/my_group_item_loading"/>
45+
<include layout="@layout/my_group_item_loading"/>
46+
<include layout="@layout/my_group_item_loading"/>
47+
<include layout="@layout/my_group_item_loading"/>
48+
<include layout="@layout/my_group_item_loading"/>
3449

35-
<include layout="@layout/my_group_item_loading"/>
36-
<include layout="@layout/my_group_item_loading"/>
37-
<include layout="@layout/my_group_item_loading"/>
38-
<include layout="@layout/my_group_item_loading"/>
39-
<include layout="@layout/my_group_item_loading"/>
40-
<include layout="@layout/my_group_item_loading"/>
41-
<include layout="@layout/my_group_item_loading"/>
42-
<include layout="@layout/my_group_item_loading"/>
50+
</LinearLayout>
4351

44-
</LinearLayout>
52+
</com.facebook.shimmer.ShimmerFrameLayout>
4553

46-
</com.facebook.shimmer.ShimmerFrameLayout>
54+
</androidx.constraintlayout.widget.ConstraintLayout>
4755

48-
</androidx.constraintlayout.widget.ConstraintLayout>
56+
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
4957
</layout>

0 commit comments

Comments
 (0)