Skip to content

Commit

Permalink
[FEAT] #14-connect full job list api
Browse files Browse the repository at this point in the history
  • Loading branch information
sohyun127 committed May 26, 2023
1 parent e341ea7 commit da091ff
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 40 deletions.
34 changes: 34 additions & 0 deletions app/src/main/java/com/sopt/carrot/data/home/FullListDto.kt
Original file line number Diff line number Diff line change
@@ -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<Post>
) {
@Serializable
data class Post(
@SerialName("title")
val title: String,
@SerialName("image")
val image: String,
@SerialName("hourlyWage")
val hourlyWage: Int
)

}
}
14 changes: 14 additions & 0 deletions app/src/main/java/com/sopt/carrot/data/home/FullListService.kt
Original file line number Diff line number Diff line change
@@ -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<ResponseFullListDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecommendedJob, FullJobAdapter.JobAdapterViewHolder>(diffUtil) {
ListAdapter<ResponseFullListDto.Detail.Post, FullJobAdapter.JobAdapterViewHolder>(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() + "만원"
}

}
Expand All @@ -26,24 +29,26 @@ class FullJobAdapter() :


companion object {
val diffUtil = object : DiffUtil.ItemCallback<RecommendedJob>() {
val diffUtil = object : DiffUtil.ItemCallback<ResponseFullListDto.Detail.Post>() {
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
}

}
}

override fun getItemCount(): Int = 3

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JobAdapterViewHolder {
val binding = ItemHomeFullJobBinding.inflate(
LayoutInflater.from(parent.context),
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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<ResponseFullListDto> = MutableLiveData()
val fullJobResponse: LiveData<ResponseFullListDto> = _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}")
}
)
)


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
)

}


}

0 comments on commit da091ff

Please sign in to comment.