From da091ff09de7a719aa6ed5e66ec2351e91e6fc67 Mon Sep 17 00:00:00 2001 From: Sohyun Date: Thu, 25 May 2023 13:58:00 +0900 Subject: [PATCH] [FEAT] #14-connect full job list api --- .../com/sopt/carrot/data/home/FullListDto.kt | 34 +++++++++++++++ .../sopt/carrot/data/home/FullListService.kt | 14 ++++++ .../presentation/home/FullJobAdapter.kt | 27 +++++++----- .../presentation/home/FullJobViewModel.kt | 43 ++++++++++--------- .../carrot/presentation/home/HomeActivity.kt | 20 +++++---- 5 files changed, 98 insertions(+), 40 deletions(-) create mode 100644 app/src/main/java/com/sopt/carrot/data/home/FullListDto.kt create mode 100644 app/src/main/java/com/sopt/carrot/data/home/FullListService.kt diff --git a/app/src/main/java/com/sopt/carrot/data/home/FullListDto.kt b/app/src/main/java/com/sopt/carrot/data/home/FullListDto.kt new file mode 100644 index 0000000..55bd215 --- /dev/null +++ b/app/src/main/java/com/sopt/carrot/data/home/FullListDto.kt @@ -0,0 +1,34 @@ +package com.sopt.carrot.data.home + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + + +@Serializable +data class ResponseFullListDto( + @SerialName("status") + val status: Integer, + @SerialName("success") + val success: Boolean, + @SerialName("message") + val message: String, + @SerialName("status") + val data: Detail +) { + @Serializable + data class Detail( + @SerialName("posts") + val posts: List + ) { + @Serializable + data class Post( + @SerialName("title") + val title: String, + @SerialName("image") + val image: String, + @SerialName("hourlyWage") + val hourlyWage: Int + ) + + } +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/carrot/data/home/FullListService.kt b/app/src/main/java/com/sopt/carrot/data/home/FullListService.kt new file mode 100644 index 0000000..35a65c1 --- /dev/null +++ b/app/src/main/java/com/sopt/carrot/data/home/FullListService.kt @@ -0,0 +1,14 @@ +package com.sopt.carrot.data.home + +import retrofit2.Call +import retrofit2.http.GET +import retrofit2.http.Header +import retrofit2.http.Query + +interface FullListService { + @GET("/posts/list") + fun getFullJobList( + @Header("Authorization") Authorization: Int, + @Query("size") size: Long + ): Call +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/carrot/presentation/home/FullJobAdapter.kt b/app/src/main/java/com/sopt/carrot/presentation/home/FullJobAdapter.kt index 97a15bf..5fa8ae1 100644 --- a/app/src/main/java/com/sopt/carrot/presentation/home/FullJobAdapter.kt +++ b/app/src/main/java/com/sopt/carrot/presentation/home/FullJobAdapter.kt @@ -5,19 +5,22 @@ import android.view.ViewGroup import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView +import com.bumptech.glide.Glide +import com.sopt.carrot.data.home.ResponseFullListDto import com.sopt.carrot.databinding.ItemHomeFullJobBinding class FullJobAdapter() : - ListAdapter(diffUtil) { + ListAdapter(diffUtil) { class JobAdapterViewHolder(private val binding: ItemHomeFullJobBinding) : RecyclerView.ViewHolder(binding.root) { - fun onBind(data: RecommendedJob) { + fun onBind(data: ResponseFullListDto.Detail.Post) { with(binding) { - ivItemHomeFullJobImg.setImageDrawable(root.context.getDrawable(data.image)) + Glide.with(root).load(data.image).into(ivItemHomeFullJobImg) tvItemHomeFullJobTitle.text = data.title - tvItemHomeFullJobTitle.text = data.salary + tvItemHomeFullJobTitle.text = data.title + tvItemHomeFullJobSalary.text = "시급 " + data.hourlyWage.toString() + "만원" } } @@ -26,17 +29,17 @@ class FullJobAdapter() : companion object { - val diffUtil = object : DiffUtil.ItemCallback() { + val diffUtil = object : DiffUtil.ItemCallback() { override fun areItemsTheSame( - oldItem: RecommendedJob, - newItem: RecommendedJob + oldItem: ResponseFullListDto.Detail.Post, + newItem: ResponseFullListDto.Detail.Post ): Boolean { - return oldItem.id == newItem.id + return oldItem.title == newItem.title } override fun areContentsTheSame( - oldItem: RecommendedJob, - newItem: RecommendedJob + oldItem: ResponseFullListDto.Detail.Post, + newItem: ResponseFullListDto.Detail.Post ): Boolean { return oldItem == newItem } @@ -44,6 +47,8 @@ class FullJobAdapter() : } } + override fun getItemCount(): Int = 3 + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JobAdapterViewHolder { val binding = ItemHomeFullJobBinding.inflate( LayoutInflater.from(parent.context), @@ -54,6 +59,6 @@ class FullJobAdapter() : } override fun onBindViewHolder(holder: JobAdapterViewHolder, position: Int) { - holder.onBind(currentList[position]) + holder.onBind(getItem(position) as ResponseFullListDto.Detail.Post) } } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/carrot/presentation/home/FullJobViewModel.kt b/app/src/main/java/com/sopt/carrot/presentation/home/FullJobViewModel.kt index a1606f9..cabd606 100644 --- a/app/src/main/java/com/sopt/carrot/presentation/home/FullJobViewModel.kt +++ b/app/src/main/java/com/sopt/carrot/presentation/home/FullJobViewModel.kt @@ -1,29 +1,32 @@ package com.sopt.carrot.presentation.home +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel -import com.sopt.carrot.R +import androidx.recyclerview.widget.RecyclerView +import com.sopt.carrot.data.ApiPool +import com.sopt.carrot.data.home.ResponseFullListDto +import com.sopt.carrot.data.home.ResponseRecommendDto +import com.sopt.carrot.util.enqueueUtil class FullJobViewModel : ViewModel() { - val mockListLists = listOf( - RecommendedJob( - id = 1, - image = R.drawable.img_test_1, - title = "당근 마켓", - salary = "시급 10,000원" - ), - RecommendedJob( - id = 2, - image = R.drawable.img_test_2, - title = "당근 알바", - salary = "시급 20,000원" + private val _fullJobResponse: MutableLiveData = MutableLiveData() + val fullJobResponse: LiveData = _fullJobResponse - ), - RecommendedJob( - id = 3, - image = R.drawable.img_test_1, - title = "당근 마켓", - salary = "시급 10,000원" + fun getFullJob(size: Long, recyclerView: RecyclerView, message: (String) -> Unit) { + ApiPool.fullListService.getFullJobList(1, size).enqueueUtil( + onSuccess = { + _fullJobResponse.value = it + recyclerView.adapter = FullJobAdapter().apply { submitList(it.data.posts) } + message.invoke(it.message) + + }, + onError = { + message.invoke("error:${it}") + } ) - ) + + + } } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/carrot/presentation/home/HomeActivity.kt b/app/src/main/java/com/sopt/carrot/presentation/home/HomeActivity.kt index 1557f36..c0de6b7 100644 --- a/app/src/main/java/com/sopt/carrot/presentation/home/HomeActivity.kt +++ b/app/src/main/java/com/sopt/carrot/presentation/home/HomeActivity.kt @@ -18,24 +18,26 @@ class HomeActivity : AppCompatActivity() { super.onCreate(savedInstanceState) binding = ActivityHomeBinding.inflate(layoutInflater) setContentView(binding.root) - setAdapter() - observe() + observeRecommend() + observeFull() } - private fun setAdapter() { - adapterList = FullJobAdapter() - binding.rvHomeList.adapter = adapterList - adapterList.submitList(viewModelList.mockListLists) - } - - private fun observe() { + private fun observeRecommend() { viewModelRecommended.getRecommendedJob( 4L, binding.rvHomeCard, message = { str -> toast(str) }) } + private fun observeFull() { + viewModelList.getFullJob( + 4L, + binding.rvHomeList, message = { str -> toast(str) } + ) + + } + } \ No newline at end of file