diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt index 7817695..b779514 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt @@ -26,7 +26,7 @@ class Api(val context: Context) { private val baseUrl: String get() = getAccount(context)?.url ?: "http://0.0.0.0/" - private fun withApiProvider(block: (ApiProvider) -> Unit) { + private fun withApiProvider(block: (ApiProvider, then: () -> Unit) -> Unit) { when (context.accountType) { AccountType.SSO -> ApiSSOFactory(context) AccountType.Direct -> ApiDirectFactory(context) @@ -48,7 +48,7 @@ class Api(val context: Context) { val parameters = mapOf("deviceName" to Build.MODEL) try { - withApiProvider { apiProvider -> + withApiProvider { apiProvider, then -> apiProvider.createDevice(parameters) ?.subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) @@ -69,6 +69,7 @@ class Api(val context: Context) { syncDevice(it) } Log.d(TAG, "mApi register: onComplete") + then() } }) } @@ -96,7 +97,7 @@ class Api(val context: Context) { fun apiDeleteDevice(block: () -> Unit = {}) { val deviceId = context.deviceId ?: return try { - withApiProvider { apiProvider -> + withApiProvider { apiProvider, then -> apiProvider.deleteDevice(deviceId) ?.subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) @@ -115,6 +116,7 @@ class Api(val context: Context) { override fun onComplete() { block() + then() } }) context.deviceId = null @@ -136,7 +138,7 @@ class Api(val context: Context) { ) } ?: return try { - withApiProvider { apiProvider -> + withApiProvider { apiProvider, then -> apiProvider.createApp(parameters) ?.subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) @@ -157,7 +159,9 @@ class Api(val context: Context) { e.printStackTrace() } - override fun onComplete() {} + override fun onComplete() { + then() + } }) } } catch (e: NoProviderException) { @@ -167,7 +171,7 @@ class Api(val context: Context) { fun apiDeleteApp(nextpushToken: String, block: () -> Unit) { try { - withApiProvider { apiProvider -> + withApiProvider { apiProvider, then -> apiProvider.deleteApp(nextpushToken) ?.subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) @@ -186,6 +190,7 @@ class Api(val context: Context) { override fun onComplete() { block() + then() } }) } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiDirectFactory.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiDirectFactory.kt index 10dba33..b1d0db7 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiDirectFactory.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiDirectFactory.kt @@ -9,8 +9,7 @@ import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory import retrofit2.converter.gson.GsonConverterFactory class ApiDirectFactory(val context: Context) : ApiProviderFactory { - - override fun getProviderAndExecute(block: (ApiProvider) -> Unit) { + override fun getProviderAndExecute(block: (ApiProvider, then: () -> Unit) -> Unit) { val account = getAccount(context) ?: run { throw NoProviderException("No account found") } @@ -26,7 +25,7 @@ class ApiDirectFactory(val context: Context) : ApiProviderFactory { .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) .baseUrl("$url$mApiEndpoint").build() .create(ApiProvider::class.java).let { - block(it) + block(it) {} } } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiProviderFactory.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiProviderFactory.kt index f0439cb..66f00ab 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiProviderFactory.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiProviderFactory.kt @@ -2,5 +2,5 @@ package org.unifiedpush.distributor.nextpush.api.provider class NoProviderException(message: String) : Exception(message) interface ApiProviderFactory { - fun getProviderAndExecute(block: (ApiProvider) -> Unit) + fun getProviderAndExecute(block: (ApiProvider, then: () -> Unit) -> Unit) } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiSSOFactory.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiSSOFactory.kt index 3ecc808..642625d 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiSSOFactory.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiSSOFactory.kt @@ -12,7 +12,7 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory { private val TAG = ApiSSOFactory::class.java.simpleName - override fun getProviderAndExecute(block: (ApiProvider) -> Unit) { + override fun getProviderAndExecute(block: (ApiProvider, then: () -> Unit) -> Unit) { var nextcloudAPI: NextcloudAPI? = null val account = getAccount(context) ?: run { throw NoProviderException("No account found") @@ -26,7 +26,9 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory { nextcloudAPI?.let { nextcloudAPI -> NextcloudRetrofitApiBuilder(nextcloudAPI, ApiProvider.mApiEndpoint) .create(ApiProvider::class.java).let { - block(it) + block(it) { + nextcloudAPI.stop() + } } } }