Skip to content

Commit

Permalink
refactor: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinliu committed Aug 22, 2019
1 parent d2e3eba commit f3f067d
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions library/src/main/java/cn/quickits/halia/LoadingDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,10 @@ class LoadingDialog<T>(private val observable: Observable<T>) {

}
.subscribeOn(Schedulers.io())
.subscribe({
isManualClose = true
processor.onNext(it)
processor.onComplete()
try {
dialog?.dismiss()
} catch (e: IllegalArgumentException) {

}
}, {
isManualClose = true
processor.onError(it)
processor.onComplete()
try {
dialog?.dismiss()
} catch (e: IllegalArgumentException) {

}
})
.subscribe(
{ data -> dispatchDataOrError(data = data) },
{ error -> dispatchDataOrError(error = error) }
)
}

dialog?.setOnDismissListener {
Expand All @@ -80,4 +65,22 @@ class LoadingDialog<T>(private val observable: Observable<T>) {
dialog?.dismiss()
}

private fun dispatchDataOrError(data: T? = null, error: Throwable? = null) {
isManualClose = true

if (data != null) {
processor.onNext(data)
} else if (error != null) {
processor.onError(error)
}

processor.onComplete()

try {
dialog?.dismiss()
} catch (e: IllegalArgumentException) {

}
}

}

0 comments on commit f3f067d

Please # to comment.