natario1 / ZoomLayout

2D zoom and pan behavior for View hierarchies, images, video streams, and much more, written in Kotlin for Android.
https://natario1.github.io/ZoomLayout
Apache License 2.0
1.03k stars 148 forks source link

Open constructor without container in ZoomEngine #193

Closed coffeemakr closed 3 years ago

coffeemakr commented 3 years ago

Solution

I have opened the constructor and setContainer function to simplify implementation of custom views. Since the container property is a lateinit var, a readable error is already thrown when the engine would use the container:

    kotlin.UninitializedPropertyAccessException: lateinit property container has not been initialized
        at com.otaliastudios.zoom.ZoomEngine.access$getContainer$p(ZoomEngine.kt:35)

In addition I have added a check in addListener because without calling addListener you don't need an engine. The following exception is thrown, if setContainer is not called and addListener is used:

     Caused by: java.lang.IllegalStateException: container is not initialized.
        at com.otaliastudios.zoom.ZoomEngine.addListener(ZoomEngine.kt:357)

If setContainer is called twice, an error like the following is thrown:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.otaliastudios.zoom.demo/com.otaliastudios.zoom.demo.MainActivity}: android.view.InflateException: Binary XML file line #26 in com.otaliastudios.zoom.demo:layout/activity_main: Binary XML file line #26 in com.otaliastudios.zoom.demo:layout/activity_main: Error inflating class com.otaliastudios.zoom.ZoomImageView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
     ...
  Caused by: java.lang.IllegalStateException: container already set
        at com.otaliastudios.zoom.ZoomEngine.setContainer(ZoomEngine.kt:541)
        at com.otaliastudios.zoom.ZoomImageView.<init>(ZoomImageView.kt:61)
markusressel commented 3 years ago

Looks good to me :)