This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun ImageView.asyncLoadBitmapFromUrl(url: URL) { | |
Observable.create<Bitmap> { emitter -> | |
emitter.onNext(BitmapFactory.decodeStream(url.openStream())) | |
emitter.onComplete() | |
} | |
.subscribeOn(Schedulers.newThread()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe { setImageBitmap(it) } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val Int.dpToPx: Int get() = (this * Resources.getSystem().displayMetrics.density + 0.5f).toInt() | |
val Int.pxToDp: Int get() = (this / Resources.getSystem().displayMetrics.density + 0.5f).toInt() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <T : View> T.afterMeasured(crossinline func: T.() -> Unit) { | |
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { | |
override fun onGlobalLayout() { | |
if (measuredWidth > 0 && measuredHeight > 0) { | |
viewTreeObserver.removeOnGlobalLayoutListener(this) | |
func() | |
} | |
} | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Result<T> { | |
class InProgress<T> : Result<T>() | |
data class Success<T>(var data: T) : Result<T>() | |
data class Failure<T>(val e: Throwable) : Result<T>() | |
companion object { | |
fun <T> inProgress(): Result<T> = InProgress() | |
fun <T> success(data: T): Result<T> = Success(data) | |
fun <T> failure(e: Throwable): Result<T> = Failure(e) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun AppCompatActivity.replace(fragment: Fragment, isBackStack: Boolean = false) { | |
val transaction = supportFragmentManager | |
.beginTransaction() | |
.replace(R.id.content, fragment) | |
if (isBackStack) { | |
transaction.addToBackStack(null) | |
} | |
if (supportFragmentManager.isStateSaved) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SquareImageView @JvmOverloads constructor( | |
context: Context, | |
attrs: AttributeSet? = null, | |
defStyleAttr: Int = 0 | |
) : ImageView(context, attrs, defStyleAttr) { | |
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec) | |
setMeasuredDimension(measuredWidth, measuredWidth) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class RxViewModel : ViewModel() { | |
protected val compositeDisposable = CompositeDisposable() | |
override fun onCleared() { | |
super.onCleared() | |
compositeDisposable.clear() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListItem(private val text: String): BindableItem<ItemListBinding>() { | |
override fun getLayout(): Int = R.layout.item_list | |
override fun bind(viewBinding: ItemListBinding, position: Int) { | |
viewBinding.text = text | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun parse_person() { | |
val json = "{\"name\": \"kaelaela\", \"email\": \"test@gmail.com\", \"job_title\": \"title\", \"age\": 27}" | |
val moshi = Moshi.Builder().add(ApplicationJsonAdapterFactory.INSTANCE).build() | |
val person = moshi.adapter<Person>(Person::class.java).fromJson(json) | |
} | |
@JsonSerializable | |
data class Person( | |
val name: String, | |
val email: String?, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 指定URLから画像を読み込み、セットする | |
// defaultUIImageには、URLからの読込に失敗した時の画像を指定する | |
extension UIImageView { | |
func loadImageAsynchronously(url: URL?, defaultUIImage: UIImage? = nil) -> Void { | |
if url == nil { | |
self.image = defaultUIImage | |
return | |
} |
OlderNewer