Skip to content

Instantly share code, notes, and snippets.

@monzee
Created August 19, 2017 13:36
Show Gist options
  • Save monzee/47eae76435ee20fdd16a84fc137aeafa to your computer and use it in GitHub Desktop.
Save monzee/47eae76435ee20fdd16a84fc137aeafa to your computer and use it in GitHub Desktop.
package em.zed.literallynothing
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.channels.BroadcastChannel
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.SendChannel
typealias Action<T> = T.() -> Unit
fun fetchItems(view: SendChannel<Action<MainActivity>>) = launch(CommonPool) {
view.send { spin(true) }
delay(10_000)
val items = listOf("foo", "bar", "baz", "quux")
view.send {
spin(false)
(rv.adapter as MyAdapter).let {
it.items = items
it.notifyDataSetChanged()
}
}
}
class Scope {
val actions = BroadcastChannel<Action<MainActivity>>(Channel.CONFLATED)
init {
fetchItems(actions)
}
}
class MainActivity : AppCompatActivity() {
internal lateinit var rv: RecyclerView
internal lateinit var spinner: ProgressBar
private lateinit var our: Scope
private lateinit var loop: Job
override fun onRetainCustomNonConfigurationInstance(): Any = our
override fun onCreate(savedInstanceState: Bundle?) {
our = lastCustomNonConfigurationInstance as? Scope ?: Scope()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rv = findViewById(R.id.the_list) as RecyclerView
rv.adapter = MyAdapter()
rv.layoutManager = LinearLayoutManager(this)
spinner = findViewById(R.id.the_spinner) as ProgressBar
}
override fun onResume() {
super.onResume()
loop = launch(UI) {
our.actions.open().use {
for (action in it) action()
}
}
}
override fun onPause() {
super.onPause()
loop.cancel(CancellationException("activity paused."))
}
internal fun spin(busy: Boolean) {
if (busy) {
rv.visibility = View.GONE
spinner.visibility = View.VISIBLE
} else {
rv.visibility = View.VISIBLE
spinner.visibility = View.GONE
}
}
}
class MyAdapter : RecyclerView.Adapter<MyViewHolder>() {
internal var items: List<String> = listOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = LayoutInflater
.from(parent.context)
.inflate(android.R.layout.simple_selectable_list_item, parent, false)
.let(::MyViewHolder)
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.label.text = items[position]
}
override fun getItemCount() = items.size
}
class MyViewHolder(root: View) : RecyclerView.ViewHolder(root) {
internal val label = root as TextView
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment