|
|
|
@ -2,8 +2,17 @@ package org.tasks.caldav
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import androidx.activity.viewModels
|
|
|
|
|
import androidx.compose.foundation.layout.*
|
|
|
|
|
import androidx.compose.foundation.layout.Arrangement
|
|
|
|
|
import androidx.compose.foundation.layout.Column
|
|
|
|
|
import androidx.compose.foundation.layout.PaddingValues
|
|
|
|
|
import androidx.compose.foundation.layout.Row
|
|
|
|
|
import androidx.compose.foundation.layout.Spacer
|
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
|
|
import androidx.compose.foundation.layout.height
|
|
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
|
|
import androidx.compose.foundation.layout.size
|
|
|
|
|
import androidx.compose.material.Icon
|
|
|
|
|
import androidx.compose.material.IconButton
|
|
|
|
|
import androidx.compose.material.MaterialTheme
|
|
|
|
|
import androidx.compose.material.Text
|
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
@ -15,10 +24,16 @@ import androidx.compose.ui.res.stringResource
|
|
|
|
|
import androidx.compose.ui.tooling.preview.Preview
|
|
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
|
|
import androidx.core.view.isVisible
|
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import org.tasks.R
|
|
|
|
|
import org.tasks.data.CaldavAccount
|
|
|
|
|
import org.tasks.data.CaldavAccount.Companion.SERVER_OWNCLOUD
|
|
|
|
|
import org.tasks.data.CaldavAccount.Companion.SERVER_SABREDAV
|
|
|
|
|
import org.tasks.data.CaldavAccount.Companion.SERVER_TASKS
|
|
|
|
|
import org.tasks.data.CaldavCalendar
|
|
|
|
|
import org.tasks.data.CaldavCalendar.Companion.ACCESS_OWNER
|
|
|
|
|
import org.tasks.data.Principal
|
|
|
|
|
import org.tasks.data.Principal.Companion.name
|
|
|
|
|
import org.tasks.data.PrincipalDao
|
|
|
|
@ -29,6 +44,8 @@ class CaldavCalendarSettingsActivity : BaseCaldavCalendarSettingsActivity() {
|
|
|
|
|
|
|
|
|
|
@Inject lateinit var principalDao: PrincipalDao
|
|
|
|
|
|
|
|
|
|
private val viewModel: CaldavCalendarViewModel by viewModels()
|
|
|
|
|
|
|
|
|
|
private val createCalendarViewModel: CreateCalendarViewModel by viewModels()
|
|
|
|
|
private val deleteCalendarViewModel: DeleteCalendarViewModel by viewModels()
|
|
|
|
|
private val updateCalendarViewModel: UpdateCalendarViewModel by viewModels()
|
|
|
|
@ -38,6 +55,14 @@ class CaldavCalendarSettingsActivity : BaseCaldavCalendarSettingsActivity() {
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
|
|
|
|
viewModel.inFlight.observe(this) { progressView.isVisible = it }
|
|
|
|
|
viewModel.error.observe(this) { throwable ->
|
|
|
|
|
throwable?.let {
|
|
|
|
|
requestFailed(it)
|
|
|
|
|
viewModel.error.value = null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createCalendarViewModel.observe(this, this::createSuccessful, this::requestFailed)
|
|
|
|
|
deleteCalendarViewModel.observe(this, this::onDeleted, this::requestFailed)
|
|
|
|
|
updateCalendarViewModel.observe(
|
|
|
|
@ -54,6 +79,29 @@ class CaldavCalendarSettingsActivity : BaseCaldavCalendarSettingsActivity() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val canRemovePrincipals: Boolean
|
|
|
|
|
get() = caldavCalendar?.access == ACCESS_OWNER && caldavAccount.canRemovePrincipal
|
|
|
|
|
|
|
|
|
|
private fun onRemove(principal: Principal) {
|
|
|
|
|
if (requestInProgress()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
dialogBuilder
|
|
|
|
|
.newDialog(R.string.remove_user)
|
|
|
|
|
.setMessage(R.string.remove_user_confirmation, principal.name, caldavCalendar?.name)
|
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ -> removePrincipal(principal) }
|
|
|
|
|
.show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun removePrincipal(principal: Principal) = lifecycleScope.launch {
|
|
|
|
|
try {
|
|
|
|
|
viewModel.remove(caldavAccount, caldavCalendar!!, principal)
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
requestFailed(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
|
private fun PrincipalList(principals: List<Principal>) {
|
|
|
|
|
tasksTheme.TasksTheme {
|
|
|
|
@ -91,6 +139,19 @@ class CaldavCalendarSettingsActivity : BaseCaldavCalendarSettingsActivity() {
|
|
|
|
|
style = MaterialTheme.typography.body1,
|
|
|
|
|
color = MaterialTheme.colors.onBackground,
|
|
|
|
|
)
|
|
|
|
|
if (canRemovePrincipals) {
|
|
|
|
|
Row(horizontalArrangement = Arrangement.End, modifier = Modifier.fillMaxWidth()) {
|
|
|
|
|
IconButton(
|
|
|
|
|
modifier = Modifier.then(Modifier.size(24.dp)),
|
|
|
|
|
onClick = { onRemove(principal) }) {
|
|
|
|
|
Icon(
|
|
|
|
|
painter = painterResource(R.drawable.ic_outline_clear_24px),
|
|
|
|
|
contentDescription = null,
|
|
|
|
|
tint = colorResource(R.color.icon_tint_with_alpha),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -112,4 +173,12 @@ class CaldavCalendarSettingsActivity : BaseCaldavCalendarSettingsActivity() {
|
|
|
|
|
Principal().apply { displayName = "user2" },
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
val CaldavAccount.canRemovePrincipal: Boolean
|
|
|
|
|
get() = when (serverType) {
|
|
|
|
|
SERVER_TASKS, SERVER_OWNCLOUD, SERVER_SABREDAV -> true
|
|
|
|
|
else -> false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|