mirror of
https://github.com/pawelorzech/Swoosh.git
synced 2026-04-01 04:15:42 +00:00
feat: fetch Ghost profile avatar for account icon, fallback to colored initial
This commit is contained in:
parent
edca4dd0c5
commit
dcb9c50c02
5 changed files with 56 additions and 18 deletions
|
|
@ -149,7 +149,7 @@ class AccountManager private constructor(
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateAccount(id: String, name: String? = null, blogUrl: String? = null, apiKey: String? = null): Boolean {
|
fun updateAccount(id: String, name: String? = null, blogUrl: String? = null, apiKey: String? = null, avatarUrl: String? = null): Boolean {
|
||||||
val accounts = getAccounts().toMutableList()
|
val accounts = getAccounts().toMutableList()
|
||||||
val index = accounts.indexOfFirst { it.id == id }
|
val index = accounts.indexOfFirst { it.id == id }
|
||||||
if (index == -1) return false
|
if (index == -1) return false
|
||||||
|
|
@ -158,7 +158,8 @@ class AccountManager private constructor(
|
||||||
accounts[index] = current.copy(
|
accounts[index] = current.copy(
|
||||||
name = name ?: current.name,
|
name = name ?: current.name,
|
||||||
blogUrl = blogUrl ?: current.blogUrl,
|
blogUrl = blogUrl ?: current.blogUrl,
|
||||||
apiKey = apiKey ?: current.apiKey
|
apiKey = apiKey ?: current.apiKey,
|
||||||
|
avatarUrl = avatarUrl ?: current.avatarUrl
|
||||||
)
|
)
|
||||||
|
|
||||||
saveAccountsList(accounts)
|
saveAccountsList(accounts)
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,9 @@ interface GhostApiService {
|
||||||
@Path("id") id: String
|
@Path("id") id: String
|
||||||
): Response<Unit>
|
): Response<Unit>
|
||||||
|
|
||||||
|
@GET("ghost/api/admin/users/me/")
|
||||||
|
suspend fun getCurrentUser(): Response<UsersResponse>
|
||||||
|
|
||||||
@Multipart
|
@Multipart
|
||||||
@POST("ghost/api/admin/images/upload/")
|
@POST("ghost/api/admin/images/upload/")
|
||||||
suspend fun uploadImage(
|
suspend fun uploadImage(
|
||||||
|
|
@ -53,3 +56,14 @@ data class UploadedImage(
|
||||||
val url: String,
|
val url: String,
|
||||||
val ref: String?
|
val ref: String?
|
||||||
)
|
)
|
||||||
|
|
||||||
|
data class UsersResponse(
|
||||||
|
val users: List<GhostUser>
|
||||||
|
)
|
||||||
|
|
||||||
|
data class GhostUser(
|
||||||
|
val id: String,
|
||||||
|
val name: String?,
|
||||||
|
val profile_image: String?,
|
||||||
|
val email: String?
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,8 @@ data class GhostAccount(
|
||||||
val blogUrl: String,
|
val blogUrl: String,
|
||||||
val apiKey: String,
|
val apiKey: String,
|
||||||
val isActive: Boolean = false,
|
val isActive: Boolean = false,
|
||||||
val colorIndex: Int = 0
|
val colorIndex: Int = 0,
|
||||||
|
val avatarUrl: String? = null
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
val ACCOUNT_COLORS = listOf(
|
val ACCOUNT_COLORS = listOf(
|
||||||
|
|
|
||||||
|
|
@ -1159,9 +1159,18 @@ fun AccountAvatar(
|
||||||
account: GhostAccount,
|
account: GhostAccount,
|
||||||
size: Int = 36
|
size: Int = 36
|
||||||
) {
|
) {
|
||||||
|
if (!account.avatarUrl.isNullOrBlank()) {
|
||||||
|
AsyncImage(
|
||||||
|
model = account.avatarUrl,
|
||||||
|
contentDescription = account.name,
|
||||||
|
modifier = Modifier
|
||||||
|
.size(size.dp)
|
||||||
|
.clip(CircleShape),
|
||||||
|
contentScale = ContentScale.Crop
|
||||||
|
)
|
||||||
|
} else {
|
||||||
val color = Color(GhostAccount.colorForIndex(account.colorIndex))
|
val color = Color(GhostAccount.colorForIndex(account.colorIndex))
|
||||||
val initial = account.name.firstOrNull()?.uppercase() ?: "?"
|
val initial = account.name.firstOrNull()?.uppercase() ?: "?"
|
||||||
|
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.size(size.dp)
|
.size(size.dp)
|
||||||
|
|
@ -1177,6 +1186,7 @@ fun AccountAvatar(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
|
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,18 @@ class SetupViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
val repo = PostRepository(getApplication())
|
val repo = PostRepository(getApplication())
|
||||||
repo.fetchPosts(page = 1, limit = 1).fold(
|
repo.fetchPosts(page = 1, limit = 1).fold(
|
||||||
onSuccess = {
|
onSuccess = {
|
||||||
|
// Fetch user profile for avatar
|
||||||
|
try {
|
||||||
|
val service = ApiClient.getService(
|
||||||
|
state.url,
|
||||||
|
apiKeyProvider = { state.apiKey }
|
||||||
|
)
|
||||||
|
val userResponse = service.getCurrentUser()
|
||||||
|
val avatarUrl = userResponse.body()?.users?.firstOrNull()?.profile_image
|
||||||
|
if (avatarUrl != null) {
|
||||||
|
accountManager.updateAccount(id = account.id, avatarUrl = avatarUrl)
|
||||||
|
}
|
||||||
|
} catch (_: Exception) { /* avatar is best-effort */ }
|
||||||
_uiState.update { it.copy(isTesting = false, isSuccess = true) }
|
_uiState.update { it.copy(isTesting = false, isSuccess = true) }
|
||||||
},
|
},
|
||||||
onFailure = { e ->
|
onFailure = { e ->
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue