Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add missing files #26

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
local.properties
.idea/*
/app/release
/app/schemas/com.github.whitescent.mastify.timeline.TestAppDatabase/*
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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<Account> = 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
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}