Skip to content

Commit 45fc880

Browse files
nbradburyclaude
andcommitted
RS Posts: Extract fetchPost and resolveAsyncFields from loadPost
Fixes detekt LongMethod violation by splitting loadPost into smaller focused methods. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 39b766f commit 45fc880

File tree

1 file changed

+47
-39
lines changed

1 file changed

+47
-39
lines changed

WordPress/src/main/java/org/wordpress/android/ui/postsrs/PostRsSettingsViewModel.kt

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -87,45 +87,9 @@ class PostRsSettingsViewModel @Inject constructor(
8787
viewModelScope.launch {
8888
@Suppress("TooGenericExceptionCaught")
8989
try {
90-
val post = withContext(Dispatchers.IO) {
91-
val client =
92-
wpApiClientProvider.getWpApiClient(site)
93-
val response = client.request {
94-
it.posts().retrieveWithEditContext(
95-
PostEndpointType.Posts,
96-
postId,
97-
PostRetrieveParams()
98-
)
99-
}
100-
when (response) {
101-
is WpRequestResult.Success ->
102-
response.response.data
103-
else -> throw PostFetchException(
104-
(response as? WpRequestResult.WpError<*>)
105-
?.errorMessage
106-
)
107-
}
108-
}
109-
val state = mapPostToUiState(post)
110-
_uiState.value = state
111-
resolveAuthor(post.author)
112-
resolveFeaturedImage(post.featuredMedia)
113-
resolveTermNames(
114-
post.categories,
115-
TermEndpointType.Categories
116-
) { names ->
117-
_uiState.value = _uiState.value.copy(
118-
categoryNames = names
119-
)
120-
}
121-
resolveTermNames(
122-
post.tags,
123-
TermEndpointType.Tags
124-
) { names ->
125-
_uiState.value = _uiState.value.copy(
126-
tagNames = names
127-
)
128-
}
90+
val post = fetchPost(site)
91+
_uiState.value = mapPostToUiState(post)
92+
resolveAsyncFields(post)
12993
} catch (e: CancellationException) {
13094
throw e
13195
} catch (e: Exception) {
@@ -144,6 +108,50 @@ class PostRsSettingsViewModel @Inject constructor(
144108
}
145109
}
146110

111+
private suspend fun fetchPost(
112+
site: org.wordpress.android.fluxc.model.SiteModel
113+
): AnyPostWithEditContext = withContext(Dispatchers.IO) {
114+
val client = wpApiClientProvider.getWpApiClient(site)
115+
val response = client.request {
116+
it.posts().retrieveWithEditContext(
117+
PostEndpointType.Posts,
118+
postId,
119+
PostRetrieveParams()
120+
)
121+
}
122+
when (response) {
123+
is WpRequestResult.Success ->
124+
response.response.data
125+
else -> throw PostFetchException(
126+
(response as? WpRequestResult.WpError<*>)
127+
?.errorMessage
128+
)
129+
}
130+
}
131+
132+
private fun resolveAsyncFields(
133+
post: AnyPostWithEditContext
134+
) {
135+
resolveAuthor(post.author)
136+
resolveFeaturedImage(post.featuredMedia)
137+
resolveTermNames(
138+
post.categories,
139+
TermEndpointType.Categories
140+
) { names ->
141+
_uiState.value = _uiState.value.copy(
142+
categoryNames = names
143+
)
144+
}
145+
resolveTermNames(
146+
post.tags,
147+
TermEndpointType.Tags
148+
) { names ->
149+
_uiState.value = _uiState.value.copy(
150+
tagNames = names
151+
)
152+
}
153+
}
154+
147155
@Suppress("ComplexCondition")
148156
private fun mapPostToUiState(
149157
post: AnyPostWithEditContext

0 commit comments

Comments
 (0)