Skip to content

Instantly share code, notes, and snippets.

@vicky7230
Last active July 4, 2018 10:27
Show Gist options
  • Save vicky7230/6bfd183fc17f5c8a0e402270905ad5cf to your computer and use it in GitHub Desktop.
Save vicky7230/6bfd183fc17f5c8a0e402270905ad5cf to your computer and use it in GitHub Desktop.
class TvFragment : BaseFragment(), TvMvpView, TvAdapter.Callback {
@Inject
lateinit var presenter: TvMvpPresenter<TvMvpView>
@Inject
lateinit var gridLayoutManager: GridLayoutManager
@Inject
lateinit var itemOffsetDecoration: ItemOffsetDecoration
@Inject
lateinit var tvAdapter: TvAdapter
var isLoading = false
companion object {
fun newInstance() = TvFragment()
}
override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
}
override fun onCreate(@Nullable savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_tv, container, false)
presenter.onAttach(this)
tvAdapter.setCallback(this)
return view
}
override fun setUp(view: View) {
gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return when (tvAdapter.getItem(position)?.type) {
"LOADING" -> 2
"TV" -> 1
else -> 1
}
}
}
tvtList.layoutManager = gridLayoutManager
tvtList.addItemDecoration(itemOffsetDecoration)
tvtList.adapter = tvAdapter
tvtList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
//super.onScrolled(recyclerView, dx, dy)
val visibleItemCount = gridLayoutManager.childCount
val totalItemCount = gridLayoutManager.itemCount
val pastVisibleItems = gridLayoutManager.findFirstVisibleItemPosition()
if (visibleItemCount + pastVisibleItems >= totalItemCount && !isLoading) {
tvAdapter.addItem(
Result(
type = "LOADING"
)
)
presenter.getTvs()
isLoading = true
}
}
})
presenter.getTvs()
}
override fun updateTvList(results: MutableList<Result>) {
if (tvAdapter.itemCount > 0)
tvAdapter.removeItem()
if (results.size > 0) {
tvAdapter.addItems(results)
isLoading = false
}
}
override fun onTvShowClick(id: Int) {
startActivity(TvDetailsActivity.getStartIntent(activity as Context, id.toString()))
}
override fun onDestroyView() {
presenter.onDetach()
super.onDestroyView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment