Skip to content
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
6 changes: 3 additions & 3 deletions android/app/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<ID>LongMethod:EditorActivity.kt$@OptIn(ExperimentalMaterial3Api::class) @Composable fun EditorScreen( configuration: EditorConfiguration, dependencies: EditorDependencies? = null, accountId: ULong? = null, enableNativeMediaUpload: Boolean = true, coroutineScope: CoroutineScope, onClose: () -&gt; Unit, onGutenbergViewCreated: (GutenbergView) -&gt; Unit = {} )</ID>
<ID>LongMethod:MainActivity.kt$@OptIn(ExperimentalMaterial3Api::class) @Composable fun MainScreen( configurations: List&lt;ConfigurationItem&gt;, onConfigurationClick: (ConfigurationItem) -&gt; Unit, onConfigurationLongClick: (ConfigurationItem) -&gt; Boolean, onAddConfiguration: (String) -&gt; Unit, onDeleteConfiguration: (ConfigurationItem) -&gt; Unit, onMediaProxyServer: () -&gt; Unit = {}, isDiscoveringSite: Boolean = false, onDismissDiscovering: () -&gt; Unit = {}, isLoadingCapabilities: Boolean = false, authError: String? = null, onDismissAuthError: () -&gt; Unit = {} )</ID>
<ID>LongMethod:MediaProxyServerActivity.kt$@OptIn(ExperimentalMaterial3Api::class) @Composable fun MediaProxyServerScreen(onBack: () -&gt; Unit)</ID>
<ID>LongMethod:PostsListActivity.kt$@OptIn(ExperimentalMaterial3Api::class) @Composable fun PostsListScreen( viewModel: PostsListViewModel, onClose: () -&gt; Unit, onPostSelected: (AnyPostWithEditContext) -&gt; Unit )</ID>
<ID>LongMethod:SitePreparationActivity.kt$@Composable private fun FeatureConfigurationCard( enableNativeInserter: Boolean, onEnableNativeInserterChange: (Boolean) -&gt; Unit, enableInserterMediaStrip: Boolean, onEnableInserterMediaStripChange: (Boolean) -&gt; Unit, enableNativeMediaUpload: Boolean, onEnableNativeMediaUploadChange: (Boolean) -&gt; Unit, siteSupportsThemeStyles: Boolean, enableThemeStyles: Boolean, onEnableThemeStylesChange: (Boolean) -&gt; Unit, siteSupportsBlockAssets: Boolean, enableThirdPartyBlocks: Boolean, onEnableThirdPartyBlocksChange: (Boolean) -&gt; Unit, isPreparing: Boolean, enableNetworkLogging: Boolean, onEnableNetworkLoggingChange: (Boolean) -&gt; Unit, postTypes: List&lt;PostTypeDetails&gt;, selectedPostType: PostTypeDetails?, onPostTypeChange: (PostTypeDetails) -&gt; Unit, showBrowseButton: Boolean = false, onBrowsePosts: () -&gt; Unit = {} )</ID>
<ID>LongMethod:PostsListActivity.kt$@OptIn(ExperimentalMaterial3Api::class) @Composable fun PostsListScreen( viewModel: PostsListViewModel, title: String, onClose: () -&gt; Unit, onPostSelected: (AnyPostWithEditContext) -&gt; Unit )</ID>
<ID>LongMethod:SitePreparationActivity.kt$@Composable private fun FeatureConfigurationCard( enableNativeInserter: Boolean, onEnableNativeInserterChange: (Boolean) -&gt; Unit, enableInserterMediaStrip: Boolean, onEnableInserterMediaStripChange: (Boolean) -&gt; Unit, enableNativeMediaUpload: Boolean, onEnableNativeMediaUploadChange: (Boolean) -&gt; Unit, siteSupportsThemeStyles: Boolean, enableThemeStyles: Boolean, onEnableThemeStylesChange: (Boolean) -&gt; Unit, siteSupportsBlockAssets: Boolean, enableThirdPartyBlocks: Boolean, onEnableThirdPartyBlocksChange: (Boolean) -&gt; Unit, isPreparing: Boolean, enableNetworkLogging: Boolean, onEnableNetworkLoggingChange: (Boolean) -&gt; Unit, postTypes: List&lt;PostTypeOption&gt;, selectedPostType: PostTypeOption?, onPostTypeChange: (PostTypeOption) -&gt; Unit, showBrowseButton: Boolean = false, onBrowsePosts: () -&gt; Unit = {} )</ID>
<ID>LongParameterList:MainActivity.kt$( configurations: List&lt;ConfigurationItem&gt;, onConfigurationClick: (ConfigurationItem) -&gt; Unit, onConfigurationLongClick: (ConfigurationItem) -&gt; Boolean, onAddConfiguration: (String) -&gt; Unit, onDeleteConfiguration: (ConfigurationItem) -&gt; Unit, onMediaProxyServer: () -&gt; Unit = {}, isDiscoveringSite: Boolean = false, onDismissDiscovering: () -&gt; Unit = {}, isLoadingCapabilities: Boolean = false, authError: String? = null, onDismissAuthError: () -&gt; Unit = {} )</ID>
<ID>LongParameterList:SitePreparationActivity.kt$( enableNativeInserter: Boolean, onEnableNativeInserterChange: (Boolean) -&gt; Unit, enableInserterMediaStrip: Boolean, onEnableInserterMediaStripChange: (Boolean) -&gt; Unit, enableNativeMediaUpload: Boolean, onEnableNativeMediaUploadChange: (Boolean) -&gt; Unit, siteSupportsThemeStyles: Boolean, enableThemeStyles: Boolean, onEnableThemeStylesChange: (Boolean) -&gt; Unit, siteSupportsBlockAssets: Boolean, enableThirdPartyBlocks: Boolean, onEnableThirdPartyBlocksChange: (Boolean) -&gt; Unit, isPreparing: Boolean, enableNetworkLogging: Boolean, onEnableNetworkLoggingChange: (Boolean) -&gt; Unit, postTypes: List&lt;PostTypeDetails&gt;, selectedPostType: PostTypeDetails?, onPostTypeChange: (PostTypeDetails) -&gt; Unit, showBrowseButton: Boolean = false, onBrowsePosts: () -&gt; Unit = {} )</ID>
<ID>LongParameterList:SitePreparationActivity.kt$( enableNativeInserter: Boolean, onEnableNativeInserterChange: (Boolean) -&gt; Unit, enableInserterMediaStrip: Boolean, onEnableInserterMediaStripChange: (Boolean) -&gt; Unit, enableNativeMediaUpload: Boolean, onEnableNativeMediaUploadChange: (Boolean) -&gt; Unit, siteSupportsThemeStyles: Boolean, enableThemeStyles: Boolean, onEnableThemeStylesChange: (Boolean) -&gt; Unit, siteSupportsBlockAssets: Boolean, enableThirdPartyBlocks: Boolean, onEnableThirdPartyBlocksChange: (Boolean) -&gt; Unit, isPreparing: Boolean, enableNetworkLogging: Boolean, onEnableNetworkLoggingChange: (Boolean) -&gt; Unit, postTypes: List&lt;PostTypeOption&gt;, selectedPostType: PostTypeOption?, onPostTypeChange: (PostTypeOption) -&gt; Unit, showBrowseButton: Boolean = false, onBrowsePosts: () -&gt; Unit = {} )</ID>
<ID>MaxLineLength:MediaProxyServerActivity.kt$Text("Size", fontFamily = FontFamily.Monospace, style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.weight(1f))</ID>
<ID>MaxLineLength:MediaProxyServerActivity.kt$Text("Throughput", fontFamily = FontFamily.Monospace, style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.weight(1f))</ID>
<ID>MaxLineLength:MediaProxyServerActivity.kt$Text("Time", fontFamily = FontFamily.Monospace, style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.weight(1f))</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,20 @@ class PostsListActivity : ComponentActivity() {
companion object {
const val EXTRA_ACCOUNT_ID = "account_id"
const val EXTRA_POST_TYPE = "post_type"
const val EXTRA_TITLE = "title"

fun createIntent(
context: Context,
accountId: ULong,
postType: PostTypeDetails,
title: String,
configuration: EditorConfiguration,
dependencies: EditorDependencies?
): Intent {
return Intent(context, PostsListActivity::class.java).apply {
putExtra(EXTRA_ACCOUNT_ID, accountId.toLong())
putExtra(EXTRA_POST_TYPE, postType)
putExtra(EXTRA_TITLE, title)
putExtra(MainActivity.EXTRA_CONFIGURATION, configuration)
if (dependencies != null) {
val filePath = EditorDependenciesSerializer.writeToDisk(context, dependencies)
Expand All @@ -95,6 +98,7 @@ class PostsListActivity : ComponentActivity() {
val accountId = intent.getLongExtra(EXTRA_ACCOUNT_ID, -1L).takeIf { it >= 0 }?.toULong()
val postType = intent.getParcelableExtra(EXTRA_POST_TYPE, PostTypeDetails::class.java)
?: PostTypeDetails.post
val title = intent.getStringExtra(EXTRA_TITLE) ?: getString(R.string.posts)
val configuration = intent.getParcelableExtra(MainActivity.EXTRA_CONFIGURATION, EditorConfiguration::class.java)
val dependenciesPath = intent.getStringExtra(EditorActivity.EXTRA_DEPENDENCIES_PATH)

Expand All @@ -116,6 +120,7 @@ class PostsListActivity : ComponentActivity() {
AppTheme {
PostsListScreen(
viewModel = viewModel,
title = title,
onClose = { finish() },
onPostSelected = { post ->
launchEditor(post, configuration, dependenciesPath, accountId)
Expand Down Expand Up @@ -209,6 +214,7 @@ class PostsListViewModel(
@Composable
fun PostsListScreen(
viewModel: PostsListViewModel,
title: String,
onClose: () -> Unit,
onPostSelected: (AnyPostWithEditContext) -> Unit
) {
Expand All @@ -222,7 +228,7 @@ fun PostsListScreen(
modifier = Modifier.fillMaxSize(),
topBar = {
TopAppBar(
title = { Text(stringResource(R.string.posts)) },
title = { Text(title) },
navigationIcon = {
IconButton(onClick = onClose) {
Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import java.util.Locale
import org.wordpress.gutenberg.model.EditorConfiguration
import org.wordpress.gutenberg.model.EditorDependencies
import org.wordpress.gutenberg.model.EditorDependenciesSerializer
import org.wordpress.gutenberg.model.PostTypeDetails

class SitePreparationActivity : ComponentActivity() {

Expand Down Expand Up @@ -197,12 +196,19 @@ class SitePreparationActivity : ComponentActivity() {

private fun launchPostsList(
accountId: ULong,
postType: PostTypeDetails,
postType: PostTypeOption,
configuration: EditorConfiguration,
dependencies: EditorDependencies?
) {
startActivity(
PostsListActivity.createIntent(this, accountId, postType, configuration, dependencies)
PostsListActivity.createIntent(
this,
accountId,
postType.details,
postType.pluralLabel,
configuration,
dependencies
)
)
}
}
Expand All @@ -214,7 +220,7 @@ fun SitePreparationScreen(
accountId: ULong?,
onClose: () -> Unit,
onStartEditor: (EditorConfiguration, EditorDependencies?, Boolean) -> Unit,
onBrowsePosts: (EditorConfiguration, EditorDependencies?, PostTypeDetails) -> Unit
onBrowsePosts: (EditorConfiguration, EditorDependencies?, PostTypeOption) -> Unit
) {
val uiState by viewModel.uiState.collectAsState()

Expand Down Expand Up @@ -426,9 +432,9 @@ private fun FeatureConfigurationCard(
isPreparing: Boolean,
enableNetworkLogging: Boolean,
onEnableNetworkLoggingChange: (Boolean) -> Unit,
postTypes: List<PostTypeDetails>,
selectedPostType: PostTypeDetails?,
onPostTypeChange: (PostTypeDetails) -> Unit,
postTypes: List<PostTypeOption>,
selectedPostType: PostTypeOption?,
onPostTypeChange: (PostTypeOption) -> Unit,
showBrowseButton: Boolean = false,
onBrowsePosts: () -> Unit = {}
) {
Expand Down Expand Up @@ -566,7 +572,7 @@ private fun FeatureConfigurationCard(
enabled = !isPreparing
)
Text(
text = postType.postType.replaceFirstChar { it.uppercase() },
text = postType.label,
modifier = Modifier.padding(start = 8.dp)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ data class SitePreparationUiState(
val enableThirdPartyBlocks: Boolean = true,
val enableNetworkLogging: Boolean = false,
/** All viewable post types fetched from the site, or empty while loading. */
val postTypes: List<PostTypeDetails> = emptyList(),
val postTypes: List<PostTypeOption> = emptyList(),
/** The post type currently selected in the picker. Null until [postTypes] loads. */
val selectedPostType: PostTypeDetails? = null,
val selectedPostType: PostTypeOption? = null,
val cacheBundleCount: Int? = null,
val isLoading: Boolean = false,
val error: String? = null,
Expand All @@ -39,6 +39,18 @@ data class SitePreparationUiState(
val loadingProgress: Float? = null
)

/** A post type offered in the picker, with the names shown for it. */
data class PostTypeOption(val details: PostTypeDetails, val label: String, val pluralLabel: String) {
companion object {
/** Falls back to the slug and REST base when the site's labels are missing or blank. */
fun from(details: PostTypeDetails, label: String? = null, pluralLabel: String? = null) = PostTypeOption(
details = details,
label = label?.ifBlank { null } ?: details.postType.replaceFirstChar { it.uppercase() },
pluralLabel = pluralLabel?.ifBlank { null } ?: details.restBase.replaceFirstChar { it.uppercase() }
)
}
}

class SitePreparationViewModel(
application: Application,
private val configurationItem: ConfigurationItem
Expand Down Expand Up @@ -113,7 +125,7 @@ class SitePreparationViewModel(
_uiState.update { it.copy(enableNetworkLogging = enabled) }
}

fun setPostType(postType: PostTypeDetails) {
fun setPostType(postType: PostTypeOption) {
// Discard dependencies prepared for the previous post type.
_uiState.update { it.copy(selectedPostType = postType, editorDependencies = null) }
}
Expand Down Expand Up @@ -227,10 +239,11 @@ class SitePreparationViewModel(

private fun createBundledConfiguration(): EditorConfiguration {
// Bundled offline editor: only the standard `post` type is meaningful.
val post = PostTypeOption.from(PostTypeDetails.post)
_uiState.update {
it.copy(
postTypes = listOf(PostTypeDetails.post),
selectedPostType = PostTypeDetails.post
postTypes = listOf(post),
selectedPostType = post
)
}
return EditorConfiguration.builder(
Expand Down Expand Up @@ -274,8 +287,8 @@ class SitePreparationViewModel(
// available (the typical case); otherwise pick the first type in the
// list. Falls back to `PostTypeDetails.post` if the call fails so the
// editor can still launch with a sensible default.
val postTypes = loadPostTypes(config) ?: listOf(PostTypeDetails.post)
val defaultPostType = postTypes.firstOrNull { it.postType == "post" } ?: postTypes.first()
val postTypes = loadPostTypes(config) ?: listOf(PostTypeOption.from(PostTypeDetails.post))
val defaultPostType = postTypes.firstOrNull { it.details.postType == "post" } ?: postTypes.first()
_uiState.update {
it.copy(
postTypes = postTypes,
Expand All @@ -286,7 +299,7 @@ class SitePreparationViewModel(
return EditorConfiguration.builder(
siteURL = config.siteUrl,
siteApiRoot = siteApiRoot,
postType = defaultPostType
postType = defaultPostType.details
)
.setPlugins(capabilities.supportsPlugins)
.setThemeStyles(capabilities.supportsThemeStyles)
Expand All @@ -310,7 +323,7 @@ class SitePreparationViewModel(

/**
* Fetches the site's post types from the WordPress REST API and returns the
* subset relevant to the editor picker.
* subset relevant to the editor picker, each with its singular and plural labels.
*
* Mirrors the iOS [SitePreparationView.loadPostTypes] filter:
* - always include the standard `post` and `page` types
Expand All @@ -322,7 +335,7 @@ class SitePreparationViewModel(
*/
private suspend fun loadPostTypes(
config: ConfigurationItem.ConfiguredEditor
): List<PostTypeDetails>? {
): List<PostTypeOption>? {
val app = getApplication<GutenbergKitApplication>()
val account = app.accountRepository.all().firstOrNull { it.id() == config.accountId }
?: return null
Expand All @@ -342,13 +355,20 @@ class SitePreparationViewModel(
}
}
.map { (_, details) ->
PostTypeDetails(
postType = details.slug,
restBase = details.restBase,
restNamespace = details.restNamespace
PostTypeOption.from(
details = PostTypeDetails(
postType = details.slug,
restBase = details.restBase,
restNamespace = details.restNamespace
),
label = details.labels.singularName,
pluralLabel = details.labels.name
)
}
.sortedBy { it.postType }
.sortedWith(
compareBy<PostTypeOption, String>(String.CASE_INSENSITIVE_ORDER) { it.label }
.thenBy { it.details.postType }
)
}

/**
Expand All @@ -372,7 +392,7 @@ class SitePreparationViewModel(
.setEnableInserterMediaStrip(_uiState.value.enableInserterMediaStrip)
.setThemeStyles(baseConfig.themeStyles && _uiState.value.enableThemeStyles)
.setPlugins(baseConfig.plugins && _uiState.value.enableThirdPartyBlocks)
.setPostType(selectedPostType)
.setPostType(selectedPostType.details)
.build()
}
}
Expand Down
15 changes: 12 additions & 3 deletions ios/Demo-iOS/Sources/Views/PostsListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ struct PostsListView: View {
@State
private var viewModel: PostsListViewModel

init(client: WordPressAPI, postTypeDetails: PostTypeDetails, editorConfiguration: EditorConfiguration, editorDependencies: EditorDependencies?) {
private let title: String

init(
client: WordPressAPI,
postTypeDetails: PostTypeDetails,
title: String,
editorConfiguration: EditorConfiguration,
editorDependencies: EditorDependencies?
) {
self.title = title
self.viewModel = PostsListViewModel(
client: client,
postTypeDetails: postTypeDetails,
Expand All @@ -37,7 +46,7 @@ struct PostsListView: View {
}
} else if viewModel.posts.isEmpty {
ContentUnavailableView {
Label("No Entires", systemImage: "doc.text")
Label("No Entries", systemImage: "doc.text")
} description: {
Text("No entries found for this post type.")
}
Expand All @@ -63,7 +72,7 @@ struct PostsListView: View {
.listStyle(.plain)
}
}
.navigationTitle(viewModel.postTypeDetails.restBase.capitalized)
.navigationTitle(title)
.task {
await viewModel.loadPosts()
}
Expand Down
Loading
Loading