diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 9964be38d..7da12899b 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -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 { diff --git a/android/app/src/main/java/com/example/gutenbergkit/EditorActivity.kt b/android/app/src/main/java/com/example/gutenbergkit/EditorActivity.kt index 2acf0d33e..24960e3ea 100644 --- a/android/app/src/main/java/com/example/gutenbergkit/EditorActivity.kt +++ b/android/app/src/main/java/com/example/gutenbergkit/EditorActivity.kt @@ -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( diff --git a/android/app/src/main/java/com/example/gutenbergkit/GutenbergKitApplication.kt b/android/app/src/main/java/com/example/gutenbergkit/GutenbergKitApplication.kt index 51df489e2..465322c52 100644 --- a/android/app/src/main/java/com/example/gutenbergkit/GutenbergKitApplication.kt +++ b/android/app/src/main/java/com/example/gutenbergkit/GutenbergKitApplication.kt @@ -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. diff --git a/android/app/src/main/java/com/example/gutenbergkit/LocalWordPressCredentials.kt b/android/app/src/main/java/com/example/gutenbergkit/LocalWordPressCredentials.kt index 1c56cd32e..3ff97ede0 100644 --- a/android/app/src/main/java/com/example/gutenbergkit/LocalWordPressCredentials.kt +++ b/android/app/src/main/java/com/example/gutenbergkit/LocalWordPressCredentials.kt @@ -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 @@ -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 ) } diff --git a/android/app/src/main/java/com/example/gutenbergkit/PostsListActivity.kt b/android/app/src/main/java/com/example/gutenbergkit/PostsListActivity.kt index efc079b8a..e4515b62c 100644 --- a/android/app/src/main/java/com/example/gutenbergkit/PostsListActivity.kt +++ b/android/app/src/main/java/com/example/gutenbergkit/PostsListActivity.kt @@ -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. diff --git a/android/app/src/main/java/com/example/gutenbergkit/SitePreparationActivity.kt b/android/app/src/main/java/com/example/gutenbergkit/SitePreparationActivity.kt index 6ae8449a7..a63ec74d3 100644 --- a/android/app/src/main/java/com/example/gutenbergkit/SitePreparationActivity.kt +++ b/android/app/src/main/java/com/example/gutenbergkit/SitePreparationActivity.kt @@ -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), @@ -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 { diff --git a/android/app/src/main/java/com/example/gutenbergkit/SitePreparationViewModel.kt b/android/app/src/main/java/com/example/gutenbergkit/SitePreparationViewModel.kt index b71615e04..c1a3bdca4 100644 --- a/android/app/src/main/java/com/example/gutenbergkit/SitePreparationViewModel.kt +++ b/android/app/src/main/java/com/example/gutenbergkit/SitePreparationViewModel.kt @@ -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( @@ -337,8 +333,7 @@ class SitePreparationViewModel( config: ConfigurationItem.ConfiguredEditor ): List? { val app = getApplication() - 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 ->