Skip to content

Instantly share code, notes, and snippets.

@icris
Last active April 5, 2017 14:05
Show Gist options
  • Select an option

  • Save icris/9e655d7f772c7cc29344e99e482e6c1f to your computer and use it in GitHub Desktop.

Select an option

Save icris/9e655d7f772c7cc29344e99e482e6c1f to your computer and use it in GitHub Desktop.
Some simple functions. Useful for displaying a progress dialog when something working in background.
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity
import com.trello.rxlifecycle2.kotlin.bindToLifecycle
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
/**
* base activity
* all activities should extend base activity
* Created by icris on 2017/3/27.
*/
@Suppress("unused")
abstract class BaseActivity : RxAppCompatActivity() {
fun <T> Observable<T>.withProgress(): Observable<T> = takeUntil(loading())
fun <T> Observable<T>.withCancelableProgress(): Observable<T> = takeUntil(loadingCancelable())
fun <T> Observable<T>.bindToLifecycle(): Observable<T> = bindToLifecycle(this@BaseActivity)
fun loading(): Observable<Any> = Observable.create<Any> {
val dialog = LoadingDialog().apply { show(supportFragmentManager, "loading") }
it.setCancellable(dialog::dismiss)
}.subscribeOn(AndroidSchedulers.mainThread())
fun loadingCancelable(): Observable<Any> = Observable.create<Any> {
val dialog = LoadingDialog(cancelable = true, onCancelListener = it::onComplete).apply {
show(supportFragmentManager, "loading")
}
it.setCancellable(dialog::dismiss)
}.subscribeOn(AndroidSchedulers.mainThread())
}
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.view.*
import com.facebook.common.util.UriUtil
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.drawable.ScalingUtils
import com.facebook.drawee.view.SimpleDraweeView
import org.jetbrains.anko.*
import org.jetbrains.anko.custom.ankoView
typealias OnCancelListener = () -> Unit
/**
* loading dialog
* Created by icris on 2017/3/27.
*/
class LoadingDialog(val cancelable: Boolean = false, val onCancelListener: OnCancelListener = {}) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
isCancelable = cancelable
return super.onCreateDialog(savedInstanceState).apply {
window?.setBackgroundDrawableResource(android.R.color.transparent)
}
}
override fun onCancel(dialog: DialogInterface?) {
super.onCancel(dialog)
onCancelListener()
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return MUI().createView(AnkoContext.create(activity, this))
}
private class MUI : AnkoComponent<LoadingDialog> {
override fun createView(ui: AnkoContext<LoadingDialog>): View = with(ui) {
frameLayout {
simpleDraweeView {
lparams(width = dip(80), height = dip(80))
imageRes = R.raw.ripple
hierarchy.actualImageScaleType = ScalingUtils.ScaleType.FIT_CENTER
}
}
}
}
}
inline fun ViewManager.simpleDraweeView(theme: Int = 0, init: SimpleDraweeView.() -> Unit)
= ankoView(::SimpleDraweeView, theme) { init() }
var SimpleDraweeView.imageRes get() = 1
set(value) {
val controller = Fresco.newDraweeControllerBuilder()
.setUri(UriUtil.getUriForResourceId(value))
.setAutoPlayAnimations(true)
.setOldController(controller)
.build()
setController(controller)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment