Skip to content

Commit

Permalink
[feat] : 전체 글 조회 카테고리 별 요청 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed Oct 7, 2023
1 parent b9062f3 commit 2dff77e
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ interface HomeService {
@GET("/api/articles/category?search=가게 소식")
fun getPostCategory(
): Call<ResponsePost>

@Headers("Content-Type: application/json;charset=UTF-8")
@GET("/api/articles/category?search=사장님 SOS")
fun getPostCategorySOS(
): Call<ResponsePost>

@Headers("Content-Type: application/json;charset=UTF-8")
@GET("/api/articles/category?search=인기글")
fun getPostCategoryLike(
): Call<ResponsePost>
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class PostAdapter(private val itemPost: ArrayList<PostItem>) :

class PostViewHolder(val binding: ItemHomePostBinding) : RecyclerView.ViewHolder(binding.root) {
fun setItem(title: String, content: String) {
binding.tvTitle.text
binding.tvTitle.text = title
binding.tvContent.text = content
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.pcandriod.kusitms_hackathon_c.data.remote.service.HomeService
import com.pcandriod.kusitms_hackathon_c.databinding.FragmentHomeBinding
import com.pcandriod.kusitms_hackathon_c.presentation.adapter.PostAdapter
import com.pcandriod.kusitms_hackathon_c.presentation.ui.main.MainActivity
import com.pcandriod.kusitms_hackathon_c.presentation.ui.main.write.WriteCustomerFragment
import com.pcandriod.kusitms_hackathon_c.presentation.ui.main.write.WriteOwnerFragment
import retrofit2.Call
import retrofit2.Callback
Expand All @@ -34,8 +35,8 @@ class HomeFragment : Fragment() {
): View? {
binding = FragmentHomeBinding.inflate(inflater)
mainActivity = activity as MainActivity

apiStart()

return binding.root
}

Expand All @@ -44,6 +45,35 @@ class HomeFragment : Fragment() {
initView()
goToWrite()

binding.btnStoreNews.setOnClickListener {
binding.btnStoreNews.setBackgroundResource(R.drawable.bg_button_true)
binding.btnStoreNews.setTextColor(resources.getColor(R.color.white))
binding.btnSos.setBackgroundResource(R.drawable.bg_button_false)
binding.btnSos.setTextColor(resources.getColor(R.color.black))
binding.btnMostLike.setBackgroundResource(R.drawable.bg_button_false)
binding.btnMostLike.setTextColor(resources.getColor(R.color.black))
apiStart()
}

binding.btnSos.setOnClickListener {
binding.btnStoreNews.setBackgroundResource(R.drawable.bg_button_false)
binding.btnStoreNews.setTextColor(resources.getColor(R.color.black))
binding.btnSos.setBackgroundResource(R.drawable.bg_button_true)
binding.btnSos.setTextColor(resources.getColor(R.color.white))
binding.btnMostLike.setBackgroundResource(R.drawable.bg_button_false)
binding.btnMostLike.setTextColor(resources.getColor(R.color.black))
sosAPI()
}

binding.btnMostLike.setOnClickListener {
binding.btnStoreNews.setBackgroundResource(R.drawable.bg_button_false)
binding.btnStoreNews.setTextColor(resources.getColor(R.color.black))
binding.btnSos.setBackgroundResource(R.drawable.bg_button_false)
binding.btnSos.setTextColor(resources.getColor(R.color.black))
binding.btnMostLike.setBackgroundResource(R.drawable.bg_button_true)
binding.btnMostLike.setTextColor(resources.getColor(R.color.white))
likeAPI()
}
}

private fun initView() {
Expand All @@ -55,34 +85,107 @@ class HomeFragment : Fragment() {
binding.fabAddPost.setOnClickListener {
val fragmentManager = requireActivity().supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.fv_main, WriteOwnerFragment())
transaction.replace(R.id.fv_main, WriteCustomerFragment())
transaction.addToBackStack(null)
transaction.commit()
}
}

private fun apiStart() {
itemList.clear()

val api = ApiModule.getInstance().create(HomeService::class.java)
api.getPostCategory()
.enqueue(object: Callback<ResponsePost> {
override fun onResponse(
call: Call<ResponsePost>,
response: Response<ResponsePost>
) {
var postList: List<ResponsePost.Content>?
Log.d("HomeFragment", "API 성공 ${response.body()}")
if (response.isSuccessful) {
// API 요청이 성공하면 데이터를 가져옵니다.
val postList = response.body()?.content?.toList()
postList = response.body()?.content?.toList()

postList?.forEach {
val postItem = PostItem(it.title, it.content)
itemList.add(postItem)
}

// itemList를 사용하여 리사이클러뷰 어댑터를 업데이트합니다.
val postAdapter = PostAdapter(itemList)
binding.rvHomePost.adapter = postAdapter
postAdapter.notifyDataSetChanged()

}
}

override fun onFailure(call: Call<ResponsePost>, t: Throwable) {
Log.e("HomeFragment", "API 실패 ${t}")
}

})
}

private fun sosAPI() {
itemList.clear()

val api = ApiModule.getInstance().create(HomeService::class.java)
api.getPostCategorySOS()
.enqueue(object: Callback<ResponsePost> {
override fun onResponse(
call: Call<ResponsePost>,
response: Response<ResponsePost>
) {
var postList: List<ResponsePost.Content>?
Log.d("HomeFragment", "API 성공 ${response.body()}")
if (response.isSuccessful) {
postList = response.body()?.content?.toList()

postList?.forEach {
val postItem = PostItem(it.title, it.content)
itemList.add(postItem)
}

val postAdapter = PostAdapter(itemList)
binding.rvHomePost.adapter = postAdapter
postAdapter.notifyDataSetChanged()

}
}

override fun onFailure(call: Call<ResponsePost>, t: Throwable) {
Log.e("HomeFragment", "API 실패 ${t}")
}

})
}

private fun likeAPI() {
itemList.clear()

val api = ApiModule.getInstance().create(HomeService::class.java)
api.getPostCategoryLike()
.enqueue(object: Callback<ResponsePost> {
override fun onResponse(
call: Call<ResponsePost>,
response: Response<ResponsePost>
) {
var postList: List<ResponsePost.Content>?
Log.d("HomeFragment", "API 성공 ${response.body()}")
if (response.isSuccessful) {
postList = response.body()?.content?.toList()

postList?.forEach {
val postItem = PostItem(it.title, it.content)
itemList.add(postItem)
}

val postAdapter = PostAdapter(itemList)
binding.rvHomePost.adapter = postAdapter
postAdapter.notifyDataSetChanged()

binding.btnStoreNews.setBackgroundResource(R.drawable.bg_button_false)
binding.btnSos.setBackgroundResource(R.drawable.bg_button_false)
binding.btnMostLike.setBackgroundResource(R.drawable.bg_button_true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
android:paddingHorizontal="16dp"
android:text="가게 소식"
android:textColor="@android:color/white"
android:theme="@style/subtitle3"
android:theme="@style/subtitle4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Expand Down
25 changes: 0 additions & 25 deletions app/src/main/res/layout/item_home_post.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,6 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_title" />

<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="8dp"
android:text="13"
android:theme="@style/subtitle6"
app:layout_constraintBottom_toTopOf="@+id/divider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_content" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="8dp"
android:text="분 전"
android:theme="@style/subtitle6"
app:layout_constraintBottom_toTopOf="@+id/divider"
app:layout_constraintStart_toEndOf="@+id/tv_time"
app:layout_constraintTop_toBottomOf="@+id/tv_content" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 2dff77e

Please sign in to comment.