Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ implement category layout #89

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.pengcook.android.presentation.category

data class Category(
val id: Long,
val title: String,
val imageUrl: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.pengcook.android.presentation.category

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import net.pengcook.android.databinding.ItemCategoryBinding

class CategoryAdapter(
private val categoryEventListener: CategoryEventListener,
) : ListAdapter<Category, CategoryViewHolder>(diffUtil) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ListAdapter 활용 굿입니다 bb

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): CategoryViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ItemCategoryBinding.inflate(layoutInflater, parent, false)
return CategoryViewHolder(binding, categoryEventListener)
}

override fun onBindViewHolder(
holder: CategoryViewHolder,
position: Int,
) {
holder.bind(getItem(position))
}

companion object {
private val diffUtil =
object : DiffUtil.ItemCallback<Category>() {
override fun areItemsTheSame(
oldItem: Category,
newItem: Category,
): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(
oldItem: Category,
newItem: Category,
): Boolean {
return oldItem == newItem
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.pengcook.android.presentation.category

interface CategoryEventListener {
fun onCategorySelect(categoryId: Long)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.pengcook.android.presentation.category

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import net.pengcook.android.R
import net.pengcook.android.databinding.FragmentCategoryBinding
import net.pengcook.android.presentation.core.style.GridSpacingItemDecoration

class CategoryFragment : Fragment() {
private var _binding: FragmentCategoryBinding? = null
private val binding: FragmentCategoryBinding
get() = _binding!!
private val viewModel: CategoryViewModel by viewModels()
private val adapter: CategoryAdapter by lazy { CategoryAdapter(viewModel) }

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
_binding = FragmentCategoryBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(
view: View,
savedInstanceState: Bundle?,
) {
super.onViewCreated(view, savedInstanceState)
binding.adapter = adapter
val categories =
List(17) { id ->
Category(
id.toLong(),
"category ${id + 1}",
"https://www.alphafoodie.com/wp-content/uploads/2021/06/Authentic-Kimchi-1-of-1-2.jpeg",
)
}
adapter.submitList(categories)
val spacingInPixels = resources.getDimensionPixelSize(R.dimen.item_spacing_category)
binding.rvCategory.addItemDecoration(GridSpacingItemDecoration(3, spacingInPixels))
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onDestory?????????? 장난한번 쳐봤습니다ㅋㅋ

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.pengcook.android.presentation.category

import androidx.recyclerview.widget.RecyclerView
import net.pengcook.android.databinding.ItemCategoryBinding

class CategoryViewHolder(
private val binding: ItemCategoryBinding,
categoryEventListener: CategoryEventListener,
) : RecyclerView.ViewHolder(binding.root) {
init {
binding.categoryEventListener = categoryEventListener
}

fun bind(category: Category) {
binding.category = category
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.pengcook.android.presentation.category

import androidx.lifecycle.ViewModel

class CategoryViewModel : ViewModel(), CategoryEventListener {
override fun onCategorySelect(categoryId: Long) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.pengcook.android.presentation.core.style

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

class GridSpacingItemDecoration(
private val spanCount: Int,
private val spacing: Int,
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State,
) {
val position = parent.getChildAdapterPosition(view)
val column = position % spanCount

outRect.left = column * spacing / spanCount
outRect.right = spacing - (column + 1) * spacing / spanCount
if (position >= spanCount) {
outRect.top = spacing
}
outRect.bottom = spacing
}
}
6 changes: 6 additions & 0 deletions android/app/src/main/res/drawable/bg_radius_default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
<corners android:radius="16dp"/>
</shape>
7 changes: 7 additions & 0 deletions android/app/src/main/res/drawable/bg_radius_filled.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/gray_300"/>
<corners android:radius="16dp"/>
<stroke android:color="@color/gray" android:width="1dp" />
</shape>
40 changes: 40 additions & 0 deletions android/app/src/main/res/layout/fragment_category.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/apk/res-auto">

<data>
<variable
name="adapter"
type="net.pengcook.android.presentation.category.CategoryAdapter" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".category.CategoryFragment">

<include
android:id="@+id/abl_category"
layout="@layout/item_appbar_default"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
bind:title="@{@string/category_appbar}"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_category"
android:layout_width="match_parent"
android:layout_height="0dp"
android:adapter="@{adapter}"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:spanCount="3"
tools:listitem="@layout/item_category"
app:layout_constraintTop_toBottomOf="@id/abl_category"
app:layout_constraintBottom_toBottomOf="parent"
tools:itemCount="6"
android:layout_marginHorizontal="12dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
2 changes: 1 addition & 1 deletion android/app/src/main/res/layout/fragment_sign_up.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
android:layout_height="120dp"
android:layout_gravity="center"
android:layout_marginHorizontal="20dp"
android:background="@drawable/bg_radius"
android:background="@drawable/bg_radius_outlined"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
52 changes: 52 additions & 0 deletions android/app/src/main/res/layout/item_category.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

<variable
name="category"
type="net.pengcook.android.presentation.category.Category" />

<variable
name="categoryEventListener"
type="net.pengcook.android.presentation.category.CategoryEventListener" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> categoryEventListener.onCategorySelect(category.id)}">

<ImageView
android:id="@+id/iv_category"
android:layout_width="120dp"
android:layout_height="0dp"
android:background="@drawable/bg_radius_filled"
android:clipToOutline="true"
android:layout_margin="8dp"
android:scaleType="centerCrop"
app:imageUrl="@{category.imageUrl}"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_launcher_background" />

<TextView
android:id="@+id/tv_category"
style="@style/Body.B2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_category"
android:text="@{category.title}"
tools:text="Category"
app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
2 changes: 1 addition & 1 deletion android/app/src/main/res/layout/item_spinner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_radius">
android:background="@drawable/bg_radius_outlined">

<Spinner
android:id="@+id/sp_default"
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/res/layout/item_textfield.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
style="@style/Body.B2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_radius"
android:background="@drawable/bg_radius_outlined"
android:hint="@{hintContent}"
android:importantForAutofill="no"
android:inputType="text"
Expand Down
14 changes: 10 additions & 4 deletions android/app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
android:id="@+id/onboardingFragment"
android:name="net.pengcook.android.presentation.onboarding.OnboardingFragment"
android:label="fragment_onboarding"
tools:layout="@layout/fragment_onboarding" >
tools:layout="@layout/fragment_onboarding">
<action
android:id="@+id/action_onboardingFragment_to_signUpFragment"
app:destination="@id/signUpFragment" />
Expand All @@ -31,17 +31,23 @@
android:id="@+id/signUpFragment"
android:name="net.pengcook.android.presentation.signup.SignUpFragment"
android:label="fragment_sign_up"
tools:layout="@layout/fragment_sign_up" >
tools:layout="@layout/fragment_sign_up">
<action
android:id="@+id/action_signUpFragment_to_homeFragment"
app:destination="@id/homeFragment"
app:popUpTo="@id/onboardingFragment"
app:popUpToInclusive="true"/>
app:popUpToInclusive="true" />
<action
android:id="@+id/action_signUpFragment_to_onboardingFragment"
app:destination="@id/onboardingFragment"
app:popUpTo="@id/onboardingFragment"
app:popUpToInclusive="false"/>
app:popUpToInclusive="false" />
</fragment>

<fragment
android:id="@+id/categoryFragment"
android:name="net.pengcook.android.presentation.category.CategoryFragment"
android:label="fragment_category"
tools:layout="@layout/item_category" />

</navigation>
2 changes: 1 addition & 1 deletion android/app/src/main/res/values/buttons.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<item name="android:textColor">@color/black_100</item>
<item name="android:fontFamily">@font/notosans_bold</item>
<item name="android:textSize">18sp</item>
<item name="android:background">@drawable/bg_radius</item>
<item name="android:background">@drawable/bg_radius_outlined</item>
<item name="android:gravity">center</item>
</style>
</resources>
3 changes: 2 additions & 1 deletion android/app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_margin">16dp</dimen>
</resources>
<dimen name="item_spacing_category">8dp</dimen>
</resources>
2 changes: 2 additions & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@
<string name="signup_country_title">Country</string>
<string name="signup_button_next">Next</string>

<string name="category_appbar">Category</string>

</resources>
Loading