Skip to content

Instantly share code, notes, and snippets.

@illuzor illuzor/CategoriesFragment.kt Secret
Created Sep 22, 2018

Embed
What would you like to do?
package com.illuzor.lesson.wallpapers.screens
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import com.illuzor.lesson.wallpapers.R
import com.illuzor.lesson.wallpapers.adapters.CategoriesAdapter
import com.illuzor.lesson.wallpapers.model.ViewModelCategories
import com.illuzor.lesson.wallpapers.model.ViewModelBase.State.*
import kotlinx.android.synthetic.main.fragment_list.*
class CategoriesFragment : AbstractFragment() {
override val layoutId = R.layout.fragment_list
private val adapter = CategoriesAdapter()
private lateinit var model: ViewModelCategories
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
model = ViewModelProviders.of(this).get(ViewModelCategories::class.java)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
contentView = recycler_view
recycler_view.layoutManager = GridLayoutManager(context, 2)
recycler_view.adapter = adapter
checkData()
}
private fun checkData() {
if (view == null) return
when (model.state) {
CREATED -> {
showProgress()
model.setListener { checkData() }
model.load()
}
PROGRESS -> {
showProgress()
model.setListener { checkData() }
}
ERROR -> {
showError(R.string.unable_to_load) {
showProgress()
model.setListener { checkData() }
model.load()
}
}
LOADED -> {
adapter.addItems(model.data.sortedBy { it.name })
showContent()
adapter.setOnclickListener { category ->
val intent = Intent(context, GalleryActivity::class.java)
intent.putExtra("category", category)
startActivity(intent)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.