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 private val baseUrl: String
get() = getAccount(context)?.url ?: "http://0.0.0.0/" 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) { when (context.accountType) {
AccountType.SSO -> ApiSSOFactory(context) AccountType.SSO -> ApiSSOFactory(context)
AccountType.Direct -> ApiDirectFactory(context) AccountType.Direct -> ApiDirectFactory(context)
@ -48,7 +48,7 @@ class Api(val context: Context) {
val parameters = mapOf("deviceName" to Build.MODEL) val parameters = mapOf("deviceName" to Build.MODEL)
try { try {
withApiProvider { apiProvider -> withApiProvider { apiProvider, then ->
apiProvider.createDevice(parameters) apiProvider.createDevice(parameters)
?.subscribeOn(Schedulers.io()) ?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread()) ?.observeOn(AndroidSchedulers.mainThread())
@ -69,6 +69,7 @@ class Api(val context: Context) {
syncDevice(it) syncDevice(it)
} }
Log.d(TAG, "mApi register: onComplete") Log.d(TAG, "mApi register: onComplete")
then()
} }
}) })
} }
@ -96,7 +97,7 @@ class Api(val context: Context) {
fun apiDeleteDevice(block: () -> Unit = {}) { fun apiDeleteDevice(block: () -> Unit = {}) {
val deviceId = context.deviceId ?: return val deviceId = context.deviceId ?: return
try { try {
withApiProvider { apiProvider -> withApiProvider { apiProvider, then ->
apiProvider.deleteDevice(deviceId) apiProvider.deleteDevice(deviceId)
?.subscribeOn(Schedulers.io()) ?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread()) ?.observeOn(AndroidSchedulers.mainThread())
@ -115,6 +116,7 @@ class Api(val context: Context) {
override fun onComplete() { override fun onComplete() {
block() block()
then()
} }
}) })
context.deviceId = null context.deviceId = null
@ -136,7 +138,7 @@ class Api(val context: Context) {
) )
} ?: return } ?: return
try { try {
withApiProvider { apiProvider -> withApiProvider { apiProvider, then ->
apiProvider.createApp(parameters) apiProvider.createApp(parameters)
?.subscribeOn(Schedulers.io()) ?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread()) ?.observeOn(AndroidSchedulers.mainThread())
@ -157,7 +159,9 @@ class Api(val context: Context) {
e.printStackTrace() e.printStackTrace()
} }
override fun onComplete() {} override fun onComplete() {
then()
}
}) })
} }
} catch (e: NoProviderException) { } catch (e: NoProviderException) {
@ -167,7 +171,7 @@ class Api(val context: Context) {
fun apiDeleteApp(nextpushToken: String, block: () -> Unit) { fun apiDeleteApp(nextpushToken: String, block: () -> Unit) {
try { try {
withApiProvider { apiProvider -> withApiProvider { apiProvider, then ->
apiProvider.deleteApp(nextpushToken) apiProvider.deleteApp(nextpushToken)
?.subscribeOn(Schedulers.io()) ?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread()) ?.observeOn(AndroidSchedulers.mainThread())
@ -186,6 +190,7 @@ class Api(val context: Context) {
override fun onComplete() { override fun onComplete() {
block() block()
then()
} }
}) })
} }

View File

@ -9,8 +9,7 @@ import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory import retrofit2.converter.gson.GsonConverterFactory
class ApiDirectFactory(val context: Context) : ApiProviderFactory { class ApiDirectFactory(val context: Context) : ApiProviderFactory {
override fun getProviderAndExecute(block: (ApiProvider, then: () -> Unit) -> Unit) {
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
val account = getAccount(context) ?: run { val account = getAccount(context) ?: run {
throw NoProviderException("No account found") throw NoProviderException("No account found")
} }
@ -26,7 +25,7 @@ class ApiDirectFactory(val context: Context) : ApiProviderFactory {
.addCallAdapterFactory(RxJava3CallAdapterFactory.create()) .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.baseUrl("$url$mApiEndpoint").build() .baseUrl("$url$mApiEndpoint").build()
.create(ApiProvider::class.java).let { .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) class NoProviderException(message: String) : Exception(message)
interface ApiProviderFactory { 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 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 var nextcloudAPI: NextcloudAPI? = null
val account = getAccount(context) ?: run { val account = getAccount(context) ?: run {
throw NoProviderException("No account found") throw NoProviderException("No account found")
@ -26,7 +26,9 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory {
nextcloudAPI?.let { nextcloudAPI -> nextcloudAPI?.let { nextcloudAPI ->
NextcloudRetrofitApiBuilder(nextcloudAPI, ApiProvider.mApiEndpoint) NextcloudRetrofitApiBuilder(nextcloudAPI, ApiProvider.mApiEndpoint)
.create(ApiProvider::class.java).let { .create(ApiProvider::class.java).let {
block(it) block(it) {
nextcloudAPI.stop()
}
} }
} }
} }