diff --git a/.gitignore b/.gitignore index b1880230..6407df8f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ local.properties .idea/* /app/release +/app/schemas/com.github.whitescent.mastify.timeline.TestAppDatabase/* diff --git a/app/src/main/java/com/github/whitescent/mastify/data/repository/AccountRepository.kt b/app/src/main/java/com/github/whitescent/mastify/data/repository/AccountRepository.kt index 85a715ab..96f1610a 100644 --- a/app/src/main/java/com/github/whitescent/mastify/data/repository/AccountRepository.kt +++ b/app/src/main/java/com/github/whitescent/mastify/data/repository/AccountRepository.kt @@ -17,14 +17,24 @@ package com.github.whitescent.mastify.data.repository +import at.connyduck.calladapter.networkresult.getOrThrow import com.github.whitescent.mastify.database.AppDatabase import com.github.whitescent.mastify.database.dao.AccountDao import com.github.whitescent.mastify.database.model.AccountEntity +import com.github.whitescent.mastify.mapper.toEntity +import com.github.whitescent.mastify.network.MastodonApi +import com.github.whitescent.mastify.network.model.account.Account +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.flow import javax.inject.Inject import javax.inject.Singleton @Singleton -class AccountRepository @Inject constructor(db: AppDatabase) { +class AccountRepository @Inject constructor( + db: AppDatabase, + private val api: MastodonApi +) { private val accountDao: AccountDao = db.accountDao() @@ -39,4 +49,30 @@ class AccountRepository @Inject constructor(db: AppDatabase) { suspend fun addAccount(newAccount: AccountEntity) { accountDao.addAccount(newAccount) } + + suspend fun fetchAccountVerifyCredentials(domain: String, token: String): Flow = flow { + emit(api.accountVerifyCredentials(domain, "Bearer $token").getOrThrow()) + } + + suspend fun fetchActiveAccountAndSaveToDatabase() { + val activeAccount = accountDao.getActiveAccount()!! + fetchAccountVerifyCredentials(activeAccount.domain, activeAccount.accessToken) + .catch { + it.printStackTrace() + } + .collect { + updateActiveAccount( + it.toEntity( + accessToken = activeAccount.accessToken, + clientId = activeAccount.clientId, + clientSecret = activeAccount.clientSecret, + isActive = activeAccount.isActive, + accountId = activeAccount.accountId, + id = activeAccount.id, + firstVisibleItemIndex = activeAccount.firstVisibleItemIndex, + offset = activeAccount.offset + ) + ) + } + } } diff --git a/app/src/main/java/com/github/whitescent/mastify/database/model/AccountEntity.kt b/app/src/main/java/com/github/whitescent/mastify/database/model/AccountEntity.kt index e6f15893..74166b2a 100644 --- a/app/src/main/java/com/github/whitescent/mastify/database/model/AccountEntity.kt +++ b/app/src/main/java/com/github/whitescent/mastify/database/model/AccountEntity.kt @@ -51,21 +51,4 @@ data class AccountEntity( val realDisplayName inline get() = this.displayName.ifEmpty { this.username } val isEmptyHeader get() = this.header.contains("missing.png") - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as AccountEntity - - if (id == other.id) return true - return domain == other.domain && accountId == other.accountId - } - - override fun hashCode(): Int { - var result = id.hashCode() - result = 31 * result + domain.hashCode() - result = 31 * result + accountId.hashCode() - return result - } }