Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Custom Fields] Read-only mode for JSON values #12708

Merged
merged 14 commits into from
Oct 1, 2024

Conversation

hichamboushaba
Copy link
Member

@hichamboushaba hichamboushaba commented Sep 26, 2024

Closes: #12699

Description

This PR adds support for displaying JSON values in read-only mode, it depends on the FluxC work done in wordpress-mobile/WordPress-FluxC-Android#3102

Steps to reproduce

  1. Add a JSON custom field to one of your orders using the API:
POST {site}/wc/v3/orders/{orderId}

{
    "meta_data": [
        {
            "key": "json_object",
            "value": {
                "Key": "Value"
            }
        },
        {
            "key": "json_array",
            "value": [
                {
                    "Key": "Value"
                }
            ]
        }
    ]
}
  1. Open the order in the app.
  2. Tap on View custom fields.

Testing information

  • Confirm the JSON fields are listed.
  • Test tapping on the JSON field and checking the popup.

The tests that have been performed

Images/gif

Object Array
Screenshot_20240926_180216 Screenshot_20240926_180224
  • I have considered if this change warrants release notes and have added them to RELEASE-NOTES.txt if necessary. Use the "[Internal]" label for non-user-facing changes.

Reviewer (or Author, in the case of optional code reviews):

Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement:

  • The PR is small and has a clear, single focus, or a valid explanation is provided in the description. If needed, please request to split it into smaller PRs.
  • Ensure Adequate Unit Test Coverage: The changes are reasonably covered by unit tests or an explanation is provided in the PR description.
  • Manual Testing: The author listed all the tests they ran, including smoke tests when needed (e.g., for refactorings). The reviewer confirmed that the PR works as expected on big (tablet) and small (phone) in case of UI changes, and no regressions are added.

@hichamboushaba hichamboushaba added type: enhancement A request for an enhancement. status: do not merge Dependent on another PR, ready for review but not ready for merge. feature: order details Related to order details. feature: product details Related to adding or editing products, includes product settings. labels Sep 26, 2024
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Sep 26, 2024

📲 You can test the changes from this Pull Request in WooCommerce-Wear Android by scanning the QR code below to install the corresponding build.
App Name WooCommerce-Wear Android
Platform⌚️ Wear OS
FlavorJalapeno
Build TypeDebug
Commit8452fc7
Direct Downloadwoocommerce-wear-prototype-build-pr12708-8452fc7.apk

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Sep 26, 2024

📲 You can test the changes from this Pull Request in WooCommerce Android by scanning the QR code below to install the corresponding build.

App Name WooCommerce Android
Platform📱 Mobile
FlavorJalapeno
Build TypeDebug
Commit8452fc7
Direct Downloadwoocommerce-prototype-build-pr12708-8452fc7.apk

Comment on lines +269 to +278
val jsonFormatted = remember(customField.value) {
runCatching {
if (customField.value.trimStart().startsWith("[")) {
JSONArray(customField.value).toString(4)
} else {
JSONObject(customField.value).toString(4)
}
}.getOrDefault(customField.value)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add this formatting logic to the ViewModel because it's UI only, and uses an Android specific class org.json.*, let me know if you have some concerns with this approach.

@hichamboushaba hichamboushaba marked this pull request as ready for review September 27, 2024 08:52
Comment on lines 283 to 289
OutlinedTextField(
value = customField.key,
onValueChange = {},
label = { Text(text = stringResource(id = R.string.custom_fields_editor_key_label)) },
readOnly = true,
modifier = Modifier
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used read-only text fields for displaying the custom field's content because it has built-in support for selection and allowing copy/paste.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! Just a minor nitpick. From a UX point of view, having the edit text fields focusable (highlighted with the purple border) when they are not editable seems confusing.
I was wondering if we should use enabled = false instead of readOnly = true. This still allows for copy pasting (although usability is worse for selecting the text), but makes it way more apparent that the content is not editable.
Wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point here @JorgeMucientes, I made some updates to disable the focus of the fields, I didn't use enabled = false reduced the experience a lot, please check the commit dbcf0be and let me know what you think.

With this commit, and the other UI updates suggested below, the result looks like this now:

Screen_recording_20241001_130945.mp4

@hichamboushaba hichamboushaba added this to the 20.7 milestone Sep 27, 2024
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Sep 27, 2024

Project dependencies changes

The following changes in project dependencies were detected (configuration vanillaReleaseRuntimeClasspath):

list
Upgraded Dependencies
org.wordpress.fluxc.plugins:woocommerce:trunk-373bc6d30f8d9da6b7750f3ddc38929611b50056, (changed from trunk-06e2a179771b9d8c726a15843d56fc897b6d5ca0)
org.wordpress.fluxc:fluxc-annotations:trunk-373bc6d30f8d9da6b7750f3ddc38929611b50056, (changed from trunk-06e2a179771b9d8c726a15843d56fc897b6d5ca0)
org.wordpress:fluxc:trunk-373bc6d30f8d9da6b7750f3ddc38929611b50056, (changed from trunk-06e2a179771b9d8c726a15843d56fc897b6d5ca0)
tree
-+--- org.wordpress:fluxc:trunk-06e2a179771b9d8c726a15843d56fc897b6d5ca0
-|    +--- org.jetbrains.kotlin:kotlin-parcelize-runtime:1.9.25
-|    |    +--- org.jetbrains.kotlin:kotlin-stdlib:1.9.25 (*)
-|    |    \--- org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.9.25
-|    |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.9.25 (*)
-|    +--- androidx.exifinterface:exifinterface:1.0.0 -> 1.3.6
-|    |    \--- androidx.annotation:annotation:1.2.0 -> 1.8.0 (*)
-|    +--- androidx.security:security-crypto:1.0.0 -> 1.1.0-alpha03
-|    |    +--- androidx.annotation:annotation:1.1.0 -> 1.8.0 (*)
-|    |    +--- com.google.crypto.tink:tink-android:1.5.0
-|    |    \--- androidx.collection:collection:1.1.0 -> 1.4.0 (*)
-|    +--- com.squareup.okhttp3:okhttp-urlconnection:4.9.0
-|    |    +--- com.squareup.okhttp3:okhttp:4.9.0 -> 4.12.0 (*)
-|    |    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.10 -> 1.9.10 (*)
-|    +--- com.google.code.gson:gson:2.8.5 -> 2.10.1
-|    +--- org.apache.commons:commons-text:1.10.0 (*)
-|    +--- androidx.room:room-runtime:2.6.1 (*)
-|    +--- androidx.room:room-ktx:2.6.1
-|    |    +--- androidx.room:room-common:2.6.1 (*)
-|    |    +--- androidx.room:room-runtime:2.6.1 (*)
-|    |    +--- org.jetbrains.kotlin:kotlin-stdlib:1.8.22 -> 1.9.25 (*)
-|    |    +--- org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1 -> 1.8.1 (*)
-|    |    +--- androidx.room:room-common:2.6.1 (c)
-|    |    \--- androidx.room:room-runtime:2.6.1 (c)
-|    +--- com.google.dagger:dagger:2.51.1
-|    |    \--- javax.inject:javax.inject:1
-|    +--- org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1 (*)
-|    +--- org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1 (*)
-|    +--- org.wordpress:wellsql:2.0.0
-|    |    +--- androidx.annotation:annotation:1.2.0 -> 1.8.0 (*)
-|    |    \--- org.wordpress.wellsql:wellsql-annotations:2.0.0
-|    +--- org.wordpress.fluxc:fluxc-annotations:trunk-06e2a179771b9d8c726a15843d56fc897b6d5ca0
-|    +--- org.greenrobot:eventbus:3.3.1 (*)
-|    +--- org.greenrobot:eventbus-java:3.3.1
-|    +--- com.squareup.okhttp3:okhttp:4.9.0 -> 4.12.0 (*)
-|    +--- com.android.volley:volley:1.1.1 -> 1.2.0
-|    +--- androidx.paging:paging-runtime:2.1.2
-|    |    +--- androidx.paging:paging-common:2.1.2
-|    |    |    +--- androidx.annotation:annotation:1.0.0 -> 1.8.0 (*)
-|    |    |    \--- androidx.arch.core:core-common:2.0.0 -> 2.2.0 (*)
-|    |    +--- androidx.arch.core:core-runtime:2.0.0 -> 2.2.0 (*)
-|    |    +--- androidx.lifecycle:lifecycle-runtime:2.0.0 -> 2.7.0 (*)
-|    |    +--- androidx.lifecycle:lifecycle-livedata:2.0.0 -> 2.7.0 (*)
-|    |    \--- androidx.recyclerview:recyclerview:1.0.0 -> 1.3.2 (*)
-|    +--- com.goterl:lazysodium-android:5.0.2
-|    +--- net.java.dev.jna:jna:5.5.0
-|    \--- org.jetbrains.kotlin:kotlin-stdlib:1.9.25 (*)
++--- org.wordpress:fluxc:trunk-373bc6d30f8d9da6b7750f3ddc38929611b50056
+|    +--- org.jetbrains.kotlin:kotlin-parcelize-runtime:1.9.25
+|    |    +--- org.jetbrains.kotlin:kotlin-stdlib:1.9.25 (*)
+|    |    \--- org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.9.25
+|    |         \--- org.jetbrains.kotlin:kotlin-stdlib:1.9.25 (*)
+|    +--- androidx.exifinterface:exifinterface:1.0.0 -> 1.3.6
+|    |    \--- androidx.annotation:annotation:1.2.0 -> 1.8.0 (*)
+|    +--- androidx.security:security-crypto:1.0.0 -> 1.1.0-alpha03
+|    |    +--- androidx.annotation:annotation:1.1.0 -> 1.8.0 (*)
+|    |    +--- com.google.crypto.tink:tink-android:1.5.0
+|    |    \--- androidx.collection:collection:1.1.0 -> 1.4.0 (*)
+|    +--- com.squareup.okhttp3:okhttp-urlconnection:4.9.0
+|    |    +--- com.squareup.okhttp3:okhttp:4.9.0 -> 4.12.0 (*)
+|    |    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.10 -> 1.9.10 (*)
+|    +--- com.google.code.gson:gson:2.8.5 -> 2.10.1
+|    +--- org.apache.commons:commons-text:1.10.0 (*)
+|    +--- androidx.room:room-runtime:2.6.1 (*)
+|    +--- androidx.room:room-ktx:2.6.1
+|    |    +--- androidx.room:room-common:2.6.1 (*)
+|    |    +--- androidx.room:room-runtime:2.6.1 (*)
+|    |    +--- org.jetbrains.kotlin:kotlin-stdlib:1.8.22 -> 1.9.25 (*)
+|    |    +--- org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1 -> 1.8.1 (*)
+|    |    +--- androidx.room:room-common:2.6.1 (c)
+|    |    \--- androidx.room:room-runtime:2.6.1 (c)
+|    +--- com.google.dagger:dagger:2.51.1
+|    |    \--- javax.inject:javax.inject:1
+|    +--- org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1 (*)
+|    +--- org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1 (*)
+|    +--- org.wordpress:wellsql:2.0.0
+|    |    +--- androidx.annotation:annotation:1.2.0 -> 1.8.0 (*)
+|    |    \--- org.wordpress.wellsql:wellsql-annotations:2.0.0
+|    +--- org.wordpress.fluxc:fluxc-annotations:trunk-373bc6d30f8d9da6b7750f3ddc38929611b50056
+|    +--- org.greenrobot:eventbus:3.3.1 (*)
+|    +--- org.greenrobot:eventbus-java:3.3.1
+|    +--- com.squareup.okhttp3:okhttp:4.9.0 -> 4.12.0 (*)
+|    +--- com.android.volley:volley:1.1.1 -> 1.2.0
+|    +--- androidx.paging:paging-runtime:2.1.2
+|    |    +--- androidx.paging:paging-common:2.1.2
+|    |    |    +--- androidx.annotation:annotation:1.0.0 -> 1.8.0 (*)
+|    |    |    \--- androidx.arch.core:core-common:2.0.0 -> 2.2.0 (*)
+|    |    +--- androidx.arch.core:core-runtime:2.0.0 -> 2.2.0 (*)
+|    |    +--- androidx.lifecycle:lifecycle-runtime:2.0.0 -> 2.7.0 (*)
+|    |    +--- androidx.lifecycle:lifecycle-livedata:2.0.0 -> 2.7.0 (*)
+|    |    \--- androidx.recyclerview:recyclerview:1.0.0 -> 1.3.2 (*)
+|    +--- com.goterl:lazysodium-android:5.0.2
+|    +--- net.java.dev.jna:jna:5.5.0
+|    \--- org.jetbrains.kotlin:kotlin-stdlib:1.9.25 (*)
-\--- org.wordpress.fluxc.plugins:woocommerce:trunk-06e2a179771b9d8c726a15843d56fc897b6d5ca0
-     +--- org.wordpress:fluxc:trunk-06e2a179771b9d8c726a15843d56fc897b6d5ca0 (*)
-     +--- com.google.code.gson:gson:2.8.5 -> 2.10.1
-     +--- com.google.dagger:dagger:2.51.1 (*)
-     +--- org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1 (*)
-     +--- org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1 (*)
-     +--- androidx.room:room-runtime:2.6.1 (*)
-     +--- org.wordpress:wellsql:2.0.0 (*)
-     +--- org.wordpress.fluxc:fluxc-annotations:trunk-06e2a179771b9d8c726a15843d56fc897b6d5ca0
-     +--- androidx.room:room-ktx:2.6.1 (*)
-     \--- org.jetbrains.kotlin:kotlin-stdlib:1.9.25 (*)
+\--- org.wordpress.fluxc.plugins:woocommerce:trunk-373bc6d30f8d9da6b7750f3ddc38929611b50056
+     +--- org.wordpress:fluxc:trunk-373bc6d30f8d9da6b7750f3ddc38929611b50056 (*)
+     +--- com.google.code.gson:gson:2.8.5 -> 2.10.1
+     +--- com.google.dagger:dagger:2.51.1 (*)
+     +--- org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1 (*)
+     +--- org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1 (*)
+     +--- androidx.room:room-runtime:2.6.1 (*)
+     +--- org.wordpress:wellsql:2.0.0 (*)
+     +--- org.wordpress.fluxc:fluxc-annotations:trunk-373bc6d30f8d9da6b7750f3ddc38929611b50056
+     +--- androidx.room:room-ktx:2.6.1 (*)
+     \--- org.jetbrains.kotlin:kotlin-stdlib:1.9.25 (*)

@JorgeMucientes JorgeMucientes self-assigned this Sep 30, 2024
Copy link
Contributor

@JorgeMucientes JorgeMucientes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @hichamboushaba looks good and works well!
I just left a minor suggestion. Let me know what you think about it.

Comment on lines 283 to 289
OutlinedTextField(
value = customField.key,
onValueChange = {},
label = { Text(text = stringResource(id = R.string.custom_fields_editor_key_label)) },
readOnly = true,
modifier = Modifier
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! Just a minor nitpick. From a UX point of view, having the edit text fields focusable (highlighted with the purple border) when they are not editable seems confusing.
I was wondering if we should use enabled = false instead of readOnly = true. This still allows for copy pasting (although usability is worse for selecting the text), but makes it way more apparent that the content is not editable.
Wdyt?

@keunes
Copy link

keunes commented Oct 1, 2024

That's great, thanks so much @hichamboushaba!

In terms of UX it'd be nice if the data could be 'transformed' into regular text, e.g.

Json_object As modal title

Key: Value
Key2: Value 2

Json_array As modal title

  • Key: Value

Having it as regular modal text would also address the point that @JorgeMucientes raised, while keeping exact text selection possible.

But no idea if that'd be feasible or unintended consequences. We'll already be very happy when this lands as is 🙂

@keunes
Copy link

keunes commented Oct 1, 2024

Regarding the 'Close' button; minor point, but I'd personally change it to a simple 'OK' in the bottom-right, more in line with e.g. this modal:
picturecache_0.jpg

(Currently, the 'Close' button attracts more attention than the content itself.)

@hichamboushaba
Copy link
Member Author

hichamboushaba commented Oct 1, 2024

In terms of UX it'd be nice if the data could be 'transformed' into regular text, e.g.

Thanks @keunes for the suggestion, but sadly it won't be as simple as this, as the JSON could have multiple nested levels for the fields, for example:

{ 
  "obj" : {
       "key" : "value"
  }
}

I'm sorry, but displaying the information as a list won't work in this case because we need to maintain the nesting level for the JSON values. We'll have to continue showing the JSON as raw text for now.

Having it as regular modal text would also address the point that @JorgeMucientes raised, while keeping exact text selection possible.

I don't think these points are related, Jorge's concern above is just about the "focus" indicator, when the fields are selected, they will be focused and be shown with a purple Color, and this could be confusing given the fields are not editable, I'll adjust this to remove this part later.

(Currently, the 'Close' button attracts more attention than the content itself.)

Thanks for bringing this up, I'll see what I can do for this.

Comment on lines 270 to 277
// We use this to disable focus on the text fields used to show the key and value as it's not needed for our case
val inactiveInteractionSource = remember {
object : MutableInteractionSource {
override val interactions: Flow<Interaction> = emptyFlow()
override suspend fun emit(interaction: Interaction) {}
override fun tryEmit(interaction: Interaction): Boolean = false
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! :TIL

@hichamboushaba hichamboushaba force-pushed the issue/12699-json-fields-read-only branch from 89541d1 to 5b2cae4 Compare October 1, 2024 15:19
@hichamboushaba hichamboushaba removed the status: do not merge Dependent on another PR, ready for review but not ready for merge. label Oct 1, 2024
@hichamboushaba hichamboushaba merged commit 14b24c0 into trunk Oct 1, 2024
14 of 15 checks passed
@hichamboushaba hichamboushaba deleted the issue/12699-json-fields-read-only branch October 1, 2024 16:01
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 76.74419% with 10 lines in your changes missing coverage. Please review.

Project coverage is 40.69%. Comparing base (392343e) to head (8452fc7).
Report is 15 commits behind head on trunk.

Files with missing lines Patch % Lines
...roid/ui/customfields/list/CustomFieldsViewModel.kt 84.00% 0 Missing and 4 partials ⚠️
...om/woocommerce/android/viewmodel/SavedStateFlow.kt 20.00% 2 Missing and 2 partials ⚠️
...merce/android/ui/customfields/CustomFieldModels.kt 84.61% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##              trunk   #12708      +/-   ##
============================================
+ Coverage     40.66%   40.69%   +0.02%     
- Complexity     5727     5730       +3     
============================================
  Files          1232     1232              
  Lines         69408    69445      +37     
  Branches       9621     9630       +9     
============================================
+ Hits          28228    28260      +32     
+ Misses        38578    38577       -1     
- Partials       2602     2608       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: order details Related to order details. feature: product details Related to adding or editing products, includes product settings. type: enhancement A request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Custom Fields] Handle showing JSON values in read-only mode
5 participants