forked from oppia/oppia-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix#3146 : Create a generic utility for filtering enums (oppia#5529)
## Explanation Fixes: oppia#3146 This PR introduces a new utility function filterByEnumCondition to standardize filtering of enums across various parts of the oppia-android codebase. This utility function allows filtering of collections based on a condition applied to enum values. This PR introduces a new utility function filterByEnumCondition to standardize filtering of enums across various parts of the oppia-android codebase. This utility function allows filtering of collections based on a condition applied to enum values. **Key Changes**: Added Utility Function: filterByEnumCondition: A generic function to filter a collection based on a condition applied to an enum extracted from each item in the collection. **Updated Existing Code**: Refactored code in getLeastRecentMetricLogIndex, getLeastRecentMediumPriorityEventIndex, and other methods to utilize the new filterByEnumCondition function. Updated the calculation of completedChapterCount and inProgressChapterCount using the new utility function. ## Essential Checklist <!-- Please tick the relevant boxes by putting an "x" in them. --> - [ x] The PR title and explanation each start with "Fix #bugnum: " (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".) - [x ] Any changes to [scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets) files have their rationale included in the PR explanation. - [x ] The PR follows the [style guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide). - [x ] The PR does not contain any unnecessary code changes from Android Studio ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)). - [ x] The PR is made from a branch that's **not** called "develop" and is up-to-date with "develop". - [x ] The PR is **assigned** to the appropriate reviewers ([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)). - --------- Co-authored-by: Adhiambo Peres <[email protected]>
- Loading branch information
1 parent
8a7fe0a
commit 33d2b8f
Showing
9 changed files
with
99 additions
and
58 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
utility/src/main/java/org/oppia/android/util/enumfilter/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
""" | ||
General purpose utility for filtering enums. | ||
""" | ||
|
||
load("@io_bazel_rules_kotlin//kotlin:android.bzl", "kt_android_library") | ||
|
||
kt_android_library( | ||
name = "enum_filter_util", | ||
srcs = [ | ||
"EnumFilterUtil.kt", | ||
], | ||
visibility = ["//:oppia_api_visibility"], | ||
) |
20 changes: 20 additions & 0 deletions
20
utility/src/main/java/org/oppia/android/util/enumfilter/EnumFilterUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.oppia.android.util.enumfilter | ||
|
||
/** | ||
* Filters a collection based on a condition applied to an enum property of each element. | ||
* | ||
* @param E the type of enum values. | ||
* @param T the type of elements in the collection. | ||
* @param collection the collection of elements to filter. | ||
* @param enumExtractor a function that extracts the enum value from each element. | ||
* @param condition a predicate function that determines if an enum value should be included in the result. | ||
* @return a list of elements from the collection that satisfy the condition when their enum property is evaluated. | ||
*/ | ||
|
||
inline fun <E : Enum<E>, T> filterByEnumCondition( | ||
collection: Collection<T>, | ||
enumExtractor: (T) -> E, | ||
condition: (E) -> Boolean | ||
): List<T> { | ||
return collection.filter { condition(enumExtractor(it)) } | ||
} |