Skip to content

Commit cb8f420

Browse files
authored
🔀 #20 from boostcampwm-2022/feat/communityTab
커뮤니티 탭 기본 틀 구현
2 parents 4baee7a + 4b2b86d commit cb8f420

File tree

10 files changed

+191
-1
lines changed

10 files changed

+191
-1
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ buildscript {
2020
activityKtxVersion = "1.6.1"
2121
fragmentKtxVersion = "1.5.4"
2222
lifecycleViewmodelKtxVersion = "2.5.1"
23+
viewPager2Version = "1.0.0"
2324
}
2425
dependencies {
2526
classpath "com.google.gms:google-services:$googleServiceVersion"

presentation/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,7 @@ dependencies {
6767

6868
// ViewModelScope
6969
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleViewmodelKtxVersion"
70+
71+
// ViewPager2
72+
implementation "androidx.viewpager2:viewpager2:$viewPager2Version"
7073
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.whyranoid.presentation.community
2+
3+
import androidx.annotation.StringRes
4+
import com.whyranoid.presentation.R
5+
6+
enum class CommunityCategory(@StringRes val stringId: Int) {
7+
BOARD(R.string.text_board),
8+
MY_GROUP(R.string.text_my_group),
9+
MY_POST(R.string.text_my_post)
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.whyranoid.presentation.community
2+
3+
import androidx.fragment.app.Fragment
4+
import androidx.viewpager2.adapter.FragmentStateAdapter
5+
6+
class CommunityCategoryAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
7+
8+
override fun getItemCount(): Int = CATEGORY_COUNT
9+
10+
override fun createFragment(position: Int): Fragment {
11+
return CommunityItemFragment.newInstance(CommunityCategory.values()[position])
12+
}
13+
14+
companion object {
15+
const val CATEGORY_COUNT = 3
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.whyranoid.presentation.community
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import androidx.viewpager2.adapter.FragmentStateAdapter
6+
import com.google.android.material.tabs.TabLayoutMediator
7+
import com.whyranoid.presentation.R
8+
import com.whyranoid.presentation.base.BaseFragment
9+
import com.whyranoid.presentation.databinding.FragmentCommunityBinding
10+
11+
internal class CommunityFragment :
12+
BaseFragment<FragmentCommunityBinding>(R.layout.fragment_community) {
13+
14+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
15+
super.onViewCreated(view, savedInstanceState)
16+
17+
val adapter = CommunityCategoryAdapter(this)
18+
setTabLayout(adapter)
19+
}
20+
21+
private fun setTabLayout(adapter: FragmentStateAdapter) {
22+
binding.viewPager.adapter = adapter
23+
TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
24+
tab.text = getString(CommunityCategory.values()[position].stringId)
25+
}.attach()
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.whyranoid.presentation.community
2+
3+
import android.os.Build
4+
import android.os.Bundle
5+
import android.view.View
6+
import androidx.fragment.app.viewModels
7+
import com.whyranoid.presentation.R
8+
import com.whyranoid.presentation.base.BaseFragment
9+
import com.whyranoid.presentation.databinding.FragmentCommunityItemBinding
10+
import dagger.hilt.android.AndroidEntryPoint
11+
12+
@AndroidEntryPoint
13+
internal class CommunityItemFragment :
14+
BaseFragment<FragmentCommunityItemBinding>(R.layout.fragment_community_item) {
15+
16+
private val viewModel: CommunityViewModel by viewModels()
17+
18+
private val category by lazy {
19+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
20+
arguments?.getSerializable(COMMUNITY_CATEGORY_KEY, CommunityCategory::class.java)
21+
?: CommunityCategory.BOARD
22+
} else {
23+
arguments?.getSerializable(COMMUNITY_CATEGORY_KEY) as? CommunityCategory
24+
?: CommunityCategory.BOARD
25+
}
26+
}
27+
28+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
29+
super.onViewCreated(view, savedInstanceState)
30+
}
31+
32+
companion object {
33+
private const val COMMUNITY_CATEGORY_KEY = "communityCategoryKey"
34+
35+
fun newInstance(communityCategory: CommunityCategory): CommunityItemFragment {
36+
val fragment = CommunityItemFragment()
37+
fragment.arguments = Bundle().apply {
38+
putSerializable(COMMUNITY_CATEGORY_KEY, communityCategory)
39+
}
40+
return fragment
41+
}
42+
}
43+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.whyranoid.presentation.community
2+
3+
import androidx.lifecycle.ViewModel
4+
import dagger.hilt.android.lifecycle.HiltViewModel
5+
import javax.inject.Inject
6+
7+
@HiltViewModel
8+
class CommunityViewModel @Inject constructor() : ViewModel()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools">
5+
6+
<data>
7+
8+
</data>
9+
10+
<androidx.constraintlayout.widget.ConstraintLayout
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"
13+
tools:context=".community.CommunityFragment">
14+
15+
<com.google.android.material.appbar.AppBarLayout
16+
android:id="@+id/app_bar"
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
app:layout_constraintTop_toTopOf="parent">
20+
21+
<com.google.android.material.appbar.MaterialToolbar
22+
android:id="@+id/topAppBar"
23+
style="@style/Widget.MaterialComponents.Toolbar.Primary"
24+
android:layout_width="match_parent"
25+
android:layout_height="?attr/actionBarSize"
26+
app:title="@string/text_community" />
27+
28+
</com.google.android.material.appbar.AppBarLayout>
29+
30+
<com.google.android.material.tabs.TabLayout
31+
android:id="@+id/tab_layout"
32+
android:layout_width="match_parent"
33+
android:layout_height="wrap_content"
34+
app:layout_constraintEnd_toEndOf="parent"
35+
app:layout_constraintStart_toStartOf="parent"
36+
app:layout_constraintTop_toBottomOf="@id/app_bar">
37+
38+
</com.google.android.material.tabs.TabLayout>
39+
40+
<androidx.viewpager2.widget.ViewPager2
41+
android:id="@+id/view_pager"
42+
android:layout_width="0dp"
43+
android:layout_height="0dp"
44+
app:layout_constraintBottom_toBottomOf="parent"
45+
app:layout_constraintEnd_toEndOf="parent"
46+
app:layout_constraintStart_toStartOf="parent"
47+
app:layout_constraintTop_toBottomOf="@id/tab_layout" />
48+
49+
</androidx.constraintlayout.widget.ConstraintLayout>
50+
</layout>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
xmlns:app="http://schemas.android.com/apk/res-auto">
5+
6+
<data>
7+
8+
</data>
9+
10+
<androidx.constraintlayout.widget.ConstraintLayout
11+
android:layout_width="match_parent"
12+
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+
24+
</androidx.constraintlayout.widget.ConstraintLayout>
25+
</layout>

presentation/src/main/res/values/strings.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
<string name="runner_start_suffix">함께 달리고 있습니다</string>
1717
<string name="runner_start_button_text">달리기</string>
1818
<string name="running_start_thumbnail_des">모각런 썸네일</string>
19-
19+
20+
<!-- 커뮤니티 탭 화면 -->
21+
<string name="text_board">게시판</string>
22+
<string name="text_my_group">내 그룹</string>
23+
<string name="text_my_post">내가 쓴 글</string>
24+
<string name="text_community">커뮤니티</string>
25+
2026
</resources>

0 commit comments

Comments
 (0)