본문 바로가기

카테고리 없음

심화 팀 프로젝트 - 삭제할 거 백업용

class SearchAdapter : ListAdapter<SearchItem, SearchAdapter.SearchViewHolder>(diffUtil)  {
    companion object {
        val diffUtil = object : DiffUtil.ItemCallback<SearchItem>() {
            override fun areItemsTheSame(oldItem: SearchItem, newItem: SearchItem): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: SearchItem, newItem: SearchItem): Boolean {
                return oldItem == newItem
            }
        }
    }

    fun clearItem() {
        currentList.clear()
        notifyDataSetChanged()
    }

    interface ItemClick{
        fun onClick(view: View, position: Int)
    }

    var itemClick : ItemClick? = null


    inner class SearchViewHolder(binding : SearchItemListBinding) : RecyclerView.ViewHolder(binding.root) {
        val img = binding.ivThumbnail
        val playtime = binding.tvPlay
        val date = binding.tvDate
        val title = binding.tvTitle
        val channel = binding.tvChannel
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder {
        val binding = SearchItemListBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return SearchViewHolder(binding)
    }

    override fun onBindViewHolder(holder: SearchViewHolder, position: Int) {
        bind(holder, position)
    }

    private fun bind(holder: SearchViewHolder, position: Int) {
        with(holder) {
            val item = currentList[position]
            Log.d("SearchAdapter", "#search adapter item = $item")

            itemView.setOnClickListener {
                itemClick?.onClick(it, position)
            }
//            playtime.text = item.details?.duration

            date.text = item.info?.publishedAt
            title.text = item.info?.title
            channel.text = item.info?.channelId
            img.load(item.info?.thumbnails?.medium?.url)
        }
    }

    override fun getItemCount(): Int {
        return currentList.size
    }
}

리스트어댑터를 못 써서 일단 삭제

 

class SearchFragment : Fragment() {
    private var _binding: FragmentSearchBinding? = null
    private val binding get() = _binding!!
    private val viewModel: SearchViewModel by viewModels()
    private lateinit var mContext: Context
    private val mAdapter by lazy { SearchAdapter() }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentSearchBinding.inflate(inflater, container, false)
        return binding.root
    }

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

    private fun initViews() {
        binding.recyclerviewSearch.adapter = mAdapter
        binding.recyclerviewSearch.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        binding.recyclerviewSearch.itemAnimator = null
        Log.d(SFTag, "#search madapter ${mAdapter.currentList}")
        binding.btnSearch.setOnClickListener {
            doSearch()
        }
        clickKeyboardEnter()
    }

    private fun getObservingFromViewModel() {
        viewModel.searchItemList.observe(viewLifecycleOwner) {
            mAdapter.submitList(it)
        }
    }

    private fun doSearch() {
        val query = binding.etSearch.text.toString()
        Log.d(SFTag, "#search query = $query")
        if (query.isNotEmpty()) {
            viewModel.searchItem(query)
        } else {
            Toast.makeText(requireContext(), "텍스트를 입력 하세요", Toast.LENGTH_SHORT).show()
        }
        hideKeyboard()
    }

    private fun clickKeyboardEnter() {
        binding.etSearch.setOnKeyListener { _, keyCode, event ->
            if (keyCode == KEYCODE_ENTER && binding.etSearch.text.isEmpty()) {
                Toast.makeText(requireContext(), "텍스트를 입력 하세요", Toast.LENGTH_SHORT).show()
                return@setOnKeyListener true
            }
            if (keyCode == KEYCODE_ENTER && event.action == ACTION_DOWN){
                val query = binding.etSearch.text.toString()
                Log.d(SFTag, "#enter query = $query")
                viewModel.searchItem(query)
                hideKeyboard()
                binding.recyclerviewSearch.requestFocus()
                return@setOnKeyListener false
            }
            return@setOnKeyListener false
        }
    }



    private fun hideKeyboard() {
        val imm = requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(binding.etSearch.windowToken,0)
    }
}