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
Comments
The hard part is that I cannot use generics with the constructor if I want to use |
// 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:
Cons:
@Suppress("UNCHECKED_CAST")
private val swapperView: SwapperView<SwapperViews>
get() = this.swapper_view as SwapperView<SwapperViews> I am thinking there is a better way. |
My idea right now is that you add in XML 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. |
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`
Fixes: #2 BREAKING CHANGE: No more needing to setup Swapper! Simply add children Views to SwapperView and swap between them with `swapTo(childView`.
Ended up with a better idea overall. Remove the need for enum, period! No more |
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 wasswapTo(.ViewName)
when you need to swap.This can be done with a generic on SwapperView.
The text was updated successfully, but these errors were encountered: