This commit is contained in:
1 parent
1dcbffd6f2
commit
d1c601a6e2
11 files changed
+84
-39
No files matched your search
@@ -56,6 +56,10 @@
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent>
|
||||
<!-- Sync adapters to ask for a calendar sync (#240, #303). -->
|
||||
<intent>
|
||||
<action android:name="android.content.SyncAdapter" />
|
||||
</intent>
|
||||
</queries>
|
||||
|
||||
<application
|
||||
|
||||
+28
-15
@@ -111,7 +111,7 @@ class CalendarRepositoryImpl @Inject constructor(
|
||||
try {
|
||||
calendarIds().filterNotNull().distinct()
|
||||
.mapNotNull { accountOf(it) }
|
||||
.filterTo(linkedSetOf()) { canSync(it) }
|
||||
.withAdapter()
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
@@ -125,11 +125,17 @@ class CalendarRepositoryImpl @Inject constructor(
|
||||
?.let { SyncAccount(it.accountName, it.accountType) }
|
||||
?: dataSource.calendarAccount(calendarId)
|
||||
|
||||
private fun canSync(account: SyncAccount): Boolean = account.isSyncable && try {
|
||||
syncRequester.hasAdapter(account.type)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Sync adapters unreadable", e)
|
||||
false
|
||||
/** The syncable accounts among these whose type has a calendar sync adapter. */
|
||||
private fun Collection<SyncAccount>.withAdapter(): Set<SyncAccount> {
|
||||
val candidates = filter { it.isSyncable }
|
||||
if (candidates.isEmpty()) return emptySet()
|
||||
val adapterTypes = try {
|
||||
syncRequester.adapterAccountTypes()
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Sync adapters unreadable", e)
|
||||
return emptySet()
|
||||
}
|
||||
return candidates.filterTo(linkedSetOf()) { it.type in adapterTypes }
|
||||
}
|
||||
|
||||
private fun requestSync(account: SyncAccount, userInitiated: Boolean): Boolean = try {
|
||||
@@ -140,11 +146,16 @@ class CalendarRepositoryImpl @Inject constructor(
|
||||
false
|
||||
}
|
||||
|
||||
private fun eventCalendar(eventId: Long, original: EventForm? = null): suspend () -> Collection<Long?> =
|
||||
{ listOf(original?.calendarId ?: dataSource.eventCalendarId(eventId)) }
|
||||
/** The event's own calendar (from [original] when given, else read), plus [others]. */
|
||||
private fun eventCalendar(
|
||||
eventId: Long,
|
||||
original: EventForm? = null,
|
||||
vararg others: Long?,
|
||||
): suspend () -> Collection<Long?> =
|
||||
{ listOf(original?.calendarId ?: dataSource.eventCalendarId(eventId), *others) }
|
||||
|
||||
private fun syncTargets(calendars: List<CalendarSource>): Set<SyncAccount> =
|
||||
calendars.syncAccounts().filterTo(linkedSetOf(), ::canSync)
|
||||
calendars.syncAccounts().withAdapter()
|
||||
|
||||
override val canRequestSync: Flow<Boolean>
|
||||
get() = calendars().map { syncTargets(it).isNotEmpty() }.distinctUntilChanged().flowOn(io)
|
||||
@@ -158,10 +169,12 @@ class CalendarRepositoryImpl @Inject constructor(
|
||||
Log.w(TAG, "Sync request skipped: calendars unreadable", e)
|
||||
return@withContext SyncRequestResult.Failed
|
||||
}
|
||||
when {
|
||||
accounts.isEmpty() -> SyncRequestResult.NothingToSync
|
||||
accounts.count { requestSync(it, userInitiated = true) } > 0 -> SyncRequestResult.Requested
|
||||
else -> SyncRequestResult.Failed
|
||||
if (accounts.isEmpty()) return@withContext SyncRequestResult.NothingToSync
|
||||
val sent = accounts.count { requestSync(it, userInitiated = true) }
|
||||
when (sent) {
|
||||
accounts.size -> SyncRequestResult.Requested
|
||||
0 -> SyncRequestResult.Failed
|
||||
else -> SyncRequestResult.PartlyFailed
|
||||
}
|
||||
}
|
||||
|
||||
@@ -383,7 +396,7 @@ class CalendarRepositoryImpl @Inject constructor(
|
||||
eventId: Long,
|
||||
original: EventForm,
|
||||
updated: EventForm,
|
||||
) = write({ listOf(original.calendarId ?: dataSource.eventCalendarId(eventId), updated.calendarId) }) {
|
||||
) = write(eventCalendar(eventId, original, updated.calendarId)) {
|
||||
dataSource.updateEvent(eventId, original, updated, allDayReminderTimeMinutes())
|
||||
}
|
||||
|
||||
@@ -396,7 +409,7 @@ class CalendarRepositoryImpl @Inject constructor(
|
||||
targetCalendarId: Long,
|
||||
original: EventForm,
|
||||
updated: EventForm,
|
||||
): Long = write({ listOf(original.calendarId ?: dataSource.eventCalendarId(eventId), targetCalendarId) }) {
|
||||
): Long = write(eventCalendar(eventId, original, targetCalendarId)) {
|
||||
dataSource.moveEvent(
|
||||
eventId, targetCalendarId, original, updated, allDayReminderTimeMinutes(),
|
||||
)
|
||||
|
||||
@@ -20,8 +20,8 @@ data class SyncAccount(val name: String, val type: String) {
|
||||
* itself stays the adapter's job; this only schedules it.
|
||||
*/
|
||||
interface SyncRequester {
|
||||
/** Whether a calendar sync adapter is installed for [accountType]; without one a request does nothing. */
|
||||
fun hasAdapter(accountType: String): Boolean
|
||||
/** Account types with a calendar sync adapter installed; for any other a request does nothing. */
|
||||
fun adapterAccountTypes(): Set<String>
|
||||
|
||||
/**
|
||||
* Schedule a sync of [account]; may throw if the system refuses. [userInitiated]
|
||||
@@ -32,10 +32,11 @@ interface SyncRequester {
|
||||
|
||||
@Singleton
|
||||
class AndroidSyncRequester @Inject constructor() : SyncRequester {
|
||||
override fun hasAdapter(accountType: String): Boolean =
|
||||
ContentResolver.getSyncAdapterTypes().any {
|
||||
it.authority == CalendarContract.AUTHORITY && it.accountType == accountType
|
||||
}
|
||||
// Lists only adapters in packages visible to the app — hence the SyncAdapter <queries> entry.
|
||||
override fun adapterAccountTypes(): Set<String> =
|
||||
ContentResolver.getSyncAdapterTypes()
|
||||
.filter { it.authority == CalendarContract.AUTHORITY }
|
||||
.mapTo(mutableSetOf()) { it.accountType }
|
||||
|
||||
override fun requestSync(account: SyncAccount, userInitiated: Boolean) {
|
||||
val extras = Bundle().apply {
|
||||
@@ -57,4 +58,4 @@ fun List<CalendarSource>.syncAccounts(): Set<SyncAccount> =
|
||||
.filterTo(linkedSetOf()) { it.isSyncable }
|
||||
|
||||
/** Outcome of a user-triggered sync request (#240). */
|
||||
enum class SyncRequestResult { Requested, NothingToSync, Failed }
|
||||
enum class SyncRequestResult { Requested, PartlyFailed, NothingToSync, Failed }
|
||||
@@ -701,8 +701,8 @@ fun CalendarHost(
|
||||
EventMoveHost(
|
||||
reschedule,
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
actionFailure = selectionFailureMessage ?: syncMessage,
|
||||
onActionFailureShown = {
|
||||
notice = selectionFailureMessage ?: syncMessage,
|
||||
onNoticeShown = {
|
||||
if (selectionFailureMessage != null) eventActions.consumeFailure() else sync.consumeResult()
|
||||
},
|
||||
)
|
||||
|
||||
@@ -41,6 +41,8 @@ import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.res.colorResource
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.ui.semantics.stateDescription
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.jeanlucmakiola.calendula.R
|
||||
import de.jeanlucmakiola.calendula.ui.filter.CalendarFilterList
|
||||
@@ -150,9 +152,16 @@ data class DrawerSync(val inFlight: Boolean, val onSync: () -> Unit)
|
||||
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
|
||||
@Composable
|
||||
private fun SyncNowRow(sync: DrawerSync) {
|
||||
val inFlightText = stringResource(R.string.sync_requested)
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.drawer_sync_now),
|
||||
position = Position.Bottom,
|
||||
modifier = if (sync.inFlight) {
|
||||
Modifier.semantics { stateDescription = inFlightText }
|
||||
} else {
|
||||
Modifier
|
||||
},
|
||||
dimmed = sync.inFlight,
|
||||
minHeight = 56.dp,
|
||||
leading = { DrawerLeadingIcon(Icons.Filled.Sync) },
|
||||
trailing = if (sync.inFlight) {
|
||||
|
||||
@@ -51,8 +51,8 @@ private val FAB_BAND = 88.dp
|
||||
fun EventMoveHost(
|
||||
viewModel: RescheduleViewModel,
|
||||
modifier: Modifier = Modifier,
|
||||
actionFailure: String? = null,
|
||||
onActionFailureShown: () -> Unit = {},
|
||||
notice: String? = null,
|
||||
onNoticeShown: () -> Unit = {},
|
||||
) {
|
||||
val prompt by viewModel.scopePrompt.collectAsStateWithLifecycle()
|
||||
val outcome by viewModel.outcome.collectAsStateWithLifecycle()
|
||||
@@ -81,7 +81,7 @@ fun EventMoveHost(
|
||||
}
|
||||
// A failed selection action (#141) shares the chip rather than stacking on it.
|
||||
val message = when (outcome) {
|
||||
null -> actionFailure
|
||||
null -> notice
|
||||
is MoveOutcome.Moved -> if (resizedLabel != null) {
|
||||
stringResource(R.string.event_resize_done, resizedLabel)
|
||||
} else {
|
||||
@@ -116,10 +116,10 @@ fun EventMoveHost(
|
||||
viewModel.consumeOutcome()
|
||||
}
|
||||
// A failure waits for the chip to be free, then has its own turn.
|
||||
LaunchedEffect(actionFailure, outcome == null) {
|
||||
if (actionFailure == null || outcome != null) return@LaunchedEffect
|
||||
LaunchedEffect(notice, outcome == null) {
|
||||
if (notice == null || outcome != null) return@LaunchedEffect
|
||||
delay(CHIP_MILLIS)
|
||||
onActionFailureShown()
|
||||
onNoticeShown()
|
||||
}
|
||||
|
||||
BoxWithConstraints(modifier = modifier.fillMaxSize()) {
|
||||
@@ -138,7 +138,7 @@ fun EventMoveHost(
|
||||
// write anyway, so offering it would be a button that does nothing.
|
||||
val undo = content.undo?.takeIf { !writeInFlight }
|
||||
SnackChip(
|
||||
visible = outcome != null || actionFailure != null,
|
||||
visible = outcome != null || notice != null,
|
||||
message = content.message,
|
||||
maxWidth = chipMaxWidth,
|
||||
actionLabel = stringResource(R.string.event_move_undo).takeIf { undo != null },
|
||||
|
||||
@@ -46,6 +46,8 @@ class SyncViewModel @Inject constructor(
|
||||
fun requestSync() {
|
||||
if (_inFlight.value) return
|
||||
_inFlight.value = true
|
||||
// A repeat receipt must land as a new one, restarting the chip.
|
||||
_result.value = null
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
// Listening before asking, so a sync that lands at once still counts.
|
||||
@@ -54,7 +56,7 @@ class SyncViewModel @Inject constructor(
|
||||
}
|
||||
val outcome = repository.requestSync()
|
||||
_result.value = outcome
|
||||
if (outcome == SyncRequestResult.Requested) settled.await() else settled.cancel()
|
||||
if (outcome == SyncRequestResult.Requested || outcome == SyncRequestResult.PartlyFailed) settled.await() else settled.cancel()
|
||||
} finally {
|
||||
_inFlight.value = false
|
||||
}
|
||||
@@ -74,6 +76,7 @@ class SyncViewModel @Inject constructor(
|
||||
@StringRes
|
||||
fun SyncRequestResult.messageRes(): Int = when (this) {
|
||||
SyncRequestResult.Requested -> R.string.sync_requested
|
||||
SyncRequestResult.PartlyFailed -> R.string.sync_request_partly_failed
|
||||
SyncRequestResult.NothingToSync -> R.string.sync_nothing_to_sync
|
||||
SyncRequestResult.Failed -> R.string.sync_request_failed
|
||||
}
|
||||
@@ -398,6 +398,7 @@
|
||||
<string name="drawer_sync_now">Sync now</string>
|
||||
<string name="sync_requested">Sync requested</string>
|
||||
<string name="sync_request_failed">Couldn\'t request a sync</string>
|
||||
<string name="sync_request_partly_failed">Sync requested, but not for every account</string>
|
||||
<string name="sync_nothing_to_sync">No synced calendars to sync</string>
|
||||
|
||||
<!-- Agenda view -->
|
||||
|
||||
@@ -49,22 +49,24 @@ fun testRepository(
|
||||
): CalendarRepositoryImpl = CalendarRepositoryImpl(source, prefs, settings, io, syncRequester)
|
||||
|
||||
/**
|
||||
* Records every sync request; [accepts] false models the system refusing one,
|
||||
* [adapterTypes] the installed calendar sync adapters (null: every type has one).
|
||||
* Records every sync request; [accepts] false models the system refusing all,
|
||||
* [refusedTypes] refusing those account types, [adapterTypes] the installed
|
||||
* calendar sync adapters (null: a fixed set covering the test accounts).
|
||||
*/
|
||||
class RecordingSyncRequester(
|
||||
var accepts: Boolean = true,
|
||||
var adapterTypes: Set<String>? = null,
|
||||
var refusedTypes: Set<String> = emptySet(),
|
||||
) : SyncRequester {
|
||||
data class Request(val account: SyncAccount, val userInitiated: Boolean)
|
||||
|
||||
val requests = mutableListOf<Request>()
|
||||
|
||||
override fun hasAdapter(accountType: String): Boolean =
|
||||
adapterTypes?.contains(accountType) ?: true
|
||||
override fun adapterAccountTypes(): Set<String> =
|
||||
adapterTypes ?: setOf("bitfire.at.davdroid", "com.google", "LOCAL")
|
||||
|
||||
override fun requestSync(account: SyncAccount, userInitiated: Boolean) {
|
||||
if (!accepts) throw SecurityException("refused")
|
||||
if (!accepts || account.type in refusedTypes) throw SecurityException("refused")
|
||||
requests += Request(account, userInitiated)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,4 +211,13 @@ class SyncNudgeTest {
|
||||
sync.adapterTypes = setOf(dav.type)
|
||||
assertThat(repo.canRequestSync.first()).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `manual sync refused for some accounts says so`(@TempDir dir: Path) = runTest {
|
||||
val sync = RecordingSyncRequester(refusedTypes = setOf(google.type))
|
||||
val repo = testRepository(fake(), dir, Dispatchers.Unconfined, syncRequester = sync)
|
||||
|
||||
assertThat(repo.requestSync()).isEqualTo(SyncRequestResult.PartlyFailed)
|
||||
assertThat(sync.requests).containsExactly(Request(dav, true))
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,11 @@ the package list (recurring writes, save conflicts, reminder delivery).
|
||||
expedited nudge for the account after each of its own writes to a synced
|
||||
calendar (#303, once per write, so a multi-row save or an import asks once
|
||||
per account, and never `MANUAL`, which would override master-sync-off), and
|
||||
a manual request from the drawer's "Sync now" (#240). Both go through
|
||||
`SyncRequester`; whether and when the adapter syncs stays its call.
|
||||
a manual request from the drawer's "Sync now" (#240), which like the
|
||||
system's own "Sync now" also runs for accounts whose auto-sync is off. Both
|
||||
go through `SyncRequester` and only ask account types with a calendar sync
|
||||
adapter (visible to the app via a `<queries>` entry); whether and when the
|
||||
adapter syncs stays its call.
|
||||
2. **Observer-driven UI.** A `ContentObserver` on the provider triggers
|
||||
re-queries; every screen recomposes from fresh provider state. After a
|
||||
write, nothing is patched by hand — the views refresh from the provider.
|
||||
|
||||
Reference in new issue
Block a user