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

Allow to use enum class for SwapperViews #2

Closed
levibostian opened this issue Oct 5, 2020 · 4 comments · Fixed by #5
Closed

Allow to use enum class for SwapperViews #2

levibostian opened this issue Oct 5, 2020 · 4 comments · Fixed by #5

Comments

@levibostian
Copy link
Owner

According to the README doc, this project suggests that you use an enum to represent the Views to swap between. This is great, but it requires more work: swapTo(SwapperView.ViewName.name). It would be great if all you needed to do was swapTo(.ViewName) when you need to swap.

This can be done with a generic on SwapperView.

@levibostian
Copy link
Owner Author

The hard part is that I cannot use generics with the constructor if I want to use SwapperView in XML.

@levibostian
Copy link
Owner Author

// delete `SwapperViewId` typealias
typealias SwapperViewSwapAnimator = (oldView: View, newView: View, duration: Long, onComplete: () -> Unit) -> Unit

class SwapperView<SwapperViewId: Any>: FrameLayout {

Then the example app...

class MainActivity : AppCompatActivity() {

    enum class SwapperViews {
        FIRST_VIEW,
        SECOND_VIEW
    }

    @Suppress("UNCHECKED_CAST")
    private val swapperView: SwapperView<SwapperViews>
        get() = this.swapper_view as SwapperView<SwapperViews>

    private fun setupViews() {
        swapperView.apply {
            viewMap = mapOf(
                Pair(SwapperViews.FIRST_VIEW, first_view),
                Pair(SwapperViews.SECOND_VIEW, second_view)
            )
            swapTo(SwapperViews.FIRST_VIEW) {
                first_view_imageview.load("https://raw.githubusercontent.com/levibostian/Swapper-iOS/d494bc41894b5e5bc7eeacc162a96ddadca024cc/Example/Swapper/Images.xcassets/little_hill.imageset/little_hill.jpg")
            }
        }

        second_view_swap_button.setOnClickListener {
            swapperView.swapTo(SwapperViews.FIRST_VIEW) 
        }
    }
}

Pros:

  • We accomplished it! Allowing any type.

Cons:

  • You have to add this hacky code:
    @Suppress("UNCHECKED_CAST")
    private val swapperView: SwapperView<SwapperViews>
        get() = this.swapper_view as SwapperView<SwapperViews>

I am thinking there is a better way.

@levibostian
Copy link
Owner Author

My idea right now is that you add in XML SwapperViewContainer

In your Activity, you call:

swapper_view_container.init(...)

Where you pass in your ViewMap to init and the first view to swap to. It will (1) return back a strongly typed instance of SwapperView and will also populate the container with the swapperview instance.

This would allow you to have a strongly typed constructor.

levibostian added a commit that referenced this issue Nov 17, 2020
Fixes: #2
BREAKING CHANGE:

SwapperView now uses a generic View identifier. This makes using enums way easier.
* Use `SwapperViewContainer` in XML instead of `SwapperView`
* Call `swapperViewContainer.init(...)` to setup strongly typed `SwapperView`
levibostian added a commit that referenced this issue Nov 17, 2020
Fixes: #2
BREAKING CHANGE:

No more needing to setup Swapper! Simply add children Views to SwapperView and swap between them with `swapTo(childView`.
@levibostian levibostian mentioned this issue Nov 17, 2020
@levibostian
Copy link
Owner Author

Ended up with a better idea overall. Remove the need for enum, period! No more viewMap = ...!

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

Successfully merging a pull request may close this issue.

1 participant