mirror of https://github.com/tasks/tasks
Add BaseInvoker and InvokerFactory
parent
0664e23076
commit
727ad6e7a4
@ -1,13 +1,13 @@
|
|||||||
package org.tasks.activities
|
package org.tasks.activities
|
||||||
|
|
||||||
import androidx.hilt.lifecycle.ViewModelInject
|
import androidx.hilt.lifecycle.ViewModelInject
|
||||||
import com.todoroo.astrid.gtasks.api.GtasksInvoker
|
|
||||||
import org.tasks.data.GoogleTaskList
|
import org.tasks.data.GoogleTaskList
|
||||||
|
import org.tasks.googleapis.InvokerFactory
|
||||||
import org.tasks.ui.ActionViewModel
|
import org.tasks.ui.ActionViewModel
|
||||||
|
|
||||||
class DeleteListViewModel @ViewModelInject constructor(
|
class DeleteListViewModel @ViewModelInject constructor(
|
||||||
private val invoker: GtasksInvoker) : ActionViewModel() {
|
private val invoker: InvokerFactory) : ActionViewModel() {
|
||||||
suspend fun deleteList(list: GoogleTaskList) {
|
suspend fun deleteList(list: GoogleTaskList) {
|
||||||
run { invoker.forAccount(list.account!!).deleteGtaskList(list.remoteId) }
|
run { invoker.getGtasksInvoker(list.account!!).deleteGtaskList(list.remoteId) }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
package org.tasks.googleapis
|
||||||
|
|
||||||
|
import com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest
|
||||||
|
import com.google.api.client.http.HttpResponseException
|
||||||
|
import com.google.api.client.json.GenericJson
|
||||||
|
import com.todoroo.andlib.utility.DateUtilities
|
||||||
|
import com.todoroo.astrid.gtasks.api.HttpCredentialsAdapter
|
||||||
|
import com.todoroo.astrid.gtasks.api.HttpNotFoundException
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import org.tasks.BuildConfig
|
||||||
|
import org.tasks.DebugNetworkInterceptor
|
||||||
|
import org.tasks.preferences.Preferences
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
abstract class BaseInvoker(
|
||||||
|
private val credentialsAdapter: HttpCredentialsAdapter,
|
||||||
|
private val preferences: Preferences,
|
||||||
|
private val interceptor: DebugNetworkInterceptor
|
||||||
|
) {
|
||||||
|
@Synchronized
|
||||||
|
@Throws(IOException::class)
|
||||||
|
protected suspend fun <T> execute(request: AbstractGoogleJsonClientRequest<T>): T? = execute(request, false)
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
|
@Throws(IOException::class)
|
||||||
|
private suspend fun <T> execute(request: AbstractGoogleJsonClientRequest<T>, retry: Boolean): T? =
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
credentialsAdapter.checkToken()
|
||||||
|
Timber.d("%s request: %s", caller, request)
|
||||||
|
val response: T?
|
||||||
|
response = try {
|
||||||
|
if (preferences.isFlipperEnabled) {
|
||||||
|
val start = DateUtilities.now()
|
||||||
|
val httpResponse = request.executeUnparsed()
|
||||||
|
interceptor.report(httpResponse, request.responseClass, start, DateUtilities.now())
|
||||||
|
} else {
|
||||||
|
request.execute()
|
||||||
|
}
|
||||||
|
} catch (e: HttpResponseException) {
|
||||||
|
return@withContext if (e.statusCode == 401 && !retry) {
|
||||||
|
credentialsAdapter.invalidateToken()
|
||||||
|
execute(request, true)
|
||||||
|
} else if (e.statusCode == 404) {
|
||||||
|
throw HttpNotFoundException(e)
|
||||||
|
} else {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Timber.d("%s response: %s", caller, prettyPrint(response))
|
||||||
|
response
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
private fun <T> prettyPrint(`object`: T?): Any? {
|
||||||
|
if (BuildConfig.DEBUG) {
|
||||||
|
if (`object` is GenericJson) {
|
||||||
|
return (`object` as GenericJson).toPrettyString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return `object`
|
||||||
|
}
|
||||||
|
|
||||||
|
private val caller: String
|
||||||
|
get() {
|
||||||
|
if (BuildConfig.DEBUG) {
|
||||||
|
try {
|
||||||
|
return Thread.currentThread().stackTrace[4].methodName
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val APP_NAME = "Tasks/${BuildConfig.VERSION_NAME}"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package org.tasks.googleapis
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import com.google.api.services.drive.DriveScopes
|
||||||
|
import com.google.api.services.tasks.TasksScopes
|
||||||
|
import com.todoroo.astrid.gtasks.api.GtasksInvoker
|
||||||
|
import com.todoroo.astrid.gtasks.api.HttpCredentialsAdapter
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
|
import org.tasks.DebugNetworkInterceptor
|
||||||
|
import org.tasks.R
|
||||||
|
import org.tasks.drive.DriveInvoker
|
||||||
|
import org.tasks.gtasks.GoogleAccountManager
|
||||||
|
import org.tasks.preferences.Preferences
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class InvokerFactory @Inject constructor(
|
||||||
|
@ApplicationContext private val context: Context,
|
||||||
|
private val googleAccountManager: GoogleAccountManager,
|
||||||
|
private val preferences: Preferences,
|
||||||
|
private val interceptor: DebugNetworkInterceptor
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun getDriveInvoker() = DriveInvoker(
|
||||||
|
context,
|
||||||
|
preferences,
|
||||||
|
HttpCredentialsAdapter(
|
||||||
|
googleAccountManager,
|
||||||
|
preferences.getStringValue(R.string.p_google_drive_backup_account) ?: "",
|
||||||
|
DriveScopes.DRIVE_FILE
|
||||||
|
),
|
||||||
|
interceptor
|
||||||
|
)
|
||||||
|
|
||||||
|
fun getGtasksInvoker(account: String) = GtasksInvoker(
|
||||||
|
HttpCredentialsAdapter(googleAccountManager, account, TasksScopes.TASKS),
|
||||||
|
preferences,
|
||||||
|
interceptor
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue