diff --git a/README.md b/README.md index 899d14c..8614043 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Add this to your module's `build.gradle` file (make sure the version matches the ```gradle dependencies { - compile 'com.afollestad:icon-request:4.0.0' + compile 'com.afollestad:icon-request:4.0.1' } ``` @@ -165,7 +165,13 @@ You can pass an `apiHost` and `apiKey` to integrate with [Arctic Request Manager With a configured `ArcticRequest` instance, you can load unthemed apps: ```kotlin +// Rely on only the onLoad and onLoadError callbacks passed into ArcticRequest(...). +// If onLoadError isn't set, an error will result in a thrown RuntimeException. request.performLoad() + +// You will still receive onLoad and onLoadError notifications, but the result is also passed +// into the parameterized lambda. +request.performLoad { list, throwable -> } ``` Your `onLoaded` callback will receive a List of unthemed apps. If an error occurs, @@ -210,7 +216,13 @@ filtered list based off the list of all loaded apps. Once you've selected apps, you can send a request: ```kotlin +// Rely on only the onSent and onSendError callbacks passed into ArcticRequest(...). +// If onSendError isn't set, an error will result in a thrown RuntimeException. request.performSend() + +// You will still receive onSent and onSendError notifications, but the result is also passed +// into the parameterized lambda. +request.performSend { count, throwable -> } ``` Your `onSent` callback will be invoked if all is well; your `onSendError` callback diff --git a/dependencies.gradle b/dependencies.gradle index f54c839..72cdf33 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,8 +1,8 @@ ext.versions = [ minSdk : 14, compileSdk : 27, - publishVersion: '4.0.0', - versionCode : 4, + publishVersion: '4.0.1', + versionCode : 5, kotlin : '1.2.41', versionsPlugin: '0.17.0', diff --git a/library/src/main/java/com/afollestad/iconrequest/ArcticRequest.kt b/library/src/main/java/com/afollestad/iconrequest/ArcticRequest.kt index 97b70ff..7c4b217 100644 --- a/library/src/main/java/com/afollestad/iconrequest/ArcticRequest.kt +++ b/library/src/main/java/com/afollestad/iconrequest/ArcticRequest.kt @@ -22,6 +22,8 @@ typealias VoidCallback = (() -> (Unit))? typealias LoadedCallback = ((List) -> (Unit))? typealias SentCallback = ((Int) -> (Unit))? typealias AppCallback = ((AppModel) -> (Unit))? +typealias LoadedAndErrorCallback = ((List, Throwable?) -> (Unit))? +typealias SentAndErrorCallback = ((Int, Throwable?) -> (Unit))? /** @author Aidan Follestad (afollestad) */ class ArcticRequest constructor( @@ -76,7 +78,7 @@ class ArcticRequest constructor( out.putSerializable(KEY_APPS, storedLoadedApps.toTypedArray()) } - fun performLoad() { + fun performLoad(callback: LoadedAndErrorCallback = null) { onLoading?.invoke() disposables += Observable.just(true) .flatMap { @@ -93,10 +95,15 @@ class ArcticRequest constructor( } .observeToMainThread() .subscribe( - { onLoaded?.invoke(it) }, { - if (onLoadError != null) onLoadError.invoke(it) - else throw RuntimeException(it) + onLoaded?.invoke(it) + callback?.invoke(it, null) + }, + { + if (onLoadError != null || callback != null) { + onLoadError?.invoke(it) + callback?.invoke(listOf(), it) + } else throw RuntimeException(it) } ) } @@ -201,7 +208,7 @@ class ArcticRequest constructor( onLoaded?.invoke(storedLoadedApps) } - fun performSend() { + fun performSend(callback: SentAndErrorCallback = null) { onSending?.invoke() disposables += Observable.just(true) .map { selectedApps } @@ -214,10 +221,13 @@ class ArcticRequest constructor( { resetSelection() onSent?.invoke(it) + callback?.invoke(it, null) }, { - if (onSendError != null) onSendError.invoke(it) - else throw RuntimeException(it) + if (onSendError != null || callback != null) { + onSendError?.invoke(it) + callback?.invoke(0, it) + } else throw RuntimeException(it) } ) }