mirror of https://github.com/tasks/tasks
Merge caldav account view model
parent
874069354d
commit
0dbd0551d1
@ -1,19 +0,0 @@
|
|||||||
package org.tasks.caldav
|
|
||||||
|
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
||||||
import org.tasks.ui.CompletableViewModel
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@HiltViewModel
|
|
||||||
class AddCaldavAccountViewModel @Inject constructor(
|
|
||||||
private val provider: CaldavClientProvider
|
|
||||||
) : CompletableViewModel<String>() {
|
|
||||||
suspend fun addAccount(url: String, username: String, password: String) {
|
|
||||||
run {
|
|
||||||
provider
|
|
||||||
.forUrl(url, username, password)
|
|
||||||
.setForeground()
|
|
||||||
.homeSet(username, password)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,28 @@
|
|||||||
|
package org.tasks.caldav
|
||||||
|
|
||||||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class CaldavAccountViewModel @Inject constructor(
|
||||||
|
private val provider: CaldavClientProvider
|
||||||
|
) : CaldavViewModel() {
|
||||||
|
suspend fun addAccount(url: String, username: String, password: String): String? =
|
||||||
|
doRequest {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
provider
|
||||||
|
.forUrl(url, username, password)
|
||||||
|
.setForeground()
|
||||||
|
.homeSet(username, password)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun updateCaldavAccount(url: String, username: String, password: String): String? =
|
||||||
|
doRequest {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
provider.forUrl(url, username, password).homeSet(username, password)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package org.tasks.caldav
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import androidx.lifecycle.MutableLiveData
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import kotlinx.coroutines.NonCancellable
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
|
abstract class CaldavViewModel : ViewModel() {
|
||||||
|
val error = MutableLiveData<Throwable?>()
|
||||||
|
val inFlight = MutableLiveData(false)
|
||||||
|
val finish = MutableLiveData<Intent>()
|
||||||
|
|
||||||
|
protected suspend fun <T> doRequest(action: suspend () -> T): T? =
|
||||||
|
withContext(NonCancellable) {
|
||||||
|
if (inFlight.value == true) {
|
||||||
|
return@withContext null
|
||||||
|
}
|
||||||
|
inFlight.value = true
|
||||||
|
try {
|
||||||
|
return@withContext action()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e)
|
||||||
|
error.value = e
|
||||||
|
return@withContext null
|
||||||
|
} finally {
|
||||||
|
inFlight.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,14 +0,0 @@
|
|||||||
package org.tasks.caldav
|
|
||||||
|
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
||||||
import org.tasks.ui.CompletableViewModel
|
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@HiltViewModel
|
|
||||||
class UpdateCaldavAccountViewModel @Inject constructor(
|
|
||||||
private val provider: CaldavClientProvider
|
|
||||||
) : CompletableViewModel<String>() {
|
|
||||||
suspend fun updateCaldavAccount(url: String, username: String, password: String) {
|
|
||||||
run { provider.forUrl(url, username, password).homeSet(username, password) }
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue