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

How to survive orientation but clear cache when closing screen #103

Open
uberchilly opened this issue Sep 12, 2020 · 3 comments
Open

How to survive orientation but clear cache when closing screen #103

uberchilly opened this issue Sep 12, 2020 · 3 comments

Comments

@uberchilly
Copy link

uberchilly commented Sep 12, 2020

Hello,
I am currently using this setup with a tag, to survive the orientation change.
image
And that is working fine.

Now I am trying to clear stored information when the user is leaving the screen so that it will be played from the beginning next time user opens it, like this
image

But I cannot figure out a way to clear cached information for the tag.
What is the proper way to clear cache?

Thanks in advance.

@eneim
Copy link
Owner

eneim commented Sep 14, 2020

@uberchilly By saying leaving the screen what Android's lifecycle events are happened during that transition? If the application is destroyed, all the information should be cleared, but if only the Activity is destroyed but not the Application, then it needs some work. I will take a look, but please help me to understand the behavior clearer.

@uberchilly
Copy link
Author

@eneim What I mean is the second scenario where the activity is destroyed but not application.
What I did to solve it:

if (savedInstanceState != null) {
	currentRebinder = savedInstanceState.getParcelable("currentPlaying")
}

val manager = kohii.register(this).addBucket(parentView)

//when switching orientation we will reuse rebinder
currentRebinder?.let {
	it.with {
		repeatMode = Common.REPEAT_MODE_OFF
		controller = object : Playback.Controller {
			override fun kohiiCanStart(): Boolean = true

			override fun kohiiCanPause(): Boolean = true

			override fun setupRenderer(playback: Playback, renderer: Any?) {
				if (renderer is PlayerView) {
					renderer.useController = true
					renderer.setControlDispatcher(kohii.createControlDispatcher(playback))
				}
			}

			override fun teardownRenderer(playback: Playback, renderer: Any?) {
				if (renderer is PlayerView) {
					val tag = renderer.tag
					if (tag is ControlDispatcher) renderer.tag = null
				}
			}
		}
	}.bind(kohii, playerView)
}

viewModel.playerUrl.observe(viewLifecycleOwner, EventObserver { url ->
	//will be triggered only when buttons are pressed
	currentRebinder = kohii.setUp(url) {
		tag = url
		repeatMode = Common.REPEAT_MODE_OFF
		controller = object : Playback.Controller {
			override fun kohiiCanStart(): Boolean = true

			override fun kohiiCanPause(): Boolean = true

			override fun setupRenderer(playback: Playback, renderer: Any?) {
				if (renderer is PlayerView) {
					renderer.useController = true
					renderer.setControlDispatcher(kohii.createControlDispatcher(playback))
				}
			}

			override fun teardownRenderer(playback: Playback, renderer: Any?) {
				if (renderer is PlayerView) {
					val tag = renderer.tag
					if (tag is ControlDispatcher) renderer.tag = null
				}
			}
		}
	}.bind(playerView, callback = { playback ->
		playback.rewind(refresh = true)
		playback.playable?.let { playable ->
			if (!playable.isPlaying())
				manager.play(playable)
		}
	})
})

In short, when orientation change happens I skip the call to playback.rewind(refresh=true)
And I force a refresh in every other situation.

@eneim
Copy link
Owner

eneim commented Sep 18, 2020

@uberchilly I see. It is not elegant I agree. Sadly I think in the latest stable release there is no convenient way for this. I have an idea about using ViewModel-alike mechanism to address this use case. I will spend some time working on this and it will be in the next release.

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

No branches or pull requests

2 participants