Android: Synchronize with myTeamSpeak; keep each installation's default identity
Settings → myTeamSpeak gets Synchronize, with conflicts settled in SyncConflictsScreen. The default identity is no longer a synced field: an identity new to the account carries it only when the account has none, and one brought here takes it only when we have none, so installations stop flipping each other's default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -27,7 +27,8 @@ class AppParts(context: Context) {
|
||||
val whisperLists: WhisperListManager = WhisperListManager()
|
||||
val audio: AudioSetup = AudioSetup(context, settings)
|
||||
val myTeamSpeak: MyTeamSpeakAccount = MyTeamSpeakAccount(context, pictures,
|
||||
TeamSpeakImporter(identityStore, bookmarkStore, settings, contactStore), bookmarks, identities) { sessions.myTeamSpeakChanged() }
|
||||
TeamSpeakImporter(identityStore, bookmarkStore, settings, contactStore), bookmarks, identities,
|
||||
identityStore, bookmarkStore, settings) { sessions.myTeamSpeakChanged() }
|
||||
|
||||
private val serverParts = ServerParts(context, settings, identityStore, audio, contactStore, myTeamSpeak)
|
||||
val sessions: SessionController = SessionController(context, settings, identities, bookmarks, serverParts,
|
||||
|
||||
@@ -7,6 +7,10 @@ import eu.lixko.ts3j.client.myts.ChatAttachment
|
||||
import eu.lixko.ts3j.client.myts.ChatAttachments
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeak
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeakLogin
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeakSync
|
||||
import eu.lixko.ts3j.client.config.Bookmarks
|
||||
import eu.lixko.ts3j.client.config.IdentityStore
|
||||
import eu.lixko.ts3j.client.config.Settings
|
||||
import eu.lixko.ts3j.client.teamspeak.SyncItem
|
||||
import eu.lixko.ts3j.client.teamspeak.TeamSpeakImporter
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -29,6 +33,9 @@ class MyTeamSpeakAccount internal constructor(
|
||||
private val importer: TeamSpeakImporter,
|
||||
private val bookmarks: BookmarkManager,
|
||||
private val identities: IdentityManager,
|
||||
identityStore: IdentityStore,
|
||||
bookmarkStore: Bookmarks,
|
||||
settings: Settings,
|
||||
/** Signed in or out, for the servers to pass on. */
|
||||
private val onSignInChanged: () -> Unit,
|
||||
) {
|
||||
@@ -37,6 +44,8 @@ class MyTeamSpeakAccount internal constructor(
|
||||
private val _signedIn = MutableStateFlow(login.isSignedIn)
|
||||
val signedIn: StateFlow<Boolean> = _signedIn
|
||||
|
||||
private val sync = MyTeamSpeakSync(login, identityStore, bookmarkStore, settings)
|
||||
|
||||
/** Files shared in chat. */
|
||||
internal val attachments = ChatAttachments(login).also {
|
||||
pictures.addSource(ChatAttachment.SCHEME, it::fetchPicture)
|
||||
@@ -76,6 +85,30 @@ class MyTeamSpeakAccount internal constructor(
|
||||
identities.refresh()
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads what changed in the account and works out what goes which way. Call on the main
|
||||
* thread, which owns the bookmarks and identities; the network part runs on [Dispatchers.IO].
|
||||
*/
|
||||
suspend fun planSync(): MyTeamSpeakSync.Plan =
|
||||
try {
|
||||
sync.plan(withContext(Dispatchers.IO) { sync.fetch() })
|
||||
} finally {
|
||||
_signedIn.value = login.isSignedIn
|
||||
}
|
||||
|
||||
/** Sends this side's changes, then brings the account's here; [plan]'s conflicts settled. Main thread. */
|
||||
suspend fun finishSync(plan: MyTeamSpeakSync.Plan): MyTeamSpeakSync.Result {
|
||||
try {
|
||||
withContext(Dispatchers.IO) { sync.upload(plan) }
|
||||
} finally {
|
||||
_signedIn.value = login.isSignedIn
|
||||
}
|
||||
return sync.apply(plan).also {
|
||||
bookmarks.refresh()
|
||||
identities.refresh()
|
||||
}
|
||||
}
|
||||
|
||||
/** Fetches a file shared in chat whole. */
|
||||
suspend fun downloadAttachment(ref: ChatAttachment.Ref): ByteArray =
|
||||
withContext(Dispatchers.IO) { attachments.download(ref, ChatAttachments.MAX_UPLOAD_BYTES) }
|
||||
|
||||
@@ -46,6 +46,7 @@ import eu.lixko.ts3j.client.android.MyTeamSpeakAccount
|
||||
import eu.lixko.ts3j.client.config.Bookmark
|
||||
import eu.lixko.ts3j.client.myts.AccountInfo
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeak
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeakSync
|
||||
import eu.lixko.ts3j.client.myts.MyTsException
|
||||
import eu.lixko.ts3j.client.teamspeak.SyncItem
|
||||
import eu.lixko.ts3j.client.teamspeak.TeamSpeakImporter
|
||||
@@ -58,8 +59,8 @@ import kotlinx.coroutines.launch
|
||||
private data class ItemRow(val item: SyncItem, val imported: Boolean, val selected: Boolean)
|
||||
|
||||
/**
|
||||
* The myTeamSpeak account: sign in and out, and pick what the account synchronises to
|
||||
* import. Signing in lasts until signing out; the password itself is not kept.
|
||||
* The myTeamSpeak account: sign in and out, pick what the account synchronises to import,
|
||||
* or synchronise both ways. Signing in lasts until signing out; the password itself is not kept.
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@@ -75,7 +76,7 @@ fun MyTeamSpeakScreen(account: MyTeamSpeakAccount, onBack: () -> Unit) {
|
||||
var info by remember { mutableStateOf<AccountInfo?>(null) }
|
||||
var all by remember { mutableStateOf(emptyList<SyncItem>()) }
|
||||
var rows by remember { mutableStateOf(emptyList<ItemRow>()) }
|
||||
BackHandler(onBack = onBack)
|
||||
var conflicted by remember { mutableStateOf<MyTeamSpeakSync.Plan?>(null) }
|
||||
|
||||
fun show(fetched: MyTeamSpeak.Account) {
|
||||
info = fetched.info()
|
||||
@@ -107,10 +108,26 @@ fun MyTeamSpeakScreen(account: MyTeamSpeakAccount, onBack: () -> Unit) {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun finishSync(plan: MyTeamSpeakSync.Plan) {
|
||||
val r = account.finishSync(plan)
|
||||
rows = rows.map { it.copy(imported = account.isImported(it.item)) }
|
||||
message = if (r.sent() == 0 && r.received() == 0) "In sync."
|
||||
else "Synchronized: ${r.sent()} sent, ${r.received()} received."
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
if (signedIn) work { show(account.fetch()) }
|
||||
}
|
||||
|
||||
conflicted?.let { plan ->
|
||||
SyncConflictsScreen(plan.conflicts()) { accepted ->
|
||||
conflicted = null
|
||||
if (accepted) work { finishSync(plan) } else message = "Synchronization cancelled."
|
||||
}
|
||||
return
|
||||
}
|
||||
BackHandler(onBack = onBack)
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
@@ -214,21 +231,31 @@ fun MyTeamSpeakScreen(account: MyTeamSpeakAccount, onBack: () -> Unit) {
|
||||
)
|
||||
}
|
||||
}
|
||||
if (rows.isNotEmpty()) {
|
||||
item {
|
||||
Button(
|
||||
item {
|
||||
Row(Modifier.padding(horizontal = 16.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
if (rows.isNotEmpty()) {
|
||||
Button(
|
||||
onClick = {
|
||||
work {
|
||||
val r = account.import(all, rows.filter { it.selected }.map { it.item })
|
||||
rows = rows.map { it.copy(imported = account.isImported(it.item), selected = false) }
|
||||
message = "Imported ${count(r.identitiesAdded(), "identity", "identities")}, " +
|
||||
"${count(r.bookmarksAdded(), "bookmark", "bookmarks")} and " +
|
||||
"${count(r.contactsAdded(), "contact", "contacts")}."
|
||||
}
|
||||
},
|
||||
enabled = !busy && rows.any { it.selected },
|
||||
) { Text("Import selected") }
|
||||
}
|
||||
OutlinedButton(
|
||||
onClick = {
|
||||
work {
|
||||
val r = account.import(all, rows.filter { it.selected }.map { it.item })
|
||||
rows = rows.map { it.copy(imported = account.isImported(it.item), selected = false) }
|
||||
message = "Imported ${count(r.identitiesAdded(), "identity", "identities")}, " +
|
||||
"${count(r.bookmarksAdded(), "bookmark", "bookmarks")} and " +
|
||||
"${count(r.contactsAdded(), "contact", "contacts")}."
|
||||
val plan = account.planSync()
|
||||
if (plan.conflicts().isEmpty()) finishSync(plan) else conflicted = plan
|
||||
}
|
||||
},
|
||||
enabled = !busy && rows.any { it.selected },
|
||||
modifier = Modifier.padding(horizontal = 16.dp),
|
||||
) { Text("Import selected") }
|
||||
enabled = !busy,
|
||||
) { Text("Synchronize") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
package eu.lixko.ts3j.client.android.ui
|
||||
|
||||
import androidx.activity.compose.BackHandler
|
||||
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.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.navigationBarsPadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.selection.selectable
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.RadioButton
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateMapOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import eu.lixko.ts3j.client.myts.MyTeamSpeakSync.Conflict
|
||||
|
||||
/**
|
||||
* Which copy to keep of each item changed both here and in myTeamSpeak since the last
|
||||
* synchronisation: the item's fields side by side, the ones that differ in bold.
|
||||
* [onDone] gets whether the user went on; the choices are recorded on the conflicts.
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun SyncConflictsScreen(conflicts: List<Conflict>, onDone: (Boolean) -> Unit) {
|
||||
// Mirrors the choices recorded on the conflicts, for recomposition.
|
||||
val choices = remember(conflicts) {
|
||||
mutableStateMapOf<Int, Conflict.Choice>().apply {
|
||||
conflicts.forEachIndexed { i, c -> c.choice()?.let { put(i, it) } }
|
||||
}
|
||||
}
|
||||
fun choose(index: Int, choice: Conflict.Choice) {
|
||||
conflicts[index].choose(choice)
|
||||
choices[index] = choice
|
||||
}
|
||||
BackHandler { onDone(false) }
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = { Text("Synchronization conflicts") },
|
||||
navigationIcon = { IconButton(onClick = { onDone(false) }) { Icon(Icons.Filled.Close, "Cancel") } },
|
||||
)
|
||||
},
|
||||
bottomBar = {
|
||||
Surface(tonalElevation = 3.dp) {
|
||||
Row(
|
||||
Modifier.fillMaxWidth().navigationBarsPadding().padding(horizontal = 8.dp, vertical = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Column(Modifier.weight(1f)) {
|
||||
TextButton(onClick = { conflicts.indices.forEach { choose(it, Conflict.Choice.HERE) } }) {
|
||||
Text("Keep all of this device's")
|
||||
}
|
||||
TextButton(onClick = { conflicts.indices.forEach { choose(it, Conflict.Choice.CLOUD) } }) {
|
||||
Text("Keep all of myTeamSpeak's")
|
||||
}
|
||||
}
|
||||
Button(onClick = { onDone(true) }, enabled = choices.size == conflicts.size) { Text("Synchronize") }
|
||||
}
|
||||
}
|
||||
},
|
||||
) { padding ->
|
||||
LazyColumn(
|
||||
Modifier.padding(padding),
|
||||
contentPadding = PaddingValues(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
item { Text("Changed here and in myTeamSpeak since the last synchronization:") }
|
||||
itemsIndexed(conflicts) { index, conflict ->
|
||||
ConflictCard(conflict, choices[index]) { choose(index, it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ConflictCard(conflict: Conflict, choice: Conflict.Choice?, onChoose: (Conflict.Choice) -> Unit) {
|
||||
Card(Modifier.fillMaxWidth()) {
|
||||
Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
Text((if (conflict.isIdentity) "Identity: " else "Bookmark: ") + conflict.title(),
|
||||
style = MaterialTheme.typography.titleMedium)
|
||||
Row {
|
||||
Spacer(0.3f)
|
||||
Header(if (conflict.deletedHere()) "This device (deleted)" else "This device")
|
||||
Header(if (conflict.deletedInCloud()) "myTeamSpeak (deleted)" else "myTeamSpeak")
|
||||
}
|
||||
for (d in conflict.differences()) {
|
||||
Row {
|
||||
Text(d.label(), Modifier.weight(0.3f), style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
Value(if (conflict.deletedHere()) "—" else d.here(), d.differs())
|
||||
Value(if (conflict.deletedInCloud()) "—" else d.cloud(), d.differs())
|
||||
}
|
||||
}
|
||||
Option(if (conflict.deletedHere()) "Delete" else "Keep this device's", choice == Conflict.Choice.HERE) {
|
||||
onChoose(Conflict.Choice.HERE)
|
||||
}
|
||||
Option(if (conflict.deletedInCloud()) "Delete" else "Keep myTeamSpeak's", choice == Conflict.Choice.CLOUD) {
|
||||
onChoose(Conflict.Choice.CLOUD)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun androidx.compose.foundation.layout.RowScope.Spacer(weight: Float) {
|
||||
androidx.compose.foundation.layout.Spacer(Modifier.weight(weight))
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun androidx.compose.foundation.layout.RowScope.Header(text: String) {
|
||||
Text(text, Modifier.weight(0.35f).padding(end = 4.dp), style = MaterialTheme.typography.labelMedium)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun androidx.compose.foundation.layout.RowScope.Value(text: String, differs: Boolean) {
|
||||
Text(text, Modifier.weight(0.35f).padding(end = 4.dp), style = MaterialTheme.typography.bodySmall,
|
||||
fontWeight = if (differs) FontWeight.Bold else FontWeight.Normal)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun Option(text: String, selected: Boolean, onClick: () -> Unit) {
|
||||
Row(
|
||||
Modifier.fillMaxWidth().selectable(selected, onClick = onClick, role = Role.RadioButton),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
RadioButton(selected, onClick = null)
|
||||
Text(text, Modifier.padding(start = 8.dp))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user