Skip to content

Instantly share code, notes, and snippets.

class EventLiveData<T> : MutableLiveData<T>() {
private var isRead: AtomicBoolean = AtomicBoolean(false)
/**
* ensure the event is non-null and can only been seen once
*/
fun observeEvent(owner: LifecycleOwner, observer: Observer<T>) {
super.observe(owner, Observer {
if (it != null && isRead.compareAndSet(false, true)) {
fun refreshSavedBillers() {
payBillManager.getAll()
.compose(defaultOptions())
.autoDisposable()
.subscribe({
displaySavedBillers(it)
}) { Timber.d(it) }
}
class StateLiveData<T> : MutableLiveData<T>() {
val state = EventLiveData<State>()
}
class SimpleViewModel : ViewModel() {
var data: LiveData<String> = MutableLiveData()
var loading: LiveData<Boolean> = MutableLiveData()
var error: LiveData<Throwable> = MutableLiveData()
}
@markchristopherng
markchristopherng / Resource.kt
Last active September 10, 2018 22:57
Resource show loading state
/**
* A generic class that holds a value with its loading status.
* @param <T>
</T> */
data class Resource<out T>(val status: Status, val data: T?, val message: String?) {
companion object {
fun <T> success(data: T?): Resource<T> {
return Resource(SUCCESS, data, null)
}
@markchristopherng
markchristopherng / BranchLocationDao.kt
Last active July 4, 2018 04:46
BranchLocationPagingDao
@Dao
interface BranchLocationDao {
@Query("SELECT * from branch_location where postcode like :arg0 OR locality like :arg0 ORDER BY locality ASC")
fun pageByKeyword(arg0: String): DataSource.Factory<Int, BranchLocation>
}
@markchristopherng
markchristopherng / FindPostcodeActivity.kt
Last active July 6, 2018 06:57
FindPostcodeActivity
class FindPostcodeActivity : KAusPostActivity() {
@Inject
lateinit var mgr: BranchLocationManager
lateinit var searchText: EditText
lateinit var listAdapter: BranchLocationPageAdapter
public override fun doOnCreate(savedInstanceState: Bundle?) {
@markchristopherng
markchristopherng / BranchLocationPageAdapter.kt
Last active July 4, 2018 04:47
BranchLocationPageAdapter
class BranchLocationPageAdapter(var context: Context) : PagedListAdapter<BranchLocation, BranchLocationPageAdapter.ViewHolder>(diffCallback), FastScrollRecyclerView.SectionedAdapter {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
bindItem(holder, position)
}
fun getBranchLocation(position: Int) = getItem(position)
fun bindItem(holder: ViewHolder, position: Int) {
val obj = getItem(position)
@markchristopherng
markchristopherng / BranchLocationDaoTest.kt
Last active April 11, 2018 05:17
BranchLocationDaoTest
package au.com.auspost.android.feature.postcode.service
import android.arch.core.executor.testing.InstantTaskExecutorRule
import android.arch.persistence.room.Room
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import au.com.auspost.android.AuspostDatabase
import au.com.auspost.android.feature.postcode.model.BranchLocation
import org.junit.After
import org.junit.Before
package au.com.auspost.android
import android.arch.persistence.room.Database
import android.arch.persistence.room.RoomDatabase
import au.com.auspost.android.feature.postcode.model.BranchLocation
import au.com.auspost.android.feature.postcode.service.BranchLocationDao
@Database(entities = [
BranchLocation::class
], version = 2)