Skip to content

Commit

Permalink
[WIP] Show only personal as flow
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Jan 14, 2025
1 parent f3333b7 commit ad96c46
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,15 @@ class AccountSettings @AssistedInject constructor(

// UI settings

data class ShowOnlyPersonal(
val onlyPersonal: Boolean,
val locked: Boolean
)

fun getShowOnlyPersonal(): ShowOnlyPersonal {
fun getShowOnlyPersonal(): Boolean {
@Suppress("DEPRECATION")
val pair = getShowOnlyPersonalPair()
return pair.first
}
fun getShowOnlyPersonalLocked(): Boolean {
@Suppress("DEPRECATION")
val pair = getShowOnlyPersonalPair()
return ShowOnlyPersonal(onlyPersonal = pair.first, locked = !pair.second)
return !pair.second
}

/**
Expand Down
24 changes: 13 additions & 11 deletions app/src/main/kotlin/at/bitfire/davdroid/ui/account/AccountScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.ui.AppTheme
import at.bitfire.davdroid.ui.PermissionsActivity
import at.bitfire.davdroid.ui.account.AccountProgress
Expand Down Expand Up @@ -114,9 +113,8 @@ fun AccountScreen(
error = model.error,
onResetError = model::resetError,
invalidAccount = model.invalidAccount.collectAsStateWithLifecycle(false).value,
showOnlyPersonal = model.showOnlyPersonal.collectAsStateWithLifecycle(
initialValue = AccountSettings.ShowOnlyPersonal(onlyPersonal = false, locked = false)
).value,
showOnlyPersonal = model.showOnlyPersonal.collectAsStateWithLifecycle().value,
showOnlyPersonalLocked = model.showOnlyPersonalLocked.collectAsStateWithLifecycle().value,
onSetShowOnlyPersonal = model::setShowOnlyPersonal,
hasCardDav = cardDavService != null,
canCreateAddressBook = model.canCreateAddressBook.collectAsStateWithLifecycle(false).value,
Expand Down Expand Up @@ -169,7 +167,8 @@ fun AccountScreen(
error: String? = null,
onResetError: () -> Unit = {},
invalidAccount: Boolean = false,
showOnlyPersonal: AccountSettings.ShowOnlyPersonal,
showOnlyPersonal: Boolean = false,
showOnlyPersonalLocked: Boolean = false,
onSetShowOnlyPersonal: (showOnlyPersonal: Boolean) -> Unit = {},
hasCardDav: Boolean,
canCreateAddressBook: Boolean,
Expand Down Expand Up @@ -257,6 +256,7 @@ fun AccountScreen(
canCreateCalendar = canCreateCalendar,
onCreateCalendar = onCreateCalendar,
showOnlyPersonal = showOnlyPersonal,
showOnlyPersonalLocked = showOnlyPersonalLocked,
onSetShowOnlyPersonal = onSetShowOnlyPersonal,
currentPage = pagerState.currentPage,
idxCardDav = idxCardDav,
Expand Down Expand Up @@ -440,7 +440,8 @@ fun AccountScreen_Actions(
onCreateAddressBook: () -> Unit,
canCreateCalendar: Boolean,
onCreateCalendar: () -> Unit,
showOnlyPersonal: AccountSettings.ShowOnlyPersonal,
showOnlyPersonal: Boolean,
showOnlyPersonalLocked: Boolean,
onSetShowOnlyPersonal: (showOnlyPersonal: Boolean) -> Unit,
currentPage: Int,
idxCardDav: Int?,
Expand Down Expand Up @@ -513,8 +514,8 @@ fun AccountScreen_Actions(
LocalMinimumInteractiveComponentSize provides Dp.Unspecified
) {
Checkbox(
checked = showOnlyPersonal.onlyPersonal,
enabled = !showOnlyPersonal.locked,
checked = showOnlyPersonal,
enabled = !showOnlyPersonalLocked,
onCheckedChange = {
onSetShowOnlyPersonal(it)
overflowOpen = false
Expand All @@ -527,10 +528,10 @@ fun AccountScreen_Actions(
Text(stringResource(R.string.account_only_personal))
},
onClick = {
onSetShowOnlyPersonal(!showOnlyPersonal.onlyPersonal)
onSetShowOnlyPersonal(!showOnlyPersonal)
overflowOpen = false
},
enabled = !showOnlyPersonal.locked
enabled = !showOnlyPersonalLocked
)

// rename account
Expand Down Expand Up @@ -651,7 +652,8 @@ fun AccountScreen_ServiceTab(
fun AccountScreen_Preview() {
AccountScreen(
accountName = "test@example.com",
showOnlyPersonal = AccountSettings.ShowOnlyPersonal(onlyPersonal = false, locked = true),
showOnlyPersonal = false,
showOnlyPersonalLocked = true,
hasCardDav = true,
canCreateAddressBook = false,
cardDavProgress = AccountProgress.Active,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ import android.content.Context
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asFlow
import androidx.lifecycle.switchMap
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.repository.AccountRepository
Expand All @@ -34,10 +31,13 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.logging.Level
import java.util.logging.Logger

Expand All @@ -62,26 +62,33 @@ class AccountScreenModel @AssistedInject constructor(
fun create(account: Account): AccountScreenModel
}

private val _showOnlyPersonal = MutableStateFlow(false)
val showOnlyPersonal = _showOnlyPersonal.asStateFlow()

private var _showOnlyPersonalLocked = MutableStateFlow(false)
val showOnlyPersonalLocked = _showOnlyPersonalLocked.asStateFlow()

/**
* Only acquire account settings on a worker thread!
*/
private val accountSettings by lazy { accountSettingsFactory.create(account) }

private suspend fun reload() = withContext(Dispatchers.Default) {
_showOnlyPersonal.value = accountSettings.getShowOnlyPersonal()
_showOnlyPersonalLocked.value = accountSettings.getShowOnlyPersonalLocked()
}

/** whether the account is invalid and the screen shall be closed */
val invalidAccount = accountRepository.getAllFlow().map { accounts ->
!accounts.contains(account)
}

private val refreshSettingsSignal = MutableLiveData(Unit)
val showOnlyPersonal = refreshSettingsSignal.switchMap<Unit, AccountSettings.ShowOnlyPersonal> {
object : LiveData<AccountSettings.ShowOnlyPersonal>() {
init {
viewModelScope.launch(Dispatchers.IO) {
val settings = accountSettingsFactory.create(account)
postValue(settings.getShowOnlyPersonal())
}
}
}
}.asFlow()
fun setShowOnlyPersonal(showOnlyPersonal: Boolean) = viewModelScope.launch(Dispatchers.IO) {
val settings = accountSettingsFactory.create(account)
settings.setShowOnlyPersonal(showOnlyPersonal)
refreshSettingsSignal.postValue(Unit)
fun setShowOnlyPersonal(showOnlyPersonal: Boolean) {
// CoroutineScope(Dispatchers.Default).launch {
// accountSettings.setShowOnlyPersonal(showOnlyPersonal)
_showOnlyPersonal.value = showOnlyPersonal
// reload()
// }
}

val cardDavSvc = serviceRepository
Expand All @@ -97,6 +104,7 @@ class AccountScreenModel @AssistedInject constructor(
dataTypes = listOf(SyncDataType.CONTACTS)
)
val addressBooks = getServiceCollectionPager(cardDavSvc, Collection.TYPE_ADDRESSBOOK, showOnlyPersonal)
.stateIn(viewModelScope, SharingStarted.Eagerly, PagingData.empty())

val calDavSvc = serviceRepository
.getCalDavServiceFlow(account.name)
Expand All @@ -112,7 +120,9 @@ class AccountScreenModel @AssistedInject constructor(
dataTypes = listOf(SyncDataType.EVENTS, SyncDataType.TASKS)
)
val calendars = getServiceCollectionPager(calDavSvc, Collection.TYPE_CALENDAR, showOnlyPersonal)
.stateIn(viewModelScope, SharingStarted.Eagerly, PagingData.empty())
val subscriptions = getServiceCollectionPager(calDavSvc, Collection.TYPE_WEBCAL, showOnlyPersonal)
.stateIn(viewModelScope, SharingStarted.Eagerly, PagingData.empty())


var error by mutableStateOf<String?>(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import androidx.paging.map
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.db.Service
import at.bitfire.davdroid.repository.DavCollectionRepository
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.settings.Settings
import at.bitfire.davdroid.settings.SettingsManager
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flattenConcat
import kotlinx.coroutines.flow.flowOf
Expand All @@ -41,9 +41,9 @@ class GetServiceCollectionPagerUseCase @Inject constructor(

@OptIn(ExperimentalCoroutinesApi::class)
operator fun invoke(
serviceFlow: Flow<Service?>,
serviceFlow: StateFlow<Service?>,
collectionType: String,
showOnlyPersonalFlow: Flow<AccountSettings.ShowOnlyPersonal?>
showOnlyPersonalFlow: StateFlow<Boolean>
): Flow<PagingData<Collection>> =
combine(serviceFlow, showOnlyPersonalFlow, forceReadOnlyAddressBooksFlow) { service, onlyPersonal, forceReadOnlyAddressBooks ->
if (service == null)
Expand All @@ -52,7 +52,7 @@ class GetServiceCollectionPagerUseCase @Inject constructor(
val dataFlow = Pager(
config = PagingConfig(PAGER_SIZE),
pagingSourceFactory = {
if (onlyPersonal?.onlyPersonal == true)
if (onlyPersonal == true)
collectionRepository.pagePersonalByServiceAndType(service.id, collectionType)
else
collectionRepository.pageByServiceAndType(service.id, collectionType)
Expand Down

0 comments on commit ad96c46

Please sign in to comment.