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
3 changes: 2 additions & 1 deletion android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ android {

buildConfigField("String", "WP_ENV_SITE_URL", "\"${wpEnvCredentials["siteUrl"] ?: ""}\"")
buildConfigField("String", "WP_ENV_SITE_API_ROOT", "\"${wpEnvCredentials["siteApiRoot"] ?: ""}\"")
buildConfigField("String", "WP_ENV_AUTH_HEADER", "\"${wpEnvCredentials["authHeader"] ?: ""}\"")
buildConfigField("String", "WP_ENV_USERNAME", "\"${wpEnvCredentials["username"] ?: ""}\"")
buildConfigField("String", "WP_ENV_APP_PASSWORD", "\"${wpEnvCredentials["appPassword"] ?: ""}\"")
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,13 @@ private suspend fun persistPost(
)

val app = context.applicationContext as GutenbergKitApplication
val account = app.accountRepository.all().firstOrNull { it.id() == accountId }
?: error("Account not found")
val account = app.findAccount(accountId) ?: error("Account not found")
val client = app.createApiClient(account)

val endpointType = when (configuration.postType.postType) {
"page" -> PostEndpointType.Pages
"post" -> PostEndpointType.Posts
else -> PostEndpointType.Custom(configuration.postType.postType)
else -> PostEndpointType.Custom(configuration.postType.restBase)
}

val params = PostUpdateParams(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ class GutenbergKitApplication : Application() {
}
}

/**
* Returns the account with the given [id], including the local WordPress
* site's in-memory account, which isn't stored.
*/
fun findAccount(id: ULong): Account? {
if (id == LocalWordPressCredentials.ACCOUNT_ID) {
return LocalWordPressCredentials.load()?.toAccount()
}
return accountRepository.get(id)
}

/**
* Constructs a [WpApiClient] for the given [account]. Used by the demo app's posts
* list and Save button to fetch/persist posts via the WordPress REST API.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
package com.example.gutenbergkit

import uniffi.wp_mobile.Account

data class LocalWordPressCredentials(
val siteUrl: String,
val siteApiRoot: String,
val authHeader: String
val username: String,
val appPassword: String
) {
/**
* An in-memory account for the local site, as the iOS demo builds, so the
* posts list and Save button can reach it without a stored account.
*/
fun toAccount(): Account = Account.SelfHostedSite(
id = ACCOUNT_ID,
domain = siteUrl,
username = username,
password = appPassword,
siteApiRoot = siteApiRoot
)

companion object {
/** Never assigned to a stored account, whose IDs start at 1. */
const val ACCOUNT_ID: ULong = 0u

/**
* Loads credentials from BuildConfig fields populated at build time from
* `.wp-env.credentials.json`. Remaps `localhost` to `10.0.2.2` so the
Expand All @@ -18,7 +36,8 @@ data class LocalWordPressCredentials(
return LocalWordPressCredentials(
siteUrl = remapLocalhost(siteUrl),
siteApiRoot = remapLocalhost(BuildConfig.WP_ENV_SITE_API_ROOT),
authHeader = BuildConfig.WP_ENV_AUTH_HEADER
username = BuildConfig.WP_ENV_USERNAME,
appPassword = BuildConfig.WP_ENV_APP_PASSWORD
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,13 @@ class PostsListViewModel(

try {
val app = application as GutenbergKitApplication
val account = app.accountRepository.all().firstOrNull { it.id() == accountId }
?: error("Account not found")
val account = app.findAccount(accountId) ?: error("Account not found")
val client = app.createApiClient(account)

val endpointType = when (postType.postType) {
"page" -> PostEndpointType.Pages
"post" -> PostEndpointType.Posts
else -> PostEndpointType.Custom(postType.postType)
else -> PostEndpointType.Custom(postType.restBase)
}

// Single page only — matches the iOS demo, which doesn't paginate either.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class SitePreparationActivity : ComponentActivity() {
TYPE_LOCAL_WORDPRESS -> ConfigurationItem.LocalWordPress
TYPE_CONFIGURED -> {
ConfigurationItem.ConfiguredEditor(
accountId = json.optLong(KEY_ACCOUNT_ID, 0).toULong(),
accountId = json.getLong(KEY_ACCOUNT_ID).toULong(),
name = json.getString(KEY_NAME),
siteUrl = json.getString(KEY_SITE_URL),
siteApiRoot = json.getString(KEY_SITE_API_ROOT),
Expand Down Expand Up @@ -155,7 +155,11 @@ class SitePreparationActivity : ComponentActivity() {
SitePreparationViewModelFactory(application, configurationItem)
)[SitePreparationViewModel::class.java]

val accountId = (configurationItem as? ConfigurationItem.ConfiguredEditor)?.accountId
val accountId = when (configurationItem) {
is ConfigurationItem.ConfiguredEditor -> configurationItem.accountId
is ConfigurationItem.LocalWordPress -> LocalWordPressCredentials.ACCOUNT_ID
is ConfigurationItem.BundledEditor -> null
}

setContent {
AppTheme {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,9 @@ class SitePreparationViewModel(
)
try {
loadConfiguration(
ConfigurationItem.ConfiguredEditor(
accountId = 0u,
name = "Local WordPress",
siteUrl = credentials.siteUrl,
siteApiRoot = credentials.siteApiRoot,
authHeader = credentials.authHeader
)
ConfigurationItem.ConfiguredEditor
.fromAccount(credentials.toAccount())
.copy(name = "Local WordPress")
)
} catch (e: java.net.ConnectException) {
throw IllegalStateException(
Expand Down Expand Up @@ -337,8 +333,7 @@ class SitePreparationViewModel(
config: ConfigurationItem.ConfiguredEditor
): List<PostTypeOption>? {
val app = getApplication<GutenbergKitApplication>()
val account = app.accountRepository.all().firstOrNull { it.id() == config.accountId }
?: return null
val account = app.findAccount(config.accountId) ?: return null
val client = app.createApiClient(account)

val result = client.request { builder ->
Expand Down
Loading