Skip to content

Instantly share code, notes, and snippets.

View babedev's full-sized avatar

Christopher Ng babedev

  • BabeDev
  • Bangkok, Thailand
View GitHub Profile
@babedev
babedev / sequence_operation.kt
Created March 25, 2018 09:49
Sequence operation
println("List ====> ")
listOf(1, 2, 3)
.filter { println("Filter: $it"); it % 2 == 0 }
.map { println("Map: $it"); it + 1 }
.forEach { println("Each: $it") }
println("Sequence ====> ")
sequenceOf(1, 2, 3)
.filter { println("Filter: $it"); it % 2 == 0 }
.map { println("Map: $it"); it + 1 }
@babedev
babedev / list_sequence_no_terminal.kt
Last active March 25, 2018 08:48
List vs Sequence no terminal operation
println("List ====> ")
listOf(1, 2, 3)
.filter {
println("Filter: $it")
it % 2 == 0
}
.map {
println("Map: $it")
it
}
@babedev
babedev / bnv-set-font.kt
Last active March 22, 2018 14:23
Set font by resource ID got BottomNavigationView
fun BottomNavigationView.setFont(@FontRes rId: Int) {
val menuView = this.getChildAt(0) as BottomNavigationMenuView
val menuCount = menuView.childCount
(0 until menuCount).forEach {
val itemView = menuView.getChildAt(it) as BottomNavigationItemView
val titleView = itemView.getChildAt(1) as BaselineLayout
val smallLabel = titleView.getChildAt(0) as TextView
val largeLabel = titleView.getChildAt(1) as TextView
@babedev
babedev / android-rotate-bitmap.kt
Created February 14, 2018 02:14
Rotate bitmap on Android
fun rotateBitmap(bitmap: Bitmap, orientation: Int): Bitmap? {
val matrix = Matrix()
when (orientation) {
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.setScale(-1F, 1F)
ExifInterface.ORIENTATION_ROTATE_180 -> matrix.setRotate(180F)
ExifInterface.ORIENTATION_FLIP_VERTICAL -> {
matrix.setRotate(180F)
matrix.postScale(-1F, 1F)
}
@babedev
babedev / bnv-disable-shift.kt
Created January 30, 2018 08:37
Disable shift mode for BottomNavigationView
@SuppressLint("RestrictedApi")
fun BottomNavigationView.disableShiftMode() {
val menuView = this.getChildAt(0) as BottomNavigationMenuView
try {
val shiftingMode = menuView.javaClass.getDeclaredField("mShiftingMode")
shiftingMode.isAccessible = true
shiftingMode.setBoolean(menuView, false)
shiftingMode.isAccessible = false
public final class BuildConfig {
public static final boolean DEBUG = false;
public static final String APPLICATION_ID = "my.app";
public static final String BUILD_TYPE = "local";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "0.1.0";
// Fields from build type: local
public static final String API_URL = "http://192.165.36.44:5000/api/v1";
}
@babedev
babedev / use-get-ip.groovy
Last active December 23, 2017 14:47
Use getIPAddress()
buildTypes {
local {
buildConfigField("String", "API_URL", "\"http://${getIPAddress()}:5000/api/v1\"")
}
debug {
buildConfigField("String", "API_URL", "\"http://my.staging.server/api/v1\"")
}
release {
@babedev
babedev / get-ip.groovy
Last active December 23, 2017 14:41
Get IP address
static def getIPAddress() {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces()
while (interfaces.hasMoreElements()) {
Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses()
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement()
if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) {
GsonBuilder().registerTypeAdapterFactory(NullStringToEmptyAdapterFactory())
.create()
.run {
return fromJson(this@toObject, T::class.java)
}
class NullStringToEmptyAdapterFactory : TypeAdapterFactory {
override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
if (type.rawType != String::class.java) {
headers = /path/to/header/hello.h