google / modernstorage

ModernStorage is a group of libraries that provide an abstraction layer over storage on Android to simplify its interactions
https://google.github.io/modernstorage/
Apache License 2.0
1.24k stars 60 forks source link

Add two functions that receive a lambda #17

Closed skydoves closed 3 years ago

skydoves commented 3 years ago

Description

In many cases, we may use getResourceByUri() like this:

mediaStore.getResourceByUri(uri)?.let {
   // .. //
}

val mediaResource = mediaStore.getResourceByUri(uri)
if (mediaResource != null) {
  // .. //
 }

I think it could be a little bit more convenient with new functions instead of using scope functions every time.

What's the difference?

Added two functions that receive a lambda that has a MediaResource as a receiver.

Before

mediaStore.getResourceByUri(uri)?.let {
    savedStateHandle.set("currentMediaUri", uri)
    _currentMedia.value = it
}

After

mediaStore.withResourceByUri(uri) {
    savedStateHandle.set("currentMediaUri", uri)
    _currentMedia.value = it
}

I think the function names are not the best choices. So If have any better ideas, hope to give me feedback. Thank you! 😃

yrezgui commented 3 years ago

Thanks @skydoves for the PR. Sorry for the late answer, I should have pinged you earlier. I'm now using the kotlin.Result type as return types for nearly all methods. It offers better result handling methods than what I've had so far. As of that change, I have to close your PR as it's not usable anymore with the new method signatures