Home > front end >  How to access a function that's nested inside an override method
How to access a function that's nested inside an override method

Time:02-19

private fun openTrailer() {
    val trailerDialog = Dialog(this)
    val trailerDialogBinding = DialogTrailerBinding.inflate(layoutInflater)
    trailerDialog.setContentView(trailerDialogBinding.root)

    youtubePLayerInit = object : YouTubePlayer.OnInitializedListener {
        override fun onInitializationSuccess(
            p0: YouTubePlayer.Provider?,
            youtubePlayer: YouTubePlayer?,
            p2: Boolean
        ) {
            fun loadTrailer2(videoId: String) {
                youtubePlayer?.loadVideo(videoId)
            }
        }

        override fun onInitializationFailure(
            p0: YouTubePlayer.Provider?,
            p1: YouTubeInitializationResult?,
        ) {
            toast("la")
        }
    }
    if (!isInitialized) {
        trailerDialogBinding.vvMovieTrailer.initialize(youtubeApiKey, youtubePLayerInit)
        isInitialized = true
    } else {
        Log.e("initializerStatus", "Already Initialized")
    }
    trailerDialog.show()
}

How do I access access the loadTrailer2 function in the main code? Load Trailer is the function inside the object of the YoutubePlayer.OnInitializedListener. I'm trying to accesss it outside the open trailer function, aka in the onCreate method

CodePudding user response:

Well, as it is right now, loadTrailer2 is a local function declared inside onInitializationSuccess - it only exists while that function is running, so you can't access it from outside that scope.

You could move it into the object itself, but since it relies on the YoutubePlayer object being passed in, how would you call it? Do you have access to that player in onCreate?

If you did have access to it (e.g. when the initialisation first succeeds, you store it in a ViewModel or something, and use that next time you create a fragment) you still have an issue: the type of youtubePlayerInit is YouTubePlayer.OnInitializedListener, and you're adding another method to your object, one that isn't part of that YouTubePlayer.OnInitializedListener interface.

What that means, is nothing actually knows that method exists. If you create that anonymous object inside a function, the compiler sees it as a special type that contains that method, so you can access it directly:

interface Thing {
    fun onSomeEvent(code: Int)
}

fun main() {
    doSetup()
}

fun doSetup() {
    val myThing = object : Thing {
        override fun onSomeEvent(code: Int) {
            println("Event code: $code")
        }
        
        fun coolFunction() {
            println("Secret function accessed")
        }
    }
    
    // calling it inside the scope the object was declared in, so the function
    // is visible here
    myThing.coolFunction()
}

>> Secret function accessed

but outside of that scope, it just looks like the type declared when creating the object (Thing in this example):

interface Thing {
    fun onSomeEvent(code: Int)
}


fun main() {
    val myThing = doSetup()
    // this only knows it's a Thing, which doesn't have this method
    myThing.coolFunction()
}

fun doSetup(): Thing {
    val myThing = object : Thing {
        override fun onSomeEvent(code: Int) {
            println("Event code: $code")
        }
        
        fun coolFunction() {
            println("Secret function accessed")
        }
    }
    
    return myThing
}

>> Unresolved reference: coolFunction

If you wanted to access that function, you'd have to e.g. declare a CoolFunctionHaver interface with that function, and either declare myThing as that type, or cast it if you really have to

  • Related