Skip to content

Instantly share code, notes, and snippets.

View pokk's full-sized avatar
🌀

Jieyi pokk

🌀
View GitHub Profile
@pokk
pokk / AppComponent.kt
Created November 17, 2017 09:48
app component for my blog.
@Singleton
@Component(modules = arrayOf(AppModule::class,
RepositoryModule::class,
BindingActivityModule::class,
AndroidSupportInjectionModule::class))
interface AppComponent : AndroidInjector<App> {
/** [AndroidInjector] Builder for using on this whole app. */
@Component.Builder
abstract class Builder : AndroidInjector.Builder<App>()
@pokk
pokk / README.kt
Created August 29, 2017 11:15
A easy way to set onMeasure function. Of course if you wanna customize more, you can do more things.
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
// Measure all of children's width & height.
this.measureChildren(widthMeasureSpec, heightMeasureSpec)
// Measure width & height of this view_group's layout(layout_width or layout_height will be `match_parent`
// no matter what we set `wrap_content` or `match_patent` when we're using getDefaultSize).
// We'll reset this method by another way for achieving `wrap_content`.
val mini = minOf(getDefaultSize(suggestedMinimumWidth, widthMeasureSpec), getDefaultSize(suggestedMinimumHeight, heightMeasureSpec))
this.setMeasuredDimension(mini, mini)
}
@pokk
pokk / README.kt
Last active August 21, 2017 07:23
Create an object from generic parameter in the class.
// There is a class with generic parameter.
abstract class A<T> {
// This is KClass version.
val t: T by lazy {
(this@A::class
.supertypes.first() // KType of A class
.arguments.first() // Data type of the generic T
.type!!.classifier as KClass<*>) // The class of the generic T
.primaryConstructor!!.call() as T // An object from the generic T
}
@pokk
pokk / README.md
Created August 18, 2017 06:53
Ruby has magic candy function as Kotlin

[TOC]

Introduction

Ruby and Kotlin have the same magic candy function!

What's kind of magic candy function?

Kotlin

  • apply (also)
@pokk
pokk / Using RxJava2 in `callback`..kt
Last active August 21, 2017 04:07
Introduction Instead of callback function hell, we will use an elegant way `RxJava2` to achieve it. How to do We can use `subject` or `processor`(with backpressure) to omit a callback function. Otherwise, if we just use an `observable`, it cant invoke `onNext` somewhere. Due to an `observable` will create so many new objects, using them is bette…
// A class
fun caller() {
// here we will set callback function.
callbackProcessor.subscriby {
// Here I can get `5566`, and do somthing.
}
}
// B class
fun callee() {
@pokk
pokk / README.md
Created August 1, 2017 02:44
What is `&&=` and `||=` meaning in ruby?

Introduction

Always I confuse &&= and ||=, what's difference between them?

Magic

operation &&=

a &amp;&amp;= b
@pokk
pokk / README.md
Created July 28, 2017 02:50
zip and unzip

Zip

def zip_files(src_path, dst_path_with_filename='zipfile.zip'):
    """
    :param src_path: a list of the files you want to zip.
    :param dst_path_with_filename: the destination path with file name of the zip file.
    :return: None
    """
@pokk
pokk / README.md
Created July 28, 2017 02:48
Tap a chain methods with condition statement.

Introcution

Sometimes we want to use chain method and Ruby also provides it.

It seems wonderful, BUT (Always BUT appears when everything is good 😭) the some chain methods only can run under some conditions. But we cannot do as like

obj.new
  .add(xxx) if xxx.present?  # Here will happen an error.
@pokk
pokk / README.md
Created July 28, 2017 02:48
Other way to expian 'if' and 'unless'

[TOC]

Introduction

How to do

Using if statment

@pokk
pokk / README.md
Created July 13, 2017 06:32
Do..End v.s Braces

What's difference between braces and do..end

Actually, there're no so big difference. But the priority is difference. We can check an example out as below then we can understand.

my_array = [1, 2, 3, 4, 5]

# <1>
p my_array.map { |item|