How can I make interface methods internal with Kotlin?

Viewed 399

I wrote a simple SwipeLayout library. My goal is to publish it via GitHub for everyone, including myself, to use. I encountered a problem when I decided to encapsulate some methods. I was hoping for an internal modifier to kick in, and solve the problem, but I can't find a proper way to do it. Here are some code and explanation:

class SwipeLayout (...) : FrameLayout(...),
  BackgroundViewsVisibilityController, ... {
  
  private val backgroundController = BackgroundController(
        this //(BackgroundViewsVisibilityController)  
  )

  override fun revealLeftUnderView() {...}

  override fun hideLeftUnderView() {...}

  override fun revealRightUnderView() {...}

  override fun hideRightUnderView() {...}
}


interface BackgroundViewsVisibilityController {
   fun revealLeftUnderView()
   fun revealRightUnderView()
   fun hideLeftUnderView()
   fun hideRightUnderView()
}

These are the methods to hide from users. How can I best achieve it?

1 Answers

internal tells the compiler that this interface, class or function can only be called from the current module. When you release a library, that internal interface can not be called (without using reflection) from your library.

You want to make SwipeLayout public (for third party usage) so you can not make the interface BackgroundViewsVisibilityController internal at the same time, because your class is extending that interface.

If you really want that clients can not call the functions from your interface, consider not having an interface (as an interface is usually there for a caller to be used) but making those functions private in SwipeLayout:

class SwipeLayout (...) : FrameLayout(...)

  private fun revealLeftUnderView() {...}
  private fun hideLeftUnderView() {...}
  private fun revealRightUnderView() {...}
  private fun hideRightUnderView() {...}
}

More information about Kotlin Visibility Modifiers

Related