Tusky-App-Android/app/src/main/java/com/keylesspalace/tusky/entity/TrendingTagsResult.kt

53 lines
1.9 KiB
Kotlin
Raw Normal View History

Add trending tags (#3149) * Add initial feature for viewing trending graphs. Currently only views hash tag trends. Contains API additions, tab additions and a set of trending components. * Add clickable system through a LinkListener. Duplicates a little code from SFragment. * Add accessibility description. * The background for the graph should match the background for black theme too. * Add error handling through a state flow system using existing code as an example. * Graphing: Use a primary and a secondary line. Remove under line fill. Apply line thickness. Dotted end of line. * Trending changes: New layout for trending: Cell. Use ViewBinding. Add padding to RecyclerView to stop the FAB from hiding content. Multiple bugs in GraphView resolved. Wide (landscape, for example) will show 4 columns, portrait will show 2. Remove unused base holder class. ViewModel invalidate scoping changed. Some renaming to variables made. For uses and accounts, use longs. These could be big numbers eventually. TagViewHolder renamed to TrendingTagViewHolder. * Trending changes: Remove old layout. Update cell textsizes and use proper string. Remove bad comment. * Trending changes: Refresh the main drawer when the tabs are edited. This will allow the trending item to toggle. * Trending changes: Add a trending activity to be able to view the trending data from the main drawer. * Trending changes: The title text should be changed to Trending Hashtags. * Trending changes: Add meta color to draw axis etc. Draw the date boundaries on the graph. Remove dates from each cell and place them in the list as a header. Graphs should be proportional to the highest historical value. Add a new interface to control whether the FAB should be visible (important when switching tabs, the state is lost). Add header to the adapter and viewdata structures. Add QOL extensions for getting the dates from history. * Trending changes: Refresh FAB through the main activity and FabFragment interface. Trending has no FAB. * Trending changes: Make graph proportional to the highest usage value. Fixes to the graph ratio calculations. * Trending changes: KtLintFix * Trending changes: Remove accidental build gradle change. Remove trending cases. Remove unused progress. Set drawer button addition explicitly to false, leaving the code there for future issue #3010. Remove unnecessary arguments for intent. Remove media preview preferences, there is nothing to preview. No padding between hashtag symbol and text. Do not ellipsize hashtags. * Trending changes: Use bottomsheet slide in animation helper for opening the hashtag intent. Remove explicit layout height from the XML and apply it to the view holder itself. The height was not being respected in XML. * Use some platform standards for styling - Align on an 8dp grid - Use android:attr for paddingStart and paddingEnd - Use textAppearanceListItem variants - Adjust constraints to handle different size containers * Correct lineWidth calculations Previous code didn't convert the value to pixels, so it was always displaying as a hairline stroke, irrespective of the value in the layout file. While I'm here, rename from lineThickness to lineWidth, to be consistent with parameters like strokeWidth. * Does not need to inherit from FabFragment * Rename to TrendingAdapter "Paging" in the adapter name is a misnomer here * Clean up comments, use full class name as tag * Simplify TrendingViewModel - Remove unncessary properties - Fetch tags and map in invalidate() - emptyList() instead of listOf() for clarity * Remove line dividers, use X-axis to separate content Experiment with UI -- instead of dividers between each item, draw an explicit x-axis for each chart, and add a little more vertical padding, to see if that provides a cleaner separation between the content * Adjust date format - Show day and year - Use platform attributes for size * Locale-aware format of numbers Format numbers < 100,000 by inserting locale-aware separators. Numbers larger are scaled and have K, M, G, ... etc suffix appended. * Prevent a crash if viewData is empty Don't access viewData without first checking if it's empty. This can be the case if the server returned an empty list for some reason, or the data has been filtered. * Filter out tags the user has filtered from their home timeline Invalidate the list if the user's preferences change, as that may indicate they've changed their filters. * Experiment with alternative layout * Set chart height to 160dp to align to an 8dp grid * Draw ticks that are 5% the height of the x-axis * Legend adjustments - Use tuskyblue for the ticks - Wrap legend components in a layout so they can have a dedicated background - Use a 60% transparent background for the legend to retain legibility if lines go under it * Bezier curves, shorter cell height * More tweaks - List tags in order of popularity, most popular first - Make it clear that uses/accounts in the legend are totals, not current - Show current values at end of the chart * Hide FAB * Fix crash, it's not always hosted in an ActionButtonActivity * Arrange totals vertically in landscape layout * Always add the Trending drawer menu if it's not a tab * Revert unrelated whitespace changes * One more whitespace revert --------- Co-authored-by: Nik Clayton <nik@ngo.org.uk>
2023-02-14 19:52:11 +01:00
/* Copyright 2023 Tusky Contributors
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.entity
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
import com.squareup.moshi.JsonClass
Add trending tags (#3149) * Add initial feature for viewing trending graphs. Currently only views hash tag trends. Contains API additions, tab additions and a set of trending components. * Add clickable system through a LinkListener. Duplicates a little code from SFragment. * Add accessibility description. * The background for the graph should match the background for black theme too. * Add error handling through a state flow system using existing code as an example. * Graphing: Use a primary and a secondary line. Remove under line fill. Apply line thickness. Dotted end of line. * Trending changes: New layout for trending: Cell. Use ViewBinding. Add padding to RecyclerView to stop the FAB from hiding content. Multiple bugs in GraphView resolved. Wide (landscape, for example) will show 4 columns, portrait will show 2. Remove unused base holder class. ViewModel invalidate scoping changed. Some renaming to variables made. For uses and accounts, use longs. These could be big numbers eventually. TagViewHolder renamed to TrendingTagViewHolder. * Trending changes: Remove old layout. Update cell textsizes and use proper string. Remove bad comment. * Trending changes: Refresh the main drawer when the tabs are edited. This will allow the trending item to toggle. * Trending changes: Add a trending activity to be able to view the trending data from the main drawer. * Trending changes: The title text should be changed to Trending Hashtags. * Trending changes: Add meta color to draw axis etc. Draw the date boundaries on the graph. Remove dates from each cell and place them in the list as a header. Graphs should be proportional to the highest historical value. Add a new interface to control whether the FAB should be visible (important when switching tabs, the state is lost). Add header to the adapter and viewdata structures. Add QOL extensions for getting the dates from history. * Trending changes: Refresh FAB through the main activity and FabFragment interface. Trending has no FAB. * Trending changes: Make graph proportional to the highest usage value. Fixes to the graph ratio calculations. * Trending changes: KtLintFix * Trending changes: Remove accidental build gradle change. Remove trending cases. Remove unused progress. Set drawer button addition explicitly to false, leaving the code there for future issue #3010. Remove unnecessary arguments for intent. Remove media preview preferences, there is nothing to preview. No padding between hashtag symbol and text. Do not ellipsize hashtags. * Trending changes: Use bottomsheet slide in animation helper for opening the hashtag intent. Remove explicit layout height from the XML and apply it to the view holder itself. The height was not being respected in XML. * Use some platform standards for styling - Align on an 8dp grid - Use android:attr for paddingStart and paddingEnd - Use textAppearanceListItem variants - Adjust constraints to handle different size containers * Correct lineWidth calculations Previous code didn't convert the value to pixels, so it was always displaying as a hairline stroke, irrespective of the value in the layout file. While I'm here, rename from lineThickness to lineWidth, to be consistent with parameters like strokeWidth. * Does not need to inherit from FabFragment * Rename to TrendingAdapter "Paging" in the adapter name is a misnomer here * Clean up comments, use full class name as tag * Simplify TrendingViewModel - Remove unncessary properties - Fetch tags and map in invalidate() - emptyList() instead of listOf() for clarity * Remove line dividers, use X-axis to separate content Experiment with UI -- instead of dividers between each item, draw an explicit x-axis for each chart, and add a little more vertical padding, to see if that provides a cleaner separation between the content * Adjust date format - Show day and year - Use platform attributes for size * Locale-aware format of numbers Format numbers < 100,000 by inserting locale-aware separators. Numbers larger are scaled and have K, M, G, ... etc suffix appended. * Prevent a crash if viewData is empty Don't access viewData without first checking if it's empty. This can be the case if the server returned an empty list for some reason, or the data has been filtered. * Filter out tags the user has filtered from their home timeline Invalidate the list if the user's preferences change, as that may indicate they've changed their filters. * Experiment with alternative layout * Set chart height to 160dp to align to an 8dp grid * Draw ticks that are 5% the height of the x-axis * Legend adjustments - Use tuskyblue for the ticks - Wrap legend components in a layout so they can have a dedicated background - Use a 60% transparent background for the legend to retain legibility if lines go under it * Bezier curves, shorter cell height * More tweaks - List tags in order of popularity, most popular first - Make it clear that uses/accounts in the legend are totals, not current - Show current values at end of the chart * Hide FAB * Fix crash, it's not always hosted in an ActionButtonActivity * Arrange totals vertically in landscape layout * Always add the Trending drawer menu if it's not a tab * Revert unrelated whitespace changes * One more whitespace revert --------- Co-authored-by: Nik Clayton <nik@ngo.org.uk>
2023-02-14 19:52:11 +01:00
import java.util.Date
/**
* Mastodon API Documentation: https://docs.joinmastodon.org/methods/trends/#tags
*
* @param name The name of the hashtag (after the #). The "caturday" in "#caturday".
* (@param url The URL to your mastodon instance list for this hashtag.)
Add trending tags (#3149) * Add initial feature for viewing trending graphs. Currently only views hash tag trends. Contains API additions, tab additions and a set of trending components. * Add clickable system through a LinkListener. Duplicates a little code from SFragment. * Add accessibility description. * The background for the graph should match the background for black theme too. * Add error handling through a state flow system using existing code as an example. * Graphing: Use a primary and a secondary line. Remove under line fill. Apply line thickness. Dotted end of line. * Trending changes: New layout for trending: Cell. Use ViewBinding. Add padding to RecyclerView to stop the FAB from hiding content. Multiple bugs in GraphView resolved. Wide (landscape, for example) will show 4 columns, portrait will show 2. Remove unused base holder class. ViewModel invalidate scoping changed. Some renaming to variables made. For uses and accounts, use longs. These could be big numbers eventually. TagViewHolder renamed to TrendingTagViewHolder. * Trending changes: Remove old layout. Update cell textsizes and use proper string. Remove bad comment. * Trending changes: Refresh the main drawer when the tabs are edited. This will allow the trending item to toggle. * Trending changes: Add a trending activity to be able to view the trending data from the main drawer. * Trending changes: The title text should be changed to Trending Hashtags. * Trending changes: Add meta color to draw axis etc. Draw the date boundaries on the graph. Remove dates from each cell and place them in the list as a header. Graphs should be proportional to the highest historical value. Add a new interface to control whether the FAB should be visible (important when switching tabs, the state is lost). Add header to the adapter and viewdata structures. Add QOL extensions for getting the dates from history. * Trending changes: Refresh FAB through the main activity and FabFragment interface. Trending has no FAB. * Trending changes: Make graph proportional to the highest usage value. Fixes to the graph ratio calculations. * Trending changes: KtLintFix * Trending changes: Remove accidental build gradle change. Remove trending cases. Remove unused progress. Set drawer button addition explicitly to false, leaving the code there for future issue #3010. Remove unnecessary arguments for intent. Remove media preview preferences, there is nothing to preview. No padding between hashtag symbol and text. Do not ellipsize hashtags. * Trending changes: Use bottomsheet slide in animation helper for opening the hashtag intent. Remove explicit layout height from the XML and apply it to the view holder itself. The height was not being respected in XML. * Use some platform standards for styling - Align on an 8dp grid - Use android:attr for paddingStart and paddingEnd - Use textAppearanceListItem variants - Adjust constraints to handle different size containers * Correct lineWidth calculations Previous code didn't convert the value to pixels, so it was always displaying as a hairline stroke, irrespective of the value in the layout file. While I'm here, rename from lineThickness to lineWidth, to be consistent with parameters like strokeWidth. * Does not need to inherit from FabFragment * Rename to TrendingAdapter "Paging" in the adapter name is a misnomer here * Clean up comments, use full class name as tag * Simplify TrendingViewModel - Remove unncessary properties - Fetch tags and map in invalidate() - emptyList() instead of listOf() for clarity * Remove line dividers, use X-axis to separate content Experiment with UI -- instead of dividers between each item, draw an explicit x-axis for each chart, and add a little more vertical padding, to see if that provides a cleaner separation between the content * Adjust date format - Show day and year - Use platform attributes for size * Locale-aware format of numbers Format numbers < 100,000 by inserting locale-aware separators. Numbers larger are scaled and have K, M, G, ... etc suffix appended. * Prevent a crash if viewData is empty Don't access viewData without first checking if it's empty. This can be the case if the server returned an empty list for some reason, or the data has been filtered. * Filter out tags the user has filtered from their home timeline Invalidate the list if the user's preferences change, as that may indicate they've changed their filters. * Experiment with alternative layout * Set chart height to 160dp to align to an 8dp grid * Draw ticks that are 5% the height of the x-axis * Legend adjustments - Use tuskyblue for the ticks - Wrap legend components in a layout so they can have a dedicated background - Use a 60% transparent background for the legend to retain legibility if lines go under it * Bezier curves, shorter cell height * More tweaks - List tags in order of popularity, most popular first - Make it clear that uses/accounts in the legend are totals, not current - Show current values at end of the chart * Hide FAB * Fix crash, it's not always hosted in an ActionButtonActivity * Arrange totals vertically in landscape layout * Always add the Trending drawer menu if it's not a tab * Revert unrelated whitespace changes * One more whitespace revert --------- Co-authored-by: Nik Clayton <nik@ngo.org.uk>
2023-02-14 19:52:11 +01:00
* @param history A list of [TrendingTagHistory]. Each element contains metrics per day for this hashtag.
* (@param following This is not listed in the APIs at the time of writing, but an instance is delivering it.)
Add trending tags (#3149) * Add initial feature for viewing trending graphs. Currently only views hash tag trends. Contains API additions, tab additions and a set of trending components. * Add clickable system through a LinkListener. Duplicates a little code from SFragment. * Add accessibility description. * The background for the graph should match the background for black theme too. * Add error handling through a state flow system using existing code as an example. * Graphing: Use a primary and a secondary line. Remove under line fill. Apply line thickness. Dotted end of line. * Trending changes: New layout for trending: Cell. Use ViewBinding. Add padding to RecyclerView to stop the FAB from hiding content. Multiple bugs in GraphView resolved. Wide (landscape, for example) will show 4 columns, portrait will show 2. Remove unused base holder class. ViewModel invalidate scoping changed. Some renaming to variables made. For uses and accounts, use longs. These could be big numbers eventually. TagViewHolder renamed to TrendingTagViewHolder. * Trending changes: Remove old layout. Update cell textsizes and use proper string. Remove bad comment. * Trending changes: Refresh the main drawer when the tabs are edited. This will allow the trending item to toggle. * Trending changes: Add a trending activity to be able to view the trending data from the main drawer. * Trending changes: The title text should be changed to Trending Hashtags. * Trending changes: Add meta color to draw axis etc. Draw the date boundaries on the graph. Remove dates from each cell and place them in the list as a header. Graphs should be proportional to the highest historical value. Add a new interface to control whether the FAB should be visible (important when switching tabs, the state is lost). Add header to the adapter and viewdata structures. Add QOL extensions for getting the dates from history. * Trending changes: Refresh FAB through the main activity and FabFragment interface. Trending has no FAB. * Trending changes: Make graph proportional to the highest usage value. Fixes to the graph ratio calculations. * Trending changes: KtLintFix * Trending changes: Remove accidental build gradle change. Remove trending cases. Remove unused progress. Set drawer button addition explicitly to false, leaving the code there for future issue #3010. Remove unnecessary arguments for intent. Remove media preview preferences, there is nothing to preview. No padding between hashtag symbol and text. Do not ellipsize hashtags. * Trending changes: Use bottomsheet slide in animation helper for opening the hashtag intent. Remove explicit layout height from the XML and apply it to the view holder itself. The height was not being respected in XML. * Use some platform standards for styling - Align on an 8dp grid - Use android:attr for paddingStart and paddingEnd - Use textAppearanceListItem variants - Adjust constraints to handle different size containers * Correct lineWidth calculations Previous code didn't convert the value to pixels, so it was always displaying as a hairline stroke, irrespective of the value in the layout file. While I'm here, rename from lineThickness to lineWidth, to be consistent with parameters like strokeWidth. * Does not need to inherit from FabFragment * Rename to TrendingAdapter "Paging" in the adapter name is a misnomer here * Clean up comments, use full class name as tag * Simplify TrendingViewModel - Remove unncessary properties - Fetch tags and map in invalidate() - emptyList() instead of listOf() for clarity * Remove line dividers, use X-axis to separate content Experiment with UI -- instead of dividers between each item, draw an explicit x-axis for each chart, and add a little more vertical padding, to see if that provides a cleaner separation between the content * Adjust date format - Show day and year - Use platform attributes for size * Locale-aware format of numbers Format numbers < 100,000 by inserting locale-aware separators. Numbers larger are scaled and have K, M, G, ... etc suffix appended. * Prevent a crash if viewData is empty Don't access viewData without first checking if it's empty. This can be the case if the server returned an empty list for some reason, or the data has been filtered. * Filter out tags the user has filtered from their home timeline Invalidate the list if the user's preferences change, as that may indicate they've changed their filters. * Experiment with alternative layout * Set chart height to 160dp to align to an 8dp grid * Draw ticks that are 5% the height of the x-axis * Legend adjustments - Use tuskyblue for the ticks - Wrap legend components in a layout so they can have a dedicated background - Use a 60% transparent background for the legend to retain legibility if lines go under it * Bezier curves, shorter cell height * More tweaks - List tags in order of popularity, most popular first - Make it clear that uses/accounts in the legend are totals, not current - Show current values at end of the chart * Hide FAB * Fix crash, it's not always hosted in an ActionButtonActivity * Arrange totals vertically in landscape layout * Always add the Trending drawer menu if it's not a tab * Revert unrelated whitespace changes * One more whitespace revert --------- Co-authored-by: Nik Clayton <nik@ngo.org.uk>
2023-02-14 19:52:11 +01:00
*/
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
@JsonClass(generateAdapter = true)
Add trending tags (#3149) * Add initial feature for viewing trending graphs. Currently only views hash tag trends. Contains API additions, tab additions and a set of trending components. * Add clickable system through a LinkListener. Duplicates a little code from SFragment. * Add accessibility description. * The background for the graph should match the background for black theme too. * Add error handling through a state flow system using existing code as an example. * Graphing: Use a primary and a secondary line. Remove under line fill. Apply line thickness. Dotted end of line. * Trending changes: New layout for trending: Cell. Use ViewBinding. Add padding to RecyclerView to stop the FAB from hiding content. Multiple bugs in GraphView resolved. Wide (landscape, for example) will show 4 columns, portrait will show 2. Remove unused base holder class. ViewModel invalidate scoping changed. Some renaming to variables made. For uses and accounts, use longs. These could be big numbers eventually. TagViewHolder renamed to TrendingTagViewHolder. * Trending changes: Remove old layout. Update cell textsizes and use proper string. Remove bad comment. * Trending changes: Refresh the main drawer when the tabs are edited. This will allow the trending item to toggle. * Trending changes: Add a trending activity to be able to view the trending data from the main drawer. * Trending changes: The title text should be changed to Trending Hashtags. * Trending changes: Add meta color to draw axis etc. Draw the date boundaries on the graph. Remove dates from each cell and place them in the list as a header. Graphs should be proportional to the highest historical value. Add a new interface to control whether the FAB should be visible (important when switching tabs, the state is lost). Add header to the adapter and viewdata structures. Add QOL extensions for getting the dates from history. * Trending changes: Refresh FAB through the main activity and FabFragment interface. Trending has no FAB. * Trending changes: Make graph proportional to the highest usage value. Fixes to the graph ratio calculations. * Trending changes: KtLintFix * Trending changes: Remove accidental build gradle change. Remove trending cases. Remove unused progress. Set drawer button addition explicitly to false, leaving the code there for future issue #3010. Remove unnecessary arguments for intent. Remove media preview preferences, there is nothing to preview. No padding between hashtag symbol and text. Do not ellipsize hashtags. * Trending changes: Use bottomsheet slide in animation helper for opening the hashtag intent. Remove explicit layout height from the XML and apply it to the view holder itself. The height was not being respected in XML. * Use some platform standards for styling - Align on an 8dp grid - Use android:attr for paddingStart and paddingEnd - Use textAppearanceListItem variants - Adjust constraints to handle different size containers * Correct lineWidth calculations Previous code didn't convert the value to pixels, so it was always displaying as a hairline stroke, irrespective of the value in the layout file. While I'm here, rename from lineThickness to lineWidth, to be consistent with parameters like strokeWidth. * Does not need to inherit from FabFragment * Rename to TrendingAdapter "Paging" in the adapter name is a misnomer here * Clean up comments, use full class name as tag * Simplify TrendingViewModel - Remove unncessary properties - Fetch tags and map in invalidate() - emptyList() instead of listOf() for clarity * Remove line dividers, use X-axis to separate content Experiment with UI -- instead of dividers between each item, draw an explicit x-axis for each chart, and add a little more vertical padding, to see if that provides a cleaner separation between the content * Adjust date format - Show day and year - Use platform attributes for size * Locale-aware format of numbers Format numbers < 100,000 by inserting locale-aware separators. Numbers larger are scaled and have K, M, G, ... etc suffix appended. * Prevent a crash if viewData is empty Don't access viewData without first checking if it's empty. This can be the case if the server returned an empty list for some reason, or the data has been filtered. * Filter out tags the user has filtered from their home timeline Invalidate the list if the user's preferences change, as that may indicate they've changed their filters. * Experiment with alternative layout * Set chart height to 160dp to align to an 8dp grid * Draw ticks that are 5% the height of the x-axis * Legend adjustments - Use tuskyblue for the ticks - Wrap legend components in a layout so they can have a dedicated background - Use a 60% transparent background for the legend to retain legibility if lines go under it * Bezier curves, shorter cell height * More tweaks - List tags in order of popularity, most popular first - Make it clear that uses/accounts in the legend are totals, not current - Show current values at end of the chart * Hide FAB * Fix crash, it's not always hosted in an ActionButtonActivity * Arrange totals vertically in landscape layout * Always add the Trending drawer menu if it's not a tab * Revert unrelated whitespace changes * One more whitespace revert --------- Co-authored-by: Nik Clayton <nik@ngo.org.uk>
2023-02-14 19:52:11 +01:00
data class TrendingTag(
val name: String,
val history: List<TrendingTagHistory>
Add trending tags (#3149) * Add initial feature for viewing trending graphs. Currently only views hash tag trends. Contains API additions, tab additions and a set of trending components. * Add clickable system through a LinkListener. Duplicates a little code from SFragment. * Add accessibility description. * The background for the graph should match the background for black theme too. * Add error handling through a state flow system using existing code as an example. * Graphing: Use a primary and a secondary line. Remove under line fill. Apply line thickness. Dotted end of line. * Trending changes: New layout for trending: Cell. Use ViewBinding. Add padding to RecyclerView to stop the FAB from hiding content. Multiple bugs in GraphView resolved. Wide (landscape, for example) will show 4 columns, portrait will show 2. Remove unused base holder class. ViewModel invalidate scoping changed. Some renaming to variables made. For uses and accounts, use longs. These could be big numbers eventually. TagViewHolder renamed to TrendingTagViewHolder. * Trending changes: Remove old layout. Update cell textsizes and use proper string. Remove bad comment. * Trending changes: Refresh the main drawer when the tabs are edited. This will allow the trending item to toggle. * Trending changes: Add a trending activity to be able to view the trending data from the main drawer. * Trending changes: The title text should be changed to Trending Hashtags. * Trending changes: Add meta color to draw axis etc. Draw the date boundaries on the graph. Remove dates from each cell and place them in the list as a header. Graphs should be proportional to the highest historical value. Add a new interface to control whether the FAB should be visible (important when switching tabs, the state is lost). Add header to the adapter and viewdata structures. Add QOL extensions for getting the dates from history. * Trending changes: Refresh FAB through the main activity and FabFragment interface. Trending has no FAB. * Trending changes: Make graph proportional to the highest usage value. Fixes to the graph ratio calculations. * Trending changes: KtLintFix * Trending changes: Remove accidental build gradle change. Remove trending cases. Remove unused progress. Set drawer button addition explicitly to false, leaving the code there for future issue #3010. Remove unnecessary arguments for intent. Remove media preview preferences, there is nothing to preview. No padding between hashtag symbol and text. Do not ellipsize hashtags. * Trending changes: Use bottomsheet slide in animation helper for opening the hashtag intent. Remove explicit layout height from the XML and apply it to the view holder itself. The height was not being respected in XML. * Use some platform standards for styling - Align on an 8dp grid - Use android:attr for paddingStart and paddingEnd - Use textAppearanceListItem variants - Adjust constraints to handle different size containers * Correct lineWidth calculations Previous code didn't convert the value to pixels, so it was always displaying as a hairline stroke, irrespective of the value in the layout file. While I'm here, rename from lineThickness to lineWidth, to be consistent with parameters like strokeWidth. * Does not need to inherit from FabFragment * Rename to TrendingAdapter "Paging" in the adapter name is a misnomer here * Clean up comments, use full class name as tag * Simplify TrendingViewModel - Remove unncessary properties - Fetch tags and map in invalidate() - emptyList() instead of listOf() for clarity * Remove line dividers, use X-axis to separate content Experiment with UI -- instead of dividers between each item, draw an explicit x-axis for each chart, and add a little more vertical padding, to see if that provides a cleaner separation between the content * Adjust date format - Show day and year - Use platform attributes for size * Locale-aware format of numbers Format numbers < 100,000 by inserting locale-aware separators. Numbers larger are scaled and have K, M, G, ... etc suffix appended. * Prevent a crash if viewData is empty Don't access viewData without first checking if it's empty. This can be the case if the server returned an empty list for some reason, or the data has been filtered. * Filter out tags the user has filtered from their home timeline Invalidate the list if the user's preferences change, as that may indicate they've changed their filters. * Experiment with alternative layout * Set chart height to 160dp to align to an 8dp grid * Draw ticks that are 5% the height of the x-axis * Legend adjustments - Use tuskyblue for the ticks - Wrap legend components in a layout so they can have a dedicated background - Use a 60% transparent background for the legend to retain legibility if lines go under it * Bezier curves, shorter cell height * More tweaks - List tags in order of popularity, most popular first - Make it clear that uses/accounts in the legend are totals, not current - Show current values at end of the chart * Hide FAB * Fix crash, it's not always hosted in an ActionButtonActivity * Arrange totals vertically in landscape layout * Always add the Trending drawer menu if it's not a tab * Revert unrelated whitespace changes * One more whitespace revert --------- Co-authored-by: Nik Clayton <nik@ngo.org.uk>
2023-02-14 19:52:11 +01:00
)
/**
* Mastodon API Documentation: https://docs.joinmastodon.org/methods/trends/#tags
*
* @param day The day that this was posted in Unix Epoch Seconds.
* @param accounts The number of accounts that have posted with this hashtag.
* @param uses The number of posts with this hashtag.
*/
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
@JsonClass(generateAdapter = true)
Add trending tags (#3149) * Add initial feature for viewing trending graphs. Currently only views hash tag trends. Contains API additions, tab additions and a set of trending components. * Add clickable system through a LinkListener. Duplicates a little code from SFragment. * Add accessibility description. * The background for the graph should match the background for black theme too. * Add error handling through a state flow system using existing code as an example. * Graphing: Use a primary and a secondary line. Remove under line fill. Apply line thickness. Dotted end of line. * Trending changes: New layout for trending: Cell. Use ViewBinding. Add padding to RecyclerView to stop the FAB from hiding content. Multiple bugs in GraphView resolved. Wide (landscape, for example) will show 4 columns, portrait will show 2. Remove unused base holder class. ViewModel invalidate scoping changed. Some renaming to variables made. For uses and accounts, use longs. These could be big numbers eventually. TagViewHolder renamed to TrendingTagViewHolder. * Trending changes: Remove old layout. Update cell textsizes and use proper string. Remove bad comment. * Trending changes: Refresh the main drawer when the tabs are edited. This will allow the trending item to toggle. * Trending changes: Add a trending activity to be able to view the trending data from the main drawer. * Trending changes: The title text should be changed to Trending Hashtags. * Trending changes: Add meta color to draw axis etc. Draw the date boundaries on the graph. Remove dates from each cell and place them in the list as a header. Graphs should be proportional to the highest historical value. Add a new interface to control whether the FAB should be visible (important when switching tabs, the state is lost). Add header to the adapter and viewdata structures. Add QOL extensions for getting the dates from history. * Trending changes: Refresh FAB through the main activity and FabFragment interface. Trending has no FAB. * Trending changes: Make graph proportional to the highest usage value. Fixes to the graph ratio calculations. * Trending changes: KtLintFix * Trending changes: Remove accidental build gradle change. Remove trending cases. Remove unused progress. Set drawer button addition explicitly to false, leaving the code there for future issue #3010. Remove unnecessary arguments for intent. Remove media preview preferences, there is nothing to preview. No padding between hashtag symbol and text. Do not ellipsize hashtags. * Trending changes: Use bottomsheet slide in animation helper for opening the hashtag intent. Remove explicit layout height from the XML and apply it to the view holder itself. The height was not being respected in XML. * Use some platform standards for styling - Align on an 8dp grid - Use android:attr for paddingStart and paddingEnd - Use textAppearanceListItem variants - Adjust constraints to handle different size containers * Correct lineWidth calculations Previous code didn't convert the value to pixels, so it was always displaying as a hairline stroke, irrespective of the value in the layout file. While I'm here, rename from lineThickness to lineWidth, to be consistent with parameters like strokeWidth. * Does not need to inherit from FabFragment * Rename to TrendingAdapter "Paging" in the adapter name is a misnomer here * Clean up comments, use full class name as tag * Simplify TrendingViewModel - Remove unncessary properties - Fetch tags and map in invalidate() - emptyList() instead of listOf() for clarity * Remove line dividers, use X-axis to separate content Experiment with UI -- instead of dividers between each item, draw an explicit x-axis for each chart, and add a little more vertical padding, to see if that provides a cleaner separation between the content * Adjust date format - Show day and year - Use platform attributes for size * Locale-aware format of numbers Format numbers < 100,000 by inserting locale-aware separators. Numbers larger are scaled and have K, M, G, ... etc suffix appended. * Prevent a crash if viewData is empty Don't access viewData without first checking if it's empty. This can be the case if the server returned an empty list for some reason, or the data has been filtered. * Filter out tags the user has filtered from their home timeline Invalidate the list if the user's preferences change, as that may indicate they've changed their filters. * Experiment with alternative layout * Set chart height to 160dp to align to an 8dp grid * Draw ticks that are 5% the height of the x-axis * Legend adjustments - Use tuskyblue for the ticks - Wrap legend components in a layout so they can have a dedicated background - Use a 60% transparent background for the legend to retain legibility if lines go under it * Bezier curves, shorter cell height * More tweaks - List tags in order of popularity, most popular first - Make it clear that uses/accounts in the legend are totals, not current - Show current values at end of the chart * Hide FAB * Fix crash, it's not always hosted in an ActionButtonActivity * Arrange totals vertically in landscape layout * Always add the Trending drawer menu if it's not a tab * Revert unrelated whitespace changes * One more whitespace revert --------- Co-authored-by: Nik Clayton <nik@ngo.org.uk>
2023-02-14 19:52:11 +01:00
data class TrendingTagHistory(
val day: String,
val accounts: String,
val uses: String
Add trending tags (#3149) * Add initial feature for viewing trending graphs. Currently only views hash tag trends. Contains API additions, tab additions and a set of trending components. * Add clickable system through a LinkListener. Duplicates a little code from SFragment. * Add accessibility description. * The background for the graph should match the background for black theme too. * Add error handling through a state flow system using existing code as an example. * Graphing: Use a primary and a secondary line. Remove under line fill. Apply line thickness. Dotted end of line. * Trending changes: New layout for trending: Cell. Use ViewBinding. Add padding to RecyclerView to stop the FAB from hiding content. Multiple bugs in GraphView resolved. Wide (landscape, for example) will show 4 columns, portrait will show 2. Remove unused base holder class. ViewModel invalidate scoping changed. Some renaming to variables made. For uses and accounts, use longs. These could be big numbers eventually. TagViewHolder renamed to TrendingTagViewHolder. * Trending changes: Remove old layout. Update cell textsizes and use proper string. Remove bad comment. * Trending changes: Refresh the main drawer when the tabs are edited. This will allow the trending item to toggle. * Trending changes: Add a trending activity to be able to view the trending data from the main drawer. * Trending changes: The title text should be changed to Trending Hashtags. * Trending changes: Add meta color to draw axis etc. Draw the date boundaries on the graph. Remove dates from each cell and place them in the list as a header. Graphs should be proportional to the highest historical value. Add a new interface to control whether the FAB should be visible (important when switching tabs, the state is lost). Add header to the adapter and viewdata structures. Add QOL extensions for getting the dates from history. * Trending changes: Refresh FAB through the main activity and FabFragment interface. Trending has no FAB. * Trending changes: Make graph proportional to the highest usage value. Fixes to the graph ratio calculations. * Trending changes: KtLintFix * Trending changes: Remove accidental build gradle change. Remove trending cases. Remove unused progress. Set drawer button addition explicitly to false, leaving the code there for future issue #3010. Remove unnecessary arguments for intent. Remove media preview preferences, there is nothing to preview. No padding between hashtag symbol and text. Do not ellipsize hashtags. * Trending changes: Use bottomsheet slide in animation helper for opening the hashtag intent. Remove explicit layout height from the XML and apply it to the view holder itself. The height was not being respected in XML. * Use some platform standards for styling - Align on an 8dp grid - Use android:attr for paddingStart and paddingEnd - Use textAppearanceListItem variants - Adjust constraints to handle different size containers * Correct lineWidth calculations Previous code didn't convert the value to pixels, so it was always displaying as a hairline stroke, irrespective of the value in the layout file. While I'm here, rename from lineThickness to lineWidth, to be consistent with parameters like strokeWidth. * Does not need to inherit from FabFragment * Rename to TrendingAdapter "Paging" in the adapter name is a misnomer here * Clean up comments, use full class name as tag * Simplify TrendingViewModel - Remove unncessary properties - Fetch tags and map in invalidate() - emptyList() instead of listOf() for clarity * Remove line dividers, use X-axis to separate content Experiment with UI -- instead of dividers between each item, draw an explicit x-axis for each chart, and add a little more vertical padding, to see if that provides a cleaner separation between the content * Adjust date format - Show day and year - Use platform attributes for size * Locale-aware format of numbers Format numbers < 100,000 by inserting locale-aware separators. Numbers larger are scaled and have K, M, G, ... etc suffix appended. * Prevent a crash if viewData is empty Don't access viewData without first checking if it's empty. This can be the case if the server returned an empty list for some reason, or the data has been filtered. * Filter out tags the user has filtered from their home timeline Invalidate the list if the user's preferences change, as that may indicate they've changed their filters. * Experiment with alternative layout * Set chart height to 160dp to align to an 8dp grid * Draw ticks that are 5% the height of the x-axis * Legend adjustments - Use tuskyblue for the ticks - Wrap legend components in a layout so they can have a dedicated background - Use a 60% transparent background for the legend to retain legibility if lines go under it * Bezier curves, shorter cell height * More tweaks - List tags in order of popularity, most popular first - Make it clear that uses/accounts in the legend are totals, not current - Show current values at end of the chart * Hide FAB * Fix crash, it's not always hosted in an ActionButtonActivity * Arrange totals vertically in landscape layout * Always add the Trending drawer menu if it's not a tab * Revert unrelated whitespace changes * One more whitespace revert --------- Co-authored-by: Nik Clayton <nik@ngo.org.uk>
2023-02-14 19:52:11 +01:00
)
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
val TrendingTag.start
get() = Date(history.last().day.toLong() * 1000L)
val TrendingTag.end
get() = Date(history.first().day.toLong() * 1000L)