<commit_before>package com.sedsoftware.yaptalker.presentation.feature.gallery.adapter

import android.support.v7.widget.RecyclerView
import android.support.v7.widget.RecyclerView.ViewHolder
import android.view.ViewGroup
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import com.sedsoftware.yaptalker.R
import com.sedsoftware.yaptalker.presentation.extensions.inflate
import com.sedsoftware.yaptalker.presentation.extensions.loadFromUrl
import com.sedsoftware.yaptalker.presentation.model.base.SinglePostGalleryImageModel
import kotlinx.android.synthetic.main.activity_topic_gallery_item.view.*
import kotlinx.android.synthetic.main.activity_topic_gallery_item_load_more.view.*
import java.util.ArrayList
import javax.inject.Inject

class TopicGalleryAdapter @Inject constructor(
  private val loadMoreCallback: TopicGalleryLoadMoreClickListener
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

  var items: MutableList<SinglePostGalleryImageModel> = ArrayList()
  var isLastPageVisible = false

  init {
    setHasStableIds(true)
  }

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = ImageViewHolder(parent)

  override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder as ImageViewHolder
    holder.bind(items[position])
  }

  override fun getItemId(position: Int) = position.toLong()

  override fun getItemCount(): Int = items.size

  fun addList(images: List<SinglePostGalleryImageModel>) {

    if (images.isNotEmpty()) {
      clearLoadingIndicators()
      items.addAll(images)
    } else {
      showLastLoadingIndicator()
    }

    notifyDataSetChanged()
  }

  private fun clearLoadingIndicators() {
    items = items
      .map { item -> SinglePostGalleryImageModel(url = item.url, showLoadMore = false) }
      .toMutableList()
  }

  private fun showLastLoadingIndicator() {
    clearLoadingIndicators()
    if (items.isNotEmpty()) {
      val newLast = SinglePostGalleryImageModel(url = items.last().url, showLoadMore = true)
      items.removeAt(items.lastIndex)
      items.add(newLast)
    }
  }

  inner class ImageViewHolder(parent: ViewGroup) :
    RecyclerView.ViewHolder(parent.inflate(R.layout.activity_topic_gallery_item)) {

    fun bind(image: SinglePostGalleryImageModel) {
      with(itemView) {
        load_more_button.isInvisible = true
        gallery_image.loadFromUrl(image.url)

        if (image.showLoadMore && !isLastPageVisible) {
          load_more_button.isVisible = true
          load_more_label.isVisible = true
          load_more_button.setOnClickListener {
            loadMoreCallback.onLoadMoreClicked()
            load_more_label.isInvisible = true
            load_more_progress.isVisible = true
          }
        }
      }
    }
  }
}
<commit_msg>Load more button tweaks<commit_after>package com.sedsoftware.yaptalker.presentation.feature.gallery.adapter

import android.support.v7.widget.RecyclerView
import android.support.v7.widget.RecyclerView.ViewHolder
import android.view.ViewGroup
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import com.sedsoftware.yaptalker.R
import com.sedsoftware.yaptalker.presentation.extensions.inflate
import com.sedsoftware.yaptalker.presentation.extensions.loadFromUrl
import com.sedsoftware.yaptalker.presentation.model.base.SinglePostGalleryImageModel
import kotlinx.android.synthetic.main.activity_topic_gallery_item.view.*
import kotlinx.android.synthetic.main.activity_topic_gallery_item_load_more.view.*
import java.util.ArrayList
import javax.inject.Inject

class TopicGalleryAdapter @Inject constructor(
  private val loadMoreCallback: TopicGalleryLoadMoreClickListener
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

  var items: MutableList<SinglePostGalleryImageModel> = ArrayList()
  var isLastPageVisible = false

  init {
    setHasStableIds(true)
  }

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = ImageViewHolder(parent)

  override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder as ImageViewHolder
    holder.bind(items[position])
  }

  override fun getItemId(position: Int) = position.toLong()

  override fun getItemCount(): Int = items.size

  fun addList(images: List<SinglePostGalleryImageModel>) {
    if (images.isNotEmpty()) {
      clearLoadingIndicators()
      items.addAll(images)
    }

    showLastLoadingIndicator()
    notifyDataSetChanged()
  }

  private fun clearLoadingIndicators() {
    items = items
      .map { item -> SinglePostGalleryImageModel(url = item.url, showLoadMore = false) }
      .toMutableList()
  }

  private fun showLastLoadingIndicator() {
    clearLoadingIndicators()
    if (items.isNotEmpty()) {
      val newLast = SinglePostGalleryImageModel(url = items.last().url, showLoadMore = true)
      items.removeAt(items.lastIndex)
      items.add(newLast)
    }
  }

  inner class ImageViewHolder(parent: ViewGroup) :
    RecyclerView.ViewHolder(parent.inflate(R.layout.activity_topic_gallery_item)) {

    fun bind(image: SinglePostGalleryImageModel) {
      with(itemView) {
        load_more_button.isInvisible = true
        load_more_label.isInvisible = true
        load_more_progress.isInvisible = true
        gallery_image.loadFromUrl(image.url)

        if (image.showLoadMore && !isLastPageVisible) {
          load_more_button.isVisible = true
          load_more_label.isVisible = true
          load_more_progress.isInvisible = true
          load_more_button.setOnClickListener {
            loadMoreCallback.onLoadMoreClicked()
            load_more_button.isVisible = true
            load_more_label.isInvisible = true
            load_more_progress.isVisible = true
          }
        }
      }
    }
  }
}

{'fraction_non_alphanumeric': 0.08615598401945458, 'fraction_numerical': 0.000694806322737537, 'mean_word_length': 4.006956521739131, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2282873', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1698, 'n_words': 280}