Skip to content
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

Add a StringDesc type that is a wrapper transform #62

Closed
dalewking opened this issue May 1, 2020 · 6 comments · Fixed by #71
Closed

Add a StringDesc type that is a wrapper transform #62

dalewking opened this issue May 1, 2020 · 6 comments · Fixed by #71
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@dalewking
Copy link

dalewking commented May 1, 2020

I have a case where I want to return something like a StringDesc to the UI so that it only has to call localized or toString(context) but where a transformation is ran on the result. The particular case is I have a String defining a date format and the transformation I want to run is use that format as an argument to a format call. I want to define that in common code so it does not know about loacalized or toString(context) and I don't want the code that receives it to have to call the format method.

I would try to define it myself but the fact that StringDesc is sealed makes that impossible

So what I am proposing is a map extension method on StringDesc. So I envision this in the common code:

return MR.strings.my_date_format.desc().map { myDate.format(it).desc() }

So in StringDesc you would need a new class:

class Mapped(stringDesc: StringDesc, transform: (String) -> StringDesc) : StringDesc

You would need extension methods on StringDesc:

fun StringDesc.map(transform: (String) -> StringDesc) = StringDesc.Mapped(this, transform)

And then the Android implementation might be:

 actual data class Mapped actual constructor(
     val stringDesc: StringDesc,
     val transform: (String) -> StringDesc
 ) : StringDesc() {
     override fun toString(context: Context) =
        stringDesc.toString(context).let(transform).toString(context)
 }
@dalewking
Copy link
Author

dalewking commented May 1, 2020

As a convenience would be nice to also have:

fun StringDesc.map(transform: (String) -> String) = StringDesc.Mapped(this, { transform(it).desc() })

This would let me shorten to:

return MR.strings.my_date_format.desc().map(myDate::format)

@dalewking dalewking changed the title Add a StringDesc type that is a wrapper function Add a StringDesc type that is a wrapper transform May 1, 2020
@dalewking
Copy link
Author

Another alternative is just remove sealed from StringDesc and then I can do it on my own

@Alex009
Copy link
Member

Alex009 commented May 4, 2020

@dalewking hello! thanks for feedback!
i will try to change StringDesc implementation to interface with companion object extensions like this https://github.com/icerockdev/moko-widgets/pull/234/files#diff-3cb3abd88c590c7b205f70d676e2f5a7
it will allow extension of StringDesc functional

@Alex009 Alex009 added the enhancement New feature or request label May 4, 2020
@Alex009 Alex009 self-assigned this May 4, 2020
@Alex009 Alex009 added this to the 0.10.0 milestone May 4, 2020
Alex009 added a commit that referenced this issue May 4, 2020
@Alex009 Alex009 linked a pull request May 4, 2020 that will close this issue
Alex009 added a commit that referenced this issue May 4, 2020
@Alex009
Copy link
Member

Alex009 commented May 4, 2020

@dalewking hi! check this mergerequest - #71
is this changes allow you do what you want?

@dalewking
Copy link
Author

I think it should work for what I want

Alex009 added a commit that referenced this issue May 5, 2020
# Conflicts:
#	sample/mpp-library/src/commonMain/kotlin/com/icerockdev/library/Testing.kt
Alex009 added a commit that referenced this issue May 5, 2020
Alex009 added a commit that referenced this issue May 5, 2020
@Alex009
Copy link
Member

Alex009 commented May 5, 2020

merged, will be released in 0.10.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants