diff --git a/app/build.gradle b/app/build.gradle index a13fc11..9476363 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/java/com/sopt/carrot/data/ApiPool.kt b/app/src/main/java/com/sopt/carrot/data/ApiPool.kt index 7a67963..a9918db 100644 --- a/app/src/main/java/com/sopt/carrot/data/ApiPool.kt +++ b/app/src/main/java/com/sopt/carrot/data/ApiPool.kt @@ -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 @@ -10,6 +11,7 @@ object ApiPool { // TODO : 구성한 Api Service interface 들을 retrofit 을 통해 create 해주세요 // ex // val loginService = RetrofitPool.toApiServer.create() + val recommendService = RetrofitPool.toApiServer.create(RecommendService::class.java) } object RetrofitPool { diff --git a/app/src/main/java/com/sopt/carrot/data/home/RecommendDto.kt b/app/src/main/java/com/sopt/carrot/data/home/RecommendDto.kt new file mode 100644 index 0000000..c2202e6 --- /dev/null +++ b/app/src/main/java/com/sopt/carrot/data/home/RecommendDto.kt @@ -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, + ) { + @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, + ) + + } + +} diff --git a/app/src/main/java/com/sopt/carrot/data/home/RecommendService.kt b/app/src/main/java/com/sopt/carrot/data/home/RecommendService.kt new file mode 100644 index 0000000..4e3c5ae --- /dev/null +++ b/app/src/main/java/com/sopt/carrot/data/home/RecommendService.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 RecommendService { + @GET("/posts/recommend") + fun getRecommendJobList( + @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/HomeActivity.kt b/app/src/main/java/com/sopt/carrot/presentation/home/HomeActivity.kt index 17bfb8c..1557f36 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 @@ -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() + private val viewModelRecommended by viewModels() private val viewModelList by viewModels() @@ -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) }) + } + + } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedJob.kt b/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedJob.kt index dc8ec99..70eb439 100644 --- a/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedJob.kt +++ b/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedJob.kt @@ -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 ) diff --git a/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedJobAdapter.kt b/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedJobAdapter.kt index 5fbe3fd..e09f891 100644 --- a/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedJobAdapter.kt +++ b/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedJobAdapter.kt @@ -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(diffUtil) { + ListAdapter( + 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() + "만원" } } @@ -27,17 +31,17 @@ class RecommendedJobAdapter() : companion object { - val diffUtil = object : DiffUtil.ItemCallback() { + val diffUtil = object : DiffUtil.ItemCallback() { 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 } @@ -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) } diff --git a/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedViewModel.kt b/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedViewModel.kt index 3db908b..4973bd3 100644 --- a/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedViewModel.kt +++ b/app/src/main/java/com/sopt/carrot/presentation/home/RecommendedViewModel.kt @@ -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 = MutableLiveData() + val recommendedJobResponse: LiveData = _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}") + } ) - ) - - - + } } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/carrot/util/ContextExtension.kt b/app/src/main/java/com/sopt/carrot/util/ContextExtension.kt index 623112b..0a4ae4c 100644 --- a/app/src/main/java/com/sopt/carrot/util/ContextExtension.kt +++ b/app/src/main/java/com/sopt/carrot/util/ContextExtension.kt @@ -16,7 +16,7 @@ fun Call.enqueueUtil( if (response.isSuccessful) { onSuccess.invoke(response.body() ?: return) } else { - Log.d("Hello", "error") + Log.d("Hello", "error:${response.message()}") onError?.invoke(response.code()) } }