Skip to content

Instantly share code, notes, and snippets.

View oakkub's full-sized avatar
:octocat:
:)

Metas Kerdwat oakkub

:octocat:
:)
  • Bangkok, Thailand
View GitHub Profile
@oakkub
oakkub / CoroutineLifecycleObserver.kt
Created September 9, 2018 17:04 — forked from chrisbanes/CoroutineLifecycleObserver.kt
LifecycleObserver which allows easy cancelling of coroutines
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@oakkub
oakkub / FieldInjected.kt
Last active August 10, 2018 03:58
Set value to private field, in case we want to inject the mock repository or something.
fun FooViewModel.setMockRepository(mockRepository: FooRepository) {
val repository = FooViewModel::class.java.getDeclaredField("fooRepository")
repository.isAccessible = true
repository.set(this, mockRepository)
}
@oakkub
oakkub / FieldInjected.kt
Created August 10, 2018 03:58
Set value to private field
fun FooViewModel.setMockRepository(mockRepository: FooRepository) {
val repository = FooViewModel::class.java.getDeclaredField("fooRepository")
repository.isAccessible = true
repository.set(this, mockRepository)
}
package com.redplanet.tune.ui.decorations
import android.graphics.Rect
import android.support.annotation.Px
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
class GridOffsetItemDecoration(@param:Px private val spacing: Int,
private val shouldIncludeEdge: Boolean) : RecyclerView.ItemDecoration() {
object GoogleApiAvailabilityHelper {
private const val REQUEST_CODE_GOOGLE_AVAILABILITY = 999
fun checkAvailability(activity: Activity): Boolean {
val googleApiAvailability = GoogleApiAvailability.getInstance()
val statusCode = googleApiAvailability.isGooglePlayServicesAvailable(activity)
val isResultSuccess = statusCode == ConnectionResult.SUCCESS
interface BarcodeManager {
fun startDetecting()
fun stopDetecting()
fun release()
}
sealed class ScannedResult {
data class Success(val barcode: String) : ScannedResult()
object PermissionDenied : ScannedResult()
object NotOperational : ScannedResult()
class LocationRequestHelper(private val context: Context,
private val locationRequest: LocationRequest,
lifecycle: Lifecycle,
private val onResult: (LocationResult) -> Unit) : LifecycleObserver {
private val locationClient by lazy { FusedLocationProviderClient(context) }
private val settingsClient by lazy { LocationServices.getSettingsClient(context) }
private val locationSettingRequest by lazy {
LocationSettingsRequest.Builder()
import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
/**
* Created by oakkub on 10/18/2017 AD.
*/
object PreferenceHelper {
@oakkub
oakkub / DialogExtensions.kt
Last active December 27, 2023 04:32
Kotlin extension functions for creating AlertDialog in a DSL way
import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.support.annotation.StringRes
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"