Skip to content

Commit

Permalink
feat : SearchFragment adapter 연결 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
SeonHwan-Kim committed May 10, 2023
1 parent 4d79397 commit 7efdacc
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.android.go.sopt.presentation.main.search

import android.content.Context
import android.view.LayoutInflater
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 org.android.go.sopt.data.remote.model.ResponseKakaoSearchDto
import org.android.go.sopt.databinding.ItemSearchBinding

class SearchAdapter(context: Context) :
ListAdapter<ResponseKakaoSearchDto.Document, SearchAdapter.SearchViewHolder>(SearchDiffCallback()) {
private val inflater by lazy { LayoutInflater.from(context) }

class SearchViewHolder(private var binding: ItemSearchBinding) :
RecyclerView.ViewHolder(binding.root) {
fun onBind(videoData: ResponseKakaoSearchDto.Document) {
with(binding) {
Glide.with(root)
.load(videoData.thumbnail)
.into(ivSearchItemThumbnail)

tvSearchItemTitle.text = videoData.title
tvSearchItemAuthor.text = videoData.author
}
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder {
val binding = ItemSearchBinding.inflate(inflater, parent, false)
return SearchViewHolder(binding)
}

override fun onBindViewHolder(holder: SearchViewHolder, position: Int) {
holder.onBind(getItem(position))
}
}

class SearchDiffCallback : DiffUtil.ItemCallback<ResponseKakaoSearchDto.Document>() {
override fun areItemsTheSame(
oldItem: ResponseKakaoSearchDto.Document,
newItem: ResponseKakaoSearchDto.Document
): Boolean {
return oldItem.url == newItem.url
}

override fun areContentsTheSame(
oldItem: ResponseKakaoSearchDto.Document,
newItem: ResponseKakaoSearchDto.Document
): Boolean {
return oldItem == newItem
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,83 @@
package org.android.go.sopt.presentation.main.search

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.doAfterTextChanged
import org.android.go.sopt.data.remote.ServicePool
import org.android.go.sopt.data.remote.model.ResponseKakaoSearchDto
import org.android.go.sopt.databinding.FragmentSearchBinding
import org.android.go.sopt.util.showShortToast
import retrofit2.Call
import retrofit2.Response

class SearchFragment : Fragment() {
private var _binding: FragmentSearchBinding? = null
private val binding: FragmentSearchBinding
get() = requireNotNull(_binding) { "앗! binding이 null이다!" }
private val kakaoSearchService = ServicePool.kakaoSearchService

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// fragment에서 binding은 activity랑 다름. 한 번 찾아보기
_binding = FragmentSearchBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

this.onClickSearchBtn()
this.setListEmpty()
}

override fun onDestroy() {
super.onDestroy()
_binding = null
}

private fun onClickSearchBtn() {
binding.ibSearchBtn.setOnClickListener {
kakaoSearchService.kakaoSearchVideo(
query = binding.etSearchSearchVideo.text.toString(),
sort = "accuracy",
page = 1,
size = 30,
).enqueue(object : retrofit2.Callback<ResponseKakaoSearchDto> {
override fun onResponse(
call: Call<ResponseKakaoSearchDto>,
response: Response<ResponseKakaoSearchDto>
) {
if (response.isSuccessful) {
binding.rvSearchList.adapter = SearchAdapter(requireContext()).apply {
submitList(response.body()?.documents)
}
} else {
requireActivity().showShortToast("실패")
}
}

override fun onFailure(call: Call<ResponseKakaoSearchDto>, t: Throwable) {
requireActivity().showShortToast(t.message.toString())
Log.d("------------------------------\n", t.message.toString())
}

})
}
}

private fun setListEmpty() {
binding.etSearchSearchVideo.doAfterTextChanged {
if (it?.isNotBlank() == false) {
binding.rvSearchList.adapter = SearchAdapter(requireContext()).apply {
submitList(null)
}
}
}
}
}

0 comments on commit 7efdacc

Please sign in to comment.