Skip to content

Commit

Permalink
[FEAT] #14-connect recommended job api
Browse files Browse the repository at this point in the history
  • Loading branch information
sohyun127 committed May 23, 2023
1 parent 9f44f01 commit 076b9dd
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 52 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

//glide
implementation 'com.github.bumptech.glide:glide:4.13.2'
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/sopt/carrot/data/ApiPool.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sopt.carrot.data

import com.sopt.carrot.BuildConfig.API_SERVER_URL
import com.sopt.carrot.data.home.RecommendService
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Expand All @@ -10,6 +11,7 @@ object ApiPool {
// TODO : 구성한 Api Service interface 들을 retrofit 을 통해 create 해주세요
// ex
// val loginService = RetrofitPool.toApiServer.create<LoginService>()
val recommendService = RetrofitPool.toApiServer.create(RecommendService::class.java)
}

object RetrofitPool {
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/com/sopt/carrot/data/home/RecommendDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.sopt.carrot.data.home

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable


@Serializable
data class ResponseRecommendDto(
@SerialName("status")
val status: Integer,
@SerialName("success")
val success: Boolean,
@SerialName("message")
val message: String,
@SerialName("data")
val data: Detail
) {
@Serializable
data class Detail(
@SerialName("nickname")
val nickname: String,
@SerialName("posts")
val posts: List<Post>,
) {
@Serializable
data class Post(
@SerialName("userId")
val userId: Int,
@SerialName("postId")
val postId: Int,
@SerialName("title")
val title: String,
@SerialName("image")
val image: String,
@SerialName("monthlyWage")
val monthlyWage: Int,
)

}

}
14 changes: 14 additions & 0 deletions app/src/main/java/com/sopt/carrot/data/home/RecommendService.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 RecommendService {
@GET("/posts/recommend")
fun getRecommendJobList(
@Header("Authorization") Authorization: Int,
@Query("size") size: Long
): Call<ResponseRecommendDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.sopt.carrot.databinding.ActivityHomeBinding
import com.sopt.carrot.util.toast

class HomeActivity : AppCompatActivity() {

private lateinit var binding: ActivityHomeBinding
private lateinit var adapterCard: RecommendedJobAdapter
private lateinit var adapterList: FullJobAdapter
private val viewModelCard by viewModels<RecommendedViewModel>()
private val viewModelRecommended by viewModels<RecommendedViewModel>()
private val viewModelList by viewModels<FullJobViewModel>()


Expand All @@ -19,17 +19,23 @@ class HomeActivity : AppCompatActivity() {
binding = ActivityHomeBinding.inflate(layoutInflater)
setContentView(binding.root)
setAdapter()
observe()

}

private fun setAdapter() {
adapterCard = RecommendedJobAdapter()
binding.rvHomeCard.adapter = adapterCard
adapterCard.submitList(viewModelCard.mockRecommendedJobLists)
adapterList = FullJobAdapter()
binding.rvHomeList.adapter = adapterList
adapterList.submitList(viewModelList.mockListLists)
}


private fun observe() {
viewModelRecommended.getRecommendedJob(
4L,
binding.rvHomeCard,
message = { str -> toast(str) })
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package com.sopt.carrot.presentation.home
import androidx.annotation.DrawableRes

data class RecommendedJob(
val id:Int,
val id: Int,
@DrawableRes val image: Int,
val title: String,
val salary: String
val salary: String,
val detail: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ 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.ResponseRecommendDto
import com.sopt.carrot.databinding.ItemHomeRecommendedJobBinding

class RecommendedJobAdapter() :
ListAdapter<RecommendedJob, RecommendedJobAdapter.CardAdapterViewHolder>(diffUtil) {
ListAdapter<ResponseRecommendDto.Detail.Post, RecommendedJobAdapter.CardAdapterViewHolder>(
diffUtil
) {


class CardAdapterViewHolder(private val binding: ItemHomeRecommendedJobBinding) :
RecyclerView.ViewHolder(binding.root) {

fun onBind(data: RecommendedJob) {
fun onBind(data: ResponseRecommendDto.Detail.Post) {
with(binding) {
ivItemHomeRecommendedJobImg.setImageDrawable(root.context.getDrawable(data.image))
Glide.with(root).load(data.image).into(ivItemHomeRecommendedJobImg)
tvItemHomeRecommendedJobTitle.text = data.title
tvItemHomeRecommendedJobSalary.text = data.salary
tvItemHomeRecommendedJobSalary.text = "월급 " + data.monthlyWage.toString() + "만원"
}

}
Expand All @@ -27,17 +31,17 @@ class RecommendedJobAdapter() :


companion object {
val diffUtil = object : DiffUtil.ItemCallback<RecommendedJob>() {
val diffUtil = object : DiffUtil.ItemCallback<ResponseRecommendDto.Detail.Post>() {
override fun areItemsTheSame(
oldItem: RecommendedJob,
newItem: RecommendedJob
oldItem: ResponseRecommendDto.Detail.Post,
newItem: ResponseRecommendDto.Detail.Post
): Boolean {
return oldItem.id == newItem.id
return oldItem.postId == newItem.postId
}

override fun areContentsTheSame(
oldItem: RecommendedJob,
newItem: RecommendedJob
oldItem: ResponseRecommendDto.Detail.Post,
newItem: ResponseRecommendDto.Detail.Post
): Boolean {
return oldItem == newItem
}
Expand All @@ -55,7 +59,7 @@ class RecommendedJobAdapter() :
}

override fun onBindViewHolder(holder: CardAdapterViewHolder, position: Int) {
holder.onBind(currentList[position])
holder.onBind(getItem(position) as ResponseRecommendDto.Detail.Post)
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +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.ResponseRecommendDto
import com.sopt.carrot.util.enqueueUtil

class RecommendedViewModel : ViewModel() {
val mockRecommendedJobLists = 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원"

),
RecommendedJob(
id = 3,
image = R.drawable.img_test_1,
title = "당근 마켓",
salary = "시급 10,000원"

),
RecommendedJob(
id = 4,
image = R.drawable.img_test_2,
title = "당근 알바",
salary = "시급 20,000원"

private val _recommendedJobResponse: MutableLiveData<ResponseRecommendDto> = MutableLiveData()
val recommendedJobResponse: LiveData<ResponseRecommendDto> = _recommendedJobResponse

fun getRecommendedJob(size: Long, recyclerView: RecyclerView, message: (String) -> Unit) {
ApiPool.recommendService.getRecommendJobList(1, size).enqueueUtil(
onSuccess = {
_recommendedJobResponse.value = it
recyclerView.adapter = RecommendedJobAdapter().apply { submitList(it.data.posts) }
message.invoke(it.message)

},
onError = {
message.invoke("error:${it}")
}
)

)




}


}
2 changes: 1 addition & 1 deletion app/src/main/java/com/sopt/carrot/util/ContextExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun <ResponseType> Call<ResponseType>.enqueueUtil(
if (response.isSuccessful) {
onSuccess.invoke(response.body() ?: return)
} else {
Log.d("Hello", "error")
Log.d("Hello", "error:${response.message()}")
onError?.invoke(response.code())
}
}
Expand Down

0 comments on commit 076b9dd

Please sign in to comment.