Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View brainail's full-sized avatar
🦊
What Does The Fox Say?

Yegor brainail

🦊
What Does The Fox Say?
View GitHub Profile
@brainail
brainail / AppCompatBottomAppBar.kt
Last active March 12, 2019 15:53
Show/hide com.google.android.material.bottomappbar.BottomAppBar programmatically
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
class WorkSpec {
// ...
@ColumnInfo(name = "worker_class_name")
var workerClassName: String
// ...
}
fun doWork(): WorkerResult {
@Query("UPDATE workspec SET run_attempt_count=run_attempt_count+1 WHERE id=:id")
fun incrementWorkSpecRunAttemptCount(id: String): Int
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
}
fun observeMyWorkPlease() {
WorkManager.getInstance().getStatusById(hardWorkId).observe(lifecycleOwner, workStatus -> {
if (null != workStatus && workStatus.state.isFinished()) {
// let's rock!
}
})
}
fun getSchedulers(): List<Scheduler> {
if (mSchedulers == null) {
mSchedulers = Arrays.asList(
Schedulers.createBestAvailableBackgroundScheduler(mContext),
GreedyScheduler(mContext, this))
}
return mSchedulers
}
internal fun createBestAvailableBackgroundScheduler(context: Context): Scheduler {
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)
}
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 September 17, 2017 12:48
Some Kitsu UI stuff to display it properly
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()
@brainail
brainail / KitsuAPI.kt
Created September 17, 2017 12:44
Kitsu API for Retrofit
object KitsuRestApi {
private val kitsuApi: KitsuSpecApi
init {
val retrofit = Retrofit.Builder()
.baseUrl("https://kitsu.io/api/edge/")
.addConverterFactory(MoshiConverterFactory.create())
.build()
kitsuApi = retrofit.create(KitsuSpecApi::class.java)