-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
[media-library] Refactor to Kotlin 4 - Asset management #14565
Conversation
71e3655
to
d1839c3
Compare
6b525ba
to
ef2aa82
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
fun getExifFullInfo(exifInterface: ExifInterface, response: Bundle) { | ||
val exifMap = Bundle() | ||
for ((type, name) in MediaLibraryConstants.exifTags) { | ||
if (exifInterface.getAttribute(name!!) != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why name
isn't null safe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result of MediaLibraryConstants
not being kotlinized yet. They're constants defined here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job 🔥
...ages/expo-media-library/android/src/main/java/expo/modules/medialibrary/assets/AssetUtils.kt
Outdated
Show resolved
Hide resolved
...ages/expo-media-library/android/src/main/java/expo/modules/medialibrary/assets/AssetUtils.kt
Outdated
Show resolved
Hide resolved
...ges/expo-media-library/android/src/main/java/expo/modules/medialibrary/assets/CreateAsset.kt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
...ages/expo-media-library/android/src/main/java/expo/modules/medialibrary/assets/AssetUtils.kt
Outdated
Show resolved
Hide resolved
...ages/expo-media-library/android/src/main/java/expo/modules/medialibrary/assets/AssetUtils.kt
Outdated
Show resolved
Hide resolved
|
||
/** | ||
* If the receiver is instance of `T`, returns the receiver, otherwise returns `null` | ||
*/ | ||
inline fun <reified T> Any?.takeIfInstanceOf(): T? = if (this is T) this else null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you can achieve this functionality with built-in as?
operator. As documentation is rather laconic, I looked up some cases in playground
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, totally forgot about existence of as?
operator 😅
What do you think - which one looks better?
// I have to add parenthesis here
(input["createdAfter"] as? Number)?.let {
or
input["createdAfter"].takeIfInstanceOf<Number>()?.let {
If we'd like to leave takeIfInstanceOf
, then I could just replace:
/** | |
* If the receiver is instance of `T`, returns the receiver, otherwise returns `null` | |
*/ | |
inline fun <reified T> Any?.takeIfInstanceOf(): T? = if (this is T) this else null | |
/** | |
* If the receiver is instance of `T`, returns the receiver, otherwise returns `null` | |
*/ | |
inline fun <reified T> Any?.takeIfInstanceOf(): T? = this as? T |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you can. However, I don't like it. From my perspective is less readable.
For example instead of writing:
input["first"].takeIfInstanceOf<Number>()?.toInt() ?: 20
you will have to do:
(input["first"] as? Number)?.toInt() ?: 20
And I know, it's less character... but you have added ugly brackets.
From the perspective of underlying code is doing the same thing. Safe cast operator compiles to something like this:
Any temp = value;
if (!(temp is T)) {
temp = null;
}
T output = (T) temp;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I've decided the following:
- When there is a method chaining, e.g.
input["first"].takeIfInstanceOf<Number>()?.toInt()?.let { ... }
, I've left thetakeIfInstanceOf
- the reason is that I don't like these parentheses too.
Hypotethic situation, when this approach has a huge advantage:// everything goes in clean, readable order val x = input["a"] ?.takeIfInstanceOf<Bundle>() ?.get("b") ?.takeIfInstanceOf<String>() ?.let { ... } // maybe shorter, but I got lost with parentheses val y = ((input["a"] as? Bundle)?.get("b") as? String)?.let { ... }
- When there's no such chaining, I replaced the occurrences with the
as?
operator - in such cases it's much cleaner:val sortBy = input["sortBy"] as? List<*>
I also updated the description of the takeIfInstanceOf
method - added an example similar to the above
Anyway, nice catch, thanks for this suggestion 😁
.../expo-media-library/android/src/main/java/expo/modules/medialibrary/assets/GetAssetsQuery.kt
Show resolved
Hide resolved
.../expo-media-library/android/src/main/java/expo/modules/medialibrary/assets/GetAssetsQuery.kt
Show resolved
Hide resolved
.../expo-media-library/android/src/main/java/expo/modules/medialibrary/assets/GetAssetsQuery.kt
Outdated
Show resolved
Hide resolved
.../expo-media-library/android/src/main/java/expo/modules/medialibrary/assets/GetAssetsQuery.kt
Outdated
Show resolved
Hide resolved
.../expo-media-library/android/src/main/java/expo/modules/medialibrary/assets/GetAssetsQuery.kt
Show resolved
Hide resolved
Hi there! 👋 I'm a bot whose goal is to ensure your contributions meet our guidelines. I've found some issues in your pull request that should be addressed (click on them for more details) 👇
|
Why, How etc...
See #14562 description for details.
This is part of a PR stack:
expo.modules.medialibrary.assets
package