Stop nextcloud API after use

This commit is contained in:
sim 2023-03-06 21:02:16 +01:00
parent 901d2b880d
commit 72f6b7fc51
4 changed files with 18 additions and 12 deletions

View File

@ -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()
}
})
}

View File

@ -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) {}
}
}
}

View File

@ -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)
}

View File

@ -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()
}
}
}
}