Skip to content

Instantly share code, notes, and snippets.

Avatar
🦊
What Does The Fox Say?

Yegor brainail

🦊
What Does The Fox Say?
View GitHub Profile
@brainail
brainail / AppCompatBottomAppBar.kt
Last active Mar 12, 2019
Show/hide com.google.android.material.bottomappbar.BottomAppBar programmatically
View AppCompatBottomAppBar.kt
class AppCompatBottomAppBar : BottomAppBar {
private val compatBehavior by lazyFast { CompatBehavior() }
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) // respect internal style
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override fun getBehavior(): CoordinatorLayout.Behavior<BottomAppBar> {
return compatBehavior
View ParamsStoredViaRoom.kt
class WorkSpec {
// ...
@ColumnInfo(name = "worker_class_name")
var workerClassName: String
// ...
}
fun doWork(): WorkerResult {
View FailWorkNextAttempt.kt
@Query("UPDATE workspec SET run_attempt_count=run_attempt_count+1 WHERE id=:id")
fun incrementWorkSpecRunAttemptCount(id: String): Int
View ObserveWorkManagerInternal.kt
override fun getStatusById(id: UUID): LiveData<WorkStatus> {
val dao = mWorkDatabase.workSpecDao()
val inputLiveData = dao.getWorkStatusPojoLiveDataForIds(listOf(id.toString()))
return LiveDataUtils.dedupedMappedLiveDataFor<List<WorkSpec.WorkStatusPojo>, WorkStatus>(inputLiveData) { input ->
var workStatus: WorkStatus? = null
if (null != input && input.isNotEmpty()) {
workStatus = input [0].toWorkStatus()
}
workStatus
}
View ObserveWorkManager.kt
fun observeMyWorkPlease() {
WorkManager.getInstance().getStatusById(hardWorkId).observe(lifecycleOwner, workStatus -> {
if (null != workStatus && workStatus.state.isFinished()) {
// let's rock!
}
})
}
View WorkManagerSchedulers.kt
fun getSchedulers(): List<Scheduler> {
if (mSchedulers == null) {
mSchedulers = Arrays.asList(
Schedulers.createBestAvailableBackgroundScheduler(mContext),
GreedyScheduler(mContext, this))
}
return mSchedulers
}
internal fun createBestAvailableBackgroundScheduler(context: Context): Scheduler {
View WorkStopper.kt
fun hardWorkerRunner(): WorkRequest {
val hardWork = OneTimeWorkRequest.Builder(HardWorker::class.java).build()
WorkManager.getInstance().enqueue(hardWork)
return hardWork
}
fun hardWorkerStopper() {
val hardWorkId = hardWorkerRunner().id
WorkManager.getInstance().cancelWorkById(hardWorkId)
}
View Work.kt
internal class HardWorker : Worker() {
override fun doWork(): Worker.WorkerResult {
reallyMassiveHeavyLongPLayingWork();
return Worker.WorkerResult.SUCCESS // or FAILURE, or RETRY if you want to run again
}
}
fun hardWorkerRunner() {
val hardWork = OneTimeWorkRequest.Builder(HardWorker::class.java).build()
WorkManager.getInstance().enqueue(hardWork)
@brainail
brainail / KitsuUIParts.kt
Created Sep 17, 2017
Some Kitsu UI stuff to display it properly
View KitsuUIParts.kt
class KitsuViewHolder(parent :ViewGroup) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.kitsu_item, parent, false)) {
var item : KitsuItem? = null
fun bindTo(item : KitsuItem?) {
this.item = item
itemView.itemTypeView.text = item?.type?.capitalize()
?: "Ouhh..."
itemView.itemSubtypeView.text = item?.attributes?.subtype?.capitalize()