diff --git a/app/src/main/java/com/swoosh/microblog/data/model/GhostModels.kt b/app/src/main/java/com/swoosh/microblog/data/model/GhostModels.kt index 263e1c8..243e47c 100644 --- a/app/src/main/java/com/swoosh/microblog/data/model/GhostModels.kt +++ b/app/src/main/java/com/swoosh/microblog/data/model/GhostModels.kt @@ -59,7 +59,8 @@ data class GhostTag( data class Author( val id: String, - val name: String? + val name: String?, + val profile_image: String? = null ) // --- Local Database Entity --- diff --git a/app/src/main/java/com/swoosh/microblog/ui/feed/FeedViewModel.kt b/app/src/main/java/com/swoosh/microblog/ui/feed/FeedViewModel.kt index 49c1691..92d3e63 100644 --- a/app/src/main/java/com/swoosh/microblog/ui/feed/FeedViewModel.kt +++ b/app/src/main/java/com/swoosh/microblog/ui/feed/FeedViewModel.kt @@ -114,25 +114,18 @@ class FeedViewModel(application: Application) : AndroidViewModel(application) { fun refreshAccountsList() { _accounts.value = accountManager.getAccounts() _activeAccount.value = accountManager.getActiveAccount() - // Fetch avatar if missing for active account - val active = _activeAccount.value - if (active != null && active.avatarUrl == null) { - viewModelScope.launch { - try { - val service = ApiClient.getService( - active.blogUrl, - apiKeyProvider = { active.apiKey } - ) - val response = service.getCurrentUser() - val avatarUrl = response.body()?.users?.firstOrNull()?.profile_image - if (avatarUrl != null) { - accountManager.updateAccount(id = active.id, avatarUrl = avatarUrl) - _activeAccount.value = accountManager.getActiveAccount() - _accounts.value = accountManager.getAccounts() - } - } catch (_: Exception) { /* best-effort */ } - } - } + } + + /** Extract avatar from post authors and save to account if missing */ + private fun tryUpdateAvatarFromPosts(posts: List) { + val active = _activeAccount.value ?: return + if (active.avatarUrl != null) return + val avatarUrl = posts.firstNotNullOfOrNull { post -> + post.authors?.firstOrNull()?.profile_image + } ?: return + accountManager.updateAccount(id = active.id, avatarUrl = avatarUrl) + _activeAccount.value = accountManager.getActiveAccount() + _accounts.value = accountManager.getAccounts() } private fun observeLocalPosts() { @@ -307,6 +300,7 @@ class FeedViewModel(application: Application) : AndroidViewModel(application) { onSuccess = { response -> remotePosts = response.posts.map { it.toFeedPost() } hasMorePages = response.meta?.pagination?.next != null + tryUpdateAvatarFromPosts(response.posts) mergePosts() _uiState.update { it.copy(isRefreshing = false) } },