Zhuinden / simple-stack-extensions

Extensions for the simple-stack library.
Apache License 2.0
7 stars 1 forks source link

Custom Variable on DefaultFragmentKey #2

Closed iNoles closed 4 years ago

iNoles commented 4 years ago

Is there is a way to have title and shouldShowUp on DefaultFragmentKey? Is there is better way other than the DefaultFragmentKey?

Zhuinden commented 4 years ago

shouldShowUp is interesting because normally in a shared toolbar you can use it with

val newKeys = stateChange.getNewKeys()
upButton.showIf { newKeys.size > 1 }

Which is especially possible if you're using the general pattern of .setStateChanger(SimpleStateChanger(this)) to delegate over to the DefaultFragmentStateChanger.

As for title though, the general idea is that you can extend DefaultFragmentKey and add any new custom properties that must be shared across all of your keys.

abstract class FragmentKey: DefaultFragmentKey() {
    abstract val title: String?
}

Then

class MyFragmentKey: FragmentKey() {
    override val title: String? = "MyFragment"
}

You can of course prefer a String resource ID instead of a String, you can make it a fun that gets a context, etc. it's configurable to your needs.

Zhuinden commented 4 years ago

Did that help?

iNoles commented 4 years ago

Sorry, I didn't have a time to test it. :(

Zhuinden commented 4 years ago

Same idea as https://github.com/Zhuinden/simple-stack/blob/master/tutorials/tutorial-sample/src/main/java/com/zhuinden/simplestacktutorials/steps/step_3/Step3Screen.kt#L5 except you extend from DefaultFragmentKey instead.

Zhuinden commented 4 years ago

Any luck?

iNoles commented 4 years ago

Yeah, it does work.