Tusky-App-Android/app/src/main/res/layout
Christophe Beyls df7b11afc3
Replace Gson library with Moshi (#4309)
**! ! Warning**: Do not merge before testing every API call and database
read involving JSON !

**Gson** is obsolete and has been superseded by **Moshi**. But more
importantly, parsing Kotlin objects using Gson is _dangerous_ because
Gson uses Java serialization and is **not Kotlin-aware**. This has two
main consequences:

- Fields of non-null types may end up null at runtime. Parsing will
succeed, but the code may crash later with a `NullPointerException` when
trying to access a field member;
- Default values of constructor parameters are always ignored. When
absent, reference types will be null, booleans will be false and
integers will be zero.

On the other hand, Kotlin-aware parsers like **Moshi** or **Kotlin
Serialization** will validate at parsing time that all received fields
comply with the Kotlin contract and avoid errors at runtime, making apps
more stable and schema mismatches easier to detect (as long as logs are
accessible):

- Receiving a null value for a non-null type will generate a parsing
error;
- Optional types are declared explicitly by adding a default value. **A
missing value with no default value declaration will generate a parsing
error.**

Migrating the entity declarations from Gson to Moshi will make the code
more robust but is not an easy task because of the semantic differences.

With Gson, both nullable and optional fields are represented with a null
value. After converting to Moshi, some nullable entities can become
non-null with a default value (if they are optional and not nullable),
others can stay nullable with no default value (if they are mandatory
and nullable), and others can become **nullable with a default value of
null** (if they are optional _or_ nullable _or_ both). That third option
is the safest bet when it's not clear if a field is optional or not,
except for lists which can usually be declared as non-null with a
default value of an empty list (I have yet to see a nullable array type
in the Mastodon API).

Fields that are currently declared as non-null present another
challenge. In theory, they should remain as-is and everything will work
fine. In practice, **because Gson is not aware of nullable types at
all**, it's possible that some non-null fields currently hold a null
value in some cases but the app does not report any error because the
field is not accessed by Kotlin code in that scenario. After migrating
to Moshi however, parsing such a field will now fail early if a null
value or no value is received.

These fields will have to be identified by heavily testing the app and
looking for parsing errors (`JsonDataException`) and/or by going through
the Mastodon documentation. A default value needs to be added for
missing optional fields, and their type could optionally be changed to
nullable, depending on the case.

Gson is also currently used to serialize and deserialize objects to and
from the local database, which is also challenging because backwards
compatibility needs to be preserved. Fortunately, by default Gson omits
writing null fields, so a field of type `List<T>?` could be replaced
with a field of type `List<T>` with a default value of `emptyList()` and
reading back the old data should still work. However, nullable lists
that are written directly (not as a field of another object) will still
be serialized to JSON as `"null"` so the deserializing code must still
be handling null properly.

Finally, changing the database schema is out of scope for this pull
request, so database entities that also happen to be serialized with
Gson will keep their original types even if they could be made non-null
as an improvement.

In the end this is all for the best, because the app will be more
reliable and errors will be easier to detect by showing up earlier with
a clear error message. Not to mention the performance benefits of using
Moshi compared to Gson.

- Replace Gson reflection with Moshi Kotlin codegen to generate all
parsers at compile time.
- Replace custom `Rfc3339DateJsonAdapter` with the one provided by
moshi-adapters.
- Replace custom `JsonDeserializer` classes for Enum types with
`EnumJsonAdapter.create(T).withUnknownFallback()` from moshi-adapters to
support fallback values.
- Replace `GuardedBooleanAdapter` with the more generic `GuardedAdapter`
which works with any type. Any nullable field may now be annotated with
`@Guarded`.
- Remove Proguard rules related to Json entities. Each Json entity needs
to be annotated with `@JsonClass` with no exception, and adding this
annotation will ensure that R8/Proguard will handle the entities
properly.
- Replace some nullable Boolean fields with non-null Boolean fields with
a default value where possible.
- Replace some nullable list fields with non-null list fields with a
default value of `emptyList()` where possible.
- Update `TimelineDao` to perform all Json conversions internally using
`Converters` so no Gson or Moshi instance has to be passed to its
methods.
- ~~Create a custom `DraftAttachmentJsonAdapter` to serialize and
deserialize `DraftAttachment` which is a special entity that supports
more than one json name per field. A custom adapter is necessary because
there is not direct equivalent of `@SerializedName(alternate = [...])`
in Moshi.~~ Remove alternate names for some `DraftAttachment` fields
which were used as a workaround to deserialize local data in 2-years old
builds of Tusky.
- Update tests to make them work with Moshi.
- Simplify a few `equals()` implementations.
- Change a few functions to `val`s
- Turn `NetworkModule` into an `object` (since it contains no abstract
methods).

Please test the app thoroughly before merging. There may be some fields
currently declared as mandatory that are actually optional.
2024-04-02 21:01:04 +02:00
..
activity_about.xml Show additional bug report info in AboutActivity (#3802) 2023-08-03 12:20:35 +02:00
activity_account.xml don't cut off names on profiles (#4052) 2023-10-09 15:43:02 +02:00
activity_account_list.xml Make links in bios of follow request and follow notifications work (#3646) 2023-05-13 15:32:56 +02:00
activity_announcements.xml Officially rename error elephant to errorphant (#3922) 2023-08-05 15:39:07 +02:00
activity_compose.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
activity_drafts.xml Officially rename error elephant to errorphant (#3922) 2023-08-05 15:39:07 +02:00
activity_edit_filter.xml Add delete button to edit filter activity. (#3553) 2023-06-11 19:59:26 +02:00
activity_edit_profile.xml update Android Image Cropper and get rid of deprecated onActivityResult (#2351) 2022-03-02 20:39:56 +01:00
activity_filters.xml 3503: SwipeRefreshLayout must be higher level (#3504) 2023-04-20 19:36:29 +02:00
activity_followed_tags.xml Adjust list UX for platform consistency (#3142) 2023-06-29 18:36:19 +02:00
activity_license.xml Replace Gson library with Moshi (#4309) 2024-04-02 21:01:04 +02:00
activity_lists.xml 3503: SwipeRefreshLayout must be higher level (#3504) 2023-04-20 19:36:29 +02:00
activity_login.xml Implement Login via WebView (#2371) 2022-03-08 21:22:19 +01:00
activity_login_webview.xml show rules on the login screen (#2746) 2022-11-05 17:37:20 +01:00
activity_main.xml 2512: Set style properly 2023-10-11 21:00:59 +02:00
activity_preferences.xml migrating to ViewBinding part 2: Activities (#2093) 2021-03-07 19:05:51 +01:00
activity_report.xml migrating to ViewBinding part 2: Activities (#2093) 2021-03-07 19:05:51 +01:00
activity_scheduled_status.xml Officially rename error elephant to errorphant (#3922) 2023-08-05 15:39:07 +02:00
activity_search.xml Adjust list UX for platform consistency (#3142) 2023-06-29 18:36:19 +02:00
activity_statuslist.xml merge ModalTimelineActivity & ViewTagActivity into StatusListActivity (#2332) 2022-02-25 18:57:31 +01:00
activity_tab_preference.xml 2512: Allow more than 5 tabs (#4051) 2023-10-11 09:54:01 +02:00
activity_trending.xml Add trending tags (#3149) 2023-02-14 19:52:11 +01:00
activity_view_media.xml Theme refactoring (#1656) 2020-01-30 21:37:28 +01:00
activity_view_thread.xml Add "Refresh" accessibility menu (#3121) 2023-03-01 19:58:18 +01:00
card_license.xml explicitly set text colors in LicenseCard (#1910) 2020-08-26 18:46:44 +02:00
dialog_add_poll.xml Instance configuration: the easy parts (#2341) 2022-03-01 19:43:36 +01:00
dialog_filter.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
dialog_focus.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
dialog_follow_hashtag.xml Add FAB to follow new hashtags from FollowedTagsActivity (#3275) 2023-02-20 20:14:16 +01:00
dialog_image_description.xml 4063: Make dialog size more stable (#4066) 2023-10-25 11:59:59 +02:00
dialog_list.xml Support "replies policy" for lists (#4072) 2023-10-26 11:21:04 +02:00
dialog_mute_account.xml migrating to ViewBinding part 1: Dialogs + Views (#2091) 2021-03-07 19:04:22 +01:00
exo_player_control_view.xml Exoplayer: Increase space between rewind, pause, ffwd buttons (#4077) 2023-11-01 09:21:43 +01:00
fragment_account_list.xml 3503: SwipeRefreshLayout must be higher level (#3504) 2023-04-20 19:36:29 +02:00
fragment_accounts_in_list.xml Officially rename error elephant to errorphant (#3922) 2023-08-05 15:39:07 +02:00
fragment_domain_blocks.xml ui improvements 2023-07-05 20:11:12 +02:00
fragment_lists_list.xml 3488 improve profile list (#3507) 2024-01-03 21:17:03 +01:00
fragment_report_done.xml Redesign report activity (#1295) 2019-06-09 16:55:34 +02:00
fragment_report_note.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
fragment_report_statuses.xml Redesign report activity (#1295) 2019-06-09 16:55:34 +02:00
fragment_search.xml migrating to ViewBinding part 4: Fragments (#2108) 2021-03-13 21:27:20 +01:00
fragment_timeline.xml 3503: SwipeRefreshLayout must be higher level (#3504) 2023-04-20 19:36:29 +02:00
fragment_timeline_notifications.xml Add the special appbar for the notifications again (#4022) 2023-09-13 09:21:08 +02:00
fragment_trending_tags.xml Rename "Trending" to "TrendingTags" or similar where necessary (#3906) 2023-08-19 12:54:35 +02:00
fragment_view_edits.xml Remove ReplacementSpan, display diffs using CharacterStyle (#3431) 2023-06-11 19:12:05 +02:00
fragment_view_image.xml Fix image zoom / pan / scroll / swipe (#3894) 2023-07-31 12:44:01 +02:00
fragment_view_thread.xml Set statusView width/height to match_parent (#3844) 2023-07-13 17:26:12 +02:00
fragment_view_video.xml Regularize show/hide logic for video player scrub/play controls (fixes #4073) (#4117) 2023-11-23 08:32:01 +01:00
item_account.xml Clean up Account adapters (#3202) 2023-02-04 20:29:13 +01:00
item_account_field.xml By explicit about the `firstStrong` text direction on UGC (#3328) 2023-03-02 18:30:26 +01:00
item_account_media.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
item_add_poll_option.xml Theme refactoring (#1656) 2020-01-30 21:37:28 +01:00
item_announcement.xml Displayed the date of each announcement. (#4041) 2023-10-10 19:17:06 +02:00
item_autocomplete_account.xml modernize autocomplete (#2510) 2022-05-17 19:55:37 +02:00
item_autocomplete_emoji.xml modernize autocomplete (#2510) 2022-05-17 19:55:37 +02:00
item_autocomplete_hashtag.xml modernize autocomplete (#2510) 2022-05-17 19:55:37 +02:00
item_blocked_domain.xml rename item_muted_domain to item_blocked_domain 2023-07-05 19:23:04 +02:00
item_blocked_user.xml Clean up Account adapters (#3202) 2023-02-04 20:29:13 +01:00
item_conversation.xml Meet accessibility guidelines for clickable spans (#3382) 2023-03-18 08:57:39 +01:00
item_draft.xml New emoji picker (#2395) 2022-04-26 18:50:58 +02:00
item_edit_field.xml Handle even more instance defaults (#2612) 2022-07-26 20:24:50 +02:00
item_emoji_button.xml ComposeActivity improvements (#548) 2018-04-13 22:37:21 +02:00
item_follow.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
item_follow_request.xml Make links in bios of follow request and follow notifications work (#3646) 2023-05-13 15:32:56 +02:00
item_follow_requests_header.xml always show follow requests in main menu (#1809) 2021-04-10 20:30:44 +02:00
item_followed_hashtag.xml Adjust list UX for platform consistency (#3142) 2023-06-29 18:36:19 +02:00
item_footer.xml fix account list loading and clean up a lot of code (#823) 2018-08-31 21:52:09 +02:00
item_hashtag.xml Adjust list UX for platform consistency (#3142) 2023-06-29 18:36:19 +02:00
item_image_preview_overlay.xml issue 2890: Add an "ALT" sticker to the media preview container (#2942) 2022-12-18 16:50:30 +01:00
item_list.xml 3488 improve profile list (#3507) 2024-01-03 21:17:03 +01:00
item_media_preview.xml Add background to sensitive media button (#3939) 2023-08-08 23:08:35 +02:00
item_muted_user.xml Clean up Account adapters (#3202) 2023-02-04 20:29:13 +01:00
item_network_state.xml Tab customization & direct messages tab (#1012) 2019-02-12 19:22:37 +01:00
item_notifications_load_state_footer_view.xml Convert NotificationsFragment and related code to Kotlin, use the Paging library (#3159) 2023-03-10 20:12:33 +01:00
item_poll.xml Allow multiline polls (this time for real) (#3608) 2023-05-03 09:05:05 +02:00
item_poll_preview_option.xml remove ThemeUtils.getTintedDrawable (#2015) 2020-12-09 19:08:16 +01:00
item_removable.xml Support the mastodon 4 filter api (#3188) 2023-03-11 13:12:50 +01:00
item_report_notification.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
item_report_status.xml Add background to sensitive media button (#3939) 2023-08-08 23:08:35 +02:00
item_scheduled_status.xml New emoji picker (#2395) 2022-04-26 18:50:58 +02:00
item_status.xml Machine translation of posts (#4307) 2024-03-09 16:12:18 +01:00
item_status_bottom_sheet.xml Upgrade to AndroidX, move to MaterialComponents theme (#953) 2018-12-17 15:25:35 +01:00
item_status_detailed.xml Machine translation of posts (#4307) 2024-03-09 16:12:18 +01:00
item_status_edit.xml Remove ReplacementSpan, display diffs using CharacterStyle (#3431) 2023-06-11 19:12:05 +02:00
item_status_filtered.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
item_status_notification.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
item_status_placeholder.xml 3311 load more more prominent (#3376) 2023-02-27 11:33:11 +01:00
item_status_wrapper.xml Support the mastodon 4 filter api (#3188) 2023-03-11 13:12:50 +01:00
item_tab_preference.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
item_tab_preference_small.xml Adjust list UX for platform consistency (#3142) 2023-06-29 18:36:19 +02:00
item_trending_cell.xml Fix trending tags being cut off (#3745) 2023-06-15 11:27:57 +02:00
item_trending_date.xml Add trending tags (#3149) 2023-02-14 19:52:11 +01:00
material_drawer_header.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00
notifications_filter.xml Resets the paging3 changes of 3159 back to the (java) fragment code before. 2023-09-09 21:29:24 +02:00
pref_slider.xml Provide a preference to scale all UI text (#3248) 2023-06-29 18:34:56 +02:00
search_view.xml Upgrade to AndroidX, move to MaterialComponents theme (#953) 2018-12-17 15:25:35 +01:00
simple_list_item_1.xml Convert NotificationsFragment and related code to Kotlin, use the Paging library (#3159) 2023-03-10 20:12:33 +01:00
toolbar_basic.xml Drafts v2 (#2032) 2021-01-21 18:57:09 +01:00
view_background_message.xml Officially rename error elephant to errorphant (#3922) 2023-08-05 15:39:07 +02:00
view_compose_options.xml simplify ComposeOptionsView (#1734) 2020-03-24 21:07:10 +01:00
view_compose_schedule.xml Use colorPrimary for the "edit" schedule icon (#3917) 2023-08-04 13:37:31 +02:00
view_poll_preview.xml Fix lint warnings (#4019) 2023-09-13 09:20:53 +02:00