From 2dff77e41250d88e2a16da2e559b37fa526f827d Mon Sep 17 00:00:00 2001 From: arinming Date: Sun, 8 Oct 2023 03:11:15 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20:=20=EC=A0=84=EC=B2=B4=20=EA=B8=80=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20?= =?UTF-8?q?=EB=B3=84=20=EC=9A=94=EC=B2=AD=20(#23)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/remote/service/HomeService.kt | 10 ++ .../presentation/adapter/PostAdapter.kt | 3 +- .../presentation/ui/main/home/HomeFragment.kt | 113 +++++++++++++++++- app/src/main/res/layout/fragment_home.xml | 2 +- app/src/main/res/layout/item_home_post.xml | 25 ---- 5 files changed, 121 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/service/HomeService.kt b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/service/HomeService.kt index bd15347..8996be3 100644 --- a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/service/HomeService.kt +++ b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/service/HomeService.kt @@ -10,4 +10,14 @@ interface HomeService { @GET("/api/articles/category?search=가게 소식") fun getPostCategory( ): Call + + @Headers("Content-Type: application/json;charset=UTF-8") + @GET("/api/articles/category?search=사장님 SOS") + fun getPostCategorySOS( + ): Call + + @Headers("Content-Type: application/json;charset=UTF-8") + @GET("/api/articles/category?search=인기글") + fun getPostCategoryLike( + ): Call } \ No newline at end of file diff --git a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/adapter/PostAdapter.kt b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/adapter/PostAdapter.kt index 35aa9be..618eef9 100644 --- a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/adapter/PostAdapter.kt +++ b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/adapter/PostAdapter.kt @@ -35,7 +35,8 @@ class PostAdapter(private val itemPost: ArrayList) : 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 } } } \ No newline at end of file diff --git a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/home/HomeFragment.kt b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/home/HomeFragment.kt index 7c9389d..23d4ccf 100644 --- a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/home/HomeFragment.kt +++ b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/home/HomeFragment.kt @@ -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 @@ -34,8 +35,8 @@ class HomeFragment : Fragment() { ): View? { binding = FragmentHomeBinding.inflate(inflater) mainActivity = activity as MainActivity - apiStart() + return binding.root } @@ -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() { @@ -55,13 +85,15 @@ 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 { @@ -69,20 +101,91 @@ class HomeFragment : Fragment() { call: Call, response: Response ) { + var postList: List? 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, 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 { + override fun onResponse( + call: Call, + response: Response + ) { + var postList: List? + 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, 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 { + override fun onResponse( + call: Call, + response: Response + ) { + var postList: List? + 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) } } diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 59640c1..a3f8137 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -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" /> diff --git a/app/src/main/res/layout/item_home_post.xml b/app/src/main/res/layout/item_home_post.xml index 70de975..c924546 100644 --- a/app/src/main/res/layout/item_home_post.xml +++ b/app/src/main/res/layout/item_home_post.xml @@ -27,31 +27,6 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tv_title" /> - - - -