Skip to content

Commit

Permalink
saving
Browse files Browse the repository at this point in the history
  • Loading branch information
John Oberhauser committed Jun 20, 2024
1 parent cdf53ec commit 5f0e39d
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,27 @@ class AccountsManager(
)
)
}

suspend fun setActiveAccount(
mastodonAccount: MastodonAccount,
) {
activeAccountDao.upsert(
ActiveAccount(
accountType = AccountType.MASTODON,
accountId = mastodonAccount.accountId,
domain = mastodonAccount.domain,
)
)
}

suspend fun updateLastSeenHomeStatusId(
mastodonAccount: MastodonAccount,
lastSeenStatusId: String,
) {
mastodonAccountsDao.upsert(
mastodonAccount.copy(
lastSeenHomeStatusId = lastSeenStatusId,
)
)
}
}
2 changes: 1 addition & 1 deletion core/repository/paging/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
implementation(project(":core:usecase:mastodon"))
implementation(project(":core:common"))
implementation(project(":core:model"))
implementation(project(":core:datastore"))
implementation(project(":core:accounts"))

implementation(libs.androidx.paging.runtime)
implementation(libs.jakewharton.timber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadType
import androidx.paging.PagingState
import androidx.paging.RemoteMediator
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.launch
import social.firefly.common.Rel
import social.firefly.core.accounts.AccountsManager
import social.firefly.core.database.model.entities.statusCollections.HomeTimelineStatusWrapper
import social.firefly.core.datastore.UserPreferencesDatastoreManager
import social.firefly.core.model.Status
import social.firefly.core.model.paging.MastodonPagedResponse
import social.firefly.core.repository.mastodon.DatabaseDelegate
Expand All @@ -30,7 +23,7 @@ class HomeTimelineRemoteMediator(
private val saveStatusToDatabase: SaveStatusToDatabase,
private val databaseDelegate: DatabaseDelegate,
private val getInReplyToAccountNames: GetInReplyToAccountNames,
private val userPreferencesDatastoreManager: UserPreferencesDatastoreManager,
private val accountsManager: AccountsManager,
) : RemoteMediator<Int, HomeTimelineStatusWrapper>() {

private var firstRefreshHasHappened = false
Expand Down Expand Up @@ -110,7 +103,6 @@ class HomeTimelineRemoteMediator(
}
}

@OptIn(ExperimentalCoroutinesApi::class)
private suspend fun fetchRefresh(
state: PagingState<Int, HomeTimelineStatusWrapper>,
): MastodonPagedResponse<Status> {
Expand All @@ -120,18 +112,7 @@ class HomeTimelineRemoteMediator(
// If this is the first time we are loading the page, we need to start where
// the user last left off. Grab the lastSeenHomeStatusId
if (!firstRefreshHasHappened) {
val lastSeenId = CompletableDeferred<String>()
with(CoroutineScope(coroutineContext)) {
launch {
userPreferencesDatastoreManager.activeUserDatastore.flatMapLatest {
it.lastSeenHomeStatusId
}.collectLatest {
lastSeenId.complete(it)
cancel()
}
}
}
olderThanId = lastSeenId.await()
olderThanId = accountsManager.getActiveAccount().lastSeenHomeStatusId
}

val mainResponse = timelineRepository.getHomeTimeline(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package social.firefly.core.usecase.mastodon.account

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flatMapLatest
import social.firefly.core.datastore.UserPreferencesDatastoreManager
import kotlinx.coroutines.flow.mapLatest
import social.firefly.core.accounts.AccountsManager

class GetDomain(
private val userPreferencesDatastoreManager: UserPreferencesDatastoreManager,
private val accountsManager: AccountsManager,
) {
@OptIn(ExperimentalCoroutinesApi::class)
operator fun invoke(): Flow<String> = userPreferencesDatastoreManager.activeUserDatastore.flatMapLatest {
it.domain
}
operator fun invoke(): Flow<String> = accountsManager.getActiveAccountFlow().mapLatest { it.domain }
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package social.firefly.core.usecase.mastodon.account

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.runBlocking
import social.firefly.core.datastore.UserPreferencesDatastoreManager
import social.firefly.core.accounts.AccountsManager

/**
* Synchronously gets the account ID of the current logged in user
*/
class GetLoggedInUserAccountId(
private val userPreferencesDatastoreManager: UserPreferencesDatastoreManager,
private val accountsManager: AccountsManager,
) {
@OptIn(ExperimentalCoroutinesApi::class)
operator fun invoke(): String =
runBlocking {
userPreferencesDatastoreManager.activeUserDatastore.flatMapLatest {
it.accountId
}.first()
accountsManager.getActiveAccount().accountId
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package social.firefly.core.usecase.mastodon.auth

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext
import social.firefly.core.datastore.AppPreferencesDatastore
import social.firefly.core.datastore.UserPreferencesDatastoreManager
import social.firefly.core.accounts.AccountsManager
import social.firefly.core.navigation.NavigationDestination
import social.firefly.core.navigation.usecases.NavigateTo
import social.firefly.core.repository.mastodon.DatabaseDelegate

class SwitchActiveAccount(
private val userPreferencesDatastoreManager: UserPreferencesDatastoreManager,
private val appPreferencesDatastore: AppPreferencesDatastore,
private val databaseDelegate: DatabaseDelegate,
private val navigateTo: NavigateTo,
private val accountsManager: AccountsManager,
){

suspend operator fun invoke(
accountId: String,
domain: String,
) {
userPreferencesDatastoreManager.dataStores.value.find {
it.accountId.first() == accountId && it.domain.first() == domain
accountsManager.getAllAccounts().find {
it.accountId == accountId && it.domain == domain
}?.let {
appPreferencesDatastore.saveActiveUserDatastoreFilename(it.fileName)
accountsManager.setActiveAccount(it)
}

withContext(Dispatchers.IO) {
Expand Down
1 change: 1 addition & 0 deletions feature/feed/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
implementation(project(":core:ui:common"))
implementation(project(":core:ui:postcard"))
implementation(project(":core:usecase:mastodon"))
implementation(project(":core:accounts"))

implementation(libs.androidx.paging.runtime)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import org.koin.core.parameter.parametersOf
import org.koin.java.KoinJavaComponent
import social.firefly.common.utils.edit
import social.firefly.core.accounts.AccountsManager
import social.firefly.core.analytics.FeedAnalytics
import social.firefly.core.analytics.FeedLocation
import social.firefly.core.datastore.UserPreferencesDatastoreManager
import social.firefly.core.navigation.BottomBarNavigationDestination
import social.firefly.core.navigation.usecases.NavigateTo
import social.firefly.core.repository.mastodon.TimelineRepository
Expand All @@ -31,13 +30,13 @@ import social.firefly.core.usecase.mastodon.account.GetLoggedInUserAccountId
@OptIn(ExperimentalPagingApi::class)
class FeedViewModel(
private val analytics: FeedAnalytics,
private val userPreferencesDatastoreManager: UserPreferencesDatastoreManager,
homeTimelineRemoteMediator: HomeTimelineRemoteMediator,
localTimelinePager: LocalTimelinePager,
federatedTimelinePager: FederatedTimelinePager,
private val timelineRepository: TimelineRepository,
getLoggedInUserAccountId: GetLoggedInUserAccountId,
private val navigateTo: NavigateTo,
private val accountsManager: AccountsManager,
) : ViewModel(), FeedInteractions {
private val userAccountId: String = getLoggedInUserAccountId()

Expand Down Expand Up @@ -114,7 +113,10 @@ class FeedViewModel(
// save the last seen status no more than once per x seconds (SAVE_RATE)
if (statusViewedJob == null) {
statusViewedJob = viewModelScope.launch {
userPreferencesDatastoreManager.activeUserDatastore.first().saveLastSeenHomeStatusId(statusId)
accountsManager.updateLastSeenHomeStatusId(
mastodonAccount = accountsManager.getActiveAccount(),
lastSeenStatusId = statusId,
)
delay(SAVE_RATE)
statusViewedJob = null
}
Expand Down
1 change: 1 addition & 0 deletions feature/post/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
implementation(project(":core:analytics"))
implementation(project(":core:ui:htmlcontent"))
implementation(project(":core:share"))
implementation(project(":core:accounts"))

implementation(libs.androidx.navigation.compose)
implementation(libs.koin.core)
Expand Down
1 change: 1 addition & 0 deletions feature/settings/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation(project(":core:ui:htmlcontent"))
implementation(project(":core:usecase:mastodon"))
implementation(project(":core:workmanager"))
implementation(project(":core:accounts"))

implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.datastore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package social.firefly.feature.settings.account
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import social.firefly.core.accounts.AccountsManager
import social.firefly.core.analytics.SettingsAnalytics
import social.firefly.core.datastore.UserPreferencesDatastoreManager
import social.firefly.core.navigation.AuthNavigationDestination
import social.firefly.core.navigation.usecases.NavigateTo
import social.firefly.core.navigation.usecases.OpenLink
Expand All @@ -24,31 +23,31 @@ class AccountSettingsViewModel(
private val navigateTo: NavigateTo,
private val switchActiveAccount: SwitchActiveAccount,
private val logoutOfAllAccounts: LogoutOfAllAccounts,
userPreferencesDatastoreManager: UserPreferencesDatastoreManager,
updateAllLoggedInAccounts: UpdateAllLoggedInAccounts,
private val accountsManager: AccountsManager,
) : ViewModel(), AccountSettingsInteractions {

val otherAccounts = userPreferencesDatastoreManager.dataStores.combine(
userPreferencesDatastoreManager.activeUserDatastore
) { dataStores, activeDataStore ->
dataStores.filterNot {
it == activeDataStore
}.map { dataStore ->
val otherAccounts = accountsManager.getAllAccountsFlow().combine(
accountsManager.getActiveAccountFlow()
) { otherAccounts, activeAccount ->
otherAccounts.filterNot {
it == activeAccount
}.map { account ->
LoggedInAccount(
accountId = dataStore.accountId,
userName = dataStore.userName,
domain = dataStore.domain,
avatarUrl = dataStore.avatarUrl,
accountId = account.accountId,
userName = account.userName,
domain = account.domain,
avatarUrl = account.avatarUrl,
)
}
}

val activeAccount = userPreferencesDatastoreManager.activeUserDatastore.map { dataStore ->
val activeAccount = accountsManager.getActiveAccountFlow().map { account ->
LoggedInAccount(
accountId = dataStore.accountId,
userName = dataStore.userName,
domain = dataStore.domain,
avatarUrl = dataStore.avatarUrl,
accountId = account.accountId,
userName = account.userName,
domain = account.domain,
avatarUrl = account.avatarUrl,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package social.firefly.feature.settings.account

import kotlinx.coroutines.flow.Flow

data class LoggedInAccount(
val accountId: Flow<String>,
val userName: Flow<String>,
val domain: Flow<String>,
val avatarUrl: Flow<String>,
val accountId: String,
val userName: String,
val domain: String,
val avatarUrl: String,
)
1 change: 1 addition & 0 deletions feature/thread/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
implementation(project(":core:common"))
implementation(project(":core:navigation"))
implementation(project(":core:analytics"))
implementation(project(":core:accounts"))

implementation(libs.androidx.paging.runtime)

Expand Down

0 comments on commit 5f0e39d

Please sign in to comment.