feat: let the user name an account (#105)
This commit is contained in:
1 parent
08c297f30a
commit
a8bc3f0084
21 files changed
+701
-18
No files matched your search
+15
@@ -147,6 +147,21 @@ class TasksDatabaseMigrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addsAnEmptyAccountLabel() {
|
||||
helper.createDatabase(TEST_DB, 3).use { db ->
|
||||
db.execSQL("INSERT INTO accounts (id, display_name) VALUES (1, 'me@example.org')")
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(TEST_DB, 4, true, TasksDatabase.MIGRATION_3_4).use { db ->
|
||||
db.query("SELECT display_name, label FROM accounts").use { c ->
|
||||
assertThat(c.moveToFirst()).isTrue()
|
||||
assertThat(c.getString(0)).isEqualTo("me@example.org")
|
||||
assertThat(c.isNull(1)).isTrue()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val TEST_DB = "migration-test.db"
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import dagger.hilt.components.SingletonComponent
|
||||
import dagger.multibindings.IntoSet
|
||||
import de.jeanlucmakiola.agendula.data.sync.AccountCreator
|
||||
import de.jeanlucmakiola.agendula.data.sync.AccountRepository
|
||||
import de.jeanlucmakiola.agendula.data.sync.AccountSyncRequests
|
||||
import de.jeanlucmakiola.agendula.data.sync.CalDavGateway
|
||||
import de.jeanlucmakiola.agendula.data.sync.LoginFlowRecord
|
||||
import de.jeanlucmakiola.agendula.data.sync.OkHttpCalDavGateway
|
||||
@@ -79,7 +80,7 @@ abstract class SyncBindModule {
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
abstract fun bindSyncRequests(impl: SyncTrigger): SyncRequests
|
||||
abstract fun bindSyncRequests(impl: AccountSyncRequests): SyncRequests
|
||||
|
||||
@Binds
|
||||
@IntoSet
|
||||
|
||||
@@ -97,6 +97,11 @@ class AccountRepository @Inject constructor(
|
||||
/** The accounts, observed, so a sync landing updates a screen that is open. */
|
||||
fun observeAll(): Flow<List<AccountEntity>> = database.accounts().observeAll()
|
||||
|
||||
/** Names the account on screen; blank goes back to the default (#105). */
|
||||
suspend fun setLabel(accountId: Long, label: String) = withContext(io) {
|
||||
database.accounts().setLabel(accountId, label.trim().ifEmpty { null })
|
||||
}
|
||||
|
||||
/** Every account's synced lists. */
|
||||
fun observeSyncedLists(): Flow<List<TaskListEntity>> = database.taskLists().observeSynced()
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package de.jeanlucmakiola.agendula.data.sync
|
||||
|
||||
import de.jeanlucmakiola.agendula.data.di.IoDispatcher
|
||||
import de.jeanlucmakiola.agendula.data.tasks.room.TasksDatabase
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.withContext
|
||||
import javax.inject.Inject
|
||||
|
||||
/** [SyncRequests] by account id; [SyncTrigger] keys on the account's stable display name. */
|
||||
class AccountSyncRequests @Inject constructor(
|
||||
private val database: TasksDatabase,
|
||||
private val trigger: SyncTrigger,
|
||||
@IoDispatcher private val io: CoroutineDispatcher,
|
||||
) : SyncRequests {
|
||||
|
||||
override suspend fun syncNow(accountId: Long) {
|
||||
val name = withContext(io) { database.accounts().account(accountId)?.displayName } ?: return
|
||||
trigger.enqueue(name)
|
||||
}
|
||||
}
|
||||
@@ -35,11 +35,7 @@ import kotlin.time.Duration.Companion.seconds
|
||||
@Singleton
|
||||
class SyncTrigger @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
) : SyncRequests {
|
||||
|
||||
override fun syncNow(accountName: String) {
|
||||
enqueue(accountName)
|
||||
}
|
||||
) {
|
||||
|
||||
/**
|
||||
* Starts a sync now.
|
||||
|
||||
@@ -15,6 +15,7 @@ import androidx.compose.material.icons.rounded.Check
|
||||
import androidx.compose.material.icons.rounded.Bolt
|
||||
import androidx.compose.material.icons.rounded.CloudSync
|
||||
import androidx.compose.material.icons.rounded.DeleteOutline
|
||||
import androidx.compose.material.icons.rounded.Edit
|
||||
import androidx.compose.material.icons.rounded.History
|
||||
import androidx.compose.material.icons.rounded.HistoryToggleOff
|
||||
import androidx.compose.material.icons.rounded.Inventory2
|
||||
@@ -26,6 +27,7 @@ import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
@@ -46,9 +48,11 @@ import de.jeanlucmakiola.agendula.data.sync.DiscardedEdit
|
||||
import de.jeanlucmakiola.agendula.data.sync.SyncNotice
|
||||
import de.jeanlucmakiola.agendula.data.tasks.room.AccountEntity
|
||||
import de.jeanlucmakiola.agendula.ui.accounts.add.message
|
||||
import de.jeanlucmakiola.agendula.ui.common.NameField
|
||||
import de.jeanlucmakiola.agendula.ui.common.OnResume
|
||||
import de.jeanlucmakiola.floret.components.CollapsingScaffold
|
||||
import de.jeanlucmakiola.floret.components.FullScreenPicker
|
||||
import de.jeanlucmakiola.floret.components.GroupedHint
|
||||
import de.jeanlucmakiola.floret.components.GroupedListInset
|
||||
import de.jeanlucmakiola.floret.components.GroupedRow
|
||||
import de.jeanlucmakiola.floret.components.Position
|
||||
@@ -90,12 +94,34 @@ internal fun AccountDetailScreen(
|
||||
// The confirmation sheet. The removal it starts outlives this screen, and is
|
||||
// shown on the accounts list rather than here.
|
||||
var confirming by remember { mutableStateOf(false) }
|
||||
var renaming by remember { mutableStateOf(false) }
|
||||
|
||||
if (renaming) {
|
||||
AccountNamePicker(
|
||||
current = account.label.orEmpty(),
|
||||
defaultTitle = account.defaultTitle(),
|
||||
onSave = { name ->
|
||||
viewModel.rename(account, name)
|
||||
renaming = false
|
||||
},
|
||||
onDismiss = { renaming = false },
|
||||
)
|
||||
}
|
||||
|
||||
CollapsingScaffold(title = identity.title, onBack = onBack, predictiveBack = true, visible = visible) {
|
||||
AccountHero(identity)
|
||||
Spacer(Modifier.height(24.dp))
|
||||
|
||||
val actions = buildList<@Composable (Position) -> Unit> {
|
||||
add { position ->
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.accounts_name),
|
||||
summary = identity.title,
|
||||
position = position,
|
||||
leading = { Icon(Icons.Rounded.Edit, contentDescription = null) },
|
||||
onClick = { renaming = true },
|
||||
)
|
||||
}
|
||||
add { position ->
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.accounts_last_sync),
|
||||
@@ -480,3 +506,30 @@ private fun errorTitle(text: String): AnnotatedString {
|
||||
buildAnnotatedString { withStyle(SpanStyle(color = error)) { append(text) } }
|
||||
}
|
||||
}
|
||||
|
||||
/** Names an account on screen (#105); left empty, it goes back to [defaultTitle]. */
|
||||
@Composable
|
||||
private fun AccountNamePicker(
|
||||
current: String,
|
||||
defaultTitle: String,
|
||||
onSave: (String) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
var name by remember { mutableStateOf(current) }
|
||||
FullScreenPicker(
|
||||
title = stringResource(R.string.accounts_name),
|
||||
onDismiss = onDismiss,
|
||||
actions = {
|
||||
TextButton(onClick = { onSave(name) }) { Text(stringResource(R.string.save)) }
|
||||
},
|
||||
) {
|
||||
NameField(
|
||||
name = name,
|
||||
placeholder = defaultTitle,
|
||||
autoFocus = true,
|
||||
onNameChange = { name = it },
|
||||
onImeAction = { onSave(name) },
|
||||
)
|
||||
GroupedHint(stringResource(R.string.accounts_name_hint, defaultTitle))
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,14 @@ internal data class AccountIdentity(
|
||||
val user: String?,
|
||||
)
|
||||
|
||||
/** The title an account has without a [AccountEntity.label]. */
|
||||
internal fun AccountEntity.defaultTitle(): String {
|
||||
val url = principalUrl?.toHttpUrlOrNull()
|
||||
val provider = url?.let { CalDavProvider.forPrincipal(it) }
|
||||
val host = url?.host?.removePrefix("www.")
|
||||
return if (provider?.hosted == true) provider.label else host ?: displayName
|
||||
}
|
||||
|
||||
internal fun AccountEntity.identity(): AccountIdentity {
|
||||
val url = principalUrl?.toHttpUrlOrNull()
|
||||
val provider = url?.let { CalDavProvider.forPrincipal(it) }
|
||||
@@ -53,7 +61,7 @@ internal fun AccountEntity.identity(): AccountIdentity {
|
||||
provider = provider,
|
||||
// The fallback is the stored name: an account with no principal URL
|
||||
// never got past discovery, so there is nothing better to call it.
|
||||
title = if (hosted) provider.label else host ?: displayName,
|
||||
title = label ?: defaultTitle(),
|
||||
secondary = if (hosted) host else provider?.label,
|
||||
user = username?.takeIf { it.isNotBlank() },
|
||||
)
|
||||
|
||||
@@ -248,6 +248,10 @@ class AccountsViewModel @Inject constructor(
|
||||
syncTrigger.enqueue(account.displayName, expedited = true)
|
||||
}
|
||||
|
||||
fun rename(account: AccountEntity, label: String) {
|
||||
viewModelScope.launch { repository.setLabel(account.id, label) }
|
||||
}
|
||||
|
||||
/** The user has read what the last sync changed. */
|
||||
fun dismissNotices(account: AccountEntity) {
|
||||
viewModelScope.launch { notices.dismiss(account.id) }
|
||||
|
||||
@@ -89,7 +89,7 @@ object DataProvideModule {
|
||||
// backed-up `.db` does not. The backup rules carry all three files and
|
||||
// the app checkpoints on ON_STOP.
|
||||
.setJournalMode(RoomDatabase.JournalMode.WRITE_AHEAD_LOGGING)
|
||||
.addMigrations(TasksDatabase.MIGRATION_1_2, TasksDatabase.MIGRATION_2_3)
|
||||
.addMigrations(TasksDatabase.MIGRATION_1_2, TasksDatabase.MIGRATION_2_3, TasksDatabase.MIGRATION_3_4)
|
||||
.build()
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,5 +2,5 @@ package de.jeanlucmakiola.agendula.data.sync
|
||||
|
||||
/** Asks for an account to be synced now. A no-op in the `offline` flavor. */
|
||||
fun interface SyncRequests {
|
||||
fun syncNow(accountName: String)
|
||||
suspend fun syncNow(accountId: Long)
|
||||
}
|
||||
@@ -37,6 +37,9 @@ interface AccountDao {
|
||||
@Query("UPDATE accounts SET last_sync_at = :at, last_sync_error = :error WHERE id = :accountId")
|
||||
fun recordSync(accountId: Long, at: Instant?, error: String?)
|
||||
|
||||
@Query("UPDATE accounts SET label = :label WHERE id = :accountId")
|
||||
fun setLabel(accountId: Long, label: String?)
|
||||
|
||||
@Query("DELETE FROM accounts WHERE id = :accountId")
|
||||
fun delete(accountId: Long): Int
|
||||
}
|
||||
@@ -26,6 +26,8 @@ data class AccountEntity(
|
||||
@ColumnInfo(name = "username") val username: String? = null,
|
||||
@ColumnInfo(name = "last_sync_at") val lastSyncAt: Instant? = null,
|
||||
@ColumnInfo(name = "last_sync_error") val lastSyncError: String? = null,
|
||||
/** A name the user gave the account (#105); [displayName] stays the stable key. */
|
||||
@ColumnInfo(name = "label") val label: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,7 @@ import androidx.room.Embedded
|
||||
data class TaskListRow(
|
||||
@Embedded val list: TaskListEntity,
|
||||
@ColumnInfo(name = "account_display_name") val accountDisplayName: String?,
|
||||
@ColumnInfo(name = "account_label") val accountLabel: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -23,5 +24,6 @@ data class TaskRow(
|
||||
@ColumnInfo(name = "list_name") val listName: String,
|
||||
@ColumnInfo(name = "list_color") val listColor: Int,
|
||||
@ColumnInfo(name = "account_display_name") val accountDisplayName: String?,
|
||||
@ColumnInfo(name = "account_label") val accountLabel: String? = null,
|
||||
@ColumnInfo(name = "list_read_only") val listReadOnly: Boolean = false,
|
||||
)
|
||||
@@ -20,7 +20,7 @@ object RoomTaskMapper {
|
||||
color = row.list.color,
|
||||
// TaskList.accountName is non-null and the lists screen groups by it, so a
|
||||
// list with no account still has to report something to group under.
|
||||
accountName = row.accountDisplayName ?: LocalAccount.NAME,
|
||||
accountName = row.accountLabel ?: row.accountDisplayName ?: LocalAccount.NAME,
|
||||
accountType = if (row.list.accountId == null) LocalAccount.TYPE else CALDAV_ACCOUNT_TYPE,
|
||||
isSynced = row.list.isSynced,
|
||||
isVisible = row.list.isVisible,
|
||||
@@ -61,7 +61,7 @@ object RoomTaskMapper {
|
||||
listColor = row.listColor,
|
||||
taskColor = row.task.color,
|
||||
listName = row.listName,
|
||||
accountName = row.accountDisplayName ?: LocalAccount.NAME,
|
||||
accountName = row.accountLabel ?: row.accountDisplayName ?: LocalAccount.NAME,
|
||||
parentId = row.task.parentId,
|
||||
isRecurring = row.task.isRecurring,
|
||||
occurrenceStart = occurrenceStart,
|
||||
|
||||
@@ -29,7 +29,7 @@ interface TaskDao {
|
||||
@Query(
|
||||
"""
|
||||
SELECT t.*, l.name AS list_name, l.color AS list_color, l.is_read_only AS list_read_only,
|
||||
a.display_name AS account_display_name
|
||||
a.display_name AS account_display_name, a.label AS account_label
|
||||
FROM tasks t
|
||||
JOIN task_lists l ON l.id = t.list_id
|
||||
LEFT JOIN accounts a ON a.id = l.account_id
|
||||
@@ -44,7 +44,7 @@ interface TaskDao {
|
||||
@Query(
|
||||
"""
|
||||
SELECT t.*, l.name AS list_name, l.color AS list_color, l.is_read_only AS list_read_only,
|
||||
a.display_name AS account_display_name
|
||||
a.display_name AS account_display_name, a.label AS account_label
|
||||
FROM tasks t
|
||||
JOIN task_lists l ON l.id = t.list_id
|
||||
LEFT JOIN accounts a ON a.id = l.account_id
|
||||
@@ -56,7 +56,7 @@ interface TaskDao {
|
||||
@Query(
|
||||
"""
|
||||
SELECT t.*, l.name AS list_name, l.color AS list_color, l.is_read_only AS list_read_only,
|
||||
a.display_name AS account_display_name
|
||||
a.display_name AS account_display_name, a.label AS account_label
|
||||
FROM tasks t
|
||||
JOIN task_lists l ON l.id = t.list_id
|
||||
LEFT JOIN accounts a ON a.id = l.account_id
|
||||
|
||||
@@ -16,7 +16,7 @@ interface TaskListDao {
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT l.*, a.display_name AS account_display_name
|
||||
SELECT l.*, a.display_name AS account_display_name, a.label AS account_label
|
||||
FROM task_lists l LEFT JOIN accounts a ON a.id = l.account_id
|
||||
ORDER BY a.display_name, l.sort_order, l.name
|
||||
"""
|
||||
@@ -25,7 +25,7 @@ interface TaskListDao {
|
||||
|
||||
@Query(
|
||||
"""
|
||||
SELECT l.*, a.display_name AS account_display_name
|
||||
SELECT l.*, a.display_name AS account_display_name, a.label AS account_label
|
||||
FROM task_lists l LEFT JOIN accounts a ON a.id = l.account_id
|
||||
WHERE l.id = :listId
|
||||
"""
|
||||
|
||||
@@ -22,7 +22,7 @@ import kotlin.time.Instant
|
||||
TaskEntity::class,
|
||||
TaskAlarmEntity::class,
|
||||
],
|
||||
version = 3,
|
||||
version = 4,
|
||||
exportSchema = true,
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
@@ -173,5 +173,12 @@ abstract class TasksDatabase : RoomDatabase() {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** v4 lets the user name an account (#105). */
|
||||
val MIGRATION_3_4 = object : Migration(3, 4) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE accounts ADD COLUMN label TEXT")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -161,7 +161,7 @@ class ImportViewModel @Inject constructor(
|
||||
val result = importer.import(ready.parsed, target)
|
||||
(chosen as? ImportChoice.Existing)?.list
|
||||
?.takeIf { it.accountId != null && result.imported > 0 }
|
||||
?.let { runCatching { syncRequests.syncNow(it.accountName) } }
|
||||
?.let { runCatching { syncRequests.syncNow(it.accountId!!) } }
|
||||
ImportStep.Done(
|
||||
listName = listName,
|
||||
imported = result.imported,
|
||||
|
||||
@@ -681,6 +681,8 @@
|
||||
<string name="accounts_remove_wipe_body">Also deletes this account\u2019s lists and tasks from this device.</string>
|
||||
<string name="accounts_never_synced">Never synced</string>
|
||||
<string name="accounts_last_sync">Last sync</string>
|
||||
<string name="accounts_name">Name</string>
|
||||
<string name="accounts_name_hint">How this account is shown in Agendula. Leave it empty to use “%1$s”.</string>
|
||||
<string name="accounts_generic_provider">CalDAV account</string>
|
||||
<string name="accounts_sync_failed">Last sync didn\u2019t finish</string>
|
||||
<string name="accounts_failed_sign_in">Sync failed \u2014 the server didn\u2019t accept the password</string>
|
||||
|
||||
@@ -27,4 +27,16 @@ class RoomTaskMapperTest {
|
||||
|
||||
assertThat(RoomTaskMapper.taskList(list).acceptsWrites).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an account's label is shown in place of its stored name`() {
|
||||
val list = TaskListRow(
|
||||
list = TaskListEntity(id = 2, name = "Work", color = 0, accountId = 1),
|
||||
accountDisplayName = "me@host",
|
||||
accountLabel = "Work server",
|
||||
)
|
||||
|
||||
assertThat(RoomTaskMapper.taskList(list).accountName).isEqualTo("Work server")
|
||||
assertThat(RoomTaskMapper.taskList(list.copy(accountLabel = null)).accountName).isEqualTo("me@host")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user